Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the double 0.0 always represented exactly in portable C?

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++?

like image 277
kirakun Avatar asked Jan 20 '26 17:01

kirakun


2 Answers

The C standard does put some limitations on how floating point values are represented. In §5.2.4.2.2 Characteristics of floating types, floating point numbers must exhibit characteristics as if they were defined by the model:

x = sbe Σk=1..p fk b-k

Where:

  • s is the sign, and must be ±1;
  • b is the base, and must be an integer > 1;
  • e is the exponent and must be an integer between emin and emax;
  • p is the precision; and
  • fk are non-negative integers < b - the digits of the significand.

Under this model, zero is always able to be exactly represented - it simply requires all the significand digits fk to be zero.

Given the following restriction in §6.3.1.4:

When a value of integer type is converted to a real floating type, if the value being converted can be represented exactly in the new type, it is unchanged.

It follows that zero must always be unchanged when converted from the integer 0 to a floating point type. Therefore the assertion must always hold.

like image 130
caf Avatar answered Jan 23 '26 07:01

caf


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.

like image 36
Stephen Canon Avatar answered Jan 23 '26 09:01

Stephen Canon