8000 DOC: Mention and try to explain pairwise summation in sum by seberg · Pull Request #13737 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

DOC: Mention and try to explain pairwise summation in sum #13737

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 3 commits into from
Jun 11, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
DOC: add Marten's comment about f8, and smuggle in "partial" to pairwise
  • Loading branch information
seberg committed Jun 10, 2019
commit 86235ecb420f1df41a3189000c16400b6b45bfaf
8 changes: 6 additions & 2 deletions numpy/core/fromnumeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -2125,15 +2125,19 @@ def sum(a, axis=None, dtype=None, out=None, keepdims=np._NoValue,
For floating point numbers the numerical precision of sum (and
``np.add.reduce``) is in general limited by directly adding each number
individually to the result causing rounding errors in every step.
However, often numpy will use a numerically better approach
(pairwise summation) leading to improved precision in many use cases.
However, often numpy will use a numerically better approach (partial
pairwise summation) leading to improved precision in many use-cases.
This improved precision is always provided when no ``axis`` is given.
When ``axis`` is given, it will depend on which axis is summed.
Technically, to provide the best speed possible, the improved precision
is only used when the summation is along the fast axis in memory.
Note that the exact precision may vary depending on other parameters.
In contrast to NumPy, Python's ``math.fsum`` function uses a slower but
more precise approach to summation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding that for lower-precision floats such as f4, one can pass in dtype='f8' to increase precision?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess so, although it is growing so long... Maybe the real solution would be to have a longer paragraph about floating point rounding in the user guide and link that...

Especially when summing a large number of lower precision floating point
numbers, such as ``float32``, numerical errors can become significant.
In such cases it can be advisable to use `dtype="float64"` to use a higher
precision for the output.

Examples
--------
Expand Down
0