Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C floating-point non-deterministic?

I have read somewhere that there is a source of non-determinism in C double-precision floating point as follows:

  1. The C standard says that 64-bit floats (doubles) are required to produce only about 64-bit accuracy.

  2. Hardware may do floating point in 80-bit registers.

  3. Because of (1), the C compiler is not required to clear the low-order bits of floating-point registers before stuffing a double into the high-order bits.

  4. This means YMMV, i.e. small differences in results can happen.

Is there any now-common combination of hardware and software where this really happens? I see in other threads that .net has this problem, but is C doubles via gcc OK? (e.g. I am testing for convergence of successive approximations based on exact equality)

like image 708
Lucas Membrane Avatar asked Jun 21 '14 08:06

Lucas Membrane


1 Answers

The behavior on implementations with excess precision, which seems to be the issue you're concerned about, is specified strictly by the standard in most if not all cases. Combined with IEEE 754 (assuming your C implementation follows Annex F) this does not leave room for the kinds of non-determinism you seem to be asking about. In particular, things like x == x (which Mehrdad mentioned in a comment) failing are forbidden since there are rules for when excess precision is kept in an expression and when it is discarded. Explicit casts and assignment to an object are among the operations that drop excess precision and ensure that you're working with the nominal type.

Note however that there are still a lot of broken compilers out there that don't conform to the standards. GCC intentionally disregards them unless you use -std=c99 or -std=c11 (i.e. the "gnu99" and "gnu11" options are intentionally broken in this regard). And prior to GCC 4.5, correct handling of excess precision was not even supported.

like image 90
R.. GitHub STOP HELPING ICE Avatar answered Oct 15 '22 19:10

R.. GitHub STOP HELPING ICE