一半,即十进制的0.5,具有精确的二进制表示:0.1
然而,如果我将它四舍五入为整数,我得到0而不是1.我尝试使用Python和C,其行为相同.例如python代码:
>>> a,b,c = 0.49, 0.5, 0.51
>>> [round(x) for x in (a,b,c)]
[0, 0, 1]
>>> "%.0f %.0f %.0f" % (a,b,c)
'0 0 1'
Run Code Online (Sandbox Code Playgroud)
有趣的是,
>>> a,b,c = 0.049, 0.05, 0.051
>>> [round(x,1) for x in (a,b,c)]
[0.0, 0.1, 0.1]
>>> "%.1f %.1f %.1f" % (a,b,c)
'0.0 0.1 0.1'
Run Code Online (Sandbox Code Playgroud)
我知道许多类似的问题,例如Python舍入错误与浮点数,浮点算术的Python教程,以及每个计算机科学家应该知道的关于浮点运算的强制性.
如果数字具有精确的二进制表示,例如(十进制)0.5,是否应该正确舍入?
编辑:问题发生在3.4.3版本中,但不在2.7.6版本中
可能重复:
带有浮点数的Python舍入错误
我在Python中有一个舍入问题.如果我计算
32.50*0.19 = 6.1749999999999998
但这应该是6.175.如果我将6.1749999999999998舍入为2位小数,则正确显示6.18.所以我可以忍受.
但如果我算这个:
32.50*0.19*3 = 18.524999999999999
这应该是18.525.如果我将值18.524999999999999四舍五入,则显示18.52.
它应该显示我18.53.我做错了什么,我该如何解决?
我正在尝试将十进制数字添加到十进制数字并且它正常工作,但是当我这样做时,1.1 + 0.1
我得到1.2000000000000002
但是我希望它等于是1.2
.当我这样做的时候,1.0 + 0.1
我会得到1.1
哪个是完美的,但我不会那样做1.1 + 0.1
.那么有什么方法可以摆脱000000000000002
来自1.2000000000000002
?
谢谢.
可能重复:
带有浮点数的Python舍入错误
我用numpy创建了一个数组a = numpy.arange(0,1e5,1,dtype=int)
.a[18645]
是预期的18645.当我创建另一个数组时b=a*10e-15
,b[18645]
是186.4999999999e-12.b[18644]
是186.44e-12.为什么Python会创建这些尾随9?
当我试图在数组中搜索元素时出现了这个问题numpy.where
.使用尾随的9s,该numpy.where
功能未能找到184.45e-12 in b
.
有一个python代码如下:
import sys
import fileinput, string
K = 3
f = raw_input("please input the initial "+str(K)+" lamba: ").split()
Z = []
sumoflamba = 0.0
for m in f:
j = m.find("/")
if j!=-1:
e=float(m[:j])/float(m[j+1:])
else:
e = float(m)
sumoflamba+=e
if e==0:
print "the initial lamba cannot be zero!"
sys.exit()
Z.append(e)
print sumoflamba
if sumoflamba!=1:
print "initial lamba must be summed to 1!"
sys.exit()
Run Code Online (Sandbox Code Playgroud)
当我用 0.7、0.2、0.1 运行它时。它将打印警告并退出!但是,当我使用 0.1、0.2、0.7 运行它时。它工作正常。0.3、0.3、0.4 也可以正常工作。我不知道......有人可以解释一下吗?对于所有这些情况,“print sumoflamda”将给出 1.0。
我不是在寻找我的代码的修复,我想了解什么机制导致我的列表定期填充不正确的数据.
x = []
h = 0.01
for i in range(101):
x.append(1+i*h)
print x
"""
~$ python ~/dev/python/inf1100/coor1.py
[1.0, 1.01, 1.02, 1.03, 1.04, 1.05, 1.06, 1.07, 1.08, 1.09, 1.1, 1.11, 1.12, 1.13, 1.1400000000000001, 1.15, 1.16, 1.17, 1.18, 1.19, 1.2, 1.21, 1.22, 1.23, 1.24, 1.25, 1.26, 1.27, 1.28, 1.29, 1.3, 1.31, 1.32, 1.33, 1.34, 1.35, 1.3599999999999999, 1.37, 1.38, 1.3900000000000001, 1.4, 1.4100000000000001, 1.42, 1.43, 1.44, 1.45, 1.46, 1.47, 1.48, 1.49, 1.5, 1.51, 1.52, 1.53, 1.54, 1.55, 1.56, 1.57, 1.58, 1.5899999999999999, 1.6, 1.6099999999999999, …
Run Code Online (Sandbox Code Playgroud) 按照我的预期.05 + .01应该等于.06但是在python中它不会发生.因为.05+.01 = 0.060000000000000005
而且不等于.06
.
>>> .01+.01
0.02
>>> .02+.01
0.03
>>> .03+.01
0.04
>>> .04+.01
0.05
>>> .05+.01
0.060000000000000005 #expected .06
>>> .06+.01
0.06999999999999999 #expected .07
>>> .07+.01
0.08
>>> .08+.01
0.09
>>> .09+.01
0.09999999999999999 #expected .10
>>> 0.09999999999999999+.01
0.10999999999999999 #expected .11
Run Code Online (Sandbox Code Playgroud)
这是什么原因?
我在python中尝试了这个简单的数学运算
>>> math.floor(8.2-0.21)
7.0
>>> math.floor(8.21-0.2)
8.0
>>> math.floor(8.2-0.2)
7.0
Run Code Online (Sandbox Code Playgroud)
第三个应该返回8但是返回7?!
UPDATE
我在PHP,ruby和JAVA中尝试过,我得到了相同的结果.
更新2 我不知道为什么这个问题得到很多投票!