当前位置:网站首页>numpy.around
numpy.around
2022-08-01 23:21:00 【Wanderer001】
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
equivalent method
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.51If 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
“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])边栏推荐
猜你喜欢

y84. Chapter 4 Prometheus Factory Monitoring System and Actual Combat -- Advanced Prometheus Alarm Mechanism (15)

解决yolov5训练时出现:“AssertionError: train: No labels in VOCData/dataSet_path/train.cache. Can not train ”

6134. 找到离给定两个节点最近的节点-力扣双百代码

cmd command

D - Linear Probing- 并查集

From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program

深度学习基础-基于Numpy的循环神经网络(RNN)实现和反向传播训练

Access the selected node in the console

Making a Simple 3D Renderer

美赞臣EDI 940仓库装运订单详解
随机推荐
Codeforces CodeTON Round 2 (Div. 1 + Div. 2, Rated, Prizes!) A-D 题解
SQL Server (design database--stored procedure--trigger)
Create virtual environments with virtualenv and Virtualenvwrapper virtual environment management tools
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
PDF转Word有那么难吗?做一个文件转换器,都解决了
基于JAX的激活函数、softmax函数和交叉熵函数
CF1705D Mark and Lightbulbs
云原生DevOps环境搭建
最短路模板
Check if point is inside rectangle
欧拉路径与欧拉回路
sys_kill系统调用
LocalDateTime转为Date类型
Getting started with IDEA is enough to read this article
drf生成序列化类代码
JS 数组去重(含简单数组去重、对象数组去重)
颜色透明参数
简单3D渲染器的制作
数据库表设计规则
excel cell contian two words, seperated by a slash