Go Down

Topic: Error on pow() with fractional exponent (Read 3261 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?

Groove

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

Try
"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

westfw

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

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