
On Wednesday 01 August 2001 10:55 am, Guido van Rossum wrote:
I believe it is typically global per thread, but that's up to the langage binding. A Java binding for Cowlishaw's decimals apparently requires passing in a context as a third argument on each operation.
I'm trying to understand how a decimal number context would work. Is the context a variable and/or flag that defines the rounding rules and precision of a number when it is used in a calculation? How is it associated with a number or a calculation? The "global per thread" description seems to associate the context with threads. Can the context be altered inside the thread? Is it possible to change the context at different levels in a stackframe? I would assume there is a default context will be used until the context is changed. If this is the case I would expect a default context would be defined at startup. Would it make sense to have a simple decimal type with no features that can be modified (a fixed context)? This simple type could be extended by deriving a new numerical type from the base decimal type. This base decimal type would be targeted at the newbie user. It would have no surprises. It would have a default precision of 18 and the rules for rounding would emulate the typical hand held calculator. Accountants who need special rounding rules would use a derived type that allowed the default rules to be overridden. It would be possible to round numbers of the simple based type, but it would be an explicit step to remove insignificant digits. An accounting decimal type might automatically round calculations to the smallest denomination. For instance, an accounting context might have automatically managed the final rounding in the following calculation: p>>> quantity = 6
tax = .06 price = 2.99 total = price * quantity * (1 + tax) total 19.0164 round(total,2) 19.02
M.-A. Lemburg" suggested looking at the SQL specification for Decimal datatypes. A decimal type is also defined as a type in XML Schema. Since this is an XML datatype there isn't a definition for how these numbers are created.
Do these say anything about semantics under numeric operations? That would seem to be outside the realm of XML and possibly even outside SQL. So I'm not sure how these help.
You are correct that it doesn't deal with numeric operations. It does define a minimum precision requirement. I am only referencing it here because it is another instance where having a decimal type in Python would be useful and because they have set a minimum requirement. Setting this minmum as a default behavior would probably make newbies comfortable with the language.
NOTE: All ·minimally conforming· processors ·must· support decimal numbers with a minimum of 18 decimal digits (i.e., with a ·totalDigits· of 18). However, ·minimally conforming· processors ·may· set an application-defined limit on the maximum number of decimal digits they are prepared to support, in which case that application-defined maximum number ·must· be clearly documented. - http://www.w3.org/TR/xmlschema-2/#decimal