-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-99880: document rounding mode for new-style formatting #121481
Conversation
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().
My opinion (not a core developer):
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jowagner, thanks for review.
The word "correctly" doesn't add anything useful here as correctness is subjective
The meaning in this context actually is not subjective at all. From § 3.9 of the C11 standard: correctly rounded result - representation in the result format that is nearest in value, subject to the current rounding mode, to what the result would be given unlimited range and precision.
Maybe it worth to add something like above definition to the glossary? It's used a lot e.g. in the decimal module.
Suggestion: "it will, on common platforms, round".
+1
Many readers may not understand what least significant bits have to do with decimals.
Well, it's a bit not a digit;) Hardly we should everywhere emphasize that floats are binary floating point numbers. I would prefer if more detailed description (in depth, with examples and with a warning!) will be consolidated in round() docs. There is a link.
Suggestion: "[...] toward the value with an even last digit (0, 2, 4, 6 or 8) in its representation."
I doubt it's a correct description. Consider this example:
>>> round(2.675, 2)
2.67
>>> from decimal import Decimal, getcontext
>>> getcontext().rounding
'ROUND_HALF_EVEN'
>>> round(Decimal('2.675'), 2)
Decimal('2.68')
Co-authored-by: Bénédikt Tran <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me @skirpichev. There is a merge conflict that needs resolution. Thanks.
Thanks, fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @skirpichev.
Thanks @skirpichev for the PR, and @willingc for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
…onGH-121481) * pythongh-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]>
…onGH-121481) * pythongh-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]>
GH-126334 is a backport of this pull request to the 3.13 branch. |
GH-126335 is a backport of this pull request to the 3.12 branch. |
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. C17 says here (p 230, "Recommended practice"): "For e, E, f, F, g, and G conversions, if the number of significant decimal digits is at most DECIMAL_DIG, then the result should be correctly rounded."
📚 Documentation preview 📚: https://cpython-previews--121481.org.readthedocs.build/