Significant figures calculation
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Jun 24 16:46:11 EDT 2011
On Fri, 24 Jun 2011 13:05:41 -0700, Harold wrote:
> 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.
Really? It works for me.
>>> import decimal
>>> D = decimal.Decimal
>>> decimal.getcontext().prec = 2
>>>
>>> D('32.01') + D('5.325') + D('12')
Decimal('49')
>>>
>>> D('25.624') / D('25')
Decimal('1.0')
The only thing you have to watch out for is this:
>>> D('1.2') == D('1.23') # no rounding
False
>>> D('1.2') == +D('1.23') # force rounding
True
--
Steven
More information about the Python-list
mailing list