Floating-point comparisons


#1

There are about 282 places in the juce code where float or double values are compared for equality or inequality. (Add -Wfloat-equal to the “Other Warning Flags” in Xcode on Mac, or to the Makefile in linux, to see these.)

Of these, 3 are in header files that seem to get included in almost any juce-using app, which means that client code that uses juce effectively can’t use -Wfloat-equal to catch any potential problems, because they’ll be swamped by the juce warnings.

Also, while many of these are safe (e.g., PathIterator::isLastInSubpath is checking for a value that should only appear if you explicitly set it to that constant; Line::getPointAlongLine is using == 0 as an optimization to skip adding it in), some aren’t (e.g., ColourGradient::ColourPoint::operator== is likely to be wrong sometimes–it just makes an == that’s exactly as unsafe as the underlying double operator).

As a side note, it might be handy to have a function template like this, both for use within juce and, more importantly, for users of juce who might not know how to do the comparisons properly themselves:

template <typename T> bool equalFloats(T x, T y) { return std::abs(x - y) < std::numeric_limits<double>::epsilon(); }


#2

Well, I’d never heard of that warning flag before, thanks for teaching me something new!

Most of the things it’s complaining about are absolutely fine - it seems ridiculous to me that it’d complain about a comparison with 0.0, which must be a completely legit thing to do. I’ll have a quick look and will at least try to remove anything I can from the public headers.