Go Down

Topic: Error on pow() with fractional exponent (Read 4147 times) previous topic - next topic

Sandro Benigno

I wanna do that kind of calc:

Example: Cubic Root of 64:

float expo = 1/3;
int cubicRoot = pow(64, expo); //the answer is 4
Serial.println(cubicRoot); //but isn't right... it prints 1

Somebody knows what is the trouble here!?  :-[
Do you know another way to do that?


Dec 09, 2009, 03:23 pm Last Edit: Dec 09, 2009, 03:30 pm by GrooveFlotilla Reason: 1
640 = 1.
(Because 1 / 3 = 0)

"float expo = 1.0 / 3.0;"
or even
"float expo = 1 / 3.0;"
should be sufficient.

[edit]BTW, this is not a hardware issue.[/edit]

Per Arduino ad Astra


Don't forget to "round", or even with Groove's fix you may get "3" rather than 4, because
a float value of 3.9999123 (or whatever) cast to an int truncates...

Sandro Benigno

Dec 09, 2009, 05:53 pm Last Edit: Dec 09, 2009, 05:57 pm by playmobil Reason: 1
:o Shame on me! LOL

Thank you guys...
It works now!  ;D

I put 1.0 / 3... and it works perfectly!

(Sorry... I put that post in wrong place)

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131