How to detect a double's significant digits

Jeremy Bowers jerf at jerf.org
Thu May 5 16:32:38 EDT 2005


On Thu, 05 May 2005 18:42:17 +0000, Charles Krug wrote:

> On 5 May 2005 10:37:00 -0700, mrstephengross <mrstephengross at hotmail.com>
> wrote:
>> Hi all... How can I find out the number of significant digits (to the
>> right of the decimal place, that is) in a double? At least, I *think*
>> that's what I'm asking for. For instance:
>> 
>> 0.103 --> 3
>> 0.0103 --> 4
>> 0.00103 --> 5
>> 0.000103 --> 6
>> 0.0000103 --> 7
>> 
>> Thanks in advance!
>> --Steve (mrstephengross at hotmail.com)
>> 
>> 
> I would say that each of these examples has three signficant figures. Each
> of them can be expressed as:
> 
> 1.03e+n
> 
> For any integer n.

You beat me to it.

Step one for mrstephengross is to *rigorously* define what he means by
"significant digits", then go from there. Since I think he mentioned
something about predicting how much space it will take to print out, my
suggestion is to run through whatever printing routines there are and get
a string out, the measure the string, as anything else will likely be
wrong. If that's not possible with the formatting library, you've already
lost; you'll have to completely correctly re-implement the formatting
library, and not only is that a major PITA, you almost never get it
bug-for-bug right...



More information about the Python-list mailing list