8000 Misc improvements to the float tutorial by rhettinger · Pull Request #102052 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Misc improvements to the float tutorial #102052

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

Merged
merged 10 commits into from
Feb 19, 2023
Prev Previous commit
Next Next commit
Shorter example
  • Loading branch information
rhettinger committed Feb 19, 2023
commit 8f02dc507c46b4fa5759ea1811394823aaf14e62
17 changes: 8 additions & 9 deletions Doc/tutorial/floatingpoint.rst
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,14 @@ each other out leaving a final sum near zero:

.. doctest::

>>> arr = [-125546.2071591587, 0.04336622547760194, 18741774.60535662,
... 57.207123035704896, 454267277684011.1, -0.04815896904694777,
... -18741774.600527752, -454267277558522.1]
>>> float(sum(map(Fraction, arr))) # Exact summation with single rounding
5.777552047891987e-12
>>> math.fsum(arr) # Single rounding
5.777552047891987e-12
>>> sum(arr) # Multiple roundings in extended precision
5.777545108998083e-12
>>> arr = [-0.10430216751806065, -266310978.67179024, 143401161448607.16,
... -143401161400469.7, 266262841.31058735, -0.003244936839808227]
>>> float(sum(map(Fraction, arr))) # Exact summation with single rounding
8.042173697819788e-13
>>> math.fsum(arr) # Single rounding
8.042173697819788e-13
>>> sum(arr) # Multiple roundings in extended precision
8.042178034628478e-13


.. _tut-fp-error:
Expand Down
0