Fixed-point and Floating-point Numbers

Also known as: floating-point error, decimal arithmetic, minor units.

Floating-point numbers are fast but cannot hold many decimal fractions exactly; money and other exact quantities are better stored as whole numbers of a small unit.

Synthetic dataExamples on this page use synthetic data created for illustration.

Definition

Floating-point numbers store values in binary with limited precision, so many decimal fractions such as 0.1 cannot be represented exactly. Fixed-point or decimal types store an amount as a whole number of a small unit, such as cents, which keeps addition and comparison of money exact.

In plain terms

Computers usually store non-integers as binary fractions. Just as one third cannot be written exactly in decimal, one tenth cannot be written exactly in binary, so each stored value is a very close neighbour. For measurements such as temperatures that is fine. For money, or for anything that must match to the last digit, the tiny errors add up and break equality checks.

The usual fix is to store the amount as an integer count of the smallest unit (cents) together with the currency, and to do arithmetic on integers. Rounding then happens in one place, by a rule you chose.

Example

In ordinary double-precision floating point:

CalculationResult shown
0.1 + 0.20.30000000000000004
1.1 + 2.23.3000000000000003
0.1 + 0.2 == 0.3false

The same sums in whole cents:

CalculationResult
10 + 2030
110 + 220330
10 + 20 == 30true

Common misunderstandings

  • “Floating point is inaccurate, so never use it.” It is the right tool for measurements and scientific values; the problem is using it where exact decimal amounts are required.
  • “Rounding at the end fixes it.” Sometimes, but errors can already have flipped a comparison or a total along the way.
  • “Decimal types are too slow.” For the volumes of most business data the cost is negligible compared with a wrong total.

References

  1. David Goldberg. What Every Computer Scientist Should Know About Floating-Point Arithmetic . ACM Computing Surveys, 1991. DOI: 10.1145/103162.103163
  2. IEEE Standard for Floating-Point Arithmetic (IEEE 754-2019) . IEEE, 2019. DOI: 10.1109/IEEESTD.2019.8766229