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


Groups > comp.lang.python > #85547

Floating point "g" format not stripping trailing zeros

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <hniksic@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; 'resulting': 0.04; '"""': 0.07; 'remaining': 0.07; 'formatting': 0.09; 'rounds': 0.09; 'trailing': 0.09; 'bug': 0.12; '"g"': 0.16; 'decimals?': 0.16; 'fixed-point': 0.16; 'notation,': 0.16; 'removed:': 0.16; 'significand,': 0.16; 'string:': 0.16; 'subject:format': 0.16; 'zeros': 0.16; '>>>': 0.22; 'import': 0.22; 'format,': 0.24; 'point': 0.28; 'message- id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'apparently': 0.31; 'decimal': 0.31; 'cases': 0.33; 'received:google.com': 0.35; 'there': 0.35; 'format.': 0.36; 'should': 0.36; 'to:addr:python- list': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'according': 0.40; 'from:charset:utf-8': 0.61; 'behavior': 0.77
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=HzAPy2WfPtTvAdj3taVS1IzAwmdsSnv569yH/P+HRZI=; b=v0Pdtv1we0z1d0NjlqT3NYfgX7UV2qFqcRcG5RvFIdEmuLaN/pNL+y6YyW2XDX6VZe 1Xkl7Da4kqzq1Dr3Lumm1G29aTsAJhKL/rMxXAcG0nUVLi3CKxx3iG3y3svHU/fbWZEv p+MmDnR84LtYNHhDs3mpQXYOuM+MMvKoPp/xBsH3jG8hsg2vAZEomDuCtZjnlQu9DmBM Vwxdx54MNEjmsRO5bKJab0dp9MEZXUNod6h0K5zKYgMdB6Z+1hlSIQbyHOtkeQIa9ksC 9S4zPgjpOz2DIQ9TPVNzpEJ4LThYu7ejsO48wsMHTqsJRdkwql0bH1fmzCUGWlRVEzMp TFVw==
MIME-Version 1.0
X-Received by 10.194.191.228 with SMTP id hb4mr593308wjc.116.1423684924464; Wed, 11 Feb 2015 12:02:04 -0800 (PST)
Date Wed, 11 Feb 2015 21:02:04 +0100
Subject Floating point "g" format not stripping trailing zeros
From Hrvoje Nikšić <hniksic@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18665.1423684925.18130.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423684925 news.xs4all.nl 2836 [2001:888:2000:d::a6]:46578
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85547

Show key headers only | View raw


According to the documentation of the "g" floating-point format,
trailing zeros should be stripped from the resulting string:

"""
General format. For a given precision p >= 1, this rounds the number
to p significant digits and then formats the result in either
fixed-point format or in scientific notation, depending on its
magnitude.[...]
In both cases insignificant trailing zeros are removed from the
significand, and the decimal point is also removed if there are no
remaining digits following it.
"""

However, in some cases, the trailing zeros apparently remain:

>>> from decimal import Decimal as D
>>> x = D(1)/D(999)
>>> '{:.15g}'.format(x)
'0.00100100100100100'

For floats, the trailing zeros are removed:

>>> '{:.15g}'.format(1. / 999)
'0.001001001001001'

This behavior is present in both 2.7.8 and 3.4.1. Is this a bug in the
formatting of Decimals?

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


Thread

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

csiph-web