Skip to content

Commit d477f4c

Browse files
committed
Restructure Abstract and remove mentions to divmod
1 parent 2086547 commit d477f4c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

peps/pep-0786.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ Post-History: `14-Feb-2025 <https://discuss.python.org/t/80760>`__,
1212
Abstract
1313
========
1414

15-
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.
1616

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.
1820

1921
This PEP amends the clause of :pep:`3101` which states "[t]he precision is ignored for integer conversions".
2022

@@ -72,9 +74,9 @@ We desire two behaviors, which motivates the implementation of a flag ``z`` to t
7274

7375
For example ``f"{-12:#.2x}"`` shall produce ``'-0x0c'``, equivalent to ``"%#.2x" % -12``.
7476

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`.
7678

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'``.
7880

7981
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.
8082

0 commit comments

Comments
 (0)