Skip to content

Commit

Permalink
pythongh-99880: document rounding mode for new-style formatting
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
skirpichev committed Jul 8, 2024
1 parent bf74db7 commit 6801e73
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Doc/library/string.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <decimal-context>` is used.


.. _formatexamples:

Expand Down

0 comments on commit 6801e73

Please sign in to comment.