Search code examples
c++mathnumerical-methodspow

Need pow(-1,1.2) to be 1


I am using math.h with GCC and GSL. I was wondering how to get this to evaluate?

I was hoping that the pow function would recognize pow(-1,1.2) as ((-1)^6)^(1/5). But it doesn't.

Does anybody know of a c++ library that will recognize these? Perhaps somebody has a decomposition routine they could share.


Solution

  • It seems like you're looking for pow(abs(x), y).


    Explanation: you seem to be thinking in terms of

    xy = (xN)(y/N)

    If we choose that N === 2, then you have

    (x2)y/2 = ((x2)1/2)y

    But

    (x2)1/2 = |x|

    Substituting gives

    |x|y

    This is a stretch, because the above manipulations only work for non-negative x, but you're the one who chose to use that assumption.