Significant digits in a float?
Steven D'Aprano
steve at pearwood.info
Tue Apr 29 01:43:19 EDT 2014
On Tue, 29 Apr 2014 13:23:07 +1000, Ben Finney wrote:
> Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>
>> By the way, you contradict yourself here. Earlier, you described 38.0
>> as having zero decimal places (which is wrong). Here you describe it as
>> having one, which is correct, and then in a later post you describe it
>> as having zero decimal places again.
>
> I get the impression that this is at the core of the misunderstanding.
> Having a number's representation ending in “….0” does not mean zero
> decimal places; it has exactly one. The value's representation contains
> the digit “0” after the decimal point, but that digit is significant to
> the precision of the representation.
>
> If the problem could be stated such that “38.0” and “38” and “38.000”
> are consistently described with the correct number of decimal digits of
> precision (in those examples: one, zero, and three), maybe the
> discussion would make more sense.
It's actually trickier than that. Digits of precision can refer to
measurement error, or to the underlying storage type. Python floats are C
doubles, so they have 64 bits of precision (approximately 17 decimal
digits, if I remember correctly) regardless of the precision of the
measurement. The OP (Roy) is, I think, trying to guess the measurement
precision after the fact, given a float. If the measurement error really
does differ from value to value, I don't think he'll have much luck:
given a float like 23.0, all we can say is that it has *at least* zero
significant decimal places. 23.1 has at least one, 23.1111 has at least
four.
If you can put an upper bound on the precision, as Roy indicates he can,
then perhaps a reasonable approach is to convert to a string rounded to
four decimal places, then strip trailing zeroes:
py> x = 1234.1 # actual internal is closer to 1234.099999999999909
py> ("%.4f" % x).rstrip('0')
'1234.1'
then count the number of digits after the dot. (This assumes that the
string formatting routines are correctly rounded, which they should be on
*most* platforms.) But again, this only gives a lower bound to the number
of significant digits -- it's at least one, but might be more.
--
Steven
More information about the Python-list
mailing list