Page 1 of 1
Calculating Powers using integer math ??
Posted: Mon Nov 25, 2013 8:23 pm
by cobra1
Hi,
I am wondering if these formulas can be calculated using integer math, I have never worked with powers before so I have no idea how to proceed.
Temperature (°C) = Temperature Output Count /(2^14 - 1) x 165 - 40
Humidity (%RH) = Humidity Output Count / (2^14 - 1) x 100%
The formulas can be found here aswell
http://www.embeddedadventures.com/datas ... %20I2C.pdf
Re: Calculating Powers using integer math ??
Posted: Mon Nov 25, 2013 11:29 pm
by cobra1
I have figured this out now

Re: Calculating Powers using integer math ??
Posted: Fri Dec 06, 2013 12:43 pm
by hyperion007
Hi,
Please share your findings with the rest of us
Thanks
Re: Calculating Powers using integer math ??
Posted: Wed Dec 18, 2013 1:21 pm
by shayanjameel08
Hi tell me that how to find unit digit with fractions raised to powers ???
Re: Calculating Powers using integer math ??
Posted: Thu Dec 19, 2013 7:30 pm
by Spanish_dude
He probably did something like this :
Code: Select all
int degree;
int percentage;
int i2c_data;
int i2c_max_data = 16383; // 2^14 - 1
// Read temp, store in i2c_data
degree = (i2c_data * 165) / i2c_max_data - 40; // Result in °C
// Read humidity, store value in i2c_data
percentage = (i2c_data * 100) / i2c_max_data; // Humidity in %
If you divide first, the result will always be 0 except for when the temperature equals 125°C.
And this is why :
1/10 = 0 in integers.
In the real world, this would be 0.1, but you can't have a floating point number with integers (That's why the float variable type exists).
- Nicolas
Re: Calculating Powers using integer math ??
Posted: Fri Dec 20, 2013 8:07 pm
by Spanish_dude
You might want to change the integers to long integers because (2^14 - 1) * 165 is bigger than what can be stored in a two byte integer.
Not sure how the compiler translates this, so just to be on the safe side, I'd change those vars to long int.
- Nicolas
Re: Calculating Powers using integer math ??
Posted: Thu Jan 23, 2014 6:59 pm
by cobra1
Hi, I meant to do an example of how I did this but forgot about it.
Here is the original equation,
Temperature (°C) = Temperature Output Count /(2^14 - 1) x 165 - 40
Humidity (%RH) = Humidity Output Count / (2^14 - 1) x 100%
In my case here I worked out what 2^14 equalled.
in case your not sure how powers work, you simply multiply the first number by itself the amount of times after the ^ symbol. So this 2^14 looks like this (2*2*2*2*2*2*2*2*2*2*2*2*2*2 = 16384)
Then we need to -1 so we have a total of 16383
16383 was stored as an int variable
Once you have this the rest of the equation is straight forward and you can store the results in the relevant size variable. i.e long etc