Searching \ for '[PIC]:16-bit divide to 2 decimal places?' in subject line. ()
Help us get a faster server
FAQ page: massmind.org/techref/microchip/math/index.htm?key=divide
Search entire site for: '16-bit divide to 2 decimal places?'.

Exact match. Not showing close matches.
'[PIC]:16-bit divide to 2 decimal places?'
2001\05\22@080232 by

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

--

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.

Michael Brown
Instant Net Solutions
http://www.KillerPCs.net

{Original Message removed}

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

--

> 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.

Peter

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics

More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...