+ 2
Explain this behavior of code? Why the floating point representation is giving the strange behavior. When the datatype is change
int j; j=2*3/4+2.0/5+8/5; printf("%d",j); float j; j=2*3/4+2.0/5+8/5; printf("%f",j);
2 Réponses
+ 2
~ swim ~ question edited thanks for the explanation. I'm beginner and floating point representation is always an headache. When I code something and get minor concept missing I would asked that to clear it that time but floating point and some c quizzes always get me in some doubts so I put question like explaining the strange behavior
0
int j;
j = (int) (2*3/4+2.0/5+8/5);
System.out.printf("%d\n",j);
System.out.printf("int %d + double %.2f + int %d = int %d\n",
2*3/4, 2.0/5, 8/5, j);
System.out.println();
float k;
k = 2*3/4+2.0f/5+8/5;
// k = (float) (2*3/4+2.0/5+8/5);
System.out.printf("%f\n",k);
System.out.printf("int %d + float %.2f + int %d = float %.02f\n",
2*3/4, 2.0f/5, 8/5, k);