Shop OBEX P1 Docs P2 Docs Learn Events
Multiplying decs with a Stamp — Parallax Forums

Multiplying decs with a Stamp

ArchiverArchiver Posts: 46,084
edited 2004-04-23 03:03 in General Discussion
Sitting around waiting for parts so thought I would post something for the
benefit of the newer fellows on the block. This has to do with multiplying a
number by a decimal. This works on anything from a BS2 on up.

Suppose you want to multiply 56 by 1.47. The Stamp won't accept decimals so
we have to use a way around it. The tool we use is the star/slash operator:

*/

Here's how we do it. First, we convert 1.47 to a 4-digit hex number.

1 = $01 - that was easy. Now we need to convert the '.47' to a hex number.

Multiply 256 by .47 = 120.32. Now convert 120 to hex = 78 (we drop the .32).

So for this purpose 1.47 in hex = $0178

Then we write

a = 56
a = a*/$0178
debug "a = ", dec a, cr

The screen will display "82". 56 * 1.47 = 82.32, so that's pretty close.
However, we can improve on that tremendously by writing:

a = 56
a = a * 100
a = a*/$0178

If we write

debug "a = ", dec a/100, ".", dec2 a, cr

we get a = 82.25. Now that's CLOSE!

Just remember - a * 100 can not exceed 65535. That's the biggest number
Stamp can handle.

Hope this helps someone out there.

Sid Weaver
Making things easier.....
http://www.visualmuses.com/chipcircuit/index.html





