@@ -2,13 +2,13 @@ PEP: 682
2
2
Title: Format Specifier for Signed Zero
3
3
Author: John Belmonte <
[email protected] >
4
4
Sponsor: Mark Dickinson <
[email protected] >
5
- Discussions-To:
5
+ Discussions-To: https://discuss.python.org/t/pep-682-format-specifier-for-signed-zero/13596
6
6
Status: Draft
7
7
Type: Standards Track
8
8
Content-Type: text/x-rst
9
9
Created: 29-Jan-2022
10
10
Python-Version: 3.11
11
- Post-History:
11
+ Post-History: 08-Feb-2022
12
12
13
13
14
14
Abstract
@@ -27,9 +27,7 @@ zero to be normalized to positive zero.
27
27
Motivation
28
28
==========
29
29
30
- Here is negative zero:
31
-
32
- .. code-block :: pycon
30
+ Here is negative zero::
33
31
34
32
>>> x = -0.
35
33
>>> x
@@ -38,9 +36,7 @@ Here is negative zero:
38
36
When formatting a number, negative zero can result from rounding. Assuming
39
37
the user's intention is truly to discard precision, the distinction between
40
38
negative and positive zero of the rounded result might be considered an
41
- unwanted artifact:
42
-
43
- .. code-block :: pycon
39
+ unwanted artifact::
44
40
45
41
>>> for x in (.002, -.001, .060):
46
42
... print(f'{x: .1f}')
@@ -49,18 +45,14 @@ unwanted artifact:
49
45
0.1
50
46
51
47
There are various approaches to clearing the sign of a negative zero. It
52
- can be achieved without a conditional by adding positive zero:
53
-
54
- .. code-block :: pycon
48
+ can be achieved without a conditional by adding positive zero::
55
49
56
50
>>> x = -0.
57
51
>>> x + 0.
58
52
0.0
59
53
60
54
To normalize negative zero when formatting, it is necessary to perform
61
- a redundant (and error-prone) pre-rounding of the input:
62
-
63
- .. code-block :: pycon
55
+ a redundant (and error-prone) pre-rounding of the input::
64
56
65
57
>>> for x in (.002, -.001, .060):
66
58
... print(f'{round(x, 1) + 0.: .1f}')
@@ -112,12 +104,12 @@ one-dimensional numerical arrays would be complicated by such pre-rounding:
112
104
""" Format a vector (any iterable) using given per-term format string."""
113
105
return f " [ { ' ,' .join(f ' { term: { format_spec}} ' for term in v)} ] "
114
106
115
- To date, there doesn't appear to be other widely-used languages or libraries
116
- providing such a formatting option for negative zero. However, the same
117
- `` z `` option syntax and semantics has been `proposed for C++ std::format() `_.
118
- While the proposal was withdrawn for C++20, a consensus proposal is promised
119
- for C++23. (The original `feature request `_ prompting this PEP was argued
120
- without knowledge of the C++ proposal.)
107
+ To date, there doesn't appear to be any other widely-used language or library
108
+ providing a formatting option for negative zero. However, the same `` z ``
109
+ option syntax and semantics specified below have been `proposed for C++
110
+ std::format() `_. While the proposal was withdrawn for C++20, a consensus
111
+ proposal is promised for C++23. (The original `feature request `_ prompting
112
+ this PEP was argued without knowledge of the C++ proposal.)
121
113
122
114
.. _`proposed for C++ std::format()` : http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1496r2.pdf
123
115
.. _`feature request` : https://bugs.python.org/issue45995
@@ -138,9 +130,7 @@ where ``z`` is allowed for numerical types other than integer. Support for
138
130
allowing the specifier to be used in f-strings, built-in ``format() ``, and
139
131
``str.format() ``. The %-formatting style will not support the new option.
140
132
141
- Synopsis:
142
-
143
- .. code-block :: pycon
133
+ Synopsis::
144
134
145
135
>>> x = -.00001
146
136
>>> f'{x:z.1f}'
0 commit comments