Floating point numbersFloating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes:
Formally as of PHP 7.4.0 (previously, underscores have not been allowed): LNUM [0-9]+(_[0-9]+)* DNUM ({LNUM}?"."{LNUM}) | ({LNUM}"."{LNUM}?) EXPONENT_DNUM (({LNUM} | {DNUM}) [eE][+-]? {LNUM}) The size of a float is platform-dependent, although a maximum of approximately 1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format). Warning
Floating point precisionFloating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.
Additionally, rational numbers that are exactly representable as floating
point numbers in base 10, like So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available. For a "simple" explanation, see the » floating point guide that's also titled "Why don’t my numbers add up?" Converting to floatFrom strings
If the string is
numeric
or leading numeric then it will resolve to the
corresponding float value, otherwise it is converted to zero
( From other typesFor values of other types, the conversion is performed by converting the value to int first and then to float. See Converting to integer for more information.
Comparing floatsAs noted in the warning above, testing floating point values for equality is problematic, due to the way that they are represented internally. However, there are ways to make comparisons of floating point values that work around these limitations. To test floating point values for equality, an upper bound on the relative error due to rounding is used. This value is known as the machine epsilon, or unit roundoff, and is the smallest acceptable difference in calculations. $a and $b are equal to 5 digits of precision.
NaN
Some numeric operations can result in a value represented by the constant
Because |