Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.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; 'python,': 0.02; '(at': 0.04; 'essentially': 0.04; 'resulting': 0.04; 'explicitly': 0.05; 'float': 0.07; 'computed': 0.09; 'converts': 0.09; 'integral': 0.09; 'mixed': 0.09; 'pep': 0.09; 'subset': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'coercions': 0.16; 'hmm.': 0.16; 'nearest': 0.16; 'precision,': 0.16; 'surprises': 0.16; 'url:peps': 0.16; 'wrote:': 0.18; "python's": 0.19; 'result.': 0.19; 'thu,': 0.19; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'documented': 0.24; 'fraction': 0.24; 'url:dev': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'post': 0.26; 'least': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'operations,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'asked': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'etc.).': 0.31; 'steven': 0.31; 'types.': 0.31; 'quite': 0.32; 'says': 0.33; 'url:python': 0.33; 'becomes': 0.33; 'level.': 0.33; 'role': 0.34; 'sense': 0.34; 'anywhere': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'possible': 0.36; 'url:org': 0.36; 'should': 0.36; 'url:library': 0.38; 'fact': 0.38; 'previous': 0.38; 'skip:u 10': 0.60; 'august': 0.61; 'helps': 0.61; 'url:3': 0.61; 'range': 0.61; 'here:': 0.62; 'real': 0.63; 'places': 0.64; 'taking': 0.65; 'account': 0.65; 'to:addr:gmail.com': 0.65; 'between': 0.67; 'glad': 0.83; 'abc': 0.84; 'computation.': 0.84; 'float,': 0.84; 'oscar': 0.84; 'subject:results': 0.91; '2013': 0.98 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:to :cc:content-type; bh=nVRSNWqN45bNGyfTqguMZRJF9rMdNMmRL7gsiikOP0g=; b=YC7KUSwSjiJ4uXgRvqksq1eDWLGBTu0leQNHRSpTPjPWkVOaFh3qkVCRl7B4rXPDUL CbRI5BGHEN2BMI/mmMJD3mVSd/3i5fBlSnyPUcw+EoBWo+hRGYVvXkScyxkRE4xjZHwt saKNSw0AE92XG8Me5yN/iyztWO/KbVyh3sTwOgaofO1//Vej+eTjdyJ2G1akkstJ+v1d LRCagmkbiBA5ysojDIQ8aAtCSw7vKkMz1U5Sy4tG3LhMx0GfBNb5SGeCjD8bs7jiMaV+ oeri3lr529bJ6TjwqkvSLztySdyeSQoTCVH7snQvle7f1fdo/+fI048F6b+0lanJNu04 kfBQ== X-Received: by 10.220.237.208 with SMTP id kp16mr227210vcb.4.1375350272162; Thu, 01 Aug 2013 02:44:32 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> <-tidnZTes6qk72TMnZ2dnUVZ8kadnZ2d@giganews.com> <51f9fe27$0$30000$c3e8da3$5496439d@news.astraweb.com> From: Oscar Benjamin Date: Thu, 1 Aug 2013 10:44:12 +0100 Subject: Re: Unexpected results comparing float to Fraction To: Chris Angelico Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375350274 news.xs4all.nl 15938 [2001:888:2000:d::a6]:38108 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51724 On 1 August 2013 07:32, Chris Angelico wrote: > On Thu, Aug 1, 2013 at 7:20 AM, Steven D'Aprano > wrote: >> I know this, and that's not what surprised me. What surprised me was that >> Fraction converts the float to a fraction, then compares. It surprises me >> because in other operations, Fractions down-cast to float. >> >> Adding a float to a Fraction converts the Fraction to the nearest float, >> then adds: >> >> py> 1/3 + Fraction(1, 3) >> 0.6666666666666666 > > Hmm. This is the one that surprises me. That would be like the > addition of a float and an int resulting in an int (at least in C; in > Python, where floats have limited range and ints have arbitrary > precision, the matter's not quite so clear-cut). Perhaps this needs to > be changed? The Python numeric tower is here: http://docs.python.org/3/library/numbers.html#module-numbers Essentially it says that Integral < Rational < Real < Complex and that numeric coercions in mixed type arithmetic should go from left to right which makes sense mathematically in terms of the subset/superset relationships between the numeric fields. When you recast this in terms of Python's builtin/stdlib types it becomes int < Fraction < {float, Decimal} < complex and taking account of boundedness and imprecision we find that the only subset/superset relationships that are actually valid are int < Fraction and float < complex In fact Fraction is a superset of both float and Decimal (ignoring inf/nan/-0 etc.). int is not a subset of float, Decimal or complex. float is a superset of none of the types. Decimal is a superset of float but the tower places them on the same level. The real dividing line between {int, Fraction} and {float, Decimal, complex} is about (in)exactness. The numeric tower ensures the property that inexactness is contagious which I think is a good thing. This is not explicitly documented anywhere. PEP 3141 makes a dangling reference to an Exact ABC as a superclass of Rational but this is unimplemented anywhere AFAICT: http://www.python.org/dev/peps/pep-3141/ The reason contagious inexactness is a good thing is the same as having contagious quite NaNs. It makes it possible to rule out inexact computations playing a role in the final computed result. In my previous post I asked what the use case is for mixing floats and Rationals in computation. I have always considered this to be something that I wanted to avoid and I'm glad that contagious inexactness helps me to avoid mixing floats into exact computations. Oscar