2 edwardkssss Edwardkssss 于 2017.01.16 17:37 提问

python 递归算法的小问题

为什么guess计算出来结果是23,而不是23.1图片说明

2个回答

rzzssss
rzzssss   2017.01.19 16:31

这是你的运算
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
1.0999999999999999
1.2
1.3
1.4000000000000001
1.5000000000000002
1.6000000000000003
1.7000000000000004
1.8000000000000005
1.9000000000000006
2.0000000000000004
2.1000000000000005
2.2000000000000006
2.3000000000000007
2.400000000000001
2.500000000000001
2.600000000000001
2.700000000000001
2.800000000000001
2.9000000000000012
3.0000000000000013
3.1000000000000014
3.2000000000000015
3.3000000000000016
3.4000000000000017
3.5000000000000018
3.600000000000002
3.700000000000002
3.800000000000002
3.900000000000002
4.000000000000002
4.100000000000001
4.200000000000001
4.300000000000001
4.4
4.5
4.6
4.699999999999999
4.799999999999999
4.899999999999999
4.999999999999998
5.099999999999998
5.1999999999999975
5.299999999999997
5.399999999999997
5.4999999999999964
5.599999999999996
5.699999999999996
5.799999999999995
5.899999999999995
5.999999999999995
6.099999999999994
6.199999999999994
6.299999999999994
6.399999999999993
6.499999999999993
6.5999999999999925
6.699999999999992
6.799999999999992
6.8999999999999915
6.999999999999991
7.099999999999991
7.19999999999999
7.29999999999999
7.39999999999999
7.499999999999989
7.599999999999989
7.699999999999989
7.799999999999988
7.899999999999988
7.999999999999988
8.099999999999987
8.199999999999987
8.299999999999986
8.399999999999986
8.499999999999986
8.599999999999985
8.699999999999985
8.799999999999985
8.899999999999984
8.999999999999984
9.099999999999984
9.199999999999983
9.299999999999983
9.399999999999983
9.499999999999982
9.599999999999982
9.699999999999982
9.799999999999981
9.89999999999998
9.99999999999998
10.09999999999998
10.19999999999998
10.29999999999998
10.399999999999979
10.499999999999979
10.599999999999978
10.699999999999978
10.799999999999978
10.899999999999977
10.999999999999977
11.099999999999977
11.199999999999976
11.299999999999976
11.399999999999975
11.499999999999975
11.599999999999975
11.699999999999974
11.799999999999974
11.899999999999974
11.999999999999973
12.099999999999973
12.199999999999973
12.299999999999972
12.399999999999972
12.499999999999972
12.599999999999971
12.69999999999997
12.79999999999997
12.89999999999997
12.99999999999997
13.09999999999997
13.199999999999969
13.299999999999969
13.399999999999968
13.499999999999968
13.599999999999968
13.699999999999967
13.799999999999967
13.899999999999967
13.999999999999966
14.099999999999966
14.199999999999966
14.299999999999965
14.399999999999965
14.499999999999964
14.599999999999964
14.699999999999964
14.799999999999963
14.899999999999963
14.999999999999963
15.099999999999962
15.199999999999962
15.299999999999962
15.399999999999961
15.499999999999961
15.59999999999996
15.69999999999996
15.79999999999996
15.89999999999996
15.99999999999996
16.09999999999996
16.19999999999996
16.29999999999996
16.399999999999963
16.499999999999964
16.599999999999966
16.699999999999967
16.79999999999997
16.89999999999997
16.99999999999997
17.099999999999973
17.199999999999974
17.299999999999976
17.399999999999977
17.49999999999998
17.59999999999998
17.69999999999998
17.799999999999983
17.899999999999984
17.999999999999986
18.099999999999987
18.19999999999999
18.29999999999999
18.39999999999999
18.499999999999993
18.599999999999994
18.699999999999996
18.799999999999997
18.9
19.0
19.1
19.200000000000003
19.300000000000004
19.400000000000006
19.500000000000007
19.60000000000001
19.70000000000001
19.80000000000001
19.900000000000013
20.000000000000014
20.100000000000016
20.200000000000017
20.30000000000002
20.40000000000002
20.50000000000002
20.600000000000023
20.700000000000024
20.800000000000026
20.900000000000027
21.00000000000003
21.10000000000003
21.20000000000003
21.300000000000033
21.400000000000034
21.500000000000036
21.600000000000037
21.70000000000004
21.80000000000004
21.90000000000004
22.000000000000043
22.100000000000044
22.200000000000045
22.300000000000047
22.40000000000005
22.50000000000005
22.60000000000005
22.700000000000053
22.800000000000054
22.900000000000055
23.000000000000057
后面的000000000057暴露了。限制一下小数位的值格式化一下。

rzzssss
rzzssss   2017.01.19 16:58

有篇帖子可以参考:
http://longhua2003.iteye.com/blog/2144176
就是精度问题,归根结底就是编码问题。

【引】

先看一段代码:
print 'hu=',hu
print 'hm=',hm

        FC = float(hm[1])-float(hm[0]) 
        FA = float(hm[2])-float(hm[0]) 
        FD = float(hm[3])-float(hm[0]) 
        GE = float(hm[6])-float(hm[4]) 

        Real_Date=[FC,FA,FD,GE] 
        print 'FC =',FC,' FA =',FA,' FD=',FD,' GE=',GE  
        wt = 0        
        for x in xrange(len(Real_Date)):                           

            if(float(Real_Date[x]) != float(dbFileDate[x])):                              
                print Real_Date[x],' break ',float(dbFileDate[x]) 
                wt = 2 
                break              
        return wt 

Real_Date的数据与dbFileDate数据是从文件中取到的:

按理来说,以上代码从这两个文件中取得数据,再去对比,肯定没有什么问题。

但是,一运行就是有问题,程序对比245.0与245.0就判定为不相等了。

当时,搞的我蒙,怎么会这样,浮点型与浮点型可以啊:

结果是

后来,我又这样写,转成整数比较吧
for x in xrange(len(Real_Date)):

Db_X = int(float(Real_Date[x]) * 1000)
print 'Db_X',Db_X
Db_Y = int(float(dbFileDate[x]) * 1000)
print ' Db_Y ',Db_Y
if (Db_X != Db_Y):

print Real_Date[x],' break ',float(dbFileDate[x])
print Db_X,' int ',Db_Y
wt = 2
break

结果同样让你心碎

转成int后,精度丢失了,245.0变成了249.9999,打了这么多年仗,我第一次感觉到这么无奈,这样折腾2,3个小时,算了,先出去走走吧,走到了外面,突然,我想到一个问题,Python的代码是用utf-8,而我的系统是XP,XP的默认编码是gbk,会不会因为这样的原因呢?
程序指定了utf-8

我马上修改了一下程序,把从文件中读取的数据全部转成utf-8
wt = 0
for x in xrange(len(Real_Date)):
x11 =str(Real_Date[x]).decode('utf-8').encode('utf-8')
x12 = float(x11)
y11 = dbFileDate[x].decode('utf-8').encode('utf-8')
y12 = float(y11)
print x12," == ",y12

if(x12 != y12):
print x12,' break ',y12

wt = 2
break
一运行程序,完美解决:

点击查看原始大小图片
归根到底,就是因为编码问题造成的

Edwardkssss
Edwardkssss 不好意思了,之前过年回家都没怎么看,现在回学校才想到看这个。我也觉得应该是精度有点问题,但是苦于没有学过Python,现在看了你的回答感觉清楚了,多谢。
10 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片