Questions / Quant development, Numerics & testing

Binary floating point is approximate

Foundation, about 6 minutes. Question D036.

In typical IEEE-754 binary floating-point arithmetic, why can 0.1 + 0.2 differ from exactly 0.3?

  1. AThe format stores these fractions exactly but adds them in decimal.
  2. BThe mismatch comes solely from printing an exactly stored decimal sum.
  3. CThe CPU may add them with extra precision, then truncate.
  4. DFinite binary formats approximate many decimal fractions.

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.

  1. A finite binary representation can exactly express only a restricted set of rational values.
  2. Decimals such as 0.1 and 0.2 generally require rounding when stored in that format.
  3. 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.

More numerics & testing questions