请考虑以下代码:
0.1 + 0.2 == 0.3 -> false
Run Code Online (Sandbox Code Playgroud)
0.1 + 0.2 -> 0.30000000000000004
Run Code Online (Sandbox Code Playgroud)
为什么会出现这些不准确之处?
基本上,我将float转换为int,但我并不总是有预期的值.
这是我正在执行的代码:
x = 2.51
print("--------- 251.0")
y = 251.0
print(y)
print(int(y))
print("--------- 2.51 * 100")
y = x * 100
print(y)
print(int(y))
print("--------- 2.51 * 1000 / 10")
y = x * 1000 / 10
print(y)
print(int(y))
print("--------- 2.51 * 100 * 10 / 10")
y = x * 100 * 10 / 10
print(y)
print(int(y))
x = 4.02
print("--------- 402.0")
y = 402.0
print(y)
print(int(y))
print("--------- 4.02 * 100")
y = x * 100
print(y)
print(int(y))
print("--------- 4.02 …
Run Code Online (Sandbox Code Playgroud) 只是为了好玩,因为它非常简单,我编写了一个简短的程序来生成嫁接数,但由于浮点精度问题,它没有找到一些更大的例子.
def isGrafting(a):
for i in xrange(1, int(ceil(log10(a))) + 2):
if a == floor((sqrt(a) * 10**(i-1)) % 10**int(ceil(log10(a)))):
return 1
a = 0
while(1):
if (isGrafting(a)):
print "%d %.15f" % (a, sqrt(a))
a += 1
Run Code Online (Sandbox Code Playgroud)
此代码缺少至少一个已知的嫁接编号.9999999998 => 99999.99998999999999949999999994999999999374999999912...
它乘以后似乎会降低额外的精度10**5
.
>>> a = 9999999998
>>> sqrt(a)
99999.99999
>>> a == floor((sqrt(a) * 10**(5)) % 10**int(ceil(log10(a))))
False
>>> floor((sqrt(a) * 10**(5)) % 10**int(ceil(log10(a))))
9999999999.0
>>> print "%.15f" % sqrt(a)
99999.999989999996615
>>> print "%.15f" % (sqrt(a) * 10**5) …
Run Code Online (Sandbox Code Playgroud) 可能重复:
Python舍入错误与浮点数
python数学是错误的
我不能让Python正确地做减法1 - 0.8并分配它.它一直提出错误的答案,0.19999999999999996.
我探讨了一下:
sq = {}
sub = {}
for i in range(1000):
sq[str(i/1000.)+'**2']=((i/1000.)**2)
sub['1-'+str(i/1000.)]=(1.0-(i/1000.))
Run Code Online (Sandbox Code Playgroud)
并发现这个错误发生在0到1到第三个小数位之间的一个随机的浮点组.当您对这些浮点数进行平方时,也会发生类似的错误,但会出现类似的错误.
我希望对此有一个解释,以及如何使Python正确运算.使用round(x,3)
是我现在使用的解决方法,但它并不优雅.
谢谢!
这是我的Python 2.7.3 shell中的一个会话:
*** Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32. ***
*** Remote Python engine is active ***
>>> 1-0.8
0.19999999999999996
>>> print 1-0.8
0.2
>>> a = 1-0.8
>>> a
0.19999999999999996
>>> print a
0.2
>>> a = 0.2
>>> print a
0.2
>>> a
0.2
>>> …
Run Code Online (Sandbox Code Playgroud) 可能重复:
带有浮点数的Python舍入错误
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> 4.2 - 1.8
2.4000000000000004
>>> 1.20 - 1.18
0.020000000000000018
>>> 5.1 - 4
1.0999999999999996
>>> 5 - 4
1
>>> 5.0 - 4.0
1.0
Run Code Online (Sandbox Code Playgroud)
为什么Python的数学错误?
我有一个表格中的字符串列表,如
a = ['str','5','','4.1']
Run Code Online (Sandbox Code Playgroud)
我想将列表中的所有数字转换为浮点数,但保持其余数字不变,就像这样
a = ['str',5,'',4.1]
Run Code Online (Sandbox Code Playgroud)
我试过了
map(float,a)
Run Code Online (Sandbox Code Playgroud)
但显然它给了我一个错误,因为一些字符串不能转换为浮点数.我也试过了
a[:] = [float(x) for x in a if x.isdigit()]
Run Code Online (Sandbox Code Playgroud)
但它只给了我
[5]
Run Code Online (Sandbox Code Playgroud)
所以浮点数和所有其他字符串都会丢失.我该怎么做才能同时保留字符串和数字?
我计算了以下内容:
>>> float(10.0-9.2)
0.800000000000000*7*
Run Code Online (Sandbox Code Playgroud)
即使做10.0-9.2也给出了上述结果.为什么额外的7会出现在结果中?
我在python 3.2上.
我想计算一个整数列表的倒数之和(看看它是否大于或等于1):
我想使用整数来避免浮点舍入问题。为此,我想这样解决:
我已经这样做了:
import numpy as np
my_list = [2, 3, 5, 7]
numerator = 0
for i in range(len(my_list)):
numerator += np.product(my_list[:i] + my_list[i+1 :])
denominator = np.product(my_list)
result = numerator>=denominator
Run Code Online (Sandbox Code Playgroud)
但我觉得应该为此写一句俏皮话。有没有一个函数可以计算分数倒数之和?或者也许是一个从列表中计算分子的函数?
我是python的初学者,并且编写了代码检查数字是否为整数的立方。该代码对于某些值似乎运行良好,但是对于某些(甚至整个多维数据集),它会将多维数据集根打印为(x-0.000000004
,即x
多维数据集根)。例如,它将给出3.9999999996
64的立方根,但是将2,5打印为8,125。有什么想法吗?
n=int(input("Please enter the number: "))
print (n)
x=n**(1/3)
print (x)
if x==int(x):
print ('%s is a whole cube'%(n))
else:
print ('%s is not a whole cube'%(n))
Run Code Online (Sandbox Code Playgroud)
忽略中间的打印语句,它们仅用于逐行调试。
一半,即十进制的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版本中