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


Groups > comp.lang.python > #85613 > unrolled thread

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

Started byHrvoje Nikšić <hniksic@gmail.com>
First post2015-02-12 21:23 +0100
Last post2015-02-12 21:23 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#85613 — Re: Floating point "g" format not stripping trailing zeros

FromHrvoje Nikšić <hniksic@gmail.com>
Date2015-02-12 21:23 +0100
SubjectRe: Floating point "g" format not stripping trailing zeros
Message-ID<mailman.18711.1423772593.18130.python-list@python.org>
> >>>>> 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'.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web