Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #85665
| Path | csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'from:addr:yahoo.co.uk': 0.04; 'context': 0.07; 'float': 0.07; 'indicating': 0.07; 'matches': 0.07; 'result,': 0.07; 'bug.': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'trailing': 0.09; 'bug': 0.12; 'language.': 0.14; '0),': 0.16; 'ah,': 0.16; 'digits.': 0.16; 'formatted': 0.16; 'precision,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'removed,': 0.16; 'subject:format': 0.16; 'underlying': 0.16; 'zeroes': 0.16; 'zeros': 0.16; 'language': 0.16; 'wrote:': 0.18; 'normally': 0.19; "skip:' 30": 0.19; 'thu,': 0.19; 'feb': 0.22; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'format,': 0.24; 'removed.': 0.24; 'specify': 0.24; 'string,': 0.24; 'looks': 0.24; '15,': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'words': 0.29; "doesn't": 0.30; "skip:' 10": 0.31; '>>>>': 0.31; 'decimal': 0.31; 'doc': 0.31; 'equivalent.': 0.31; 'languages': 0.32; 'supposed': 0.32; 'says': 0.33; '"the': 0.34; 'there,': 0.34; 'skip:d 20': 0.34; "i'd": 0.34; 'knows': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'yield': 0.36; 'should': 0.36; 'example,': 0.37; 'two': 0.37; 'being': 0.38; 'version,': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; '12,': 0.39; 'subject:" ': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'ian': 0.60; 'referred': 0.60; 'numbers': 0.61; "you're": 0.61; 'first': 0.61; "you've": 0.63; 'such': 0.63; 'our': 0.64; 'telling': 0.64; 'default': 0.69; 'behavior': 0.77; '2015': 0.84; 'notion': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
| Subject | Re: Floating point "g" format not stripping trailing zeros |
| Date | Fri, 13 Feb 2015 23:05:58 +0000 |
| References | <CAK291fpAycSecNuV6hDnVXK+rhB0CWJTAuC3BKNUz8B4aDg2kQ@mail.gmail.com> <CAK291fpKxW_V=yCvNHj9RRoEhC=i_DYJCa9-cbjxDwRwH4-KfQ@mail.gmail.com> <CALwzidk_bPYHGAYxL-yCwZ8xz3_qfjK_0RjhSeHtL7eukQjjbw@mail.gmail.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| X-Gmane-NNTP-Posting-Host | host-92-24-222-48.ppp.as43234.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 |
| In-Reply-To | <CALwzidk_bPYHGAYxL-yCwZ8xz3_qfjK_0RjhSeHtL7eukQjjbw@mail.gmail.com> |
| 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.18739.1423868773.18130.python-list@python.org> (permalink) |
| Lines | 80 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1423868773 news.xs4all.nl 2894 [2001:888:2000:d::a6]:42609 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:85665 |
Show key headers only | View raw
On 12/02/2015 23:46, Ian Kelly wrote:
> On Thu, Feb 12, 2015 at 1:23 PM, Hrvoje Nikšić <hniksic@gmail.com> wrote:
>>>>>>>> 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.
>
> The doc says with "insignificant" trailing zeros removed, not all
> trailing zeros.
>
>> 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.
>
> Ah, I see now what's going on here. With floats, there is really no
> notion of significant digits. The values 0.5 and 0.50000 are
> completely equivalent. With decimals, that's not exactly true; if you
> give the decimal a trailing zero then you are telling it that the zero
> is significant.
>
>>>> Decimal('0.5')
> Decimal('0.5')
>>>> Decimal('0.50000')
> Decimal('0.50000')
>>>> Decimal('0.5').as_tuple()
> DecimalTuple(sign=0, digits=(5,), exponent=-1)
>>>> Decimal('0.50000').as_tuple()
> DecimalTuple(sign=0, digits=(5, 0, 0, 0, 0), exponent=-5)
>
> These are distinct; the decimal knows how many significant digits you
> passed it. As a result, these are also distinct:
>
>>>> '{:.4g}'.format(Decimal('0.5'))
> '0.5'
>>>> '{:.4g}'.format(Decimal('0.50000'))
> '0.5000'
>
> Now what happens in your original example of 1/999? The default
> decimal context uses 28 digits of precision, so the result of that
> calculation will have 28 significant digits in it.
>
>>>> decimal.getcontext().prec
> 28
>>>> Decimal(1) / Decimal(999)
> Decimal('0.001001001001001001001001001001')
>
> When you specify the a precision of 15 in your format string, you're
> telling it to take the first 15 of those. It doesn't care that the
> last couple of those are zeros, because as far as it's concerned,
> those digits are significant.
>
I still think it's a bug as the 'p' being referred to in the OP's
original message is "The precision is a decimal number indicating how
many digits should be displayed after the decimal point for a floating
point value formatted with 'f' and 'F', or before and after the decimal
point for a floating point value formatted with 'g' or 'G'". In other
words is has nothing to do with the precision of the underlying number.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Floating point "g" format not stripping trailing zeros Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-13 23:05 +0000 Re: Floating point "g" format not stripping trailing zeros Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-02-14 16:22 +1300
csiph-web