Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'float': 0.05; '22,': 0.09; 'calculating': 0.09; 'whichever': 0.09; 'result.': 0.15; '53-bit': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'how,': 0.16; 'nearest': 0.16; 'one-element': 0.16; 'operation,': 0.16; 'skip:{ 30': 0.16; 'wrote:': 0.17; 'comparing': 0.17; 'integer': 0.17; 'skip:{ 20': 0.17; '>>>': 0.18; 'feb': 0.19; 'error.': 0.21; 'subject:problem': 0.22; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'set.': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'received:209.85.212': 0.28; 'chris': 0.28; 'initial': 0.28; '>>>>': 0.29; 'case,': 0.29; "i'm": 0.29; 'fri,': 0.30; 'problem.': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'done': 0.34; 'consistent': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'explain': 0.36; 'but': 0.36; 'should': 0.36; 'does': 0.37; 'being': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'help': 0.40; 'matter': 0.61; 'different': 0.63; 'show': 0.63; 'results': 0.65; 'talking': 0.66; 'power': 0.74; '"oh,': 0.84; '2013': 0.84; 'mac.': 0.84; 'route': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=2yaCwx37XpDhwARqbg0qkH0k4azqPiqczwfpVjlq1os=; b=WyKhBmqr4NFCgiQWTWUQHu5jvO/8ETcNVumNPdtduRKq/KpRnVsQg0fUEjXYYrEypR Bb76dCF5r4oqQCJRVhQ70iQ9mY7cVNXognlej7NRQqhtFP/BRzTaSrxGpnbdxFTHt6ev 0EwAPsjuN8xbY+qK6l4064b2+oh6JKzUz4w15tDLgkfe7KJzMMvBv6uPWJp0BfFPkWWN GiKIblOKzkFsTnnxqr2v7e9pRFxWpy/FaKJ+oAeBL6EwXapnGrSj0lGYNSLrZzdsAoLc PGLHDL+FpEX82ZlqPOlrfZUCPOIrOc4fFYtXX7KVlhu8BAJ6nPIQ18cICSAs9G0liUyu z9yA== MIME-Version: 1.0 X-Received: by 10.52.22.194 with SMTP id g2mr29718568vdf.91.1361484696276; Thu, 21 Feb 2013 14:11:36 -0800 (PST) In-Reply-To: References: <512682B3.8070909@davea.name> <51268867.1000800@davea.name> Date: Fri, 22 Feb 2013 09:11:35 +1100 Subject: Re: Confusing math problem From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361484698 news.xs4all.nl 6984 [2001:888:2000:d::a6]:44242 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39471 On Fri, Feb 22, 2013 at 8:59 AM, Peter Pearson wrote: > On Fri, 22 Feb 2013 08:23:27 +1100, Chris Angelico wrote: >> In theory, a float should hold the nearest representable value to the >> exact result. Considering that only one operation is being performed, >> there should be no accumulation of error. The integer results show a >> small number (618) of collisions, eg 2**16 and 4**8; why should some >> of those NOT collide when done with floating point? My initial thought >> was "Oh, this is comparing floats for equality", but after one single >> operation, that should be not a problem. > > Does this help explain it? > >>>> print hex(int(math.pow(3,60))); print hex(3**60) > 0x88f924eeceeda80000000000L > 0x88f924eeceeda7fe92e1f5b1L > I understand how the inaccuracy works, but I'm expecting it to be as consistent as Mr Grossmith's entertainments. It doesn't matter that math.pow(3,60) != 3**60, but the number of collisions is different when done with floats on the OP's Mac. Here's what I'm talking about: >>> set((3**60,9**30,27**20)) {42391158275216203514294433201} >>> set((math.pow(3,60),math.pow(9,30),math.pow(27,20))) {4.23911582752162e+28} Note how, in each case, calculating three powers that have the same real-number result gives a one-element set. Three to the sixtieth power can't be perfectly rendered with a 53-bit mantissa, but it's rendered the same way whichever route is used to calculate it. ChrisA