Velocity Reviews > How to detect a double's significant digits

# How to detect a double's significant digits

mrstephengross
Guest
Posts: n/a

 05-05-2005
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

--Steve ((E-Mail Removed))

James Stroud
Guest
Posts: n/a

 05-05-2005
Significant digits are an accounting concept. As such, it is up to the
accountant to keep track of these as only she knows the precision of her
measurements.

Koan for the day:

What are the significant digits of 0.1?

Hint:

>>> `0.1`

James

On Thursday 05 May 2005 10:37 am, so sayeth mrstephengross:
> 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
>
> --Steve ((E-Mail Removed))

--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/

mrstephengross
Guest
Posts: n/a

 05-05-2005
So how can I get the kind of information I want then?

For example:

0.103 --> 3
0.0103 --> 4
0.00103 --> 5
0.000103 --> 6
0.0000103 --> 7

Any ideas?
--Steve

phil
Guest
Posts: n/a

 05-05-2005
fl = 1.0002
x = str(fl)
pos = x.find('.')
print len( x[pos+1:] )
>>> 4

mrstephengross 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
>
> --Steve ((E-Mail Removed))
>
>

mrstephengross
Guest
Posts: n/a

 05-05-2005
Ok, that won't work. First of all, str() is not a function. If I want
to convert the float into a string, the conversion function will have
to use some kind of numeric precision, which will screw things up.
Consider this:

float f = 1.004;
ostringstream s;
s << f;
cout << s.str();

The above code may produce "1.004", or "1.0040", or "1.00400",
depending on the stream's precision setting. I need a way to detect the
number of digits to the right of decimal point *prior* to doing any
kind of string conversion.

--Steve

Steven Bethard
Guest
Posts: n/a

 05-05-2005
mrstephengross wrote:
> So how can I get the kind of information I want then?
>
> For example:
>
> 0.103 --> 3
> 0.0103 --> 4
> 0.00103 --> 5
> 0.000103 --> 6
> 0.0000103 --> 7

Beware that this is probably only relevant if you have your numbers as
strings, not as floats:

py> 0.103
0.10299999999999999

But, assuming you have your numbers as strings, I would suggest looking
at str.split() and len(). I'd give you an example, but this sounds
kinda like a homework assignment.

STeVe

Steven Bethard
Guest
Posts: n/a

 05-05-2005
mrstephengross wrote:
> First of all, str() is not a function.

Yes it is.

> float f = 1.004;
> ostringstream s;
> s << f;
> cout << s.str();

This doesn't look like Python to me. Are you sure you're on the right
newsgroup?

STeVe

mrstephengross
Guest
Posts: n/a

 05-05-2005
>But, assuming you have your numbers as strings, I would suggest
looking
at str.split() and len().

Well, the numbers are in fact stored as numbers, so string processing
won't work.

>I'd give you an example, but this sounds kinda like a homework

assignment.

The task may sound like it comes from class, but I can assure you that
I am indeed a professional developer. I'm doing some rather intricate
text processing / rendering stuff these days, and C++ is unfortunately
none too handy for that sort of thing. Unfortunately, I have to use it

Thanks,
--Steve

Steven Bethard
Guest
Posts: n/a

 05-05-2005
mrstephengross wrote:
> Well, the numbers are in fact stored as numbers, so string processing
> won't work.

What kind of numbers? Python floats?

STeVe

Steven Bethard
Guest
Posts: n/a

 05-05-2005
mrstephengross wrote:
>>But, assuming you have your numbers as strings, I would suggest

>
> looking
> at str.split() and len().
>
> Well, the numbers are in fact stored as numbers, so string processing
> won't work.

py> def digits(f):
.... return len(str(f).split('.')[1].rstrip('0'))
....
py> for f in [0.103, 0.1030, 0.0103, 0.010300]:
.... print f, digits(f)
....
0.103 3
0.103 3
0.0103 4
0.0103 4

I believe the rstrip is unnecessary because I think str() will never
produce additional following zeros, but you can guarantee it by calling
rstrip if you want.

Note that, for example, 0.103 and 0.1030 are identical as far as Python
is concerned, so I hope you're not hoping to show a difference between
these two...

STeVe