cbrt() function only working on constant variables.

Hello hello, first time poster and programmer here.

I’m making a cool project for a math class that is supposed to showcase a related rates problem. Every thing was going great until I discovered that asking the Arduino to take a cube root wasn’t as easy as I thought. So i went on Google and found out that I needed a library called math.h to use a function called cbrt on it.

A few things i noticed where that although using the cbrt function on a constant worked, using it on a changing variables like reading an analog value off a pin, and then using the cbrt() function did not. I also noticed that although cbrt function seems to work, when I type #include <math.h> the math text doesn’t turn orange like if I used an included library like Servo.h. what’s up with that?

Here’s my sketch; It’s quite a mess:

//Libraries
#include <LiquidCrystal.h>
#include <Servo.h>
#include <math.h>

//constant vars

const int pot = 5;
const float pi = 3.14;


//Objects
Servo myservo;
LiquidCrystal lcd(12,11,5,4,3,2);

// vars
int servorate;
int printvar;
float IRC;
float radius;
float time;


void setup()
{
  
 myservo.attach(6);
 Serial.begin(9600);
 lcd.begin(4,20);
 
 
 lcd.setCursor(2,1);
 lcd.print("Goodman & Harris");
 lcd.setCursor(5,2);
 lcd.print("- 2013 -");
 delay(5000);
lcd.clear(); 

}



void loop()
{
 printvar = analogRead(pot);
 servorate = printvar;

 
 printvar = map(printvar,0,1023,0,59);
 printvar = printvar + 1;
 
 
 IRC = (((10000-(printvar*100))/((4/3)*pi)));
 IRC = cbrt(IRC);
 
 //map printvar to servorate, write to servo
 servorate = map(printvar,1,60,18,170);
 myservo.write(servorate);
 

 
 
 
 Serial.println(printvar);
 lcd.setCursor(0,0);
 lcd.print("Time = ");
 lcd.print(printvar);
 lcd.print(" seconds");
 lcd.setCursor(0,2);
 lcd.print("IRC = ");
 lcd.print(IRC);
 
 delay(1);
  
}

The code won’t compile and returns the error " Undefined reference to cbrt."

Thanks in advanced,
-M

Lots of versions of c define math.h but they are not all the same, and the arduino one might not have this alleged function in it, I have never heard of it.

It's quite easy to take a cube root without this so-called cbrt() function.

double x=777.0 ;
double y = exp( (log(x)/3.0) ) ; // will be the cube root of x.

double x=777.0 ;
double y =pow(x, 0.33333333) ;   //  will be the cube root of x.

michinyon:
Lots of versions of c define math.h but they are not all the same, and the arduino one might not have this alleged function in it, I have never heard of it.

The ARM libs have it, but not the AVR libs

It's quite easy to take a cube root without this so-called cbrt() function.

double x=777.0 ;
double y = exp( (log(x)/3.0) ) ; // will be the cube root of x.

Nice!

Excellent!

those methods for finding cube roots worked like a charm, thanks!

I guess I didn't need avr-libc after all.

(and they say no one ever uses algebra!) :slight_smile: