+ 6

floats

why the result of bellow code is true while logically it seems to be false(cause they're equal)?! i know that an integer float(no number after decimal point) behave normally in comparisons but the problem starts as we add some numbers after the decimal point. for example: float a=0.8 if(0.8>a) printf("true"); else printf("false"); //output true

6th Feb 2020, 1:28 PM
{ SorousH }
{ SorousH } - avatar
2 Answers
6th Feb 2020, 1:38 PM
Mihai Apostol
Mihai Apostol - avatar
+ 1
1) 0.8 is double (IEEE754 using 64 bits) and float a = 0.8; // IEEE754 using 32 bits IEEE754 is the most common format used by computers to represent non-integer numbers... 2) They are stored as: double 0.8: 0x3FE999999999999A float 0.8: 0x3F4CCCCD They are different numbers! 3) If we convert them to decimal : double 0.8 -> 0.80000000000000004441 float 0.8 --> 0.80000001192092895508 >>> float 0.8 > double 0.8 :)
6th Feb 2020, 3:06 PM
unChabon
unChabon - avatar