I know it’s dangerous to test floats for equality due to precision errors but is it safe to test for zero? I can think of some cases, for example in optimizing special cases in algorithms, where you would want to do this. The question is regarding floats but I would assume the answer also applies to doubles as well.
Consider the following code:
float factor = calculateFactor();
if(factor != 0.0f)
applyComplexAlgorithm(factor);
It is safe in the sense that if the value is set explicitly to 0.0f, it will return true there.
It is NOT safe in the sense that you should not expect that the value resultant from calculations will be exactly 0.0f.
So you’re really using 0.0f as a special magic value of sorts, not as a real comparison against zero.