You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PEP proposes implementing the standard format specifier ``.`` of :pep:`3101` as "precision" for integers formatted with the binary, octal, decimal, and hexadecimal presentation types, and implementing the standard format specifier ``z`` as a "modulo" flag for integers formatted with precision and the binary, octal, and hexadecimal presentation types, which first reduces the integer into ``range(base ** precision)``, resulting in a predictable two's complement style formatting.
15
+
This PEP proposes implementing the standard format specifiers ``.`` and ``z`` of :pep:`3101` for integer fields as "precision" and "modulo-precision" respectively. Both are presented together in this PEP as the alternative rejected implementations entail intertwined combinations of both.
16
16
17
-
Both "precision" (``.``), and the "modulo-precision" flag (``z``) are presented in this PEP, as the alternative rejected implementations entail combinations of both.
17
+
``.`` ("precision") shall format an integer to a specified *minimum* number of digits, identical to the behavior of old-style ``%`` formatting. This shall be implemented for all integer presentation types except ``'c'``.
18
+
19
+
``z`` ("modulo-precision") shall be permitted as an optional "modulo" flag when formatting an integer with precision and one of the binary, octal, or hexadecimal presentation types (bases that are powers of two). This first reduces the integer into ``range(base ** precision)`` using the ``%`` operator. The result is a predictable two's complement style formatting with the *exact* number of digits equal to the precision.
18
20
19
21
This PEP amends the clause of :pep:`3101` which states "[t]he precision is ignored for integer conversions".
20
22
@@ -72,9 +74,9 @@ We desire two behaviors, which motivates the implementation of a flag ``z`` to t
72
74
73
75
For example ``f"{-12:#.2x}"`` shall produce ``'-0x0c'``, equivalent to ``"%#.2x" % -12``.
74
76
75
-
* For precision with the ``z`` flag, ``q, r = divmod(x, base ** n)`` is first taken when formatting ``f"{x:z.{n}{base_char}}"``, and ``r`` is passed on to precision, the resulting string being equivalent to ``f"{r:.{n}{base_char}}"``. Because ``r`` is in ``range(base ** n)`` the number of digits will always be exactly ``n``, resulting in a predictable two's complement style formatting, which is useful to the end user in environments that deal with machine-width oriented integers such as :mod:`struct`.
77
+
* For precision with the ``z`` flag, ``r = x % base ** n`` is first taken when formatting ``f"{x:z.{n}{base_char}}"``, and ``r`` is passed on to precision, the resulting string being equivalent to ``f"{r:.{n}{base_char}}"``. Because ``r`` is in ``range(base ** n)`` the number of digits will always be exactly ``n``, resulting in a predictable two's complement style formatting, which is useful to the end user in environments that deal with machine-width oriented integers such as :mod:`struct`.
76
78
77
-
For example in formatting ``f"{-1:z#.2x}"``, ``-1`` is reduced modulo ``256`` via ``-1, 255 = divmod(-1, 256)``, the resulting string being equivalent to ``f"{255:#.2x}"``, which is ``'0xff'``.
79
+
For example in formatting ``f"{-1:z#.2x}"``, ``-1`` is reduced modulo ``256`` via ``255 = -1 % 256``, the resulting string being equivalent to ``f"{255:#.2x}"``, which is ``'0xff'``.
78
80
79
81
The ``z`` flag shall only be implemented for presentation types corresponding to bases that are powers of two, specifically at present binary, octal, and hexadecimal. Whilst reduction of integers modulo by powers of ten is computationally possible, a 'ten's complement?' has no demand and so precision is unimplemented for decimal presentation types. The ``z`` flag shall work for all integers, not just negatives.
0 commit comments