Skip to main content
gamedev.net
🔒 Locked

pow() problem

Started by shaobohou Feb 22, 2005 at 9:05 AM 3 replies 900+ views
Original Post
shaobohou
shaobohou
Can anyone tell me why pow(-8.0, 1.0 / 3.0) (cubic root of -8) returns nan (in linux) and negative infinity or something (in devcpp in windows), instead of -2? The problem seems to be that pow can't handle cubic root of negative number, I mean, a calculator could do it or am I using the wrong function here. My current solution is a hack which detects negative input and handle it differently. I suspect it is something to do with the fact the power is represented as a floating point value.
Just because it is not nice, doesn''t mean it is not miraculous.
Charles B
Charles B
I suppose that it's implemented this way : exp(1/3*log(-8)) thus the NAN error.
"Coding math tricks in asm is more fun than Java"
shaobohou
shaobohou
Any idea why calculators don't have the same problem, how do they implement this sort of thing?
Just because it is not nice, doesn''t mean it is not miraculous.
Anon Mike
Anon Mike
A quick google yielded a couple sites that say that for pow(x, y) if x is negative y must be an integer. My guess would be that it has something to do with avoiding getting into the whole complex roots thing.
-Mike
Robot Guy
Robot Guy
I just checked the C spec and it says :

The pow functions compute x raised to the power y. A domain error occurs if x is finite and negative and y is finite and not an integer value. A domain error may occur if x is zero and y is less than or equal to zero. A range error may occur.

If you really want to compute a cube root (rather than an arbitary root) you should probably use the cbrt function (assuming that you have a compiler that adheres. to the 1999 standard).

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.