Practice question · Put in order
Order the steps of diagnosing and repairing a failing exact-equality test on floating-point values.
- Replace the equality test with a check that the absolute difference is below that tolerance
- Re-run and confirm the test now passes for the right reason
- Print the values to full precision and see that they differ in the last digits
- Notice that a test fails although the two values print identically
- Choose a tolerance small compared with the quantities being compared
Hints
- The diagnosis has to come before the repair, and the repair needs a number chosen for this problem.
- You cannot write the tolerance comparison until you have decided what tolerance means here.
Show the answer
- Notice that a test fails although the two values print identically
- Print the values to full precision and see that they differ in the last digits
- Choose a tolerance small compared with the quantities being compared
- Replace the equality test with a check that the absolute difference is below that tolerance
- Re-run and confirm the test now passes for the right reason
Why
The full-precision printout is what turns a baffling failure into an understood one, and the tolerance must be sized to the quantities before the comparison can be written. Choosing a tolerance blindly, say 1e-15 on values of order a million, reproduces the original failure with extra steps.
Practise Program Design for Scientific or Engineering Tasks
The app has 8 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.