Pages: [1]   Go Down
Author Topic: Error on pow() with fractional exponent  (Read 3059 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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]

« Last Edit: December 09, 2009, 09:30:57 am by GrooveFlotilla » Logged

Per Arduino ad Astra

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 137
Posts: 6788
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 :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)
« Last Edit: December 09, 2009, 11:57:21 am by playmobil » Logged

Pages: [1]   Go Up
Jump to: