Search code examples
mathdirectxdirectx-10

Performance difference between built in magnitude function and this code


Is there a big difference between using the DirectX vector magnitude math function as opposed to the following code?

float hyp = sqrt(pow(globalVector.x, 2) + pow(globalVector.y, 2))

Solution

  • Not enough for you to care.

    There are improvements that I'd recommend to the function you posted.

    No need to call the power function; x*x and y*y will be just as good and cheaper.

    I'd protect against roundoff by scaling, like this pseudo code:

    if (abs(x) > abs(y)) {
       r = abs(y/x);
       hyp = x*sqrt(1 + r*r);
    } else {
        r = abs(x/y);
        hyp = y*sqrt(1 + r*r);
    }