views:

62

answers:

1

I have 3 questions pertaining to decimal arithmetic in Python, all 3 of which are best asked inline:

1)

>>> from decimal import getcontext, Decimal
>>> getcontext().prec = 6
>>> Decimal('50.567898491579878') * 1
Decimal('50.5679')
>>> # How is this a precision of 6? If the decimal counts whole numbers as
>>> # part of the precision, is that actually still precision?
>>> 

and

2)

>>> from decimal import getcontext, Decimal
>>> getcontext().prec = 6
>>> Decimal('50.567898491579878')
Decimal('50.567898491579878')
>>> # Shouldn't that have been rounded to 6 digits on instantiation?
>>> Decimal('50.567898491579878') * 1
Decimal('50.5679')
>>> # Instead, it only follows my precision setting set when operated on.
>>> 

3)

>>> # Now I want to save the value to my database as a "total" with 2 places.
>>> from decimal import Decimal
>>> # Is the following the correct way to get the value into 2 decimal places,
>>> # or is there a "better" way?
>>> x = Decimal('50.5679').quantize(Decimal('0.00'))
>>> x  # Just wanted to see what the value was
Decimal('50.57')
>>> foo_save_value_to_db(x)
>>> 
+5  A: 
  1. Precision follows sig figs, not fractional digits. The former is more useful in scientific applications.

  2. Raw data should never be mangled. Instead it does the mangling when operated upon.

  3. This is how it's done.

Ignacio Vazquez-Abrams
I am not convinced that #1 is because of sig figs. If it were, then the answer would not go out to 5 decimal places (because 1 only has 1 sig fig). Take a look at the link I provided--I think that probably holds the answer.
Stargazer712
`1` is an integer literal. Integers are considered "countable", which means that there is no loss of precision when multiplying by them.
Ignacio Vazquez-Abrams
The answer went to 4 decimal places.
orokusaki
That may be, but Decimal('50.567898491579878') * Decimal(1.01) still provides too many sig figs in the answer. It would make no sense for python to impose such a restrictive business rule on those who may or may not be doing anything that has to do with science.
Stargazer712
`Decimal` does not actually *do* math with sig figs, it simply allows the programmer to do so.
Ignacio Vazquez-Abrams
Ah, now I am seeing where you are coming from. I am far too used to sig-figs dealing primarily with the interaction between numbers.
Stargazer712