Python cdecimal InvalidOperation
Question: I am trying to read financial data and store it. The place I get the financial
data from stores the data with incredible precision, however I am only
interested in 5 figures after the decimal point. Therefore, I have decided to
use t = .quantize(cdecimal.Decimal('.00001'), rounding=cdecimal.ROUND_UP) on
the Decimal I create, but I keep getting an InvalidOperation exception. Why is
this?

    
    
    >>> import cdecimal
    >>> c = cdecimal.getcontext()
    >>> c.prec = 5
    >>> s = '45.2091000080109'
    >>> # s = '0.257585003972054' works!
    >>> t = cdecimal.Decimal(s).quantize(cdecimal.Decimal('.00001'), rounding=cdecimal.ROUND_UP)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      cdecimal.InvalidOperation: [<class 'cdecimal.InvalidOperation'>]
    

Why is there an invalid operation here? If I change the precision to 7 (or
greater), it works. If I set s to be '0.257585003972054' instead of the
original value, that also works! What is going on?

Thanks!


Answer: decimal version gives a better description of the error:

    
    
    Python 2.7.2+ (default, Feb 16 2012, 18:47:58) 
    >>> import decimal
    >>> s = '45.2091000080109'
    >>> decimal.getcontext().prec = 5
    >>> decimal.Decimal(s).quantize(decimal.Decimal('.00001'), rounding=decimal.ROUND_UP)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/usr/lib/python2.7/decimal.py", line 2464, in quantize
        'quantize result has too many digits for current context')
      File "/usr/lib/python2.7/decimal.py", line 3866, in _raise_error
        raise error(explanation)
    decimal.InvalidOperation: quantize result has too many digits for current context
    >>> 
    

[Docs](http://docs.python.org/library/decimal.html#decimal.Decimal.quantize):

> Unlike other operations, if the length of the coefficient after the quantize
> operation would be greater than precision, then an InvalidOperation is
> signaled. This guarantees that, unless there is an error condition, the
> quantized exponent is always equal to that of the right-hand operand.

But i must confess i don't know what this means.

