user2939992
user2939992

Reputation: 29

Typecasting in C -2147483648

I am using C, and in my program I am typecasting a variable from double to int using this code:

x1=(int)((x1+0.25)*2);

For some reason I get the value of x1 as -2147483648.00

What am I doing wrong?

Full code:

#include <stdio.h>

int main(){
double x1=-10., y1=15.,x2=10.,y2=15.,t;
int i, Y=0,X=20,j;
char check;
do {            
scanf("%c",&check);

if (check!='T')   
  break;

scanf("ime: %lf ( %lf, %lf), ( %lf, %lf)\n",&t,&x1,&y1,&x2,&y2);    
printf("Time: %.1lf\n",t);

for (i=20; i>=Y; i--) {      
  printf("|");           
  for (j=-20; j<=X; j++) {
if (x1>0)   
  x1=(int)((x1+0.25)*2);   
else if (x1<0)
  x1=(int)((10-0.25)*2);
else 
  x1=0;
if (x2>0)
  x2=(int)((x2+0.25)*2);
    else if (x2<0)
  x2=(int)((x2-0.25)*2);
else 
  x2=0;
(y1>0)?(y1=(int)(y1+0.5)):(y1=(int)(y1-0.5));        
(y2>0)?(y2=(int)(y2+0.5)):(y2=(int)(y2-0.5));
if ((x1==x2)&&(x2==j)&&(y1==y2)&&(y2==i))             
  printf("*");                   
else if ((x1==j) && (y1==i))
  printf("1");
else if ((x2==j) && (y2==i))
  printf("2");
else
  printf(" ");
  } 
  printf("|\n");
}
for (i=0;i<43;i++)
  printf("-");                   
printf("\n");

printf ("x1=%.2lf, y1=%.2lf, x2=%.2lf, y2=%.2lf",x1,y1,x2,y2);
printf("\n");
}
 while (check=='T');     

return 0;
}

Input: Without the asterisk and space

Time: 0.00 ( -10.00, 20.00), ( 10.00, 20.00)
Time: 0.10 ( -8.00, 14.95), ( 8.00, 14.95)
Time: 0.20 ( -6.00, 14.80), ( 6.00, 14.80)
Time: 0.30 ( -4.00, 14.56), ( 4.00, 14.56)
Time: 0.40 ( -2.00, 14.22), ( 2.00, 14.22)
Time: 0.50 (  0.00, 13.77), ( 0.00, 13.77)
Boom!

Upvotes: 0

Views: 174

Answers (2)

Neha Somani
Neha Somani

Reputation: 112

This is simple one:

You are trying to typecast double to int. whereas the x2 is exceeding way beyond integer range. check this: http://www.cplusplus.com/forum/beginner/44774/

And that's why when you try to get x2 from int to double again, it's impossible.

Try to run your program by removing typecast for x2, and see, it's working.

You can truncate double using math.h in C. Or though you can add floating values directly it will not make huge difference; but definitely not a good programming practice.

And yeah, please post program with proper indenting and formatting to make it more readable.

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222714

When I run the program you have provided, it is x2 that becomes -2,147,483,648 first, not x1.

When you convert a floating-point value to int, the C standard does not define the behavior of the integer portion of the floating-point value is not representable in the int type. Apparently, on the system you are using:

  • The int type represents values from -2,147,483,648 to 2,147,483,647.
  • Your program creates a floating-point value outside that range.
  • When a value outside that range is converted to int, your C implementation produces -2,147,483,648.

There are two problems:

  • You need to figure out why your program is producing such large values and decide whether you want to do that or not.
  • When you want to take the integer part of a floating-point value, you should #include <math.h> and use trunc rather than converting it to an int. If you want to round to the nearest integer, use round, rather than adding .5 (or subtracting if negative) and taking the integer part.

Your code is essentially doubling x1 and x2 repeatedly, and it is not clear why or what the goal is.

Upvotes: 0

Related Questions