floating-point rounding error

The tiny inaccuracy in float results caused by storing decimal fractions in binary, such as 0.1 + 0.2 giving 0.30000000000000004.

In more detail

It is not a Python bug; every language using standard binary floats behaves the same. Display with a format specifier, compare with math.isclose(a, b) instead of ==, and use the decimal module or whole numbers of cents for money.

Example

import math
math.isclose(0.1 + 0.2, 0.3)   # True

Related terms

Taught in

References

Certification courses