Is it right output of C programe -


possible duplicate:
floating point comparison

i have simple c code , output comes out unexpected me

main()  {    float f1 = 1.0;    double f2 = 1.0;    if(f1==f2)   printf("equal");   else    printf("unequal");    } 

i expecting output "unequal" output equal .why so?

since float , double have different precision ,output should unequal.

precision matters when number can't represented exactly. since both floats , doubles (being ieee 754 single , double precision values) can represent 1.0 exactly, precision doesn't come it.

1.0 0 sign bit, exponent bits except highest set 1, , no mantissa bits set. in single precision, that's binary:

0-01111111-00000000000000000000000 

and, double precision:

0-01111111111-0000000000000000000000000000000000000000000000000000 

not numbers representable in ieee 754 - example, 1.1 mention in comment stored 1.100000023841858 in single precision.

have @ this answer example of decoding floating point value.

harald schmidt's online single-precision converter excellent site play around if want understand formats. liked much, made desktop version in case ever disappeared (and capable of doing double precision well).


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -