• numpy.around


    参考   numpy.around - 云+社区 - 腾讯云

    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:

    1. >>> np.round(56294995342131.5, 3)
    2. 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:

    1. >>> np.format_float_positional(56294995342131.5, precision=3)
    2. '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:

    1. >>> round(56294995342131.5, 3)
    2. 56294995342131.5
    3. >>> np.round(16.055, 2), round(16.055, 2) # equals 16.0549999999999997
    4. (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

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

  • 相关阅读:
    服务器环境的关键组成部分
    程序员年薪50万有多难?背后真相曝光,溢价程度超乎你想象
    kubernetes 实战学习
    44岁的「老板」想变年轻
    java计算机毕业设计云端小区物业智能管理系统源码+系统+mysql数据库+lw文档+部署
    前端CSS射门动画-为梅西最后一届世界杯加油
    隔山打牛:金融大崩溃
    手把手入门MO | 如何使用SeaTunnel将数据写入MatrixOne
    抢票攻略来了!疫情后的首届云栖大会
    Makefile
  • 原文地址:https://blog.csdn.net/weixin_36670529/article/details/102457572