Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'that?': 0.05; 'remaining': 0.07; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'thus,': 0.09; 'trailing': 0.09; 'python': 0.11; 'wrote': 0.14; '2.7': 0.14; '2100': 0.16; '6th': 0.16; 'floats;': 0.16; 'ignoring': 0.16; 'map:': 0.16; 'precision,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'str()': 0.16; 'unexpected': 0.16; 'code.': 0.18; 'parse': 0.24; "i've": 0.25; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'correct': 0.29; 'thus': 0.29; "i'm": 0.30; 'that.': 0.31; 'bunch': 0.31; 'decimal': 0.31; 'strip': 0.31; 'values.': 0.31; 'something': 0.35; 'convert': 0.35; 'point.': 0.35; 'there': 0.35; 'possible': 0.36; 'subject:?': 0.36; 'should': 0.36; 'example,': 0.37; 'skip:4 10': 0.37; 'to:addr :python-list': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'problems.': 0.60; 'numbers': 0.61; 'range': 0.61; "you're": 0.61; 'such': 0.63; 'map': 0.64; 'between': 0.67; 'smith': 0.68; 'obvious': 0.74; 'algorithm,': 0.84; 'intuit': 0.84; 'limits,': 0.84; 'subject::': 0.85 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re:Significant digits in a float? Date: Mon, 28 Apr 2014 15:09:15 -0400 (EDT) Organization: news.gmane.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: dpc6744197045.direcpc.com X-Newsreader: PiaoHong.NewsGroup.Client.VIP:1.52 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398711804 news.xs4all.nl 2913 [2001:888:2000:d::a6]:42049 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70689 Roy Smith Wrote in message: > I'm using Python 2.7 > > I have a bunch of floating point values. For example, here's a few (printed as reprs): > > 38.0 > 41.2586 > 40.75280000000001 > 49.25 > 33.795199999999994 > 36.837199999999996 > 34.1489 > 45.5 > > Fundamentally, these numbers have between 0 and 4 decimal digits of precision, and I want to be able to intuit how many each has, ignoring the obvious floating point roundoff problems. Thus, I want to map: > > 38.0 ==> 0 > 41.2586 ==> 4 > 40.75280000000001 ==> 4 > 49.25 ==> 2 > 33.795199999999994 ==> 4 > 36.837199999999996 ==> 4 > 34.1489 ==> 4 > 45.5 ==> 1 > > Is there any clean way to do that? The best I've come up with so far is to str() them and parse the remaining string to see how many digits it put after the decimal point. > > The numbers are given to me as Python floats; I have no control over that. I'm willing to accept that fact that I won't be able to differentiate between float("38.0") and float("38.0000"). Both of those map to 1, which is OK for my purposes. > Ignoring the unexpected terminology, you seem to be looking for the number of decimal places, and you're not interested in 2100 ==> -2 If you know something about the possible range of the numbers, and/or you know the valid range of decimal places, then you should convert to string in a way that will round the 3rd, 5th, and 6th values. Then if the string has no decimal, the answer is 0. If there are any trailing zeroes, strip them. Then just count digits after the decimal point. Without such limits, there can be no unique algorithm, and thus no correct code. -- DaveA