Jump to content
  • Advertisement
Sign in to follow this  
robinei

pow returning -1.#IND

This topic is 4144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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)?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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)).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!