How to detect a double's significant digits

John Roth newsgroups at jhrothjr.com
Fri May 6 09:10:53 EDT 2005


"mrstephengross" <mrstephengross at hotmail.com> wrote in message 
news:1115314620.902071.323410 at f14g2000cwb.googlegroups.com...
> 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)

As a lot of the responders have pointed out, it's effectively
impossible to do so in any meaningful fashion if all you
have is the binary representation of the number.

One way of doing it is the ScientificDouble class in
PyFit (and in all other versions of FIT, for that matter.)
That provides a class that saves the original string
representation so it can be used to determine precision
for compares. It's a niche approach: it works well for
FIT's purposes, but it's not a general purpose method
of estimating precision throughout a lengthy calculation.
That requires quite different techniques.

See:

fit.c2.com
www.fitnesse.org
FitNesse Yahoo mailing list (see the files section for PyFit.)

John Roth
> 




More information about the Python-list mailing list