From 6801e732ca6816b5442b74ec1033ae7b2e6845f7 Mon Sep 17 00:00:00 2001 From: Sergey B Kirpichev Date: Mon, 8 Jul 2024 10:17:43 +0300 Subject: [PATCH] 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(). --- Doc/library/string.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/string.rst b/Doc/library/string.rst index c3c0d732cf18d4..486e19ab8f9300 100644 --- a/Doc/library/string.rst +++ b/Doc/library/string.rst @@ -588,6 +588,13 @@ 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. For :class:`float`, used rounding mode match the one +for :func:`round` builtin, i.e. it should round results midway between +representable values toward the nearest value with an even (0) least +significant bit. For :class:`~decimal.Decimal`, rounding mode of the current +:ref:`context ` is used. + .. _formatexamples: