[issue4796] Decimal to receive from_float method

Raymond Hettinger report at bugs.python.org
Thu Jan 1 01:38:24 CET 2009


Raymond Hettinger <rhettinger at users.sourceforge.net> added the comment:

The decimal constructor should be lossless.  The underlying spec was
designed with the notion that all numbers in decimal are exact;
operations can be lossy but the numbers themselves are exact. 
Accordingly, I recommend Decimal.from_float(f) with no qualifiers or
optional arguments.

To support the use case of wanting to round the input, I suggest a
separate method modeled on Context.create_decimal().  It can either be
an extension of the existing method or a new method like
Context.create_decimal_from_float().  I recommend the former since
rounding is already implied by the context qualifier.  Either way, the
effect would be the same as Decimal.from_float(f) + 0 in a given
context.  Per the docs:  "Creates a new Decimal instance from num but
using self as context. Unlike the Decimal constructor, the context
precision, rounding method, flags, and traps are applied to the conversion."

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4796>
_______________________________________


More information about the Python-bugs-list mailing list