Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #88100

Re: Best way to calculate fraction part of x?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.010
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'float': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'suggest': 0.14; '23,': 0.16; 'number?': 0.16; 'other,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'math': 0.24; 'mon,': 0.24; 'possibly': 0.26; 'header:X -Complaints-To:1': 0.27; 'van': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "d'aprano": 0.31; 'steven': 0.31; 'there': 0.35; 'subject:?': 0.36; 'jason': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'mar': 0.68; 'lose': 0.68; 'results': 0.69; '2015': 0.84; 'subject:Best': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Russell Owen <rowen@uw.edu>
Subject Re: Best way to calculate fraction part of x?
Date Thu, 26 Mar 2015 15:02:56 -0700
References <55100c7a$0$13004$c3e8da3$5496439d@news.astraweb.com> <meqbmo$4rb$1@ger.gmane.org> <CAEk9e3o7fGcJw=V+C1TwLJ3+6mVQ6c1ShCUca4QAtp6hK1BVjA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host d-128-208-13-159.dhcp4.washington.edu
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:31.0) Gecko/20100101 Thunderbird/31.5.0
In-Reply-To <CAEk9e3o7fGcJw=V+C1TwLJ3+6mVQ6c1ShCUca4QAtp6hK1BVjA@mail.gmail.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 <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.220.1427407398.10327.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1427407398 news.xs4all.nl 2961 [2001:888:2000:d::a6]:44389
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:88100

Show key headers only | View raw


On 3/24/15 6:39 PM, Jason Swails wrote:
>
>
> On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille <emile@fenx.com
> <mailto:emile@fenx.com>> wrote:
>
>     On 3/23/2015 5:52 AM, Steven D'Aprano wrote:
>
>         Are there any other, possibly better, ways to calculate the
>         fractional part
>         of a number?
>
>
>     float (("%6.3f" % x)[-4:])
>
>
> ​In general you lose a lot of precision this way...​

I suggest modf in the math library:

math.modf(x)
Return the fractional and integer parts of x. Both results carry the 
sign of x and are floats.


Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


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