Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Adding a positive number and a negative number

Reply
Thread Tools

Re: Adding a positive number and a negative number

 
 
MRAB
Guest
Posts: n/a
 
      01-30-2009
> In two€™s complement representation, can adding one positive and one negative give you overflow?
>
No.
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      01-31-2009
Scott David Daniels wrote:
> Grant Edwards wrote:
>> On 2009-01-30, MRAB <(E-Mail Removed)> 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/
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      01-31-2009
On Jan 31, 4:10*am, Scott David Daniels <(E-Mail Removed)> wrote:
> Grant Edwards wrote:
> > On 2009-01-30, MRAB <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
floor(positive double) vs trunc(positive double) different Hicham Mouline C Programming 2 04-23-2010 06:50 PM
Adding a positive number and a negative number EK Python 0 01-30-2009 04:13 AM
Use of both positive reference and negative reference of the same signal for Xilinx chips would cause additional LUT delay? Weng Tianxiang VHDL 6 03-19-2007 04:58 PM
Decide if a number is negative or positive? Johs C Programming 19 01-07-2007 10:48 PM
Positive and Negative integer counter StaR.BoY2005@gmail.com Java 3 09-29-2006 01:49 PM



Advertisments