Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Re: Adding a positive number and a negative number (http://www.velocityreviews.com/forums/t667986-re-adding-a-positive-number-and-a-negative-number.html)

MRAB 01-30-2009 01:58 PM

Re: Adding a positive number and a negative number
 
> In two‚€™s complement representation, can adding one positive and one negative give you overflow?
>
No.

Steve Holden 01-31-2009 12:35 AM

Re: Adding a positive number and a negative number
 
Scott David Daniels wrote:
> Grant Edwards wrote:
>> On 2009-01-30, MRAB <google@mrabarnett.plus.com> 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/

John Machin 01-31-2009 02:41 AM

Re: Adding a positive number and a negative number
 
On Jan 31, 4:10*am, Scott David Daniels <Scott.Dani...@Acm.Org> wrote:
> Grant Edwards wrote:
> > On 2009-01-30, MRAB <goo...@mrabarnett.plus.com> 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 :-)


All times are GMT. The time now is 05:25 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.