Significant digits in a float?
Roy Smith
roy at panix.com
Mon Apr 28 12:00:23 EDT 2014
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 at panix.com
More information about the Python-list
mailing list