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


Groups > comp.lang.python > #70683 > unrolled thread

Re: Significant digits in a float?

Started byNed Batchelder <ned@nedbatchelder.com>
First post2014-04-28 12:07 -0400
Last post2014-04-28 15:00 -0400
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

#70683 — Re: Significant digits in a float?

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-04-28 12:07 -0400
SubjectRe: Significant digits in a float?
Message-ID<mailman.9550.1398701251.18130.python-list@python.org>
On 4/28/14 12:00 PM, Roy Smith wrote:
> 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.

That sounds like a pretty clean way:  len(str(num).partition(".")[2]), 
though it also sounds like you understand all of the inaccuracies in 
that technique.

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

[toc] | [next] | [standalone]


#70687

FromRoy Smith <roy@panix.com>
Date2014-04-28 11:39 -0700
Message-ID<238c9d43-9182-42f7-a4b4-4b73e4cf2a92@googlegroups.com>
In reply to#70683
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.

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.

[toc] | [prev] | [next] | [standalone]


#70688

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-04-28 15:00 -0400
Message-ID<mailman.9552.1398711661.18130.python-list@python.org>
In reply to#70687
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web