[Solved] Help? Math Error or Rounding Issue?

Hi All,

I'm hoping someone here can please help, I'm getting serious code rage!

Basically I'm trying to use a BMP085 sensor but I'm getting an unexpected result from one of the math equations. I have checked against other examples (adafruit, sparkfun) but can't see how my code is different and would be causing this result.

The calculation of X2 is causing all the grief, using the datasheet example it says X2 should be -2344 but I get -2343. I'm using the code below (I've hacked this example together to simplify it down to just this equation issue as much as possible).

Here is the code:

//BMP085 Variables
unsigned int BMP085_AC5; 
unsigned int BMP085_AC6;
int BMP085_MC;
int BMP085_MD;

long BMP085_B5;         //Calculated value for true temperature and pressure 


void setup()
{
  long UT=27898;             //Uncompensated Temperature - Datasheet value
  long TrueTemperature;    //Stores the calculated temperature 

  BMP085_AC5=32757;       //Change Variables to datasheet values
  BMP085_AC6=23153;
  BMP085_MC=-8711;
  BMP085_MD=2868;  

  Serial.begin(9600);
  
  TrueTemperature=BMP085_Calculate_Temperature(UT);
  Serial.print("Temperature in C is (*0.1): "); Serial.println(TrueTemperature);
}


void loop()
{
  
}


// Calculate the true temperature from UT
long BMP085_Calculate_Temperature(long UT)
{
  long X1;
  long X2; //Temporary values to hold calculations
 
  X1=(UT-(long)BMP085_AC6)*((long)BMP085_AC5)>>15;

  X2=((long)BMP085_MC<<11)/(X1+(long)BMP085_MD);

  BMP085_B5=X1+X2;

  Serial.print("UT= "); Serial.println(UT);
  Serial.print("X1 = "); Serial.println(X1, DEC);
  Serial.print("X2 = "); Serial.println(X2, DEC);
  Serial.print("B5 = "); Serial.println(BMP085_B5, DEC);
  
  return ((BMP085_B5+8)>>4);
}
  • When I recreate the same formula in excel to zero decimal places I get the correct result.
  • I've tried testing it using direct substitution of some/all of the values and I haven't been able to pin point the issue.
  • I am wondering if maybe it is some sort of rounding issue caused by the calculation and a long value type.

Any help would be greatly received.

Thanks,
Shaz

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Excel uses floating point values and rounds. The Arduino is using integer math. It's not surprising to get a different answer when comparing floating point math calculation and integer math calculations. This is particularly true when you division. Integer math generally truncates the number, floating point math will round the numbers.

Thanks for the reply DD.

I guess I was concerned because the datasheet specifies int/long for the variables in its example and shows a different result.

So if this is "correct" any ideas whether I should be doing all the calculations as floating point values first?

Cheers,
Shaz

I've decided to live with the rounding error as the effect on the calculations is very small. Both of the example libraries I have seen also have the same behavior.

Thank you. :slight_smile:

I found an interesting paper which talks about the rounding errors using integer math causes with the BMP085.

I'm not sure, but I think the Arduino can perform the floating point equations suggested in the paper. Of course floating point math on the Arduino is much slower than integer math.

If being able to use the floating point equations at high speed is important, you might want to look for a controller with hardware floating point math capabilities.