[issue20502] Context.create_decimal_from_float() inconsistent precision for zeros after decimal mark

Stefan Krah report at bugs.python.org
Tue Feb 4 18:54:34 CET 2014


Stefan Krah added the comment:

Mauricio de Alencar <report at bugs.python.org> wrote:
> The floats I posted are examples of computation results. The meaningful
> figures are related to the precision of the measurements fed to the
> computation.

Thank you, that makes it clear.  Constructing Decimal('256.2') preserves
the exact value, regardless of the context precision.  All arithmetic
functions accept arbitrary precision input and use all digits regardless
of the context precision.

To put it differently, decimal refuses to guess and treats any input
as having the correct number of significant digits.

If you want to attribute a significance to a series of input numbers,
I guess you have to do it manually, using something like:

def mk_full_coeff(x):
    prec = getcontext().prec
    adj = x.adjusted()
    if adj >= prec:
        return +x
    else:
        return x.quantize(Decimal(1).scaleb(adj-prec+1))

>>> c = getcontext()
>>> c.prec = 3
>>> [mk_full_coeff(x) for x in [Decimal('256.2'), Decimal('1.3'), Decimal('0.5')]]
[Decimal('256'), Decimal('1.30'), Decimal('0.500')]

----------

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


More information about the Python-bugs-list mailing list