Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'value,': 0.04; 'assuming': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'url:github': 0.09; 'language.': 0.14; 'int(x)': 0.16; 'number?': 0.16; 'other,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sparks': 0.16; 'language': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'fraction': 0.24; 'refers': 0.24; "haven't": 0.24; 'possibly': 0.26; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; "i'm": 0.30; "d'aprano": 0.31; 'decimal': 0.31; 'faster,': 0.31; 'steven': 0.31; 'but': 0.35; 'there': 0.35; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'confirmed': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'our': 0.64; 'charset:windows-1252': 0.65; 'here': 0.66; 'fact.': 0.84; 'float,': 0.84; 'received:as9105.com': 0.84; 'received:dsl.as9105.com': 0.84; 'received:dynamic.dsl.as9105.com': 0.84; 'subject:Best': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Best way to calculate fraction part of x? Date: Mon, 23 Mar 2015 13:11:04 +0000 References: <55100c7a$0$13004$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 80-44-198-123.dynamic.dsl.as9105.com User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 In-Reply-To: <55100c7a$0$13004$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427116295 news.xs4all.nl 2887 [2001:888:2000:d::a6]:42760 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87829 On 23/03/2015 12:52, Steven D'Aprano wrote: > I have a numeric value, possibly a float, Decimal or (improper) Fraction, > and I want the fractional part. E.g. fract(2.5) should give 0.5. > > Here are two ways to do it: > > py> x = 2.5 > py> x % 1 > 0.5 > py> x - int(x) > 0.5 > > x % 1 is significantly faster, but has the disadvantage of giving the > complement of the fraction if x is negative: > > py> x = -2.75 > py> x % 1 > 0.25 > > Are there any other, possibly better, ways to calculate the fractional part > of a number? > Any sparks here http://stackoverflow.com/questions/875238/fractional-part-of-the-number-question ? I'm assuming that the reference to "pkaeding's algorithm" refers to https://github.com/pkaeding but haven't confirmed that to be fact. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence