Floating Point Arithmetic: Issues and Limitations¶
Floating-point numbers in Python (represented by the float type) are stored internally in binary format (base 2) using IEEE 754 double-precision representation.
Because computers store fractions using powers of 2 rather than powers of 10, certain decimal fractions cannot be represented exactly.
The Precision Problem¶
Consider the following common test in Python:
Why Does This Happen?¶
In decimal (base 10), the fraction $⅓$ cannot be written as a finite decimal; it repeats endlessly as $0.333333...$.
Similarly, in binary (base 2), the fraction $1/10$ ($0.1$ in decimal) cannot be written as a finite binary fraction; it repeats endlessly:
Because a computer's processor has a finite number of bits (53 bits of precision for IEEE 754 doubles), it must truncate this value. The resulting stored approximation is slightly different from the exact mathematical fraction.
How to Compare Floats Safely¶
Never compare floating-point numbers using the == operator. Instead, use Python's built-in math.isclose() function:
Syntax¶
Rounding in Python: Banker's Rounding¶
Python's built-in round() function implements round half to even (also called Banker's Rounding).
If a number is exactly halfway between two integers, it rounds to the nearest even number:
print(round(2.5)) # Rounds to 2 (nearest even number)
print(round(3.5)) # Rounds to 4 (nearest even number)
print(round(4.5)) # Rounds to 4 (nearest even number)
This strategy minimizes cumulative statistical rounding bias when summing large arrays of numbers.
Exact Calculations: The decimal Module¶
For accounting, banking, financial systems, or any domain where exact decimal precision is required, Python provides the decimal module:
from decimal import Decimal
# Always pass strings to Decimal to prevent float conversion error
price1 = Decimal("0.10")
price2 = Decimal("0.20")
total = price1 + price2
print(total)
print(total == Decimal("0.30"))
Exact Rational Numbers: The fractions Module¶
If you need exact rational arithmetic without decimal conversion, Python provides the fractions module: