Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.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.067 X-Spam-Evidence: '*H*': 0.87; '*S*': 0.00; 'value,': 0.04; 'modify': 0.07; 'better?': 0.16; 'int(x)': 0.16; 'number?': 0.16; 'other,': 0.16; 'to:name:python list': 0.16; 'wrote:': 0.18; 'obviously': 0.18; '>>>': 0.22; 'fraction': 0.24; 'equivalent': 0.26; 'possibly': 0.26; 'header:In-Reply-To:1': 0.27; 'primarily': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'gives': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'faster,': 0.31; 'steven': 0.31; 'though.': 0.31; 'guess': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'in.': 0.36; 'subject:?': 0.36; 'should': 0.36; 'two': 0.37; 'depends': 0.38; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'march': 0.61; "you're": 0.61; 'here': 0.66; '2015': 0.84; 'float,': 0.84; 'oscar': 0.84; 'do:': 0.91; 'subject:Best': 0.91 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=XmXRunDk5lfohqTzNnEsy5K9Oh/03o4F4oOBrChB8LU=; b=aB34NpHIrfr3zBZEcyzF2ragdi5788L5aycXAnOpac86h3SAnbS44dBXUZbXviMPDE 1sHrW4mTLf7WwyH2ZSrgiCAEh6PoyQN8QGphZADx9RgSZnKsbGfWcRsndKBX6y1sYSdt mc9VHROP90Xy9lsKoh9d9/Yt/QhJMsHCSmHH7FeUhsK3f7ENvKGiWJ3iixaqt11p8riG cTAYvah1y4KtKXPvSx05KZLZLucZkkbdpfrVaPYZWtHpQOGvPG5dgplrlwTpboYzVcWh C+KMuNkB/ECdT6pLMghsLT8AHRMUK4Ze5VBBVQC3F1Fbc33wMAULpAXSalzu4N88A3FJ lxzA== X-Received: by 10.194.133.101 with SMTP id pb5mr32966572wjb.40.1427407164676; Thu, 26 Mar 2015 14:59:24 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <55100c7a$0$13004$c3e8da3$5496439d@news.astraweb.com> References: <55100c7a$0$13004$c3e8da3$5496439d@news.astraweb.com> From: Oscar Benjamin Date: Thu, 26 Mar 2015 21:59:04 +0000 Subject: Re: Best way to calculate fraction part of x? To: Python List Content-Type: text/plain; charset=UTF-8 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: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427407165 news.xs4all.nl 2907 [2001:888:2000:d::a6]:41682 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88099 On 23 March 2015 at 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 The other version gives -0.75 in this case so I guess that's what you want. > Are there any other, possibly better, ways to calculate the fractional part > of a number? What do you mean by better? Is it just faster? To modify the % version so that it's equivalent you can do: >>> x = -2.75 >>> (x % 1) - (x < 0) -0.75 I'm not sure if that's faster than x - int(x) though. Obviously it depends which numeric type you're primarily interested in. Oscar