Jump to content
  • Advertisement
Sign in to follow this  
shaobohou

problem with pow()

This topic is 5409 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

I wrote some code to solve cubic polynomial equations however I appear to have encountered a bug, when I tried to do the following: pow(-8.0, 1.0 / 3.0); in other word cubic root of -8, which should be -2, the code returns non sensible answers, i.e. nan or something (the exact escapes me at the moment). This apppears to be true when I try to take the cubic root of any negative numbers. Is this caused by the specific implementation of pow()? I use dev-cpp 4.9.8 under windows.

Share this post


Link to post
Share on other sites
Advertisement
According to my nearest documentation on pow(), if the base is negative, then the exponent must be a whole number. You can't do pow(0, 0), either. If you find an implementation that does otherwise, you shouldn't rely on it.

Share this post


Link to post
Share on other sites
Quote:

The schoolbook definition of the cube root of a negative number is (-x)1/3 = -1(x1/3).


So you can make it work like this:


-1.0 * pow(8.0, (1.0 / 3.0));


[Edited by - snk_kid on September 25, 2004 10:13:13 AM]

Share this post


Link to post
Share on other sites
I'm not an expert on the standards of math functions, but part of the answer is that most floating-point numbers cannot be stored exactly - they can only be approximated to a certain precision. For example, 1/3 in binary is approximately 0.0101010101010101012. Mathematically, (-8)0.0101010101010101012 is not real. The simplest solution, as snk_kid said, is to take the negative of the cube root of +8.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!