Re: Maths Problem
Kübek Tonyemail (remove spam text)
Well as Tony Nixon suggested an fixed format is the way to go here.
I would suggest an 16.8 or if range is enough 8.8.
It's then very easy to scale this by using Nicolai's code generator.
For example for 16.8 (binary) to 6.2 ( fixed point with 2 digits after
decimalpoint ) you need to scale something like this:
16.8 / 256 is the integer part, but we also want 2 digit from the
part and hence we add * 100 and gets:
16.8 * 100 / 256 = 16.8 * 0.390625
Enter this in the code generator ( i.e constant 0.390625 )
input size 24 bits. And all the code you need will be generated.
Example ( using your value here ):
26.47 ( decimal ) would be in 16.8 format:
Integer-part -> 26 = top two bytes = 0x001A in hexadecimal.
Decimal part -> 47 * 2.56 = 120.32 ( this code could also be generated
by above code generator ), we cut the decimal part and have 120 left
0x78 in hex.
We now have the original value in 16.8 format as:
0x001A78 ( which btw is 6776 decimal if treated as 24 bit value )
Run this though the code generated above ( 0x001A78 * 0.390625 )
and you will have:
0x000A56 or 0x000A57 ( depending how you handle rounding )
which is 2646 or 2647 in decimal. Now as we have decided to have an
'fixed' point with two decimals you already know where to set the
point. take the 24 bit value run it through the 24->ascii routine found
piclist site, place the decimal point two digits from the last and there
Tony Kübek, Flintab AB
http://www.piclist.com hint: The PICList is archived three different
ways. See http://www.piclist.com/#archives for details.
See also: www.piclist.com/techref/microchip/math/index.htm?key=math
You must be a member of the
piclist mailing list
(not only a www.piclist.com member) to post to the