[Python-Dev] Decimal data type issues

Tim Peters tim.one at comcast.net
Thu Apr 15 11:32:15 EDT 2004


[Batista, Facundo]
> I'm lost. Help me with an example (that later I'll shamelessly stole to
> use in a test case):
>
>    d1 = Decimal('123')
>    d2 = Decimal('456')
>
> So, long, they have the same context, right?

Context is associated with operations, not with instances, so d1 and d2
don't "have" any context.  It's unclear to me what use (if any) the
Decimal() constructor makes of context right now.  To the extent that the
Decimal() constructor makes of context, to exactly that same extent the
Decimal constructor is "an operation", so while context may affect
Decimal(), I don't know exactly how it does now.  In any case, context has
no effect on the instances d1 and d2 after they're constructed (although
context both affects and is affected by any operations d1 and d2 may later
take part in).

>    getcontext().prec = 46
>
> The precision should change for both d1 and d2, right?

Sorry, it simply doesn't make sense to talk about context wrt instances,
only wrt operations.  If, e.g., you do an operation now, like

    d4 = d1 + d2

then the result of the operation "d1+d2" has precision 46.

>    d3 = Decimal('789')
>
> d3 also has the precision in 46, right?

That depends on how Decimal() is implemented, and I don't know the answer to
that.





More information about the Python-Dev mailing list