Reputation: 2120
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);
Upvotes: 8
Views: 3382
Reputation: 993183
It's certainly safe, but you do have to consider what it means for your algorithms. If your algorithm uses factor
as a divisor (and doesn't check for divide-by-zero itself), then yes, it's perfectly reasonable to check for factor != 0.0f
before calling applyComplexAlgorithm(factor)
.
Now, whether or not you should be checking for a value less than some epsilon before using factor
, is totally up to what your code means and cannot be determined in isolation with the code you've provided.
If (as you alluded to in another comment) you want to use the special value 0.0f
as a sentinel value that means something specific (such as the inability to calculate a factor), then yes, it's absolutely safe to compare using ==
. For example, the following code's use of 0.0f
is deterministic and is never subject to any kind of roundoff error:
float calculateFactor()
{
int phase = moon_phase();
if (phase == FULL) { // whatever special case requires returning 0.0f
return 0.0f;
} else {
return 1.0 + (phase * phase); // something that is never 0.0f
}
}
float factor = calculateFactor();
if(factor != 0.0f)
applyComplexAlgorithm(factor);
Upvotes: 4
Reputation: 606
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.
Upvotes: 10
Reputation: 346317
No, it's not safe, because the calculation in calculateFactor()
will probably not result in 0.0 even through it arithmetically should. A trivial example: (0.4-0.1)-0.3 when done using double
results in 5.551115123125783e-17
Upvotes: 6