Significant figures calculation

Harold dadapapa at googlemail.com
Fri Jun 24 16:05:41 EDT 2011


Hi,

I am looking for an easy way to do significant figure calculations in
python (which I want to use with a class that does unit calculations).
Significant figure calculations should have the semantics explained,
e.g., here: http://chemistry.about.com/od/mathsciencefundamentals/a/sigfigures.htm

My hope was that the decimal module would provide this functionality,
but:

>>> print Decimal('32.01') + Decimal(5.325) + Decimal('12')
49.335     # instead of 49
>>> print Decimal('25.624') / Decimal('25')
1.02496   # instead of 1.0
>>> print Decimal('1.2') == Decimal('1.23')
False      # actually not sure how the semantics should be

I tried to modify the DecimalContext (e.g. getcontext().prec = 2) but
that did not lead to the correct behavior. Google and this list didn't
yield a good answer yet...  so I'd be happy to get a good
recommendations or pointers.

P.S. I am aware that significant figure calculation is controversial
and makes implicit assumptions on the probability distributions of the
variables. I am simply looking for an implementation of the (well
defined) arithmetics as defined on the cited website.



More information about the Python-list mailing list