pow returning -1.#IND

Started by
5 comments, last by TheUnbeliever 17 years, 2 months ago
Here's the relevant code:

const float t0 = c2 + sqrt(c3);
const float t1 = 1.0f/3.0f;
const float c4 = pow(t0, t1);

I print these later in the function and the output is: t0 = -12.1406 t1 = 0.333333 c4 = -1.#IND What's going on here? I'm using Visual C++ 2005 Express.
Advertisement
Negative numbers can't be raised to a non-integral power, if you expect to get a real number as a result. I forget what happens if you remove the real number limitation, though. Imaginary numbers might be sufficient.

Think about it. What is (-1)1? (-1)2? (-1)3. If the exponent is odd, then it is -1, and if even, it is +1. But what if it's neither? What result is neither positive nor negative (nor zero)?
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Hmm, my calculator gives me a real number: (-12.1406)^(1/3) = -2.2983

Edit: but I get your point.. I don't know where it get the sign from
That won't work for arbitrary non-integral powers, though. In this case, it happens to be equivelant to the cube root of -12.1406 which is a negative real number.

I'll hazard a guess that the floating point inaccuracy in 1.0f/3.0f is sufficient to mean that this is no longer the case and that the answer is a complex number.

EDIT: As in, you're really doing (-12.1406)^(0.99999998/3.000000001) or something like that (which, to continue the example, Google informs us is (1.14916766 + 1.99041668i) where i is the imaginary unit sqrt(-1)).
[TheUnbeliever]
Quote:Original post by TheUnbeliever
I'll hazard a guess that the floating point inaccuracy in 1.0f/3.0f is sufficient to mean that this is no longer the case and that the answer is a complex number.

Even if floating point accuracy was good enough, it still wouldn't work. pow in C++ can't raise a negative number to a non-integral power because a very large range of these result in complex numbers. For instance if we can trust the data in your original post (debuggers sometimes round-off), then you tried to do:
(-12.1406)^(0.333333) = (-12.1406)^(333333/10^6) = the 10^6-th root of (-12.1406)^(333333)
That is an even root of a negative number, which results in a complex number since no real number when multiplied by itself an even amount of times gets negative (negative * negative = positive, positive*positive=positive). Your calculator may work with rational numbers or it may just force the power function to return a real number.
Thank you guys, I've just pillaged a cube root function from somewhere for now.
Quote:Original post by CTar
pow in C++ can't raise a negative number to a non-integral power because a very large range of these result in complex numbers.


D'oh... Trying to be too smart for my own good.
[TheUnbeliever]

This topic is closed to new replies.

Advertisement