I'm looking to consistently solve the m^n case, including conditions where m is negative and n is non-integer. I'd like to, additionally, catch the error when it isn't possible.

Some examples to think about.

(-.5)^(.2) which is, effectively, the fifth root of (-1/2) which has a solution. If I were to simplify the numbers as something like (-1/32)^(1/5) then the answer is clearly -1/2. However, pow(m,n) will not solve that correctly.

Let's be explicit and compare that to the condition like (-.5)^.125, which is the eighth root, since the power is 1/8, and therefore the code would need to throw/catch that as isComplexNumber error.

The thing is, I know I can make an attempt to resolve the fractional equivalency of the number, check the base, and if it's an odd then negative the result of pow(abs(m),n), but due to irregularities in doubles, it's hard sometimes to resolve that decimal exponent into a fraction. What's more that's just inelegant coding.

The other potential solution path I see is to create a for-loop which brute-force solves for the root using guess and check (with some marginal inherent inaccuracy), which is even less attractive.

Thoughts? This has been solved by TI, Mathematica, etc. Am I missing something basic?

  • 2
    $\begingroup$ Do you want to catch them as errors, or would you prefer to handle them as complex numbers from the beginning? $\endgroup$ – abiessu Nov 27 '13 at 3:15
  • 1
    $\begingroup$ It seems like this question is missing some context. What is $\text{pow}(m,n)$? What is the isComplexNumber error? Are you talking about some particular programming language? (Also, it seems like this might be more of a programming question than a math question, but I could be wrong about that.) $\endgroup$ – Trevor Wilson Nov 27 '13 at 3:33
  • $\begingroup$ If you want real roots, I would suggest looking into De Moivre's formula and trying to find a value that makes the $\sin$ (imaginary) term disappear. $\endgroup$ – apnorton Nov 27 '13 at 3:34
  • $\begingroup$ You don't say what the $m^n$ "case" is. The $f(z) = z^m$ is well defined on the negative real axis. Some values of m will lead to multiple branches of the function, and you have to figure out which branch you want. You talk about the fifth root of 1/2 -- it has 5 solutions, and so does the fifth root of everything else. Also the 4th root. Wht are you trying to do? $\endgroup$ – Betty Mock Nov 27 '13 at 5:16
  • $\begingroup$ Man! I was unnecessarily vague wasn't I. Apologies. The error call was arbitrary, and pow(m,n) is math library call from C. De Moivres! Right! Okay, wait, what happens when the power is like 1/5, doesn't de moivres extend in the root case to have multiple solutions? How do you pick? $\endgroup$ – Meshach Nov 28 '13 at 10:30

The underlying issue here is that (assuming you want to stay within the real numbers) when $c<0$, the function $c^x$ is undefined for most values of $x$. Specifically, it's undefined unless $x$ is a rational number whose denominator is odd. There is no continuous/differentiable function underlying the places where it is defined.

Therefore, there is no possible guess-and-check algorithm that gradually becomes more accurate. First, guess-and-check algorithms require an underlying continuous function. Second, the value you're seeking might simply not be defined.

So the need to determine whether the exponent is a fraction with odd denominator, which in other contexts might be considered inelegant, here is simply a necessary step in the problem you're trying to solve. (And really, people shouldn't be inputting $c^x$ when $c<0$ and $x$ is a decimal ... they're just asking for trouble, for all the reasons mentioned above.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.