diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2022-12-23 22:35:58 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-23 22:35:58 (GMT) |
commit | 5d84966cce6c596da22922a07f49bde959ff5201 (patch) | |
tree | e44414a0ff5f02233158709844e0932b08f54c63 /Doc | |
parent | 1ecfd1ebf1f53ef6ac82085b25ed09952b470d4e (diff) | |
download | cpython-5d84966cce6c596da22922a07f49bde959ff5201.zip cpython-5d84966cce6c596da22922a07f49bde959ff5201.tar.gz cpython-5d84966cce6c596da22922a07f49bde959ff5201.tar.bz2 |
GH-100425: Improve accuracy of builtin sum() for float inputs (GH-100426)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/functions.rst | 4 | ||||
-rw-r--r-- | Doc/library/math.rst | 7 | ||||
-rw-r--r-- | Doc/tutorial/floatingpoint.rst | 2 |
3 files changed, 6 insertions, 7 deletions
diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 2110990..817c1f8 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -1733,6 +1733,10 @@ are always available. They are listed here in alphabetical order. .. versionchanged:: 3.8 The *start* parameter can be specified as a keyword argument. + .. versionchanged:: 3.12 Summation of floats switched to an algorithm + that gives higher accuracy on most builds. + + .. class:: super() super(type, object_or_type=None) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 559c6ec..aeebcaf 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -108,12 +108,7 @@ Number-theoretic and representation functions .. function:: fsum(iterable) Return an accurate floating point sum of values in the iterable. Avoids - loss of precision by tracking multiple intermediate partial sums: - - >>> sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) - 0.9999999999999999 - >>> fsum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1]) - 1.0 + loss of precision by tracking multiple intermediate partial sums. The algorithm's accuracy depends on IEEE-754 arithmetic guarantees and the typical case where the rounding mode is half-even. On some non-Windows diff --git a/Doc/tutorial/floatingpoint.rst b/Doc/tutorial/floatingpoint.rst index e1cd7f9..cedade6 100644 --- a/Doc/tutorial/floatingpoint.rst +++ b/Doc/tutorial/floatingpoint.rst @@ -192,7 +192,7 @@ added onto a running total. That can make a difference in overall accuracy so that the errors do not accumulate to the point where they affect the final total: - >>> sum([0.1] * 10) == 1.0 + >>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 1.0 False >>> math.fsum([0.1] * 10) == 1.0 True |