Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51724
| 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 | <oscar.j.benjamin@gmail.com> |
| 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 | <CAPTjJmqzhDb3FBuua7oy4osH7A0ODb7=T58WAvXQs5WhEdsOKQ@mail.gmail.com> |
| References | <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> <-tidnZTes6qk72TMnZ2dnUVZ8kadnZ2d@giganews.com> <51f9fe27$0$30000$c3e8da3$5496439d@news.astraweb.com> <CAPTjJmqzhDb3FBuua7oy4osH7A0ODb7=T58WAvXQs5WhEdsOKQ@mail.gmail.com> |
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
| Date | Thu, 1 Aug 2013 10:44:12 +0100 |
| Subject | Re: Unexpected results comparing float to Fraction |
| To | Chris Angelico <rosuav@gmail.com> |
| 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 <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.72.1375350274.1251.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 1 August 2013 07:32, Chris Angelico <rosuav@gmail.com> wrote:
> On Thu, Aug 1, 2013 at 7:20 AM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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