# [Python-Dev] Mixing float and Decimal -- thread reboot

Raymond Hettinger raymond.hettinger at gmail.com
Mon Mar 22 00:11:13 CET 2010

```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.

```