Better way to prevent divide by zero?

DSP, Plugin and Host development discussion.
RELATED
PRODUCTS

Post

davidguda wrote: if(f != f) {
//do something here like giving it a particular value o so..
}
Best to use these for checking nan / finite..

__isfinite();
__isnan();

On MSVC at least, not sure if it's the same on other compilers.
Chris Jones
www.sonigen.com

Post

alexirae wrote:Don't know if this idea could work for you:

Code: Select all

const float eps = 1e-29f; // Or any other epsilon you consider appropriate

float safeDivision(float numerator, float denominator)
{
    float safeDenominator = (denominator > 0.0f) ? max(eps, denominator) : min(-eps, denominator);
    return numerator / safeDenominator;
}
Maybe some experienced coder around here can do this with some bit twiddling black magic :P
Bit twiddling incurs a latency, but a mask could be applied with SSE instead.
Which raises my next question, does condition logic with SSE (floats) use SSE or the ALU?

Post Reply

Return to “DSP and Plugin Development”