Skip to content

Commit

Permalink
gh-99880: document rounding mode for new-style formatting (GH-121481)
Browse files Browse the repository at this point in the history
* gh-99880: document rounding mode for new-style formatting

The CPython uses _Py_dg_dtoa(), which does rounding to nearest with half
to even tie-breaking rule.

If that functions is unavailable, PyOS_double_to_string() fallbacks to
system snprintf().  Since CPython 3.12, build requirements include C11
compiler *and* support for IEEE 754 floating point numbers (Annex F).
This means that FE_TONEAREST macro is available and, per default,
printf-like functions should use same rounding mode as _Py_dg_dtoa().

* Update Doc/library/string.rst

Co-authored-by: Bénédikt Tran <[email protected]>

---------

(cherry picked from commit 7d7d56d)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
  • Loading branch information
2 people authored and miss-islington committed Nov 2, 2024
1 parent f4bc64d commit ab9bf99
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Doc/library/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,11 @@ The available presentation types for :class:`float` and
| | as altered by the other format modifiers. |
+---------+----------------------------------------------------------+

The result should be correctly rounded to a given precision ``p`` of digits
after the decimal point. The rounding mode for :class:`float` matches that
of the :func:`round` builtin. For :class:`~decimal.Decimal`, the rounding
mode of the current :ref:`context <decimal-context>` will be used.

The available presentation types for :class:`complex` are the same as those for
:class:`float` (``'%'`` is not allowed). Both the real and imaginary components
of a complex number are formatted as floating-point numbers, according to the
Expand Down

0 comments on commit ab9bf99

Please sign in to comment.