We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › float g = pow(-0.21027565, 1.2674413); => NaN
Page Index Toggle Pages: 1
float g = pow(-0.21027565, 1.2674413); => NaN (Read 286 times)
float g = pow(-0.21027565, 1.2674413); => NaN
Oct 22nd, 2008, 6:24am
 
Hi everyone

I was programming and I got into a situation where I had to rise a float number to the power of another float number. It all was going fine until the base number became negative as in the example below:

float g = pow(-0.21027565, 1.2674413);
println(g);

This ended up printing a NaN message. I fixed my problem with an if statement and a -1 multiplication, yet I don't seem to understand where does the error come from.
If any of you know, it would be nice to hear it.

Cheers
Re: float g = pow(-0.21027565, 1.2674413); => N
Reply #1 - Oct 22nd, 2008, 5:10pm
 
pow(a,b) returns NaN for negative a and fractional b -- the result is complex and plain old pow() can't do it.  perhaps (?) it's clearer to see that pow(a,b) implies exp(b*log(a)), and log(a) is undefined for negative values. btw, also note that -pow(a,b) != pow(-a,b).  does that help?
Re: float g = pow(-0.21027565, 1.2674413); => N
Reply #2 - Oct 24th, 2008, 7:51am
 
Thanks davbol that did, help.

I guess doing it right would imply using imaginary numbers, but since I want to use the results as weights to a function, using -pow(a,b) instead of pow(-a,b) should work fine.

Cheers
Page Index Toggle Pages: 1