Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Floating Point Numbers

Floating point numbers, also known as floats, doubles, or real numbers, can be specified using a decimal dot and a mantissa.

They may also use a number separator _: it may be placed anywhere between two digits, to help make the number more readable.

Floats used to be called real, though this was abandoned progressively, since PHP 7.0.

Floats are stored in binary, and most decimal fractions, such as 0.1, have no exact binary representation. This makes direct equality comparison a classic bug: 0.1 + 0.2 === 0.3 is false, since the actual stored value is 0.30000000000000004. To compare floats safely, check that the absolute difference is below a small tolerance (an epsilon), rather than using == or ===.

<?php

    $a = 1.234; 
    $b = 1.2e3; 
    $c = 7E-10;
    $d = 1_234.567; // as of PHP 7.4.0

    var_dump(0.1 + 0.2 === 0.3); // false, classic bug
    var_dump(abs((0.1 + 0.2) - 0.3) < PHP_FLOAT_EPSILON); // true, safe comparison

?>

Documentation

See Also