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


Groups > comp.lang.python > #70688

Re: Significant digits in a float?

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.021
X-Spam-Evidence '*H*': 0.96; '*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; 'way:': 0.09; 'ah,': 0.16; 'defined.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'str()': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'skip:l 30': 0.24; 'looks': 0.24; "i've": 0.25; 'right.': 0.26; 'header:X-Complaints- To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'specifically': 0.29; 'that.': 0.31; 'decimal': 0.31; 'produces': 0.31; 'monday,': 0.33; 'could': 0.34; 'point.': 0.35; 'but': 0.35; 'there': 0.35; 'subject:?': 0.36; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'little': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'how': 0.40; 'guarantee': 0.63; 'map': 0.64; 'different': 0.65; 'details': 0.65; 'smith': 0.68; 'special': 0.74; 'behavior': 0.77; 'actually,': 0.84; 'batchelder': 0.84; 'float,': 0.84; 'thing,': 0.91; 'imagine': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: Significant digits in a float?
Date Mon, 28 Apr 2014 15:00:38 -0400
References <B654EA7E-35C7-4A78-A673-4CAD0C23B5C0@panix.com> <mailman.9550.1398701251.18130.python-list@python.org> <238c9d43-9182-42f7-a4b4-4b73e4cf2a92@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 18.189.27.191
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.4.0
In-Reply-To <238c9d43-9182-42f7-a4b4-4b73e4cf2a92@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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.9552.1398711661.18130.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1398711661 news.xs4all.nl 2967 [2001:888:2000:d::a6]:39901
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:70688

Show key headers only | View raw


On 4/28/14 2:39 PM, Roy Smith wrote:
> On Monday, April 28, 2014 12:07:14 PM UTC-4, Ned Batchelder wrote:
>
>> On 4/28/14 12:00 PM, Roy Smith wrote:
>>> 38.0  ==> 0
>>> [...]
>>> 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.
>>
>> That sounds like a pretty clean way:  len(str(num).partition(".")[2]),
>> though it also sounds like you understand all of the inaccuracies in
>
> Well, it's actually, a little uglier, because I want to map 38.0 ==>0, so I need to special case that.

Ah, right.

>
> The other annoying thing about using str() is its behavior isn't well defined.  It looks like it does the right thing, but I imagine the details could change in a different implementation.
>

I don't have a reference, but in recent Pythons, str() was specifically 
changed to guarantee that it produces the shortest string that when 
re-interpreted as a float, produces the same float.

-- 
Ned Batchelder, http://nedbatchelder.com

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


Thread

Re: Significant digits in a float? Ned Batchelder <ned@nedbatchelder.com> - 2014-04-28 12:07 -0400
  Re: Significant digits in a float? Roy Smith <roy@panix.com> - 2014-04-28 11:39 -0700
    Re: Significant digits in a float? Ned Batchelder <ned@nedbatchelder.com> - 2014-04-28 15:00 -0400

csiph-web