Shop OBEX P1 Docs P2 Docs Learn Events
Multiplication overflow — Parallax Forums

Multiplication overflow

ArchiverArchiver Posts: 46,084
edited 2003-06-03 16:29 in General Discussion
Hi,

x var word
x = 63744

debug dec x * 2

prints 61952?

Why is this so? How does the overflow work?

Cheers

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-06-03 10:04
    At 08:48 AM 6/3/03 +0000, smorillas wrote:

    >Hi,
    >
    >x var word
    >x = 63744
    >
    >debug dec x * 2
    >
    >prints 61952?
    >
    >Why is this so? How does the overflow work?
    >
    >Cheers

    It's not so much overflow as it is left-side truncation.

    63744 * 2 = 127488 restated in HEX 1F200 . Lop off the HEX 10000 and you're
    left with HEX F200 . F200 in decimal = 61952.

    You may also want to make reference to the "Intermediate Results" Section of
    the PBASIC Manual for more details.



    >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-06-03 13:46
    Hi,

    Umm I still don't fully get it. If I take the number 127488 and minus
    hex 10000 (65653) then I get 61835, not 61952? What I'm trying to do
    is understand how it works so that I can emulate the maths in VB.

    Cheers...


    --- In basicstamps@yahoogroups.com, Bruce Bates <bvbates@u...> wrote:
    > At 08:48 AM 6/3/03 +0000, smorillas wrote:
    >
    > >Hi,
    > >
    > >x var word
    > >x = 63744
    > >
    > >debug dec x * 2
    > >
    > >prints 61952?
    > >
    > >Why is this so? How does the overflow work?
    > >
    > >Cheers
    >
    > It's not so much overflow as it is left-side truncation.
    >
    > 63744 * 2 = 127488 restated in HEX 1F200 . Lop off the HEX 10000
    and you're
    > left with HEX F200 . F200 in decimal = 61952.
    >
    > You may also want to make reference to the "Intermediate Results"
    Section of
    > the PBASIC Manual for more details.
    >
    >
    >
    > >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-06-03 14:00
    At 12:46 PM 6/3/03 +0000, smorillas wrote:
    >Hi,
    >
    >Umm I still don't fully get it. If I take the number 127488 and minus
    >hex 10000 (65653) then I get 61835, not 61952? What I'm trying to do
    >is understand how it works so that I can emulate the maths in VB.
    >
    >Cheers...

    As I mentioned in the posting you sent me offlist, left truncate, don't
    subtract.



    >--- In basicstamps@yahoogroups.com, Bruce Bates <bvbates@u...> wrote:
    > > At 08:48 AM 6/3/03 +0000, smorillas wrote:
    > >
    > > >Hi,
    > > >
    > > >x var word
    > > >x = 63744
    > > >
    > > >debug dec x * 2
    > > >
    > > >prints 61952?
    > > >
    > > >Why is this so? How does the overflow work?
    > > >
    > > >Cheers
    > >
    > > It's not so much overflow as it is left-side truncation.
    > >
    > > 63744 * 2 = 127488 restated in HEX 1F200 . Lop off the HEX 10000
    >and you're
    > > left with HEX F200 . F200 in decimal = 61952.
    > >
    > > You may also want to make reference to the "Intermediate Results"
    >Section of
    > > the PBASIC Manual for more details.
    > >
    > >
    > >
    > > >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/
    >
    >
    >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-06-03 15:12
    Both left truncation and Modules 65536 produce the same result. When I do
    the math, it comes out as expected. You mention below that $10000 is 65653,
    but it's 65536.

    Steve

    Original Message
    From: "smorillas" <egroups@d...>
    To: <basicstamps@yahoogroups.com>
    Sent: Tuesday, June 03, 2003 2:46 PM
    Subject: [noparse][[/noparse]basicstamps] Re: Multiplication overflow


    > Hi,
    >
    > Umm I still don't fully get it. If I take the number 127488 and minus
    > hex 10000 (65653) then I get 61835, not 61952? What I'm trying to do
    > is understand how it works so that I can emulate the maths in VB.
    >
    > Cheers...
    >
    >
    > --- In basicstamps@yahoogroups.com, Bruce Bates <bvbates@u...> wrote:
    > > At 08:48 AM 6/3/03 +0000, smorillas wrote:
    > >
    > > >Hi,
    > > >
    > > >x var word
    > > >x = 63744
    > > >
    > > >debug dec x * 2
    > > >
    > > >prints 61952?
    > > >
    > > >Why is this so? How does the overflow work?
    > > >
    > > >Cheers
    > >
    > > It's not so much overflow as it is left-side truncation.
    > >
    > > 63744 * 2 = 127488 restated in HEX 1F200 . Lop off the HEX 10000
    > and you're
    > > left with HEX F200 . F200 in decimal = 61952.
    > >
    > > You may also want to make reference to the "Intermediate Results"
    > Section of
    > > the PBASIC Manual for more details.
    > >
    > >
    > >
    > > >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/
    >
    >
    > 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-06-03 15:21
    The Stamp has both the * operator for multiplication, and another one, **.

    x = 63744
    y=x*2
    z=x**2
    debug dec z ,tab ' prints 1
    debug dec y ,tab ' prints 61952
    debug ihex z,hex4 y ' prints $1F200

    The ** operator leaves you with the most significant bits of the
    double precision multiplication. As Bruce pointed out, displaying it
    in hex shows the result in a compact form. You can also understand
    the result as the following:
    result = 1*65536 + 61952 = 127488
    or
    result = 1*$10000 + $F200 = $1F200

    There are ways to deal systematically with double precision (32 bit)
    numbers on the Stamp, <http://www.emesystems.com/BS2math6.htm> but it
    is not as easy as it is in VB! If you need floating point numbers,
    you might be interested in one of Al William's math coprocessor chips.

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







    >At 08:48 AM 6/3/03 +0000, smorillas wrote:
    >>Hi,
    >>
    >>x var word
    > >x = 63744
    >>
    >
    > >debug dec x * 2
    > >
    > >prints 61952?
    >>
    >>Why is this so? How does the overflow work?
    >>
    >>Cheers
    >
    >It's not so much overflow as it is left-side truncation.
    >
    >63744 * 2 = 127488 restated in HEX 1F200 . Lop off the HEX 10000 and you're
    >left with HEX F200 . F200 in decimal = 61952.
    >
    >You may also want to make reference to the "Intermediate Results" Section of
    >the PBASIC Manual for more details.
    >
  • ArchiverArchiver Posts: 46,084
    edited 2003-06-03 16:29
    You have bitten off a really big one, as VB math is (generally)
    floating point, and you are working with an integer processor.

    On Tuesday, June 3, 2003, at 08:46 AM, smorillas wrote:

    > Hi,
    >
    > Umm I still don't fully get it. If I take the number 127488 and minus
    > hex 10000 (65653) then I get 61835, not 61952? What I'm trying to do
    > is understand how it works so that I can emulate the maths in VB.
    >
    > Cheers...
    >
    >
    > --- In basicstamps@yahoogroups.com, Bruce Bates <bvbates@u...> wrote:
    >> At 08:48 AM 6/3/03 +0000, smorillas wrote:
    >>
    >>> Hi,
    >>>
    >>> x var word
    >>> x = 63744
    >>>
    >>> debug dec x * 2
    >>>
    >>> prints 61952?
    >>>
    >>> Why is this so? How does the overflow work?
    >>>
    >>> Cheers
    >>
    >> It's not so much overflow as it is left-side truncation.
    >>
    >> 63744 * 2 = 127488 restated in HEX 1F200 . Lop off the HEX 10000
    > and you're
    >> left with HEX F200 . F200 in decimal = 61952.
    >>
    >> You may also want to make reference to the "Intermediate Results"
    > Section of
    >> the PBASIC Manual for more details.
    >>
    >>
    >>
    >>> 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/
    >
    >
    > 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/
    >
    >
    >
Sign In or Register to comment.