Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.016 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'that?': 0.05; 'remaining': 0.07; 'string': 0.09; 'thus,': 0.09; 'python': 0.11; '2.7': 0.14; 'floats;': 0.16; 'ignoring': 0.16; 'map:': 0.16; 'precision,': 0.16; 'precision.': 0.16; 'roy': 0.16; 'str()': 0.16; 'to:name:python list': 0.16; '>>>': 0.22; 'print': 0.22; 'parse': 0.24; 'skip:l 30': 0.24; '---': 0.24; "i've": 0.25; 'header:In- Reply-To:1': 0.27; 'point': 0.28; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'that.': 0.31; 'bunch': 0.31; 'decimal': 0.31; 'values.': 0.31; 'could': 0.34; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'false': 0.36; 'subject:?': 0.36; 'hi,': 0.36; 'example,': 0.37; 'positive': 0.37; 'skip:4 10': 0.37; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'fact': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'problems.': 0.60; 'numbers': 0.61; 'map': 0.64; 'between': 0.67; 'smith': 0.68; 'results': 0.69; 'obvious': 0.74; 'email addr:panix.com': 0.84; 'intuit': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=Z3XGhHAE+mtejLwIR3qVEamIxWQOKkoMx9oFF7WwpJ4=; b=Dk3+Wnkrid66/uMqb00vFBP1LgTKnKf9d5vxlbyWdaJaL/XDlyrQ+57ybxsG3Hb+/I Gt88DL+lOpTUNDc1cgaS8BDa/jcUoMb8VWfSdPBrUgA1sdAfEVS5weawkK8s8bIdMGJJ ACsMhAFBPJn1oTk49D0Y4ktunt0nJTiwU0TuBon/Q5tbX84Cru6/W34DRy2zP9LBEdmm falyjlrHbdldhhUh4aNKl3EAosIrajk7uAfLwBBWgZyVCWDvkoXrIAGiJmPbuEPa2Rh5 r8gVvTM6mc3vEypi3ZJ5g77uRE5D5yfvdTv5LB5MNouDJcZO4A9+G+t1xTzfwlpINGuJ Xpyw== MIME-Version: 1.0 X-Received: by 10.140.47.18 with SMTP id l18mr375915qga.9.1398788300586; Tue, 29 Apr 2014 09:18:20 -0700 (PDT) In-Reply-To: References: Date: Tue, 29 Apr 2014 18:18:20 +0200 Subject: Re: Significant digits in a float? From: Vlastimil Brom To: Python List Content-Type: text/plain; charset=UTF-8 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398788304 news.xs4all.nl 2946 [2001:888:2000:d::a6]:45237 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70717 2014-04-28 18:00 GMT+02:00 Roy Smith : > 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. > > --- > Roy Smith > roy@panix.com > Hi, I doubt, many would consider a string/regex approach very clean here, but anyway; hopefully the results conform to your specs (as far as I understood it correctly). Alternatively, the floats can be rounded before, if e.g. 39.9999999 could be a false positive for 4-digits precision. hth, vbr = = = = = = = >>> for fl in (38.0, 41.2586, 40.75280000000001, 49.25, 33.795199999999994, 36.837199999999996, 34.1489, 45.5, 40.0010, 39.00000009, 39.9999999, 38.00009, 40.0100, 41.2000, 43.0001): ... print repr(fl), "==>", len(re.match(r"^-?\d+\.([0-9]{0,4})(? 0 41.2586 ==> 4 40.75280000000001 ==> 4 49.25 ==> 2 33.795199999999994 ==> 4 36.837199999999996 ==> 4 34.1489 ==> 4 45.5 ==> 1 40.001 ==> 3 39.00000009 ==> 0 39.9999999 ==> 4 38.00009 ==> 0 40.01 ==> 2 41.2 ==> 1 43.0001 ==> 4 >>> = = = = = = =