0

How this is possible?

/* If the Values are ending with .5 or .0 the condition is True but I don't know how this is happenning. */ #include <stdio.h> int main() { float s=2.4; //try with 2.0 or 2.5 double t=2.4; if(s==t){ printf ("True"); } else{ printf ("False"); } return 0; }

26th Jul 2019, 4:00 AM
Arun
Arun - avatar
1 Réponse
+ 2
The floating point format cannot represents .4 perfectly, while it can perfectly represents .5 and .0 A double data type is more precise than float, so 2.4 in float and in double won't be the same You can do this printf("%.16f\n%.16f", (float)2.4, 2.4); And you can see that double is more precise and they have different value
26th Jul 2019, 4:11 AM
Agent_I
Agent_I - avatar