Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Unexpected NANs in complex arithmetic Date: Wed, 22 Jun 2016 12:57:55 +1000 Lines: 53 Message-ID: References: <5769fc9b$0$1622$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de /5AwL6rjRT5cED/1JULTUA+E8hjcatI2Fz/bNQ5w7BEg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '"""': 0.05; 'cc:addr :python-list': 0.09; '22,': 0.09; 'unexpected': 0.09; 'wed,': 0.15; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'googled': 0.16; 'inf': 0.16; 'infinity': 0.16; 'notably': 0.16; 'occurred.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'tending': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'saying': 0.22; 'this:': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'message-id:@mail.gmail.com': 0.27; 'arithmetic': 0.29; 'factor': 0.29; 'far,': 0.29; 'idea,': 0.29; "i'm": 0.30; 'guess': 0.31; 'certain': 0.31; 'role': 0.32; '[1]': 0.32; 'possibly': 0.32; 'getting': 0.33; 'run': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.35; 'so,': 0.35; 'direction': 0.35; 'involving': 0.35; "isn't": 0.35; 'should': 0.36; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'doing': 0.38; 'received:209': 0.38; 'end': 0.39; 'where': 0.40; 'some': 0.40; 'your': 0.60; 'real': 0.62; 'more': 0.63; 'road': 0.65; '"go': 0.84; 'chrisa': 0.84; 'received:209.85.215.42': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc :content-transfer-encoding; bh=IohgYlBh31ueAvXXhEYVDINAkei+mLf6UgCeJagU5sc=; b=LxbVlP+O8/mybXcDVZh1R3hgBnPl4rYFrxJe4+Tt7/L+HOk5DTvKUvQsKc21mQyYb8 LyvPSeyR1ZmOUYhQA5U3S4CwO8Edqpxkv7G+l9cN9QkGKpHcjsrt8ZYnB5xXGrQrztn0 uLDQcoquB9SFmDaiYshc98MIcIPmDCTPc3YptX/n1/X1lD6fEr1WUUGYFu1j7MpQk6ZY Evv7fsBTBoO20KgBDRAMxwhlGJa8xzKGJGZ4CkG+gcadty4+YTb/NYuwrEwYdwPFDlzo RGz/Z1sJADPjGVHsNw2Et9RvZf2WqGRAOorOehD53hYiqXG4sGSuNPkDY3bNdS2IB1Op QXFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc:content-transfer-encoding; bh=IohgYlBh31ueAvXXhEYVDINAkei+mLf6UgCeJagU5sc=; b=hVzFsNyklpl1I2hR5OpBFxl/0bTn/JvgdMybkwe7WZgXkewTIvReO0DnL5Nh5UcbQ/ rKd6Gk3XFr4ivwQ2BSSXl/mALdoECKGYuwr6LehWUTfXQ7QjCSbrqJE5wMh+1F4JaLnM jDEvEjlPy92IidYdC8zDHXgMErQ4G6NWfWjWB0NszZqL6efcjG+WCi2DB41yIq59UwbJ YlbXEkdYkDnyVz2sia7qUAfjFOfJR5jZIDOQaqhscgoKKTTNhPlNgJtqUAi150/zxnMW EywcFH6ZTDVRTm9j/enBF1Q7n5gR6KIbk/tWNbFxNZXRCpEJj6UZqM8SA3Dx2FYCp08Q 3nQQ== X-Gm-Message-State: ALyK8tK3oCxF5fvdPQciONS/z0fB3ueDW5MhyaHpKKfZNWDtSFSpfrQI6nfx4i29Prkw+g8F1i4OwiCjvc2ZkQ== X-Received: by 10.28.44.87 with SMTP id s84mr5899029wms.61.1466564275896; Tue, 21 Jun 2016 19:57:55 -0700 (PDT) In-Reply-To: <5769fc9b$0$1622$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <5769fc9b$0$1622$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:110281 On Wed, Jun 22, 2016 at 12:48 PM, Steven D'Aprano wro= te: > I'm doing some arithmetic on complex numbers involving INFs, and getting > unexpected NANs. > > py> INF =3D float('inf') > py> z =3D INF + 3j > py> z > (inf+3j) > py> -z > (-inf-3j) > > So far, nothing unexpected has occurred. But: > > py> -1*z # should be the same as -z > (-inf+nanj) > > > And even more strange: > > py> 1*z > (inf+nanj) > > > > Is this the right behaviour? If so, what's the justification for it? I've no idea, so I Googled StackExchange [1] and found this: http://math.stackexchange.com/questions/585766/what-is-infinity-in-complex-= plane-and-what-are-operation-with-infinity-extended Notably this: """ To some extent, +=E2=88=9E+=E2=88=9E and =E2=88=92=E2=88=9E=E2=88=92=E2=88= =9E also play this role on the real axis: they are not a destination, they are road signs that tell us to go in a certain direction and never stop. """ So when your real part is float("inf"), what you're really saying is "Go as far as you possibly can in the positive direction, then keep going (because you haven't run out of numbers yet), and tell me, what is 1*z tending towards?". Infinity isn't a number, and the imaginary part of your complex number isn't really a factor in figuring out where you're likely to end up with your multiplication. I guess that's a justification for it coming out as NaN. ChrisA [1] http://www.theallium.com/engineering/computer-programming-to-be-officia= lly-renamed-googling-stackoverflow/