From 99bb6a48ced27e5ce7bbf1fc7b606c5bd840af3a Mon Sep 17 00:00:00 2001 From: Atharva-Vidwans <57441520+Atharva-Vidwans@users.noreply.github.com> Date: Mon, 17 May 2021 08:03:23 +0530 Subject: DOC: Improve cumsum documentation (#18895) Add note and example potential differences with np.sum. Co-authored-by: Matti Picus --- numpy/core/fromnumeric.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'numpy/core/fromnumeric.py') diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py index 5c7b3372b..65a42eb1e 100644 --- a/numpy/core/fromnumeric.py +++ b/numpy/core/fromnumeric.py @@ -2498,6 +2498,10 @@ def cumsum(a, axis=None, dtype=None, out=None): Arithmetic is modular when using integer types, and no error is raised on overflow. + ``cumsum(a)[-1]`` may not be equal to ``sum(a)`` for floating-point + values since ``sum`` may use a pairwise summation routine, reducing + the roundoff-error. See `sum` for more information. + Examples -------- >>> a = np.array([[1,2,3], [4,5,6]]) @@ -2516,6 +2520,14 @@ def cumsum(a, axis=None, dtype=None, out=None): array([[ 1, 3, 6], [ 4, 9, 15]]) + ``cumsum(b)[-1]`` may not be equal to ``sum(b)`` + + >>> b = np.array([1, 2e-9, 3e-9] * 1000000) + >>> b.cumsum()[-1] + 1000000.0050045159 + >>> b.sum() + 1000000.0050000029 + """ return _wrapfunc(a, 'cumsum', axis=axis, dtype=dtype, out=out) -- cgit v1.2.1