C Comparison of zero floating point

Will the following code, containing nothing between the lines, always return a value truefor the boolean b?

double d = 0.0;
bool b = (d == 0.0);

I am using g ++ version 4.8.1.

+4
source share
1 answer

Assuming IEEE-754(and probably most floating point representations), this is true as it 0.0appears accurately in all formats IEEE-754.

Now, if we take another literal that does not appear exactly in binary formats IEEE-754, for example 0.1:

double d = 0.1;
bool b = (d == 0.1);

This can lead to a value falsein the object b!

, , double d .

(C99, 5.2.4.2.2p8) " ( ), , , , , ."

+7

All Articles