Mathematics I / Floating-Point Numbers
Practice question · Sort into groups

Sort each way of comparing two floating-point values.

Groups: Safe · Unsafe

Hints
  1. Anything that demands the last bits agree is fragile.
  2. 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.

Read the lesson: Floating-Point Numbers →

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