Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88099
| 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 | <oscar.j.benjamin@gmail.com> |
| 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 <oscar.j.benjamin@gmail.com> |
| Date | Thu, 26 Mar 2015 21:59:04 +0000 |
| Subject | Re: Best way to calculate fraction part of x? |
| To | Python List <python-list@python.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <https://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 | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.219.1427407165.10327.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
On 23 March 2015 at 12:52, Steven D'Aprano <steve+comp.lang.python@pearwood.info> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Best way to calculate fraction part of x? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-23 23:52 +1100 Re: Best way to calculate fraction part of x? Skip Montanaro <skip.montanaro@gmail.com> - 2015-03-23 08:02 -0500 Re: Best way to calculate fraction part of x? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-23 13:11 +0000 Re: Best way to calculate fraction part of x? wxjmfauth@gmail.com - 2015-03-23 06:17 -0700 Re: Best way to calculate fraction part of x? Emile van Sebille <emile@fenx.com> - 2015-03-23 17:38 -0700 Re: Best way to calculate fraction part of x? Jason Swails <jason.swails@gmail.com> - 2015-03-24 21:39 -0400 Re: Best way to calculate fraction part of x? Emile van Sebille <emile@fenx.com> - 2015-03-26 13:48 -0700 Re: Best way to calculate fraction part of x? Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-03-26 21:59 +0000 Re: Best way to calculate fraction part of x? Russell Owen <rowen@uw.edu> - 2015-03-26 15:02 -0700
csiph-web