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: 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 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> 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: 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: 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 On 3/24/15 6:39 PM, Jason Swails wrote: > > > On Mon, Mar 23, 2015 at 8:38 PM, Emile van Sebille > 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.