Search code examples
c++mathoptimizationsignlerp

Linear Interpolation Optimization For Sign-ness


In a tight loop, I am doing a linear interpolation between two floating point values. However, the only necessary part of the result is the sign (whether the result is negative or positive). I'm doing a typical lerp operation right now, f way between a and b.

a + f * (b - a);

Is there something more efficient considering I just need to know the resulting sign and not the actual lerped value?

Edit: 'f' is a set of fixed distances along the interpolation, which are known beforehand.


Solution

  • You can calculate whether interpolated value changes sign at given range:

    if Sign(a) <> Sign(b) then  //don't forget about zero sign
      change occurs
    

    In this case find f parameter, where lerp = 0

    a + f0 * (b - a) = 0
    f0 = a / (a+b)
    

    For smaller values lerp has the same sign as a, for larger - the same sign as b, so you don't need to calculate lerp value - just compare f with f0