Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!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.055 X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; 'mrab': 0.05; 'float': 0.07; 'feature,': 0.09; 'formatting': 0.09; 'integers': 0.09; 'pretend': 0.09; 'constructor.': 0.16; 'did.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'rounding': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'trying': 0.19; '>>>': 0.22; 'fraction': 0.24; 'mon,': 0.24; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'correct': 0.29; 'chris': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'division': 0.31; 'actual': 0.34; 'candidate': 0.34; 'problem': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'in.': 0.36; "didn't": 0.36; 'useful': 0.36; 'similar': 0.36; 'should': 0.36; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'ian': 0.60; 'worry': 0.60; 'range': 0.61; 'become': 0.64; 'more': 0.64; 'relatively': 0.65; 'worth': 0.66; 'direct': 0.67; 'between': 0.67; 'jul': 0.74; 'prime': 0.74; 'subject:results': 0.91; 'imagine': 0.93; 'instant': 0.97; '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=4NDWkvwuqLJ8QOnSMHZ17+sIcgWunrbNE292YToP74Y=; b=rc+8zPCUSBAfKMCjnVG6R2lxmb8JzxwxcDDecJ6L2FWbU3Uktx1SRr3khNNe6hwe0e /y8hZRGsjqiGi77+qLbre/wfunWxGK35OvjYBmVa9NE+tidXGZT7MjJ4ks1LTSA6XHxg jaDwK8auSNE8jraAPl7viw7teIf3KEH8DkGuoG1bOFFdvjhl48K6uQ6bksW+10bEt+/w xxG2/Lcf/NF571lxilU3h1dDsG3OInL2RIptbsYlpDg4rsUBEZeWPccxzg3nqsnLv9z7 Q7zmKOnfAb+X5mL0n7VlL/tPYyf0LutnyXC8FRZpc1I91CoxcW0sJd3nSZqNYEV0UBEz /66g== MIME-Version: 1.0 X-Received: by 10.52.178.194 with SMTP id da2mr11158190vdc.28.1375118040856; Mon, 29 Jul 2013 10:14:00 -0700 (PDT) In-Reply-To: References: <51f68d9c$0$30000$c3e8da3$5496439d@news.astraweb.com> <51F693B4.9000201@mrabarnett.plus.com> Date: Mon, 29 Jul 2013 18:14:00 +0100 Subject: Re: Unexpected results comparing float to Fraction From: Chris Angelico To: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1375118049 news.xs4all.nl 15959 [2001:888:2000:d::a6]:37862 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:51464 On Mon, Jul 29, 2013 at 5:40 PM, Ian Kelly wrote: > On Mon, Jul 29, 2013 at 10:20 AM, Chris Angelico wrote: >> On Mon, Jul 29, 2013 at 5:09 PM, MRAB wrote: >>> 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). >> >> At what point should it become Fraction(1, 3)? > > At the point where the float is exactly equal to the value you get > from the floating-point division 1/3. If it's some other float then > the user didn't get there by entering 1/3, so it's not worth trying to > pretend that they did. > > We do a similar rounding when formatting floats to strings, but in > that case one only has to worry about divisors that are powers of 10. > I imagine it's going to take more time to find the correct fraction > when any pair of relatively prime integers can be a candidate > numerator and denominator. I imagine it is, and that's where the problem comes in. The true value is somewhere between (X-0.5)/2**n and (X+0.5)/2**n, or whatever the actual range is, and finding a "nice" fraction in that range isn't an instant and direct translation. It's a useful feature, but not IMO necessary for the constructor. ChrisA