Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.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.114 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.78; '*S*': 0.01; 'float': 0.07; 'python': 0.11; 'coercions': 0.16; 'unexpected': 0.16; 'wrote:': 0.18; 'import': 0.22; 'this?': 0.23; 'comparing': 0.24; 'fraction': 0.24; 'mon,': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'apparently': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'sense': 0.34; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'doing': 0.36; 'should': 0.36; 'expected': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'such': 0.63; 'jul': 0.74; 'float,': 0.84; 'cast': 0.91; '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 :content-type; bh=8dNE4Y7tJdn+1PUEtMpb3ZdlKvPcM9b5tqg69h7lFlk=; b=pJJobKkMzwfOKCfvSwuSQZU6+1SgKsNouT6ZqF49vUOODZ/Gw/3DM/9RwXSqsXYvau AoYqh9OPxLgQQ23NZvW+chFpSQReclKEJgn3PlYmbWWqS7pV6CHXmUHEz6MXzpmdfdMe S5upcIBWA/sciDb3gqCOwMcSsr+zLlKkrgP36A+h7/YXXzrHPrqyJEyNUkWGkMfv1uvM QMF6RlluRmyo1OeWxhsfTChFBOQz+83CEljAIknP2VwSMyoF2foL3gWopj1tGSK/zMLd wH8AQe438xDrmsgJB58mslmFGJZYnvT0eDFveOhPoH13n6JxoM0FVfJs381ZWt/xQ0zQ aOAg== X-Received: by 10.68.164.225 with SMTP id yt1mr67634616pbb.195.1375113074339; Mon, 29 Jul 2013 08:51:14 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> References: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Mon, 29 Jul 2013 09:50:34 -0600 Subject: Re: Unexpected results comparing float to Fraction To: Python 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375113077 news.xs4all.nl 15974 [2001:888:2000:d::a6]:45512 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51450 On Mon, Jul 29, 2013 at 9:43 AM, Steven D'Aprano wrote: > Comparing floats to Fractions gives unexpected results: > > # Python 3.3 > py> from fractions import Fraction > py> 1/3 == Fraction(1, 3) > False > > but: > > py> 1/3 == float(Fraction(1, 3)) > True > > > I expected that float-to-Fraction comparisons would convert the Fraction > to a float, but apparently they do the opposite: they convert the float > to a Fraction: > > py> Fraction(1/3) > Fraction(6004799503160661, 18014398509481984) > > > Am I the only one who is surprised by this? Is there a general rule for > which way numeric coercions should go when doing such comparisons? Any float can be precisely represented as a Fraction. Not so in the other direction. So from that standpoint it makes sense to me to cast to Fraction when comparing.