[noparse][[/noparse]Non-text portions of this message have been removed]

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-04-22 17:46
    Hi Sid,

    In your example, you can get even closer to the exact answer with,

    a=56
    y=a */ 37632 ' equiv to a * 1.47
    debug dec y/100,".",dec2 y ' prints 82.32
    'works for values of a: {0 <= a < 446}

    The constant 37632 comes from 1.47*100*256.

    Background, we are finding the constant, K, that satisfies this proportion:
    K/256 = 147/100 ' k=376 or k=$0178
    Just as "1.47" is shorthand for 147/100, the Stamp thinks in binary
    arithmetic, approximating the same proportion as K/256.

    Inside the Stamp, it does "a * y" to get a 32 bit product, and then
    shifts that product 8 bits right, which is the same as dividing by
    256.

    Or, for better accuracy, in decimal times 100:
    K/256 = 14700/100 ' k=37632 or k=$9300
    The number base you use for the */ factor does not matter.

    Note that
    y = a */ 376
    is good for values of a from 0 to 44581, while
    y = a */ 37632
    is good only for values of a from 0 to 445, and there are more
    significant figures in y then there are in a, a dubious situation. A
    compromise is,
    y = a */ 3763
    debug dec y/10,".",dec1 y
    which is good for values of a from 0 to 4458, with answer y from 0 to
    6552.9, and that gives a good basis for roundoff back to 4
    significant digits.

    By tthe way, the */ operator is commutative. That is, a */ k = k */
    a. The proof is left to the end of the chapter!

    -- Tracy








    >Sitting around waiting for parts so thought I would post something for the
    >benefit of the newer fellows on the block. This has to do with multiplying a
    >number by a decimal. This works on anything from a BS2 on up.
    >
    >Suppose you want to multiply 56 by 1.47. The Stamp won't accept decimals so
    >we have to use a way around it. The tool we use is the star/slash operator:
    >
    >*/
    >
    >Here's how we do it. First, we convert 1.47 to a 4-digit hex number.
    >
    >1 = $01 - that was easy. Now we need to convert the '.47' to a hex number.
    >
    >Multiply 256 by .47 = 120.32. Now convert 120 to hex = 78 (we drop the .32).
    >
    >So for this purpose 1.47 in hex = $0178
    >
    >Then we write
    >
    >a = 56
    >a = a*/$0178
    >debug "a = ", dec a, cr
    >
    >The screen will display "82". 56 * 1.47 = 82.32, so that's pretty close.
    >However, we can improve on that tremendously by writing:
    >
    >a = 56
    >a = a * 100
    >a = a*/$0178
    >
    >If we write
    >
    >debug "a = ", dec a/100, ".", dec2 a, cr
    >
    >we get a = 82.25. Now that's CLOSE!
    >
    >Just remember - a * 100 can not exceed 65535. That's the biggest number
    >Stamp can handle.
    >
    >Hope this helps someone out there.
    >
    >Sid Weaver
    >Making things easier.....
    >http://www.visualmuses.com/chipcircuit/index.html
    >
    >
    >
    >
    >
    >[noparse][[/noparse]Non-text portions of this message have been removed]
    >
    >
    >
    >To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    >from the same email address that you subscribed. Text in the
    >Subject and Body of the message will be ignored.
    >
    >Yahoo! Groups Links
    >
    >
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2004-04-23 03:03
    I think that was english....I DID read it....and I think I'll quit now and
    go find that 2by4 to knock myself with! haha

    Well Done Tracy....


    Original Message
    From: "Tracy Allen" <tracy@e...>
    To: <basicstamps@yahoogroups.com>
    Sent: Thursday, April 22, 2004 12:46 PM
    Subject: Re: [noparse][[/noparse]basicstamps] Multiplying decs with a Stamp


    > Hi Sid,
    >
    > In your example, you can get even closer to the exact answer with,
    >
    > a=56
    > y=a */ 37632 ' equiv to a * 1.47
    > debug dec y/100,".",dec2 y ' prints 82.32
    > 'works for values of a: {0 <= a < 446}
    >
    > The constant 37632 comes from 1.47*100*256.
    >
    > Background, we are finding the constant, K, that satisfies this
    proportion:
    > K/256 = 147/100 ' k=376 or k=$0178
    > Just as "1.47" is shorthand for 147/100, the Stamp thinks in binary
    > arithmetic, approximating the same proportion as K/256.
    >
    > Inside the Stamp, it does "a * y" to get a 32 bit product, and then
    > shifts that product 8 bits right, which is the same as dividing by
    > 256.
    >
    > Or, for better accuracy, in decimal times 100:
    > K/256 = 14700/100 ' k=37632 or k=$9300
    > The number base you use for the */ factor does not matter.
    >
    > Note that
    > y = a */ 376
    > is good for values of a from 0 to 44581, while
    > y = a */ 37632
    > is good only for values of a from 0 to 445, and there are more
    > significant figures in y then there are in a, a dubious situation. A
    > compromise is,
    > y = a */ 3763
    > debug dec y/10,".",dec1 y
    > which is good for values of a from 0 to 4458, with answer y from 0 to
    > 6552.9, and that gives a good basis for roundoff back to 4
    > significant digits.
    >
    > By tthe way, the */ operator is commutative. That is, a */ k = k */
    > a. The proof is left to the end of the chapter!
    >
    > -- Tracy
    >
    >
    >
    >
    >
    >
    >
    >
    > >Sitting around waiting for parts so thought I would post something for
    the
    > >benefit of the newer fellows on the block. This has to do with
    multiplying a
    > >number by a decimal. This works on anything from a BS2 on up.
    > >
    > >Suppose you want to multiply 56 by 1.47. The Stamp won't accept decimals
    so
    > >we have to use a way around it. The tool we use is the star/slash
    operator:
    > >
    > >*/
    > >
    > >Here's how we do it. First, we convert 1.47 to a 4-digit hex number.
    > >
    > >1 = $01 - that was easy. Now we need to convert the '.47' to a hex
    number.
    > >
    > >Multiply 256 by .47 = 120.32. Now convert 120 to hex = 78 (we drop the
    .32).
    > >
    > >So for this purpose 1.47 in hex = $0178
    > >
    > >Then we write
    > >
    > >a = 56
    > >a = a*/$0178
    > >debug "a = ", dec a, cr
    > >
    > >The screen will display "82". 56 * 1.47 = 82.32, so that's pretty
    close.
    > >However, we can improve on that tremendously by writing:
    > >
    > >a = 56
    > >a = a * 100
    > >a = a*/$0178
    > >
    > >If we write
    > >
    > >debug "a = ", dec a/100, ".", dec2 a, cr
    > >
    > >we get a = 82.25. Now that's CLOSE!
    > >
    > >Just remember - a * 100 can not exceed 65535. That's the biggest number
    > >Stamp can handle.
    > >
    > >Hope this helps someone out there.
    > >
    > >Sid Weaver
    > >Making things easier.....
    > >http://www.visualmuses.com/chipcircuit/index.html
    > >
    > >
    > >
    > >
    > >
    > >[noparse][[/noparse]Non-text portions of this message have been removed]
    > >
    > >
    > >
    > >To UNSUBSCRIBE, just send mail to:
    > > basicstamps-unsubscribe@yahoogroups.com
    > >from the same email address that you subscribed. Text in the
    > >Subject and Body of the message will be ignored.
    > >
    > >Yahoo! Groups Links
    > >
    > >
    > >
    > >
    >
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    > Yahoo! Groups Links
    >
    >
    >
    >
    >
Sign In or Register to comment.