Does anyone have a routine that divides a 16 bit number by another 16 Bit number, and gives the result to 2 decimal places (i.e.answer= 1663.65). I have looked all over the web and this site and cannot find one. I have looked at Microchip AN575 but sorry it lost me!
Thanks Gary
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads
Hello,
My suggestion would be to multiply the first 16 bit integer by 100 creating
a 24 bit resultant integer. Then divide this by the second 16 bit integer
obtaining a 24 bit resultant integer. Then just insert the decimal point at
the appropriate location when displaying the result. In other words instead
of trying to deal with floating point math, just do it as all integer.
(AN617) Hope this helps.
questuk wrote:
>
> Does anyone have a routine that divides a 16 bit number by another
> 16 Bit number, and gives the result to 2 decimal places
> (i.e.answer= 1663.65). I have looked all over the web and this site
> and cannot find one. I have looked at Microchip AN575 but sorry it
> lost me!
>
Assume the number is stored in memory as:
(MS Byte)(LS Byte)
There is a binary point (decimal point equivalent for binary numbers) to
the right of the LS byte of the number. So if we consider the number as:
(MS Byte)(LS Byte).(00000000)
which is a 24-bit binary number with the least significant bit
representing the number 1/256 (= 0.00390625 in decimal) or about 2.5
decimal places of precision.
Dividing the 24-bit number by a 16 bit number (using any of the
appropriate techniques) results in a number that should fulfill your
requirements.
Consider 51966 divided by 64:
51966 = 0xCAFE = 1100101011111110 = 1100101011111110.00000000
I chose the 64 for simplicity, to show how this works, since
dividing by 64 is just a right shift of 6 bits, therefore just
like moving the binary point left 6 bits,
1100101011111110.00000000/64
= 1100101011.11111000000000
of course in memory this would look like
(11001010)(11111110).(00000000) before divide
(00000011)(00101011).(11111000) after divide
which is 512+256+32+8+2+1+(1/2)+(1/4)+(1/8)+(1/16)+(1/32)
or 811.96875
David W. Gulley
Destiny Designs
-- http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads
> Does anyone have a routine that divides a 16 bit number by another 16 =
> Bit number, and gives the result to 2 decimal places (i.e.answer=3D =
> 1663.65). I have looked all over the web and this site and cannot find =
> one. I have looked at Microchip AN575 but sorry it lost me!
>
> Thanks Gary
You want to divide and obtain something with two *decimal* places or a
fixed point division in binary ? If you want the second kind, shift the 16
bit number left 2 bits (obtaining a 18 bit number), and divide. The result
will be on 18 bits too (worst case) and the lowest two bits will be the
desired binary 'decimals'.
If you really really want decimal decimals (i.e. convert and obtain as you
showed) then you need to multiply the number by 100 and then integer
divide. Thus the input number will be represented on ~23 bits (not 16).
There are other more brainy and elegant ways to do this of course.