PHP Reference ยท Math Functions
Floating-point precision in PHP
Why PHP floats are inexact, how to compare them safely with an epsilon, round for display, and use integers or BCMath for money and exact decimals.
PHP's float type is a 64-bit IEEE 754 double, the same format used by JavaScript, Java's double and most other languages. It stores numbers in binary, and many everyday decimal fractions such as 0.1 have no exact binary form. The stored value is the nearest representable one, so tiny errors appear and can pile up.
This is not a PHP bug, and it does not mean floats are unusable. It means you must pick the right tool: floats for measurements and scientific values, integers or BCMath for money and counts that must be exact, and an explicit tolerance whenever you compare floats.
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
How the error arises
A float stores a sign, a 52-bit fraction and an exponent. Numbers like 0.5 and 0.25 are sums of powers of two and are exact. 0.1 is like one third in decimal (0.3333...): its binary expansion never ends, so it is cut off. The cut-off values for 0.1 and 0.2 add up to a number that is a hair above the stored 0.3.
Important consequences:
- Doubles carry about 15 to 17 significant decimal digits.
PHP_FLOAT_DIGis 15, the number of digits that survive a round trip through a decimal string. - Integers up to
2**53are exact as floats. Above that, some integers cannot be represented. PHP_FLOAT_EPSILON(about2.22e-16) is the gap between1.0and the next float. It is a scale for relative error, not a universal tolerance.
Rules of thumb
- Never compare floats with
==unless both come from the same exact computation. Compare the absolute difference against a small tolerance (as innearlyEqual()above). - Keep money in integers (cents) or in strings with BCMath. Convert to a decimal format only for display.
- Round at the end, not after every step. Use
round()for the value andnumber_format()orprintf("%.2f")for text. - Do not use
floor()/(int)on results like0.1 + 0.7times 10 without rounding first; the value can be7.999.... - Be careful with
round()on halves. A value like1.955is stored slightly below or above the half, so the result may not match school arithmetic. PHP'sround()uses a pre-rounding check that fixes many cases, but not all of them are predictable. If it matters, use integers or BCMath. echohides digits. Output is rounded using theprecisionsetting (14 by default), whilevar_dump()andvar_export()useserialize_precision(-1: the shortest string that reads back exactly).