Engineering numerical format PEP discussion

Keith keith.brafford at gmail.com
Mon Apr 26 20:42:20 EDT 2010


On Apr 26, 5:33 am, Stefan Krah <stefan-use... at bytereef.org> wrote:
> Keith <keith.braff... at gmail.com> wrote:
> > Even though this uses the to_eng_string() function, and even though I
> > am using the decimal.Context class:
>
> > >>> c = decimal.Context(prec=5)
> > >>> decimal.Decimal(1234567).to_eng_string(c)
> > '1234567'
>
> > That is not an engineering notation string.
>
> To clarify further: The spec says that the printing functions are not
> context sensitive, so to_eng_string does not *apply* the context.
>
> The context is only passed in for the 'capitals' value, which determines
> whether the exponent letter is printed in lower or upper case.
>
> This is one of the unfortunate situations where passing a context can
> create great confusion for the user. Another one is:
>
> >>> c = Context(prec=5)
> >>> Decimal(12345678, c)
>
> Decimal('12345678')
>
> Here the context is passed only for the 'flags' and 'traps' members:
>
> >>> Decimal("wrong", c)
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/usr/lib/python3.2/decimal.py", line 548, in __new__
>     "Invalid literal for Decimal: %r" % value)
>   File "/usr/lib/python3.2/decimal.py", line 3836, in _raise_error
>     raise error(explanation)
> decimal.InvalidOperation: Invalid literal for Decimal: 'wrong'
>
> >>>c.traps[InvalidOperation] = False
> >>> Decimal("wrong", c)
>
> Decimal('NaN')
>
> Stefan Krah

Thank you for that illustrative clarification, Stefan.  I should not
have used decimal.Context in that case, nor should I have implied that
it would have helped prove my case.

--Keith Brafford



More information about the Python-list mailing list