Can the following code be expected to work in all environments that have an ANSI-compliant C compiler?
double n = 0;
assert(n == 0);
What about C++?
You're not asking if 0.0
is always represented exactly.
In the statement assert(n == 0)
, 0
is converted to double
before the comparison occurs. Thus, the assert can only be triggered if converting 0
from int
to double
is not reproducible. This is a much weaker restriction than what you're asking about, and will almost certainly hold (though I can't think of a standards reference to guarantee it off the top of my head).
To the question you intended to ask:
As others mentioned, the C standard does not require that floating-point types map to IEEE-754, but I am not aware of any floating-point representation used with any C compiler that does not have an exact representation of zero. That said, it would be "legal" for a C implementation to use a format for double
that did not have an exact zero.