Calculating Powers using integer math ??

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Calculating Powers using integer math ??

Post 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

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: Calculating Powers using integer math ??

Post by cobra1 »

I have figured this out now :)

hyperion007
Posts: 528
Joined: Sat Dec 01, 2012 1:23 pm
Location: Sweden
Has thanked: 49 times
Been thanked: 101 times
Contact:

Re: Calculating Powers using integer math ??

Post by hyperion007 »

Hi,

Please share your findings with the rest of us :)

Thanks

shayanjameel08
Posts: 4
Joined: Mon Dec 16, 2013 7:48 am
Been thanked: 1 time
Contact:

Re: Calculating Powers using integer math ??

Post by shayanjameel08 »

Hi tell me that how to find unit digit with fractions raised to powers ???

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Calculating Powers using integer math ??

Post 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

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times
Contact:

Re: Calculating Powers using integer math ??

Post 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

cobra1
Posts: 175
Joined: Thu Feb 04, 2010 7:44 am
Has thanked: 3 times
Been thanked: 3 times
Contact:

Re: Calculating Powers using integer math ??

Post 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

Post Reply