[Python-ideas] Python Numbers as Human Concept Decimal System
Oscar Benjamin
oscar.j.benjamin at gmail.com
Sat Mar 8 00:42:14 CET 2014
On 7 March 2014 19:20, Guido van Rossum <guido at python.org> wrote:
>
> Maybe we can fix the conversion between Decimal and float (if this is really
> all that matters to Mark, as it appears to be from his last email -- I'd
> already written most of the above before it arrived). Could it be as simple
> as converting the float to a string using repr()? Given the smarts in the
> float repr() that should fix the examples Mark complained about. Are there
> any roadblocks in the implementation or assumptions of the Decimal type
> here?
The current Decimal constructor comes with a guarantee of exactness.
It accepts int, str and float (and tuple) and creates a Decimal object
having the exact value of the int or float or the exact value that
results from a decimal interpretation of the str. This exactness is
not mandated by any of the standards on which the decimal module is
based but I think it is a good thing. I strongly oppose a change that
would have it use heuristics for interpreting other numeric types.
>Perhaps the default Decimal context being set for a much higher
> precision makes it philosophically unacceptable?
The context is ignored by the Decimal constructor which will always
create an exact value. If you want the created value rounded to
context use unary +:
>>> from decimal import Decimal as D
>>> D(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> +D(0.1)
Decimal('0.1000000000000000055511151231')
Oscar
More information about the Python-ideas
mailing list