PHP - Float


Is double the same as float?


What is a floating point number in PHP?

Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes:

$a = 1.234; 
$b = 1.2e3; 
$c = 7E-10;

What is the size of a floating point number in PHP?

The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format).

Why is comparison of floating point variables problematic and how can work around these limitations?

As 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 = 1.23456789;
$b = 1.23456780;
$epsilon = 0.00001;

if(abs($a-$b) < $epsilon) {
    echo "true";
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License