Hi everyone,
I am trying to apply a mathematical model (5th order polynomial) to a sensor output value on a Teensy3.6.
I know there is only a 2-decimal precision and that the usual workaround is to multiply by several power of 10. I have been changing things over, variable types, multiplying, dividing but in the end I am either loosing precision or overflowing... I am not sure I make the best use of variable type either ( I know that on Teensy 3.X there are some 64-bit variables)
Any advice ?
Many thanks
I know that float a=0.042866 is not right as it truncates a to 0.04
I am trying to apply a mathematical model (5th order polynomial) to a sensor output value on a Teensy3.6.
I know there is only a 2-decimal precision and that the usual workaround is to multiply by several power of 10. I have been changing things over, variable types, multiplying, dividing but in the end I am either loosing precision or overflowing... I am not sure I make the best use of variable type either ( I know that on Teensy 3.X there are some 64-bit variables)
Any advice ?
Many thanks
Code:
void setup() {
Serial.begin(9600);
}
void loop() {
int rawMin=5;
int rawMax=700;
double calMin=calibrate_sensor(rawMin);
double calMax=calibrate_sensor(rawMax);
Serial.println(calMin); // calibrated value should be close to 0.001
Serial.println(calMax); // calibrated value should be close to 3.927
}
double calibrate_sensor(int sensorOutput){
float a=0.042866;
float b=0.321221;
float c=0.875095;
float d=1.249905;
float e=1.610887;
float f=1.529406;
double mean=540.4259;
double std=212.4908;
double x=(sensorOutput-mean)/std; // transpose
double result=a*pow(x,5)+b*pow(x,4)+c*pow(x,3)+d*pow(x,2)+e*x+f;
return result;
}
I know that float a=0.042866 is not right as it truncates a to 0.04