Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.05; '64-bit': 0.07; 'compiler': 0.07; '2147483647': 0.09; 'assuming': 0.09; 'differently.': 0.09; 'integers': 0.09; 'negative.': 0.09; 'pixels': 0.09; 'spec': 0.09; 'undefined': 0.09; 'cc:addr:python- list': 0.11; '-2147483648': 0.16; 'behave': 0.16; 'comparison.': 0.16; 'compiler.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integer.': 0.16; 'merely': 0.16; 'overflow.': 0.16; 'subject:search': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'integer': 0.24; 'helpful': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'source': 0.25; 'code:': 0.26; 'url:edu': 0.26; 'signed': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; '[1]': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'asked': 0.31; '25,': 0.31; "d'aprano": 0.31; 'gcc': 0.31; 'int,': 0.31; 'steven': 0.31; 'probably': 0.32; 'another': 0.32; 'says': 0.33; 'fri,': 0.33; 'programmers': 0.33; 'screen': 0.34; "can't": 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'positive': 0.37; 'two': 0.37; 'represent': 0.38; 'sure': 0.39; 'even': 0.60; 'negative': 0.60; 'then,': 0.60; 'tell': 0.60; 'simply': 0.61; 'first': 0.61; 'such': 0.63; 'become': 0.64; 'total': 0.65; 'here': 0.66; '*according': 0.84; '2015': 0.84; 'subject:find': 0.84; 'subject:Best': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=flsM33Wh9yG7mjJzubobiH/98kYEQkVbOLgi/FZzDLA=; b=MySoZAyxvBN8WfSTe88geGrevmkQxHhT6yj6+Fom1PfuPerr+1EcxrIpTJ8YfqOnsB CXysL18RYfvxPbSiF91KYSEbWSbEbocThYoiS7T7n75RhgOJdCw5YuiinGGXFgb9YqIq qhPbrvXddbP7mDevcRYoqIbdp6BXSPfxHAhnSQFCSkaKhuN2YMaf00EwJ7bDdXSGZw9H OqnUgvB1G9kmjPyMrU64i02NXvdrtAR2+f21XmhaMRQV7HuTU4VfZFE9D6y+pln5vzGJ JfVK3cZPDM4mKniU9+nUftsgtWHnONE5xFZi/go5rGtVZuouBpReylECgs2L13pKGuld BEwg== MIME-Version: 1.0 X-Received: by 10.42.50.134 with SMTP id a6mr16363270icg.12.1428597396586; Thu, 09 Apr 2015 09:36:36 -0700 (PDT) In-Reply-To: <5526a59a$0$12999$c3e8da3$5496439d@news.astraweb.com> References: <2e3a3c01-20b3-4948-9b32-bd80ed46822b@googlegroups.com> <552460cc$0$13002$c3e8da3$5496439d@news.astraweb.com> <876196cm1e.fsf@elektro.pacujo.net> <87k2xlzknu.fsf@elektro.pacujo.net> <8738498qf7.fsf@dpt-info.u-strasbg.fr> <87fv89zd0m.fsf@elektro.pacujo.net> <87y4m1795n.fsf@dpt-info.u-strasbg.fr> <874mopzarw.fsf@elektro.pacujo.net> <552696be$0$13012$c3e8da3$5496439d@news.astraweb.com> <5526a59a$0$12999$c3e8da3$5496439d@news.astraweb.com> Date: Fri, 10 Apr 2015 02:36:36 +1000 Subject: Re: Best search algorithm to find condition within a range From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428597399 news.xs4all.nl 2897 [2001:888:2000:d::a6]:59861 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88738 On Fri, Apr 10, 2015 at 2:15 AM, Steven D'Aprano wrote: > On Fri, 10 Apr 2015 01:34 am, Chris Angelico wrote: > >> It's equally impossible for the addition of two positive signed >> integers to result in a negative integer. > > Why oh why do C programmers tell such porkies??? *semi-wink* > > [steve@ando c]$ gcc add.c > [steve@ando c]$ ./a.out > a = 1 > b = 2147483647 > a+b = -2147483648 > > > Looks like a negative integer to me, but then, given that its undefined > behaviour, perhaps the compiler flipped some pixels on the screen so it > merely *looks* negative. Precisely. Another compiler, or another CPU architecture, is welcome to behave differently. *According to the C standard*, you just did something impossible, so it's equally valid for the output to be 5, 3, 7, or 25, all of which would probably be produced by Princess Ida's compiler. (Mind you, her compiler might do that when asked to add 2 and 2, so I'm not sure it's all that helpful a comparison. [1]) > Here's the source code: > > [steve@ando c]$ cat add.c > a = 1; > b = 2147483647; > printf("a+b = %d\n", a+b); The first undefined part here is that this operation might not even overflow. If the compiler uses a 64-bit int, this will simply produce 2147483648. But even assuming that it can't represent that total in a signed int, there's nothing in the spec that says it has to become negative. ChrisA [1] http://diamond.boisestate.edu/gas/princess_ida/webop/pi_10d.html