Re: Adding a positive number and a negative number

Discussion in 'Python' started by MRAB, Jan 30, 2009.

  1. MRAB

    MRAB Guest

    > In two’s complement representation, can adding one positive and one negative give you overflow?
    >
    No.
     
    MRAB, Jan 30, 2009
    #1
    1. Advertising

  2. MRAB

    Steve Holden Guest

    Scott David Daniels wrote:
    > Grant Edwards wrote:
    >> On 2009-01-30, MRAB <> wrote:
    >>>> In two's complement representation, can adding one positive
    >>>> and one negative give you overflow?
    >>> No.
    >> AFAIK, in Python adding integers never gives you overlow
    >> regardless of sign.

    >
    > Right, but he wants his homework answer.


    And Python doesn't use pure two's complement notation when it promotes
    regular integers to longs.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Jan 31, 2009
    #2
    1. Advertising

  3. MRAB

    John Machin Guest

    On Jan 31, 4:10*am, Scott David Daniels <> wrote:
    > Grant Edwards wrote:
    > > On 2009-01-30, MRAB <> wrote:
    > >>> In two's complement representation, can adding one positive
    > >>> and one negative give you overflow?
    > >> No.
    > > AFAIK, in Python adding integers never gives you overlow
    > > regardless of sign.

    >
    > Right, but he wants his homework answer.


    For extra brownie points, here's a simple proof of the more general
    proposition that adding a non-negative integer p and a non-positive
    integer n can't overflow whatever the representation.

    Let a be the most negative integer and b the most positive. So we're
    given a <= n <= 0 <= p <= b and need to show that a <= (p + n) <= b.

    max(p) is b, max(n) is 0, so max(p + n) is b.
    Similarly min(p + n) is a.
    Q.E.D.

    IEEE 754 floating point? I don't know. Go read the standard :)
     
    John Machin, Jan 31, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,343
    Patricia Shanahan
    Sep 29, 2006
  2. Johs
    Replies:
    19
    Views:
    490
    Old Wolf
    Jan 7, 2007
  3. Weng Tianxiang
    Replies:
    6
    Views:
    791
    Ray Andraka
    Mar 19, 2007
  4. EK
    Replies:
    0
    Views:
    320
  5. Hicham Mouline
    Replies:
    2
    Views:
    777
    Keith Thompson
    Apr 23, 2010
Loading...

Share This Page