## Trouble with integer math [Ignore]

5 posts / 0 new
Author
Message

Shame on me :( My brain has ceased to function.

I have a int16_t called result.
I wish to divide result by sixteen. I want 2 decimal places of precision.
The whole number is simple... int x = result / 16.
It's the decimal part thats hurting my head.

int tmp = result - (x * 16) that should give me the fractional part, no?

I'm looking for whole_number, decimal_part. But I'm going around in circles :(

i.e. 363/16 = 22.69 whole_number = 22, decimal part = 69

WTF am I doing wrong (today)... ?

EDIT: forget it. I'm a fool :(

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

363.0 / 16 = 22.6875

Your printf() function will look after the rounding up or down. The compiler looks after do the right kind of maths.

If you want to avoid floating point, you need to do a lot of extra work. e.g. we want 2 d.p. so work in units of 0.001 and round up with 0.05

(363 * 1000) / 16 = 22687
rounding
22687 + 5 = 22692
divide by 1000 to get 22
modulo is 692, divide by 10 to get your decimals

Note that you have to cast up to 32-bit to make the 363*1000 avoid any overflows.

You can also work in 200th's rather than 1000th's but is not so intuitive.

David.

200/16 = 12.5

integer division
200/16 = 12

remainder = 200-12*16 = 8

decimal part = 8/16