Sub topic 15
Floating-Point Pitfalls: Inexact Decimals and Non-Associativity
Prerequisites
This chapter assumes that you know binary place values, normalized scientific notation, and the basic fields of an IEEE 754 floating-point number. You should also know that a floating-point value has limited precision and that arithmetic results are rounded to the available format.
Decimal Fractions Are Often Inexact
A decimal fraction has a finite binary representation only when its reduced denominator is a power of two. For example:
- \(0.5 = 1/2\), so it is exact in binary.
- \(0.25 = 1/4\), so it is exact in binary.
- \(0.1 = 1/10\), so it has a repeating binary expansion.
A floating-point format stores only a finite prefix of that repeating expansion. The stored value is therefore the nearest representable number, not the exact mathematical value.
import sys
from decimal import Decimal
print(Decimal(0.1))
print(0.1 + 0.2 == 0.3)
print(sys.float_info.epsilon == 2**-52)A typical output is:
0.1000000000000000055511151231257827021181583404541015625
False
TrueThe Decimal conversion reveals the exact value represented by the binary64 number near 0.1. The difference is tiny, but it can affect comparisons and later computations.
Rounding Error Accumulates
Every operation may produce a result that cannot be represented exactly. The result is rounded again. The error can accumulate in a long calculation, especially when:
- many values are added;
- values have very different magnitudes;
- subtraction removes nearly equal values;
- intermediate results overflow or underflow;
- a calculation repeatedly rounds the same quantity.
The following table summarizes common tests:
| Situation | Main problem | Safer approach |
|---|---|---|
| Testing two floats with `==` | Tiny representation errors | Compare with a tolerance |
| Adding very large and small values | The small value may disappear | Use suitable ordering or compensated summation |
| Subtracting nearly equal values | Loss of significant digits | Reformulate the expression |
| Repeated financial calculations | Binary fractions may not represent decimal cents | Use decimal or integer units |
Addition Is Not Always Associative
For real numbers, addition is associative:
\[ (a+b)+c = a+(b+c) \]
For floating-point numbers, each parenthesized operation rounds separately. Therefore, changing the order can change the result.
a = 1e16
b = -1e16
c = 1.0
left = (a + b) + c
right = a + (b + c)
print(left) # 1.0
print(right) # 0.0In the left expression, a + b is exactly zero before c is added. In the right expression, b + c rounds to -1e16, because adding 1.0 does not change a number of that magnitude in binary64. The final result is therefore zero.
The same principle applies to multiplication and mixed arithmetic. Floating-point operations usually preserve useful approximations, but they do not obey all algebraic identities exactly.
GATE-Style Reasoning
For an exam question, first determine the format and rounding rule. Then check whether each operand is exactly representable. If not, track the rounded stored values rather than the intended decimal values.
A useful shortcut is to compare the magnitude of a small operand with the spacing between adjacent floating-point numbers near the large operand. If the small operand is less than about half that spacing, adding it may have no effect.
When a question asks whether an expression is always true, test extreme magnitudes and cancellation cases. The expression may hold for real numbers but fail for finite-precision numbers.
Exercise
Why can (a + b) + c differ from a + (b + c) in floating-point arithmetic?
Show answer
Each operation rounds its result to the available precision. Different grouping changes the intermediate result, so the rounding errors can differ.