Does PHP really use IEEE-754 floating point numbers?

The IEEE-754 floating point standard says:

Four mutually exclusive relationships are possible: less, equal, more and disordered. The latter case occurs when at least one operand is NaN. Each NaN will compare unordered with everything, including itself.

And yet ( code here ):

<?php echo phpversion() . " " . zend_version() . " " . php_uname() . "\n"; // 5.2.5 2.2.0 Linux 2cf38fbc9b9e 3.11.0-15-generic #25-Ubuntu SMP // Thu Jan 30 17:22:01 UTC 2014 x86_64 NAN < NAN; // true NAN > NAN; // true INF < INF; // true INF > INF; // true 

So it is clear that there are several relationships between NAN and NAN (and between INF and INF), when there should be only one. In many languages ​​(most? All?) With "unordered" floats, IEEE-754 means that NaN < NaN is false, NaN > NaN is false, and NaN == NaN is false. Does this mean that PHP does not use the IEEE-754 floating point numbers?

+7
floating-point php ieee-754
source share
1 answer

It is useful to separate two ideas:

  • Floating point format
  • Language rules for the behavior of numbers.

Language standards bodies may indicate or leave unspecified as much or as little for IEEE floating point behavior as they see fit. You cannot tell how NaN comparisons behave, regardless of whether the IEEE floating point format is used.

For example, Java indicates a behavior for float and double that would be very difficult to implement without using the 32-bit and 64-bit IEEE 754 binary formats. On the other hand, Float and Double have comparison methods that consider NaN to be equal and more. than all other floating point numbers.

According to the PHP Floating Point Language Reference "Although this is system dependent, PHP typically uses the IEEE 754 double-precision format .."

+3
source share

All Articles