Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Restructure Abstract and remove mentions to divmod
  • Loading branch information
jb2170 committed May 22, 2025
commit d477f4cea14bac021ba40f0bacc8fbbe505016ba
10 changes: 6 additions & 4 deletions peps/pep-0786.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ Post-History: `14-Feb-2025 <https://discuss.python.org/t/80760>`__,
Abstract
========

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

Both "precision" (``.``), and the "modulo-precision" flag (``z``) are presented in this PEP, as the alternative rejected implementations entail combinations of both.
``.`` ("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'``.

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

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

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

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

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

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

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.

Expand Down