Shop OBEX P1 Docs P2 Docs Learn Events
32-bit Multiplication Question — Parallax Forums

32-bit Multiplication Question

ArchiverArchiver Posts: 46,084
edited 2003-09-13 02:43 in General Discussion
I had been looking around ways to do 32-bit multiplication with BS2.
Most of the solutions I found only display the upper 32-bit Word for
display purposes using Debug like the following:

ValueX VAR Word
ValueY VAR Word
ValueZ0 VAR Word
VAlueZ1 VAR Word

DO
DEBUG "Value1:"
DEBUGIN HEX4 ValueX
DEBUG CR
DEBUGIN HEX4 ValueY
DEBUG CR

ValueZ0 = ValueX * ValueY
ValueZ1 = ValueY ** ValueY

DEBUG HEX4 ValueX," * ",HEX4 ValueY," = ",HEX4 ValueZ1,HEX4 ValueZ0

What I really want is to work with it rather than just displaying like
the following:

ValueX = 700
ValueY = (ValueX * 1000) / 900

In the real world that would give me 777.7.... But in BS2 if I
multiply 700 * 1000, that would exceed the 16-bit limit. I don't want
to multiply it by 10 or 100. I need more precision. As you can see my
final value that I was looking for is still 16-bit even though I can't
get fractions in BS2 but I'm still happy with 777.

- Johari

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-09-13 02:08
    Johari

    I've posted double and quad precision multiply and divide examples at:
    http://www.emesystems.com/BS2math6.htm

    You have to still have to watch out for the scaling of the variables.
    It depends on what you want to do.

    Al Williams offers a floating point math coprocessor chip.

    -- regards
    Tracy Allen
    electronically monitored ecosystems
    http://www.emesystems.com
    mailto:tracy@e...


    >I had been looking around ways to do 32-bit multiplication with BS2.
    >Most of the solutions I found only display the upper 32-bit Word for
    >display purposes using Debug like the following:
    >
    >ValueX VAR Word
    >ValueY VAR Word
    >ValueZ0 VAR Word
    >VAlueZ1 VAR Word
    >
    >DO
    >DEBUG "Value1:"
    >DEBUGIN HEX4 ValueX
    >DEBUG CR
    >DEBUGIN HEX4 ValueY
    >DEBUG CR
    >
    >ValueZ0 = ValueX * ValueY
    >ValueZ1 = ValueY ** ValueY
    >
    >DEBUG HEX4 ValueX," * ",HEX4 ValueY," = ",HEX4 ValueZ1,HEX4 ValueZ0
    >
    >What I really want is to work with it rather than just displaying like
    >the following:
    >
    >ValueX = 700
    >ValueY = (ValueX * 1000) / 900
    >
    >In the real world that would give me 777.7.... But in BS2 if I
    >multiply 700 * 1000, that would exceed the 16-bit limit. I don't want
    >to multiply it by 10 or 100. I need more precision. As you can see my
    >final value that I was looking for is still 16-bit even though I can't
    >get fractions in BS2 but I'm still happy with 777.
    >
    >- Johari
    >
    >
    >
    >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.
    >
    >
    >Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
  • ArchiverArchiver Posts: 46,084
    edited 2003-09-13 02:43
    >ValueX = 700
    >ValueY = (ValueX * 1000) / 900
    >
    >In the real world that would give me 777.7.... But in BS2 if I
    >multiply 700 * 1000, that would exceed the 16-bit limit. I don't want
    >to multiply it by 10 or 100. I need more precision. As you can see my
    >final value that I was looking for is still 16-bit even though I can't
    >get fractions in BS2 but I'm still happy with 777.
    >
    >- Johari


    For that example, where 1000 and 900 are constants, the multiplier is 10/9, so
    X=700
    y=X * 10/9
    debug dec y,cr
    can be done by the Stamp and prints an answer of 777.

    If you need one more digit of precision you can do,
    X=700
    y = (X * 10/9 *10) + (X*10//9 *10 /9)
    debug dec y/10,".",dec1 y,cr

    which prints 777.7

    Another way to do it is to use the ** operator, which works with a 32
    bit intermediate result.

    X=700
    y = X + (X ** 7282) ' 1/9 = 7282/65536
    debug dec y,cr ' prints 777

    or for more precision,
    y = X*10 + (X*2**36409) '
    debug dec y/10,".",dec1 y,cr ' prints 777.7

    The ** method works even when the constants are not simple like 10/9,
    for example, 1023/917 = 1.1156.

    If the other values are also variables, not constants, then you have
    to fall back on true double precision methods.
    more on ** and multiply by constant at
    http://www.emesystems.com/BS2math1.htm
    and more on double precision at
    http://www.emesystems.com/BS2math6.htm


    -- Tracy
Sign In or Register to comment.