EmbeddedRelated.com
Forums

Decimal

Started by "mr.mattyg" April 24, 2008
Hi Guys,
I'm trying to think of the best way to input floating point numbers
into my microcontroller and then operate on these numbers with
multiplications and divisions.

My input values will most likely be restricted to a fixed number of
decimal places, say 1 or 2. Would it be worth going through the
hassle of putting these into BCD Floating Point representation, or
just shifting (multiplying by 10) all my numbers to integers and
taking that into consideration when I do my internal math.

One example is, I need to, say, input 17.2 and I want to eventually
divide 250000 by 17.2. To me, right away I would think the easiest
thing to do is shift 17.2 -> 172 and then shift 250000 -> 2500000 and
then do the division.

In the end I'm only always worried about the quotient of the division.

I'm working in assembly on the 68HC12.

Thanks for any help/suggestions,
Matt
Hi,

I would convert decimals to integers and resolve the divide by 10 (or 100 for two decimal places) when you have the quotient result.

If you use a fixed number of decimal places for your input it does simplify the maths.

John

Testometric
materials testing machines
Unit 1, Lincoln Business Park
Lincoln Close, Rochdale
Lancashire, England OL11 1NR
Telephone: +44 (0) 1706 654039
Facsimile: +44 (0) 1706 646089
Website: www.testometric.co.uk

-----Original Message-----
From: 6... [mailto:6...]On Behalf Of mr.mattyg
Sent: 24 April 2008 14:40
To: 6...
Subject: [68HC12] Decimal

Hi Guys,
I'm trying to think of the best way to input floating point numbers
into my microcontroller and then operate on these numbers with
multiplications and divisions.

My input values will most likely be restricted to a fixed number of
decimal places, say 1 or 2. Would it be worth going through the
hassle of putting these into BCD Floating Point representation, or
just shifting (multiplying by 10) all my numbers to integers and
taking that into consideration when I do my internal math.

One example is, I need to, say, input 17.2 and I want to eventually
divide 250000 by 17.2. To me, right away I would think the easiest
thing to do is shift 17.2 -> 172 and then shift 250000 -> 2500000 and
then do the division.

In the end I'm only always worried about the quotient of the division.

I'm working in assembly on the 68HC12.

Thanks for any help/suggestions,
Matt

Click here to report this email as spam.


Use fixed point decimals, that is, one byte for the decimal portion of your
number (or as many as you need) and as many bytes you need for the integer
part. That way it is easy to handle multiplication and division by just
treating the numbers as an integers, only the least significant byte is
actually the fraction of the integer in 1/256 parts.

/Ruben
http://www.rjjournal.net

> Hi Guys,
> I'm trying to think of the best way to input floating point numbers
> into my microcontroller and then operate on these numbers with
> multiplications and divisions.
>
> My input values will most likely be restricted to a fixed number of
> decimal places, say 1 or 2. Would it be worth going through the
> hassle of putting these into BCD Floating Point representation, or
> just shifting (multiplying by 10) all my numbers to integers and
> taking that into consideration when I do my internal math.
>
> One example is, I need to, say, input 17.2 and I want to eventually
> divide 250000 by 17.2. To me, right away I would think the easiest
> thing to do is shift 17.2 -> 172 and then shift 250000 -> 2500000 and
> then do the division.
>
> In the end I'm only always worried about the quotient of the division.
>
> I'm working in assembly on the 68HC12.
>
> Thanks for any help/suggestions,
> Matt
>
Yeah, I was kinda thinking about setting it up like that, but I can't
visualize how I would do the division with separate parts.
thanks,
Matt

On Thu, Apr 24, 2008 at 5:41 PM, Ruben Jsson wrote:

> Use fixed point decimals, that is, one byte for the decimal portion of
> your
> number (or as many as you need) and as many bytes you need for the integer
> part. That way it is easy to handle multiplication and division by just
> treating the numbers as an integers, only the least significant byte is
> actually the fraction of the integer in 1/256 parts.
>
> /Ruben
> http://www.rjjournal.net
>
> > Hi Guys,
> > I'm trying to think of the best way to input floating point numbers
> > into my microcontroller and then operate on these numbers with
> > multiplications and divisions.
> >
> > My input values will most likely be restricted to a fixed number of
> > decimal places, say 1 or 2. Would it be worth going through the
> > hassle of putting these into BCD Floating Point representation, or
> > just shifting (multiplying by 10) all my numbers to integers and
> > taking that into consideration when I do my internal math.
> >
> > One example is, I need to, say, input 17.2 and I want to eventually
> > divide 250000 by 17.2. To me, right away I would think the easiest
> > thing to do is shift 17.2 -> 172 and then shift 250000 -> 2500000 and
> > then do the division.
> >
> > In the end I'm only always worried about the quotient of the division.
> >
> > I'm working in assembly on the 68HC12.
> >
> > Thanks for any help/suggestions,
> > Matt
> >
> >
> >
> >
> >
You don't do it with separate parts, that's the beauty of it. Just treat it as
large integers and deal with the decimal point after each calculation.

