Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #51493
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.016 |
| X-Spam-Evidence | '*H*': 0.97; '*S*': 0.00; 'mrab': 0.05; 'float': 0.07; 'constructor': 0.09; 'observation': 0.09; 'def': 0.12; '>>': 0.16; 'algorithm.': 0.16; 'numpy': 0.16; 'true:': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'bit': 0.19; '>>>': 0.22; 'email addr:gmail.com>': 0.22; '>>>': 0.24; 'fraction': 0.24; '>': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'guess': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'being': 0.38; '8bit%:86': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'break': 0.61; 'more': 0.64; 'jul': 0.74; 'iterative': 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:date:message-id:subject:from:to :content-type; bh=u1W1VJp73/vbRpqUnx6xEenum1jaMnMEtOJau4iEIeI=; b=gP8IkEudvkjOABELy7IsnETeZotGVv6BjaGTx5/CQyqVYM0+E1GDkVaGTK8Y3ssvKE YrxyPLntljhbLEl0lz3hQ8bB6b3GWaCuf6ZqYG04gV9lNb71l+vEV7baBpCyRuZwTE4N 9NBDgnF7T8VbzDUX41qJPViG4DU8RYMpvk8VlQyhAyb2szIjLdfeT+mjRqdVASPxsOph pVK1WAv5H13sl5ye5wYIakyqk7WYTQziRJeRu8Dqjw7JQdJU8Sr1wl8VPllVVB17U+D+ qnAri9HzouscqzbaG/opyTdETRFlq6ISEHcKf139SA+jBw7bCSzJz+dl045wAiAH4s9+ Nx0w== |
| MIME-Version | 1.0 |
| X-Received | by 10.68.28.232 with SMTP id e8mr70572535pbh.94.1375130121941; Mon, 29 Jul 2013 13:35:21 -0700 (PDT) |
| In-Reply-To | <kt6g36$d6s$1@ger.gmane.org> |
| References | <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> <51F693B4.9000201@mrabarnett.plus.com> <kt6g36$d6s$1@ger.gmane.org> |
| Date | Mon, 29 Jul 2013 14:35:21 -0600 |
| Subject | Re: Unexpected results comparing float to Fraction |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| To | Python <python-list@python.org> |
| Content-Type | multipart/alternative; boundary=bcaec520eacd703b2404e2ac6eeb |
| 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.5267.1375130131.3114.python-list@python.org> (permalink) |
| Lines | 92 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1375130131 news.xs4all.nl 15971 [2001:888:2000:d::a6]:41093 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:51493 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Jul 29, 2013 1:37 PM, "Serhiy Storchaka" <storchaka@gmail.com> wrote: > > 29.07.13 19:09, MRAB написав(ла): > >> 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). > > > >>> def approximate_fraction(f): > prev_numer, numer = 0, 1 > prev_denom, denom = 1, 0 > r = f > while True: > i = math.floor(r) > prev_numer, numer = numer, i * numer + prev_numer > prev_denom, denom = denom, i * denom + prev_denom > if i == r or numer / denom == f: > break > r = 1 / (r - i) > > return Fraction(numer, denom) > > >>> approximate_fraction(1/3) > Fraction(1, 3) > >>> approximate_fraction(1e-17) > Fraction(1, 100000000000000000) > >>> approximate_fraction(math.pi) > Fraction(245850922, 78256779) > > I guess the Fraction constructor is more faster than this function. You might be able to speed it up a bit with numpy and the observation that the update is a matrix multiplication. But I don't think that you can get away from it being an iterative algorithm.
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