Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.048 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'float': 0.07; 'python': 0.11; 'coercions': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'unexpected': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'import': 0.22; 'this?': 0.23; 'header:User- Agent:1': 0.23; 'comparing': 0.24; 'fraction': 0.24; 'header:In- Reply-To:1': 0.27; "i'm": 0.30; 'gives': 0.31; 'apparently': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'convert': 0.35; 'but': 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; 'more': 0.64; 'header :Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'float,': 0.84; 'reply-to:addr:python.org': 0.84; 'subject:results': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=KrN0hwmN c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=K2DDQYBT4xIA:10 a=POziZqpqTb0A:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=qLDMBdlwUyoA:10 a=RdPq5cUHhR6Bl2yKv-QA:9 a=wPNLvfGTeEIA:10 X-AUTH: mrabarnett:2500 Date: Mon, 29 Jul 2013 17:09:24 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130620 Thunderbird/17.0.7 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Unexpected results comparing float to Fraction References: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375114343 news.xs4all.nl 15904 [2001:888:2000:d::a6]:52770 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51452 On 29/07/2013 16:43, 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? > I'm surprised that Fraction(1/3) != Fraction(1, 3); after all, floats are approximate anyway, and the float value 1/3 is more likely to be Fraction(1, 3) than Fraction(6004799503160661, 18014398509481984).