/ THE IDEA
Humans usually write fractions in base 10. Computers usually store floating-point fractions in base 2. Some values end neatly in one base but repeat forever in another. One third is 0.333… in decimal. In the same way, one tenth becomes 0.000110011… in binary. A finite computer must cut that expansion off and store a nearby value.
THE FORMAL IDEA
0.1₁₀ = 0.0001100110011…₂
| subscript 10 = written in decimal | | subscript 2 = written in binary | | the repeating tail must be rounded to fit finite memory |
|
RUN THE TINY EXAMPLE
Compare with a tolerance
Computed: 0.1 + 0.2 = 0.30000000000000004 JavaScript exact test: result === 0.3 → false Tolerance test: |result − 0.3| < 10⁻⁹ → true
|
The tolerance asks the question you normally mean: are these values close enough for this job? The right tolerance depends on the values’ scale and the application. For currency, integer pennies or decimal arithmetic may be more appropriate.
/ SO WHAT?
You can predict where bugs hide: repeated additions, subtracting nearly equal values, comparing for exact equality and mixing very large with very small numbers. The representation is an approximation, so numerical methods must manage approximation deliberately.
ONE CAVEAT |
| Floating point is not random or generally inaccurate. IEEE 754 arithmetic is carefully specified and extremely useful; the error becomes dangerous when code assumes every decimal fraction is represented exactly. |
KEEP THIS
A computer stores many decimal numbers as the closest binary fraction, so decide how much numerical error your task can tolerate.
|
NEXT: Relativity is running inside GPS
|