numpy.around

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/weixin_36670529/article/details/102457572

numpy.around(a, decimals=0, out=None)[source]

Evenly round to the given number of decimals.

Parameters:

a:array_like

Input data.

decimals:int, optional

Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.

out:ndarray, optional

Alternative output array in which to place the result. It must have the same shape as the expected output, but the type of the output values will be cast if necessary. See doc.ufuncs (Section “Output arguments”) for details.

Returns:

rounded_array:ndarray

An array of the same type as a, containing the rounded values. Unless out was specified, a new array is created. A reference to the result is returned.

The real and imaginary parts of complex numbers are rounded separately. The result of rounding a float is a float.

See also

ndarray.round

equivalent method

ceil, fix, floor, rint, trunc

Notes:

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.

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 has error due to 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
>>> np.round(16.055, 2), round(16.055, 2)  # equals 16.0549999999999997
(16.06, 16.05)

References

1

“Lecture Notes on the Status of IEEE 754”, William Kahan, https://people.eecs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF

2

“How Futile are Mindless Assessments of Roundoff in Floating-Point Computation?”, William Kahan, https://people.eecs.berkeley.edu/~wkahan/Mindless.pdf

Examples

>>> np.around([0.37, 1.64])
array([0.,  2.])
>>> np.around([0.37, 1.64], decimals=1)
array([0.4,  1.6])
>>> np.around([.5, 1.5, 2.5, 3.5, 4.5]) # rounds to nearest even value
array([0.,  2.,  2.,  4.,  4.])
>>> np.around([1,2,3,11], decimals=1) # ndarray of ints is returned
array([ 1,  2,  3, 11])
>>> np.around([1,2,3,11], decimals=-1)
array([ 0,  0,  0, 10])

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • logging.Formatter 日期格式

    formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s","%Y%b%d-%H...

    于小勇
  • tf.compat.v1.to_int32

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明...

    于小勇
  • tf.contrib.layers.batch_norm

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    于小勇
  • 近似模型计数,Sparse XOR约束和最小距离

    摘要:计算给定布尔公式的模型数量的问题具有许多应用,包括计算定量信息流中的确定性程序的泄漏。模型计数是一个很难的#P完全问题。出于这个原因,在过去十年中已经开发...

    罗大琦
  • Java 中的本地线程 ThreadLocal<T> 与同步机制的比较和最佳实践

    ThreadLocal 解决线程安全持有对象访问的问题 . 通过 ThreadLocal.set() 方法将对象实例保存在每个线程自己所拥有的 ThreadLo...

    一个会写诗的程序员
  • 迈向与价值一致的体系的步骤(Computers and Society)

    算法(包括AI/ML)决策工件是我们决策生态系统中已建立和正在发展的一部分。他们现在是不可或缺的工具,帮助我们管理洪水般的信息,我们试图在一个复杂的世界作出有效...

    用户6869393
  • SAP Cloud for Customer如何实现从标准UI到自开发UI的跳转

    Create a new OBN button in the toolbar of a standard UI, for example the item ta...

    Jerry Wang
  • CONSAC:基于条件样本一致性的鲁棒多模型拟合(CS CV)

    本文提出了一种基于噪声测量的多参数模型的鲁棒估计方法。应用包括在人造场景中寻找多个消失点,将平面拟合到建筑图像中,或在同一序列中估计多个刚性运动。与以往的多模型...

    DANDAN用户6837186
  • 【Codeforces】1213A - Chips Moving

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    喜欢ctrl的cxk
  • dbus-python 指南

    This tutorial requires Python 2.4 or up, and dbus-python 0.80rc4 or up.

    py3study

扫码关注云+社区

领取腾讯云代金券