Skip to content
Tutorials Center
My learning

Topics

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

PHP Seeing the error

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.

PHP Safe comparison and exact alternatives

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_DIG is 15, the number of digits that survive a round trip through a decimal string.
  • Integers up to 2**53 are exact as floats. Above that, some integers cannot be represented.
  • PHP_FLOAT_EPSILON (about 2.22e-16) is the gap between 1.0 and the next float. It is a scale for relative error, not a universal tolerance.

Rules of thumb

  1. Never compare floats with == unless both come from the same exact computation. Compare the absolute difference against a small tolerance (as in nearlyEqual() above).
  2. Keep money in integers (cents) or in strings with BCMath. Convert to a decimal format only for display.
  3. Round at the end, not after every step. Use round() for the value and number_format() or printf("%.2f") for text.
  4. Do not use floor()/(int) on results like 0.1 + 0.7 times 10 without rounding first; the value can be 7.999....
  5. Be careful with round() on halves. A value like 1.955 is stored slightly below or above the half, so the result may not match school arithmetic. PHP's round() uses a pre-rounding check that fixes many cases, but not all of them are predictable. If it matters, use integers or BCMath.
  6. echo hides digits. Output is rounded using the precision setting (14 by default), while var_dump() and var_export() use serialize_precision (-1: the shortest string that reads back exactly).