More precise floating-point comparisons
Whenever improvements are needed, one of the first things to look for is a way to measure the current design. Back in Chapter 5, Adding More Confirm Types, we examined floating-point numbers and I explained that comparing any floating-point type value—float, double, or long double—directly with another floating-point value is a bad idea. The comparison is too sensitive to small rounding errors and will usually result in the two values comparing not equal.
In Chapter 5, I showed you how to add a small margin to the comparison so that an accumulation of errors would not throw off the comparison as long as the two numbers being compared were close enough in value to each other. In other words, two values can compare equal as long as they are close enough to each other.
But what margin should be used? We simply picked some small numbers, and that solution worked. We’re going to improve that solution. And now that you’...