Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85613

Re: Floating point "g" format not stripping trailing zeros

References <CAK291fpAycSecNuV6hDnVXK+rhB0CWJTAuC3BKNUz8B4aDg2kQ@mail.gmail.com>
Date 2015-02-12 21:23 +0100
Subject Re: Floating point "g" format not stripping trailing zeros
From Hrvoje Nikšić <hniksic@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.18711.1423772593.18130.python-list@python.org> (permalink)

Show all headers | View raw


> >>>>> from decimal import Decimal as D
> >>>>> x = D(1)/D(999)
> >>>>> '{:.15g}'.format(x)
> >>
> >> '0.00100100100100100'
[...]
> > I'd say it's a bug.  P is 15, you've got 17 digits after the decimal place
> > and two of those are insignificant trailing zeros.
>
> Actually it's the float version that doesn't match the documentation.
> In the decimal version, sure there are 17 digits after the decimal
> place there, but the first two -- which are leading zeroes -- would
> not normally be considered significant.

{:.15g} is supposed to give 15 digits of precision, but with trailing
zeros removed.  For example, '{:.15g}'.format(Decimal('0.5')) should
yield '0.5', not '0.500000000000000' -- and, it indeed does.  It is
only for some numbers that trailing zeros are not removed, which looks
like a bug.  The behavior of floats matches both the documentation and
other languages using the 'g' decimal format, such as C.

> The float version OTOH is only giving you 13 significant digits when
> 15 were requested.

It is giving 15 significant digits if you count the trailing zeros
that have been removed.  If those two digits had not been zeros, they
would have been included.  This is again analogous to
'{:.15g}'.format(0.5) returning '0.5'.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Floating point "g" format not stripping trailing zeros Hrvoje Nikšić <hniksic@gmail.com> - 2015-02-12 21:23 +0100

csiph-web