The variable a
is a float
that holds some value close to the mathematical value 2.3.
The literal 2.3
is a double
that also holds some value close to the mathematical value 2.3, but because double
has greater precision than float
, this may be a different value from the value of a
. Both float
and double
can only represent a finite number of values, so there are necessarily mathematical real numbers that cannot be represented exactly by either of those two types.
In the comparison a == 2.3
, the left operand is promoted from float
to double
. This promotion is exact and preserves the value (as all promotions do), but as discussed above, that value may be a different one from that of the 2.3
literal.
To make a comparison between floats, you can use an appropriate float literal:
assert(a == 2.3f);
// ^