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.063 X-Spam-Evidence: '*H*': 0.88; '*S*': 0.00; 'binary': 0.05; 'float': 0.05; '22,': 0.09; 'result.': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hex': 0.16; 'nearest': 0.16; 'operation,': 0.16; 'zero,': 0.16; 'wrote:': 0.17; 'comparing': 0.17; 'integer': 0.17; 'feb': 0.19; 'error.': 0.21; 'subject:problem': 0.22; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'initial': 0.28; 'fri,': 0.30; 'gets': 0.32; 'problem.': 0.32; 'could': 0.32; 'print': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'received:209.85.220': 0.35; 'too.': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'should': 0.36; 'being': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'show': 0.63; 'results': 0.65; 'cast': 0.65; 'middle': 0.66; '"oh,': 0.84; '2013': 0.84; 'awesome,': 0.84; 'casting': 0.84; 'do:': 0.91; 'skip:4 20': 0.91; 'angel': 0.93; 'magical': 0.93 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=0XrdmcdDKRnQeFRIQ8J0goYlHYez+v18+qDdguA88dw=; b=CFQke1Im5FFfVfbdfJQuaq3/0NO306gyaayOLRuC0TTQ85JTDip2MwZm9YEIJTS4MS Y47F5twXwj2LJzPJIqABnEMmwhQ2CLDk5Q1mrqmOWgnd/UThAMkGkAULR6Xi4Dku+nlk nNWfGte8z49k21QJkfNWcqupjaNg8TD3dSbpWETyS8Uz3jHnCArUQPMUDr7AjynSh8Dt KLBEg6Rii69Iwi8W46oJtQYdbimyzxFEBCzvelvzH30ym1tlNYr1L8v1ev1QJ7EDIvKJ /g8J9n4g4Jlx6YA7OHeAeDFNs79A//Y0G4vsHPcmcnfcqLvZaQWhunRBaeok/mpzHopl NZyA== MIME-Version: 1.0 X-Received: by 10.52.22.194 with SMTP id g2mr29553940vdf.91.1361481807424; Thu, 21 Feb 2013 13:23:27 -0800 (PST) In-Reply-To: <51268867.1000800@davea.name> References: <512682B3.8070909@davea.name> <51268867.1000800@davea.name> Date: Fri, 22 Feb 2013 08:23:27 +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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361481815 news.xs4all.nl 6988 [2001:888:2000:d::a6]:40191 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39462 On Fri, Feb 22, 2013 at 7:49 AM, Dave Angel wrote: > However, if I do: > > print 3**60, "\n", int(math.pow(3,60)), "\n", pow(3,60) > > > I get: > > 42391158275216203514294433201 > 42391158275216203520420085760 > 42391158275216203514294433201 > > > and the middle one is the one that's wrong. 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. > You can tell by casting out 9's. The middle one gets 1 > instead of zero, showing that it's NOT divisible by 3. Which I thought so cool and magical and awesome, until I started exploring other bases and found that you could cast out F's in hex and 7's in octal... and you can cast out 1's in binary to find out if it's a multiple of 1, too. ChrisA