summaryrefslogtreecommitdiff
path: root/numpy/core/fromnumeric.py
diff options
context:
space:
mode:
authorAllan Haldane <allan.haldane@gmail.com>2019-08-28 11:40:58 -0400
committerAllan Haldane <allan.haldane@gmail.com>2019-08-31 20:04:06 -0400
commit2ca7ea980fbf0fa8753c3040f332fef82d963e3a (patch)
tree9694a427c663f97bd91de180f4129715f31e1857 /numpy/core/fromnumeric.py
parent9cc5f99f3281ade5eef6b097f5853eb8bb471416 (diff)
downloadnumpy-2ca7ea980fbf0fa8753c3040f332fef82d963e3a.tar.gz
DOC: update np.around docstring with note about floating-point error
Fixes #14391 [ci-skip]
Diffstat (limited to 'numpy/core/fromnumeric.py')
-rw-r--r--numpy/core/fromnumeric.py33
1 files changed, 29 insertions, 4 deletions
diff --git a/numpy/core/fromnumeric.py b/numpy/core/fromnumeric.py
index 3314e516e..422ebe2de 100644
--- a/numpy/core/fromnumeric.py
+++ b/numpy/core/fromnumeric.py
@@ -3125,10 +3125,35 @@ def around(a, decimals=0, out=None):
-----
For values exactly halfway between rounded decimal values, NumPy
rounds to the nearest even value. Thus 1.5 and 2.5 round to 2.0,
- -0.5 and 0.5 round to 0.0, etc. Results may also be surprising due
- to the inexact representation of decimal fractions in the IEEE
- floating point standard [1]_ and errors introduced when scaling
- by powers of ten.
+ -0.5 and 0.5 round to 0.0, etc.
+
+ ``np.around`` uses a fast but sometimes inexact algorithm to round
+ floating-point datatypes. For positive `decimals` it is equivalent to
+ ``np.true_divide(np.rint(a * 10**decimals), 10**decimals)``, which is
+ inexact for large floating-point values or large values of `decimals` due
+ the inexact representation of decimal fractions in the IEEE floating point
+ standard [1]_ and errors introduced when scaling by powers of ten. For
+ instance, note the extra "1" in the following:
+
+ >>> np.round(56294995342131.5, 3)
+ 56294995342131.51
+
+ If your goal is to print such values with a fixed number of decimals, it is
+ preferable to use numpy's float printing routines to limit the number of
+ printed decimals:
+
+ >>> np.format_float_positional(56294995342131.5, precision=3)
+ '56294995342131.5'
+
+ The float printing routines use an accurate but much more computationally
+ demanding algorithm to compute the number of digits after the decimal
+ point.
+
+ Alternatively, Python's builtin `round` function uses a more accurate
+ but slower algorithm for 64-bit floating point values:
+
+ >>> round(56294995342131.5, 3)
+ 56294995342131.5
References
----------