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

Recommended Answers

All 6 Replies

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..??

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

3.3 could really be 3.300001 or 3.299998.

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;
}

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.