Issue 23460: Decimals do not obey ':g' exponential notation formatting rules (original) (raw)

process

Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: LorenzMende, bdbaraban, docs@python, ezio.melotti, ikelly, mark.dickinson, mdk, sir-sigurd, skrah, tuomas.suutari, xtreak
Priority: normal Keywords: easy, patch

Created on 2015-02-13 17:15 by ikelly, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue23460.patch tuomas.suutari,2015-03-07 11:10 review
Pull Requests
URL Status Linked Edit
PR 11850 merged bdbaraban,2019-02-14 07:05
PR 15954 merged miss-islington,2019-09-11 13:59
Messages (18)
msg235904 - (view) Author: Ian Kelly (ikelly) Date: 2015-02-13 17:15
>>> '{:g}'.format(D('0.000001')) '0.000001' Formatted with '{:e}', the exponent would be -6, so per the formatting rules described under the 'g' specifier at https://docs.python.org/3/library/string.html#format-specification-mini-language the above should be formatted using exponential notation.
msg235984 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2015-02-14 18:53
For Decimal the cutoff point is -6 instead of -4 (following the decimal specification instead of the C standard).
msg237025 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2015-03-02 07:46
Should we add a note to the format docs, or just close this?
msg237032 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2015-03-02 08:42
I don't think we should close: the documentation as written explicitly says that the rules apply to both Decimal and float: "The available presentation types for floating point and decimal values are ...". But the details listed for 'g' are incorrect. We could either fix the description for 'g' to explain what happens for `Decimal`, or state up front that the table only applies directly to the `float` type, and that `Decimal` is similar but not identical.
msg237438 - (view) Author: Tuomas Suutari (tuomas.suutari) * Date: 2015-03-07 11:10
Here's a patch that fixes the description for 'g' to explain what happens for `Decimal` and also documents the Decimal.__format__ in the documentation of the decimal module.
msg324473 - (view) Author: Lorenz Mende (LorenzMende) * Date: 2018-09-02 08:02
I want to bring this issue up again. As the others correctly stated, either the documentation or the implementation of Decimal 'g' formatting is incorrect. For floats the implementation suits the docu: >>> '{:g}'.format(0.00001) '1e-05' For decimals: >>> '{:g}'.format(decimal.Decimal('0.00001')) '0.00001' As there is a deviation between documentation and implementation, I advise to modify one of both.
msg335183 - (view) Author: Brennan D Baraban (bdbaraban) * Date: 2019-02-11 00:36
What is the status of this issue? I can submit a PR based on Tuomas Suutari's patch.
msg335198 - (view) Author: Tuomas Suutari (tuomas.suutari) * Date: 2019-02-11 08:12
> What is the status of this issue? I can submit a PR based on Tuomas Suutari's patch. Don't know more about the status, but nobody seemed to care to take my patch forward. If contributions are accepted as GitHub pull requests these days, I can do it myself too.
msg335199 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2019-02-11 08:31
The patch LGTM, but I'm not sure if we need to document __format__(). Personally I probably wouldn't.
msg335200 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-02-11 08:31
> If contributions are accepted as GitHub pull requests these days, I can do it myself too. GitHub pull requests are accepted. Please see https://devguide.python.org/ . Thanks
msg335201 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2019-02-11 08:34
Yes, these days PRs are the only way to get anything done. Before GitHub it would have been possible to just commit the small diff directly to master.
msg335251 - (view) Author: Brennan D Baraban (bdbaraban) * Date: 2019-02-11 18:44
Well, Tuomas, I defer submission of a PR to you, as its your original patch.
msg335503 - (view) Author: Brennan D Baraban (bdbaraban) * Date: 2019-02-14 07:25
I submitted a PR just now. I only pushed the change to `string.rst`, as per Stefan Krah's suggestion.
msg336470 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2019-02-24 13:53
I just noticed that there are other differences, for example the "removal of trailing zeros" passage does not apply to Decimal: >>> format(Decimal("1.000000e-6"), "g") '0.000001000000' Perhaps it would be easier to have separate docs for float and decimal after all, or a long footnote for decimal.
msg336471 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2019-02-24 14:05
On the other hand, the docs say "insignificant trailing zeros are removed from the significand", so technically it *is* correct, because trailing zeros are significant in decimal.
msg337049 - (view) Author: Brennan D Baraban (bdbaraban) * Date: 2019-03-03 22:36
Hi Stefan. Is there an update you would like me to make on this PR? Otherwise, pinging for review.
msg351885 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2019-09-11 13:59
New changeset 1660a61a105bcd62e2dfa77885959a8992e9f14e by Julien Palard (Brennan D Baraban) in branch 'master': bpo-23460: Fix documentation for decimal string :g formatting (GH-11850) https://github.com/python/cpython/commit/1660a61a105bcd62e2dfa77885959a8992e9f14e
msg351909 - (view) Author: Julien Palard (mdk) * (Python committer) Date: 2019-09-11 14:38
New changeset 629f1f87e9b0bfd9f1dc3d02f966decde5c65201 by Julien Palard (Miss Islington (bot)) in branch '3.8': bpo-23460: Fix documentation for decimal string :g formatting (GH-11850) (GH-15954) https://github.com/python/cpython/commit/629f1f87e9b0bfd9f1dc3d02f966decde5c65201
History
Date User Action Args
2022-04-11 14:58:12 admin set github: 67648
2019-09-11 14:38:47 mdk set status: open -> closedresolution: fixedstage: patch review -> resolved
2019-09-11 14:38:28 mdk set messages: +
2019-09-11 13:59:48 miss-islington set pull_requests: + <pull%5Frequest15589>
2019-09-11 13:59:40 mdk set nosy: + mdkmessages: +
2019-03-03 22:36:45 bdbaraban set messages: +
2019-02-24 14:05:33 skrah set messages: +
2019-02-24 13:53:55 skrah set messages: +
2019-02-14 07:25:15 bdbaraban set messages: +
2019-02-14 07:05:30 bdbaraban set stage: needs patch -> patch reviewpull_requests: + <pull%5Frequest11881>
2019-02-11 18:44:41 bdbaraban set messages: +
2019-02-11 08:34:10 skrah set messages: +
2019-02-11 08:31:34 xtreak set nosy: + xtreakmessages: +
2019-02-11 08:31:21 skrah set messages: +
2019-02-11 08:12:22 tuomas.suutari set messages: +
2019-02-11 00:36:42 bdbaraban set nosy: + bdbarabanmessages: +
2018-10-24 20:05:10 sir-sigurd set nosy: + sir-sigurd
2018-09-02 08:02:24 LorenzMende set nosy: + LorenzMendemessages: +
2015-03-07 11:10:51 tuomas.suutari set files: + issue23460.patchnosy: + tuomas.suutarimessages: + keywords: + patch
2015-03-02 09:12:24 ezio.melotti set keywords: + easystage: needs patchtype: enhancementversions: + Python 2.7, Python 3.5
2015-03-02 08:42:44 mark.dickinson set messages: +
2015-03-02 07:46:41 ezio.melotti set nosy: + mark.dickinson, ezio.melottimessages: +
2015-02-14 18:53:37 skrah set nosy: + docs@python, skrahmessages: + assignee: docs@pythoncomponents: + Documentation, - Library (Lib)type: behavior -> (no value)
2015-02-13 17:15:21 ikelly create