For example:

4.5 / 2 becomes 0x000480 / 0x000200 if using one byte as fractional part and
two bytes as integer part. As you can see, the result is 0x000240 (after
adjusting the decimal point) which means 2.25.

Some more about this here:

http://www.gameprogrammer.com/4-fixed.html

/Ruben
http://www.rjjournal.net

> Yeah, I was kinda thinking about setting it up like that, but I can't
> visualize how I would do the division with separate parts.
> thanks,
> Matt
>
> On Thu, Apr 24, 2008 at 5:41 PM, Ruben Jsson wrote:
>
> > Use fixed point decimals, that is, one byte for the decimal portion of
> > your
> > number (or as many as you need) and as many bytes you need for the integer
> > part. That way it is easy to handle multiplication and division by just
> > treating the numbers as an integers, only the least significant byte is
> > actually the fraction of the integer in 1/256 parts.
> >
> > /Ruben
> > http://www.rjjournal.net
> >
> >
> >
> > > Hi Guys,
> > > I'm trying to think of the best way to input floating point numbers
> > > into my microcontroller and then operate on these numbers with
> > > multiplications and divisions.
> > >
> > > My input values will most likely be restricted to a fixed number of
> > > decimal places, say 1 or 2. Would it be worth going through the
> > > hassle of putting these into BCD Floating Point representation, or
> > > just shifting (multiplying by 10) all my numbers to integers and
> > > taking that into consideration when I do my internal math.
> > >
> > > One example is, I need to, say, input 17.2 and I want to eventually
> > > divide 250000 by 17.2. To me, right away I would think the easiest
> > > thing to do is shift 17.2 -> 172 and then shift 250000 -> 2500000 and
> > > then do the division.
> > >
> > > In the end I'm only always worried about the quotient of the division.
> > >
> > > I'm working in assembly on the 68HC12.
> > >
> > > Thanks for any help/suggestions,
> > > Matt
> > >
> > >
> > >
> > >
> > >
Oh yeah, I get it.
Thanks man!

Skate or Die,
Matt

On Thu, Apr 24, 2008 at 10:39 PM, Ruben Jsson wrote:

> You don't do it with separate parts, that's the beauty of it. Just treat
> it as
> large integers and deal with the decimal point after each calculation.
>
> For example:
>
> 4.5 / 2 becomes 0x000480 / 0x000200 if using one byte as fractional part
> and
> two bytes as integer part. As you can see, the result is 0x000240 (after
> adjusting the decimal point) which means 2.25.
>
> Some more about this here:
>
> http://www.gameprogrammer.com/4-fixed.html
> /Ruben
> http://www.rjjournal.net
>
> > Yeah, I was kinda thinking about setting it up like that, but I can't
> > visualize how I would do the division with separate parts.
> > thanks,
> > Matt
> >
> > On Thu, Apr 24, 2008 at 5:41 PM, Ruben Jsson >
> wrote:
> >
> > > Use fixed point decimals, that is, one byte for the decimal portion of
> > > your
> > > number (or as many as you need) and as many bytes you need for the
> integer
> > > part. That way it is easy to handle multiplication and division by just
> > > treating the numbers as an integers, only the least significant byte is
> > > actually the fraction of the integer in 1/256 parts.
> > >
> > > /Ruben
> > > http://www.rjjournal.net
> > >
> > >
> > >
> > > > Hi Guys,
> > > > I'm trying to think of the best way to input floating point numbers
> > > > into my microcontroller and then operate on these numbers with
> > > > multiplications and divisions.
> > > >
> > > > My input values will most likely be restricted to a fixed number of
> > > > decimal places, say 1 or 2. Would it be worth going through the
> > > > hassle of putting these into BCD Floating Point representation, or
> > > > just shifting (multiplying by 10) all my numbers to integers and
> > > > taking that into consideration when I do my internal math.
> > > >
> > > > One example is, I need to, say, input 17.2 and I want to eventually
> > > > divide 250000 by 17.2. To me, right away I would think the easiest
> > > > thing to do is shift 17.2 -> 172 and then shift 250000 -> 2500000 and
> > > > then do the division.
> > > >
> > > > In the end I'm only always worried about the quotient of the
> division.
> > > >
> > > > I'm working in assembly on the 68HC12.
> > > >
> > > > Thanks for any help/suggestions,
> > > > Matt
> > > >
> > > >
> > > >
> > > >
> > > >
Ruben Jsson wrote:

