c++performancemathinverse

Performance of reciprocal calculation in C++


Is there any way to improve the performance of calculating the reciprocal of a number without losing precision? Currently I'm simply doing:

1 / (1 + a * b)

Where a and b are double. Value of a is constant. I need to do millions of such calculations.


Solution

  • Let's separate when a == 0, a != 0,

    (c is also constant) then entire expression becomes

        c / (b + c)
    

    This will save one of multiplication calculation.