Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51450

Re: Unexpected results comparing float to Fraction

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 <ian.g.kelly@gmail.com>
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 <ian.g.kelly@gmail.com>
Date Mon, 29 Jul 2013 09:50:34 -0600
Subject Re: Unexpected results comparing float to Fraction
To Python <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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5237.1375113077.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Mon, Jul 29, 2013 at 9:43 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Unexpected results comparing float to Fraction Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-29 15:43 +0000
  Re: Unexpected results comparing float to Fraction Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-29 09:50 -0600
  Re: Unexpected results comparing float to Fraction MRAB <python@mrabarnett.plus.com> - 2013-07-29 17:09 +0100
  Re: Unexpected results comparing float to Fraction Chris Angelico <rosuav@gmail.com> - 2013-07-29 17:20 +0100
    Re: Unexpected results comparing float to Fraction Rotwang <sg552@hotmail.co.uk> - 2013-07-29 19:20 +0100
    Re: Unexpected results comparing float to Fraction Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-29 19:32 +0000
  Re: Unexpected results comparing float to Fraction MRAB <python@mrabarnett.plus.com> - 2013-07-29 17:48 +0100
    Re: Unexpected results comparing float to Fraction Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-29 17:27 +0000
  Re: Unexpected results comparing float to Fraction Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-29 10:40 -0600
    Re: Unexpected results comparing float to Fraction Rotwang <sg552@hotmail.co.uk> - 2013-07-29 19:16 +0100
      Re: Unexpected results comparing float to Fraction Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-29 13:33 -0600
  Re: Unexpected results comparing float to Fraction MRAB <python@mrabarnett.plus.com> - 2013-07-29 18:04 +0100
    Re: Unexpected results comparing float to Fraction Grant Edwards <invalid@invalid.invalid> - 2013-07-29 18:46 +0000
    Re: Unexpected results comparing float to Fraction Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-29 20:19 +0000
  Re: Unexpected results comparing float to Fraction Terry Reedy <tjreedy@udel.edu> - 2013-07-29 13:08 -0400
    Re: Unexpected results comparing float to Fraction Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-29 17:29 +0000
      Re: Unexpected results comparing float to Fraction Terry Reedy <tjreedy@udel.edu> - 2013-07-29 16:48 -0400
  Re: Unexpected results comparing float to Fraction Chris Angelico <rosuav@gmail.com> - 2013-07-29 18:14 +0100
  Re: Unexpected results comparing float to Fraction Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-29 11:35 -0600
  Re: Unexpected results comparing float to Fraction Serhiy Storchaka <storchaka@gmail.com> - 2013-07-29 22:34 +0300
  Re: Unexpected results comparing float to Fraction Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-29 14:35 -0600
  Unexpected results comparing float to Fraction Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-30 14:32 +0100
  Re: Unexpected results comparing float to Fraction Tony the Tiger <tony@tiger.invalid> - 2013-07-31 15:23 -0500
    Re: Unexpected results comparing float to Fraction Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-01 06:20 +0000
      Re: Unexpected results comparing float to Fraction Chris Angelico <rosuav@gmail.com> - 2013-08-01 07:32 +0100
      Re: Unexpected results comparing float to Fraction Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-08-01 10:44 +0100
      Re: Unexpected results comparing float to Fraction Chris Angelico <rosuav@gmail.com> - 2013-08-01 10:48 +0100

csiph-web