Fix a bad bit of floating point math, and extract a utility class (with tests) for it.