The precision of floating point is limited. However, depending on the system, PHP usually uses the IEEE754 dual-precision format. The maximum relative error caused by rounding is 1.11e-16. Non-basic mathematical operations may give a larger error, and take into account the error transfer during composite operations. Float (also called float, double or real) can be defined using any of the following syntax:
Floating point representation:
LNUM [0-9] +
DNUM ([0-9] * [\.] {LNUM}) | ({LNUM} [\.] [0-9] *)
EXPONENT_DNUM [+-]? ({LNUM} | {DNUM}) [eE] [+-]? {LNUM })
The length of the floating point is related to the platform, although the maximum value is usually 1.8e308 with 14-bit decimal digits (64-bit IEEE format ).
Warning
Floating point precision
The precision of floating point is limited. However, depending on the system, PHP generally uses the IEEE 754 dual-precision format, the maximum relative error caused by the integer is 1.11e-16. Non-basic mathematical operations may give a larger error, and take into account the error transfer during composite operations.
In addition, rational numbers that can be accurately expressed in decimal format, such as 0.1 or 0.7, cannot be accurately expressed by any number of tails in binary format, therefore, the binary format cannot be converted without a slight loss of precision. This will lead to chaotic results: for example, floor (0.1 + 0.7) * 10) usually returns 7 instead of 8 in expectation, because the internal representation of the result is similar to 7. 9999999999999991118 ....
Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal. If higher precision is required, use any precision mathematical function or gmp function.
See the simple description on the floating point Guide page.
Convert to floating point
For more information about when and how to convert a string to a floating point, see "convert a string to a numeric value. For other types of values, it is similar to converting the value to an integer and then to a floating point. For more information, see "convert to integer. If you try to convert an object to a floating point number from PHP 5, an E_NOTICE error message is sent.
Compare floating point numbers
As the above warning information says, it is a problem to compare two floating point numbers for internal expressions. However, there is still a roundabout way to compare floating point values.
To test whether floating point numbers are equal, use a minimum error value that is only a little bit larger than the value. This value is also known as the epsilon or the smallest unit to take an integer, which is the smallest difference value acceptable in calculation.
Both $ a and $ B have the same precision within the decimal point.
NaN
Some mathematical operations produce a result represented by the constant NAN. This result represents an undefined or undefined value in the floating point number operation. The result of any loose or strict comparison between this value and any other value is FALSE.
Because NAN represents any different value, it should not be compared with other values, including its own, and is_nan () should be used for check.