Will the following code, with nothing in between the lines, always produce a value of true
for the boolean b
?
double d = 0.0;
bool b = (d == 0.0);
I'm using g++ version 4.8.1.
Will the following code, with nothing in between the lines, always produce a value of true
for the boolean b
?
double d = 0.0;
bool b = (d == 0.0);
I'm using g++ version 4.8.1.
Copyright © 2021 Jogjafile Inc.
Assuming
IEEE-754
(and probably most floating point representations), this is correct as0.0
is representable exactly in allIEEE-754
formats.Now if we take another literal that is not representable exactly in
IEEE-754
binary formats, like0.1
:This may result in
false
value inb
object!The implementation has the right to use for example a
double
precision ford
and a greater precision for the comparison with the literal.