Ok, so I’ve gathered from this question that when you take pow() using a negative number with a non-integral exponent, the result is NaN.
So my question is: Is there some alternative method I can use to get an answer? I just want to be able to do things like this:
MyPowFunction(-3125, 0.2);
and have the result be (correctly) -5.
Any thoughts?
As an addendum, it is possible to do this (somehow). Google has it figured out: https://www.google.com/search?q=%28-3125%29%5E0.2&oq=%28-3125%29%5E0.2
One more note: I’m not interested in a double result–I’m perfectly fine with the result being complex.
If a is positive and b is (complex) arbitrary, then one may define a^b as exp(b log a). This extends directly the case where b is a rational number.
Now, if a is complex (but nonzero), one may define a^b by analogy (proper term is “analytic continuation”) as exp(b Log a), where Log is one possible inverse of exp.
For instance, one possible Log function (there are infintely many, differing by multiples of 2 i pi) is
Check that for any k, this gives an inverse to exp (I skip the details about the domain of definition: just pretend there is only one z, and we want to find Log z such that exp(Log z) = z).
The way you choose k will thus influence the answer. If b is irrational, then
and since k can be arbitrary, exp(2i k pi * b) can be arbitrary close to whatever number you want on the unit circle (unit circle = numbers with unit modulus), there are infinitely many values of “a^b” possible.
If b is rational (like 0.2 = 1/5 in your example), then you’ll get 5 different candidates for a^0.2 (one of which is real).