Floating point type (also called floating point number float, double precision number double or real number real) can be defined with any of the following syntax:
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>
Represented in the form of floating point number:
LNUM -9]*[.]{LNUM}) | ({LNUM}[.][0-9]*)
EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-] ? {LNUM})
The word length of floating point numbers is platform-dependent, although typically the maximum value is 1.8e308 with a precision of 14 decimal digits (64-bit IEEE format).
Warning
Precision of floating point numbers
Floating point numbers have limited precision. Although it depends on the system, PHP usually uses the IEEE 754 double format, so the maximum relative error due to rounding is 1.11e-16. Non-basic mathematical operations may give larger errors, and error propagation when doing compound operations needs to be taken into account.
In addition, rational numbers such as 0.1 or 0.7 that can be accurately represented in decimal, no matter how many mantissas there are, cannot be accurately represented by the binary used internally, and therefore cannot be converted to binary format without losing a little bit of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, because the internal representation of the result is actually something like 7.9999999999999991118... .
So never believe that the floating point number result is accurate to the last digit, and never compare whether two floating point numbers are equal. If you really need higher precision, you should use arbitrary precision math functions or the gmp function.
See » Floating Point Guide webpage for a simple explanation.
Convert to Floating Point Numbers
If you wish to learn about when and how to convert a string to a floating point number, see the "Converting a String to a Number" section. For other types of values, the situation is similar to converting the value to an integer and then to a floating point. See the "Converting to Integer" section for more information. As of PHP 5, if you try to convert an object to a floating point number, an E_NOTICE error message is issued.
Compare floating point numbers
As the above warning message states, due to internal expression reasons, there is a problem in comparing two floating point numbers for equality. However, there are roundabout ways to compare floating point values.
To test floating point numbers for equality, use a minimum error value that is only a tiny bit larger than that value. This value, also known as the machine epsilon or smallest unit integer, is the smallest difference value that can be accepted in the calculation.
$a and $b are equal to five decimal places of precision.
<?php
$a = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;
if(abs($a-$b) < $epsilon) {
echo "true";
}
?>
NaN
Certain mathematical operations produce a result represented by the constant NAN. This result represents a value that is undefined or unrepresentable in floating-point arithmetic. The result of any loose or strict comparison of this value with any other value is FALSE.
Since NAN represents any different value, NAN should not be compared with other values, including itself, and should be checked using is_nan().