On Mar 21, 2010, at 3:50 PM, Greg Ewing wrote: > Raymond Hettinger wrote: > >> Since decimal also allows arbitrary sizes, all long ints can be >> exactly represented (this was even one of the design goals >> for the decimal module). > > There may be something we need to clarify here. I've been > imagining that the implicit conversions to Decimal that > we're talking about would be done to whatever precision > is set in the context. Am I wrong about that?
Yes. The conversion to decimal is independent of the current context. The background docs for the decimal module makes its design intention clear. All numbers are exact. Rounding and context adjustments only apply to the *results (of operations. The module is designed that way. Its background documents confirm that viewpoint. And its operations are designed to support that world view (i.e. unary plus is an operation that can change a value). Also, we confirmed that point-of-view with the person who wrote the spec. > Is the intention > to always use enough digits to get an exact representation? Yes. That is in-fact what Decimal.fromfloat() does. That agrees with the decimal constructor itself which is not context sensitive: >>> decimal.getcontext().prec = 5 >>> decimal.Decimal('3.1415926535') # Notice this value doesn't get rounded Decimal('3.1415926535') >>> decimal.Decimal('3.1415926535') + 0 # This result does get rounded. Decimal('3.1416') >>> # Also, rounding does not get applied during an equality check >>> decimal.getcontext().prec = 5 >>> decimal.Decimal('3.1415926535') == decimal.Decimal('3.1416') False Raymond P.S. Thanks for asking the question. _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com