# 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.

## 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 on other sites
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 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 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 on other sites
but the strange thing is that my calculator can do it why should pow() have any problem.

how does the calculator do it anyway?

• 17
• 10
• 21
• 16
• 9