Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:not': 0.03; 'float': 0.07; 'matches': 0.07; 'bug.': 0.09; 'trailing': 0.09; 'precision,': 0.16; 'removed,': 0.16; 'requested.': 0.16; 'subject:format': 0.16; 'zeroes': 0.16; 'zeros': 0.16; 'normally': 0.19; "skip:' 30": 0.19; 'import': 0.22; 'format,': 0.24; 'removed.': 0.24; 'looks': 0.24; '15,': 0.26; 'header:In-Reply- To:1': 0.27; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "skip:' 10": 0.31; 'decimal': 0.31; 'languages': 0.32; 'supposed': 0.32; 'there,': 0.34; "i'd": 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'returning': 0.36; 'yield': 0.36; 'should': 0.36; 'example,': 0.37; 'two': 0.37; 'version,': 0.38; 'to:addr:python-list': 0.38; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'numbers': 0.61; 'from:charset:utf-8': 0.61; 'first': 0.61; "you've": 0.63; 'such': 0.63; 'behavior': 0.77 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=0ooXxA9vVdBWUWBn6+2F79FZwvn+OUsfH2gzoXXdQ+I=; b=0XLbtypjrK0K7mjq5dewtGcvxD7mmINF2LDz7GwuA3BY/a78lKJVLcgfR1LPIHAFVb ywQweI+ZWdZEeSBmYm49CTICRs46nonX2ZAwQf467iSqfJaf284Z2VL44pNHkU79IMBN uaMwSRwyruy3+0xA6oM2tJG6hUKA2DIN5qn9lUUB2xITp0BHS4EY0eMxq19XeAZloi4v XxjbLIOA6Hm/wRdDyPKbejC8ht+3efprJ3C+yV+/tM4AiwpYZ0zr/FKHFyY4lPIM+DAP F5NgYMeosdvxyCCaNhiySY60nCsD8VbM+6Y73M5hONwaLmmxv6YS2PoqLw4Bcbudovak QTHw== MIME-Version: 1.0 X-Received: by 10.194.109.36 with SMTP id hp4mr11158278wjb.17.1423772590715; Thu, 12 Feb 2015 12:23:10 -0800 (PST) In-Reply-To: References: Date: Thu, 12 Feb 2015 21:23:10 +0100 Subject: Re: Floating point "g" format not stripping trailing zeros From: =?UTF-8?B?SHJ2b2plIE5pa8WhacSH?= 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423772593 news.xs4all.nl 2902 [2001:888:2000:d::a6]:44146 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85613 > >>>>> 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'.