> You don't do it with separate parts, that's the beauty of it. Just treat
> it as
> large integers and deal with the decimal point after each calculation.
>
> For example:
>
> 4.5 / 2 becomes 0x000480 / 0x000200 if using one byte as fractional part
> and
> two bytes as integer part. As you can see, the result is 0x000240 (after
> adjusting the decimal point) which means 2.25.

Yes, but

0x00000480 / 0x00000200 = 0x00000002

You need to shift numerator left by the width of fractional part (<<8bit
positions in this case), then divide:

0x00048000 / 0x00000200 = 0x00000240

It's the same like

2.5 / 1.3 = 25 / 13 . Calculating that with integers we would get fractional
part lost. Shifting 25 left before division we get 250 / 13 = 19.
And when multiplying you mul first 19*13$7, then shift it right and get
24. (1.9*1.3=2.47)

Edward

>
> Some more about this here:
>
> http://www.gameprogrammer.com/4-fixed.html
>
> /Ruben
> http://www.rjjournal.net
>
>> Yeah, I was kinda thinking about setting it up like that, but I can't
>> visualize how I would do the division with separate parts.
>> thanks,
>> Matt
>>
>> On Thu, Apr 24, 2008 at 5:41 PM, Ruben Jsson wrote:
>>
>> > Use fixed point decimals, that is, one byte for the decimal portion
>> > of
>> > your
>> > number (or as many as you need) and as many bytes you need for the
>> > integer
>> > part. That way it is easy to handle multiplication and division by just
>> > treating the numbers as an integers, only the least significant byte is
>> > actually the fraction of the integer in 1/256 parts.
>> >
>> > /Ruben
>> > http://www.rjjournal.net
>> >
>> >
>> >
>> > > Hi Guys,
>> > > I'm trying to think of the best way to input floating point numbers
>> > > into my microcontroller and then operate on these numbers with
>> > > multiplications and divisions.
>> > >
>> > > My input values will most likely be restricted to a fixed number of
>> > > decimal places, say 1 or 2. Would it be worth going through the
>> > > hassle of putting these into BCD Floating Point representation, or
>> > > just shifting (multiplying by 10) all my numbers to integers and
>> > > taking that into consideration when I do my internal math.
>> > >
>> > > One example is, I need to, say, input 17.2 and I want to eventually
>> > > divide 250000 by 17.2. To me, right away I would think the easiest
>> > > thing to do is shift 17.2 -> 172 and then shift 250000 -> 2500000 and
>> > > then do the division.
>> > >
>> > > In the end I'm only always worried about the quotient of the
>> > > division.
>> > >
>> > > I'm working in assembly on the 68HC12.
>> > >
>> > > Thanks for any help/suggestions,
>> > > Matt
>> > >
>> > >
>> > >
>> > >
>> > >
> Ruben Jsson wrote:
>
> > You don't do it with separate parts, that's the beauty of it. Just treat it as
> > large integers and deal with the decimal point after each calculation.
> >
> > For example:
> >
> > 4.5 / 2 becomes 0x000480 / 0x000200 if using one byte as fractional part and
> > two bytes as integer part. As you can see, the result is 0x000240 (after
> > adjusting the decimal point) which means 2.25.
>
> Yes, but
>
> 0x00000480 / 0x00000200 = 0x00000002
>
> You need to shift numerator left by the width of fractional part (<<8bit
> positions in this case), then divide:
>
> 0x00048000 / 0x00000200 = 0x00000240
>
> It's the same like
>
> 2.5 / 1.3 = 25 / 13 . Calculating that with integers we would get fractional
> part lost. Shifting 25 left before division we get 250 / 13 = 19.
> And when multiplying you mul first 19*13$7, then shift it right and get
> 24. (1.9*1.3=2.47)
>
Yes, that's rigth. That's what I meant by adjusting the decimal point. It was a
while ago I did this with assembler routines (Use C and ready made libraries
instead) but I think I did my division routines (shifting and subtracting) so
that they would return more fractional bytes and then adjusted the decimal
point afterwards. But that's just doing the same thing in two different ways I
guess.

0x00000480 / 0x00000200 = 0x0000000240 (two bytes for the fractional part in
the result and only one byte in the operands).

Keep in mind that the (intermediate) result when doing multiplication and
division always has to be larger (byte count wise) than the two operands in
order to not get overflow or loose precision.

/Ruben

http://www.rjjournal.net
=============================Ruben Jsson
AB Liros Electronic
Box 9124, 200 39 Malm Sweden
TEL INT +46 40142078
FAX INT +46 40947388
r...@pp.sbbs.se
=============================