如何从零舍入
How to round away from zero
我是 python 的新手,据我所知,python 没有 "mathematical" 舍入。或者它有吗?
我有一个温度数组,例如:
temp = [-20.5, -21.5, -22.5, -23.5, 10.5, 11.5, 12.5, 13.5]
我无法用数学方法将值四舍五入为:
>> [-21, -21, -22, -22, -22, -24, -23, -23, -23, -23, -24, ...]
因此 -20.5 舍入为 -21,-21.5 舍入为 -22,-23.5 舍入为 -24,10.5 舍入为 11,11.5 舍入为 12,12.5 舍入为 13。数学准确性对我来说很重要.
对我来说最好的选择是使用像 numpy.around() 这样的函数,因为它对所有值进行四舍五入,而不是一个一个地舍入(我想,它更快)。有没有这样的功能,或者有这样的方式吗?
我得到的结果:
np.around(temp, decimals=0)
>>[-20. -22. -22. -24. 10. 12. 12. 14.]
np.rint(temp)
>>[-20. -22. -22. -24. 10. 12. 12. 14.]
np.round(temp)
>>[-20. -22. -22. -24. 10. 12. 12. 14.]
np.trunc(temp)
>>[-20. -21. -22. -23. 10. 11. 12. 13.]
正如 chepner 所说,您描述的舍入类型是 远离零的舍入方式。
您可以执行以下操作:
import math
import numpy as np
def round_away_from_zero(x):
a = abs(x)
r = math.floor(a) + math.floor(2 * (a % 1))
return r if x >= 0 else -r
temp = [-20.5, -21.5, -22.5, -23.5, 10.5, 11.5, 12.5, 13.5]
temp_np = np.array(temp)
t = map(round_away_from_zero, temp_np)
print(t)
你得到的输出是:
[-21.0, -22.0, -23.0, -24.0, 11.0, 12.0, 13.0, 14.0]
备注
根据用户评论,我将简要说明 np.array()
和 map()
:
np.array()
:这个函数接受一个数组对象并将其转换为一个 Numpy 对象,允许使用与其关联的功能,您可以在下面的内容中阅读 link
map()
: function 用于对指定的iterable和return map对象的所有元素应用一个函数。 Python 地图对象是一个迭代器,所以我们可以迭代它的元素。我们还可以将map对象转换为序列对象,如列表、元组等
numpy
有一个 around
,其中文件:
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. 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.
四舍五入到最接近的偶数在数学上与从零四舍五入一样有效。此类规则的目的是提供一致性和某种程度的统计一致性(减少偏差)。
但它也警告浮点值很少 'exactly' 一半。我们经常收到有关显示为 20.49999999
.
的值的问题
===
另一个答案中 round_away_from_zero
函数的 numpy 版本:
def round_away(x):
a = np.abs(x)
b = np.floor(a) + np.floor(2*(a%1))
return np.sign(x)*b
In [279]: np.array(temp)
Out[279]: array([-20.5, -21.5, -22.5, -23.5, 10.5, 11.5, 12.5, 13.5])
In [280]: round_away(temp)
Out[280]: array([-21., -22., -23., -24., 11., 12., 13., 14.])
我是 python 的新手,据我所知,python 没有 "mathematical" 舍入。或者它有吗? 我有一个温度数组,例如:
temp = [-20.5, -21.5, -22.5, -23.5, 10.5, 11.5, 12.5, 13.5]
我无法用数学方法将值四舍五入为:
>> [-21, -21, -22, -22, -22, -24, -23, -23, -23, -23, -24, ...]
因此 -20.5 舍入为 -21,-21.5 舍入为 -22,-23.5 舍入为 -24,10.5 舍入为 11,11.5 舍入为 12,12.5 舍入为 13。数学准确性对我来说很重要.
对我来说最好的选择是使用像 numpy.around() 这样的函数,因为它对所有值进行四舍五入,而不是一个一个地舍入(我想,它更快)。有没有这样的功能,或者有这样的方式吗?
我得到的结果:
np.around(temp, decimals=0)
>>[-20. -22. -22. -24. 10. 12. 12. 14.]
np.rint(temp)
>>[-20. -22. -22. -24. 10. 12. 12. 14.]
np.round(temp)
>>[-20. -22. -22. -24. 10. 12. 12. 14.]
np.trunc(temp)
>>[-20. -21. -22. -23. 10. 11. 12. 13.]
正如 chepner 所说,您描述的舍入类型是 远离零的舍入方式。
您可以执行以下操作:
import math
import numpy as np
def round_away_from_zero(x):
a = abs(x)
r = math.floor(a) + math.floor(2 * (a % 1))
return r if x >= 0 else -r
temp = [-20.5, -21.5, -22.5, -23.5, 10.5, 11.5, 12.5, 13.5]
temp_np = np.array(temp)
t = map(round_away_from_zero, temp_np)
print(t)
你得到的输出是:
[-21.0, -22.0, -23.0, -24.0, 11.0, 12.0, 13.0, 14.0]
备注
根据用户评论,我将简要说明 np.array()
和 map()
:
np.array()
:这个函数接受一个数组对象并将其转换为一个 Numpy 对象,允许使用与其关联的功能,您可以在下面的内容中阅读 linkmap()
: function 用于对指定的iterable和return map对象的所有元素应用一个函数。 Python 地图对象是一个迭代器,所以我们可以迭代它的元素。我们还可以将map对象转换为序列对象,如列表、元组等
numpy
有一个 around
,其中文件:
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. 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.
四舍五入到最接近的偶数在数学上与从零四舍五入一样有效。此类规则的目的是提供一致性和某种程度的统计一致性(减少偏差)。
但它也警告浮点值很少 'exactly' 一半。我们经常收到有关显示为 20.49999999
.
===
另一个答案中 round_away_from_zero
函数的 numpy 版本:
def round_away(x):
a = np.abs(x)
b = np.floor(a) + np.floor(2*(a%1))
return np.sign(x)*b
In [279]: np.array(temp)
Out[279]: array([-20.5, -21.5, -22.5, -23.5, 10.5, 11.5, 12.5, 13.5])
In [280]: round_away(temp)
Out[280]: array([-21., -22., -23., -24., 11., 12., 13., 14.])