This is certainly safe, but you need to consider what this means for your algorithms. If your algorithm uses factor
as a divisor (and does not check its division by zero), then yes, it is reasonable to check factor != 0.0f
before calling applyComplexAlgorithm(factor)
.
Now, whether you need to check a value smaller than any epsilon before using factor
depends entirely on what your code means and cannot be determined separately from the code you provided.
If (as you noted in another comment), you want to use the special value 0.0f
as a sentinel value, which means something specific (for example, the inability to calculate the coefficient), then yes, it is absolutely safe to compare using ==
. For example, the following use of the 0.0f
code is deterministic and never undergoes any rounding 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);
Greg hewgill
source share