0

hello daniweb...
i got my head struck with the code, and unable to understand the output.please help..

int main(void)
{
   float i=3.3;
   if(i < 3.3) 
        printf("hy");
   else
        printf("bye");
  return 0;
}

output should by bye as 3.3 < 3.3 condition false , but why it is printing hy.
i am using gcc compiler

Edited by geeksforgeek: n/a

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Narue
0

Sorry Narue, i studies both links but cant find why the code is giving such output... :(

i checked the code

int main(void)
{
   float i=3.3;
   if(i < 3.3) 
        printf("hy %f\n",fabs(i)-fabs(3.3));
   else
        printf("bye");n why 
  return 0;
}

but its giving output hy 0.000000 if difference of there precision is zero, then why if condition is getting true..??

Edited by geeksforgeek: n/a

0

Because 3.3 is not 3.3 in computers. Floating point is inaccurate.

3.3 could really be 3.300001 or 3.299998.

0

Try running this code

#include <stdio.h>

int main(void)
{
   float i=3.3;
   
   fprintf(stdout, "%f\n", i - 3.3);
   fprintf(stdout, "%f\n", 3.3 - i);
  return 0;
}
0

yep checked, got something:

nos like 3.3 and 5.2 are recurring float number so it different for float and double.............Number likes 4.5, 3.25, 5.0 will store same values in float and double data type.

is this the answer

0

The answer is that floating-point values are not always exact, and if you expect them to always be exact, you get what you deserve.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.