Questions / Quant development, Numerics & testing
Binary floating point is approximate
In typical IEEE-754 binary floating-point arithmetic, why can 0.1 + 0.2 differ from exactly 0.3?
Answer it in the app It's free. You get hints, a second try if you miss, and a follow-up question.
Show the answer and worked solution
Answer: D. Finite binary formats approximate many decimal fractions.
- A finite binary representation can exactly express only a restricted set of rational values.
- Decimals such as 0.1 and 0.2 generally require rounding when stored in that format.
- Addition operates on the stored approximations and rounds its result, so equality with a separately rounded 0.3 is not guaranteed.
Interview shortcut. 0.1 has no exact binary form; sums round.
Takeaway. Inspect representation and operation error before changing an algorithm.
Why the other choices fail
- The format stores these fractions exactly but adds them in decimal. The decimal fractions themselves are generally approximated in the binary format before addition occurs.
- The mismatch comes solely from printing an exactly stored decimal sum. Formatting can expose or hide a discrepancy, but the underlying binary result need not equal the representation of 0.3.
- The CPU may add them with extra precision, then truncate. Excess precision is not why the stored values differ.
In the app this has a follow-up where one thing about the setup changes. Try the follow-up.