python decimal library dmath.py v0.3 released

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Mon Mar 3 17:44:27 EST 2014


On Monday, March 3, 2014 10:18:37 PM UTC+1, Mark H. Harris wrote:
> On Monday, March 3, 2014 2:03:19 PM UTC-6, Mark H. Harris wrote:
> 
> Wolfgang,  answer is not so much, in fact, not at all. 
> But it is an interesting question for me; where I am 
> continuing to learn the limits of Decimal, and the 
> decimal context. I don't need rounding for integer 
> multiplication, of course.
>
 
You don't want it and you don't get it for integer multiplication, but you may get it with Decimal multiplication and not high-enough precision:

>>> with localcontext() as ctx:
    	ctx.prec=2
	Decimal(11)*Decimal(11)
	
Decimal('1.2E+2')

This is the very nature of rounding to context precision and functions dealing with Decimals shouldn't behave differently in my opinion. If you don't want rounding either use sufficiently high precision or use integers.

> 
> I am interested in arbitrary limits, like emax, for instance. 
> The doc is a little ambiguous. Is emax the max exponent, 
> and if so, is 999999999 the limit, or is that the default 
> context value which might be bumped up? 
>

I don't find much ambiguity in the docs here:

" class decimal.Context(prec=None, rounding=None, Emin=None, Emax=None, capitals=None, clamp=None, flags=None, traps=None)

    Creates a new context. If a field is not specified or is None, the default values are copied from the DefaultContext. If the flags field is not specified or is None, all flags are cleared.
..
    The Emin and Emax fields are integers specifying the outer limits allowable for exponents. Emin must be in the range [MIN_EMIN, 0], Emax in the range [0, MAX_EMAX]."

So, Emax is the maximal exponent allowed in a specific context and the constant MAX_EMAX is the maximum Emax that you can set in any context.
Also (on my system):

>>> from decimal import getcontext
>>> getcontext()

Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999, Emax=999999, capitals=1, clamp=0, flags=[], traps=[InvalidOperation, DivisionByZero, Overflow])

shows that my default context Emax is way smaller than MAX_EMAX.


> I have discovered just by playing with integer multiplication  
> that those BIGNUMS don't seem to have a physical limit. Of 
> course there isn't a decimal to keep track of, and they can 
> just grow and grow; wouldn't want to make a Decimal from 
> one of those, other than it is interesting to me as I'm trying
> to understand Decimal floating point.
>

decimal can handle BIGNUMS fairly well, you just need to increase context Emax. Have you ever tried to calculate stuff with ints as big as MAX_EMAX (10**999999999999999999) or even close to it and still had a responsive system ??
My point in suggesting the fix for your epx function was that the unnecessary juggling of extremely large values (Decimal or int) is a killer for performance.

> marcus



More information about the Python-list mailing list