Practice question · Sort into groups
Sort each way of comparing two floating-point values.
Groups: Safe · Unsafe
- Test a == b
- Test that the absolute difference is below 0.000000001
- Test a != b
- Test that the difference is below a small multiple of the larger magnitude
- Loop while x is not exactly 1.0, increasing x by 0.1 each pass
Hints
- Anything that demands the last bits agree is fragile.
- Inequality is just as strict as equality, it too depends on the last bits.
Show the answer
Safe: Test that the absolute difference is below 0.000000001, Test that the difference is below a small multiple of the larger magnitude
Unsafe: Test a == b, Test a != b, Loop while x is not exactly 1.0, increasing x by 0.1 each pass
Why
Both == and != depend on exact bit agreement, and the loop is worse still: adding 0.1 ten times does not land exactly on 1.0, so it never terminates. Absolute and relative tolerance tests are the safe forms.
Practise Floating-Point Numbers
The app has 6 more questions on this lesson, and keeps your place in the course. Mathematics I is free to start.
More questions on Floating-Point Numbers
- In 1996 the Ariane 5 rocket destroyed itself 37 seconds after launch, from converting a 64-bit float to a…
- In that same toy system, representable values are spaced 0.25 apart. What is the largest possible round-off…
- Select every statement about floating-point arithmetic that is TRUE.
- Floating-point numbers are spaced evenly in a toy system like 1.0, 1.25, 1.5, 1.75, 2.0 - but across the full…