Floating point "g" format not stripping trailing zeros

Ian Kelly ian.g.kelly at gmail.com
Fri Feb 13 16:01:11 EST 2015


On Fri, Feb 13, 2015 at 1:33 PM, Grant Edwards <invalid at invalid.invalid> wrote:
> On 2015-02-13, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> On Fri, Feb 13, 2015 at 7:02 AM, Grant Edwards <invalid at invalid.invalid> wrote:
>>> On 2015-02-12, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>>> On Thu, Feb 12, 2015 at 1:23 PM, Hrvoje Nikšić <hniksic at gmail.com> wrote:
>>>>
>>>>> {:.15g} is supposed to give 15 digits of precision, but with trailing
>>>>> zeros removed.
>>>>
>>>> The doc says with "insignificant" trailing zeros removed, not all
>>>> trailing zeros.
>>>
>>> Can somebody explain the difference between "significant" and
>>> "insignificant" tailing zeros to somebody who barely passed his single
>>> numerical methods class?  [Though I have, on occasion, had to tinker
>>> with the innards of SW floating point libraries and could fish a
>>> hardcopy of IEEE-754 out of a filing cabinet if needed.]
>>
>> Significant digits are within the precision of the calculation.
>> Writing 1.230 indicates that the fourth digit is known to be zero.
>> Writing 1.23 outside a context of exact calculation indicates that the
>> fourth digit is unknown due to insufficient precision.
>
> I knew that, but I was asking in the context of float/decimal's
> formatting function.  I didn't realize that float and/or decimal had a
> "significant digit" property, and therefore possess significant vs.
> insignificant trailing zeros when represented in base-10.

A Decimal object consists of a sign, a tuple of decimal digits, and an
exponent. If the digits are, e.g, (1, 2, 3, 0, 0, 0), then that would
be equal to the Decimal with the digits (1, 2, 3) and the same sign
and exponent, but the explicit presence of the trailing zeros
indicates their significance. If this doesn't answer your question,
then I'm not really sure what you're asking.



More information about the Python-list mailing list