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++?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You’re not asking if
0.0is always represented exactly.In the statement
assert(n == 0),0is converted todoublebefore the comparison occurs. Thus, the assert can only be triggered if converting0frominttodoubleis 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
doublethat did not have an exact zero.