先看一段代码:
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数据是从文件中取到的:
[img]http://dl2.iteye.com/upload/attachment/0102/0413/d73c302d-50b6-3210-b22d-e9977384ba85.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0102/0415/7611271d-699d-317a-bc76-b7d7ac1b67ea.jpg[/img]
按理来说,以上代码从这两个文件中取得数据,再去对比,肯定没有什么问题。
但是,一运行就是有问题,程序对比245.0与245.0就判定为不相等了。
[img]http://dl2.iteye.com/upload/attachment/0102/0417/0d0c107a-4694-3475-a70b-ad84274ab37a.jpg[/img]
当时,搞的我蒙,怎么会这样,浮点型与浮点型可以啊:
[img]http://dl2.iteye.com/upload/attachment/0102/0419/edea5262-389d-322f-835b-019fd0fd5a64.jpg[/img]
结果是
[img]http://dl2.iteye.com/upload/attachment/0102/0421/675024dc-2f9b-3bdc-8f01-5624122d90e4.jpg[/img]
后来,我又这样写,转成整数比较吧
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
结果同样让你心碎
[img]http://dl2.iteye.com/upload/attachment/0102/0423/0f26ab54-8fef-32cb-aaeb-dc052b7b8a03.jpg[/img]
转成int后,精度丢失了,245.0变成了249.9999,打了这么多年仗,我第一次感觉到这么无奈,这样折腾2,3个小时,算了,先出去走走吧,走到了外面,突然,我想到一个问题,Python的代码是用utf-8,而我的系统是XP,XP的默认编码是gbk,会不会因为这样的原因呢?
程序指定了utf-8
[img]http://dl2.iteye.com/upload/attachment/0102/0427/fc1d7d86-dd5d-316d-a6dc-8bd57d486419.jpg[/img]
我马上修改了一下程序,把从文件中读取的数据全部转成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
一运行程序,完美解决:
[img]http://dl2.iteye.com/upload/attachment/0102/0429/796705df-e747-39b0-a3be-599b1da17dd1.jpg[/img]
归根到底,就是因为编码问题造成的
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数据是从文件中取到的:
[img]http://dl2.iteye.com/upload/attachment/0102/0413/d73c302d-50b6-3210-b22d-e9977384ba85.jpg[/img]
[img]http://dl2.iteye.com/upload/attachment/0102/0415/7611271d-699d-317a-bc76-b7d7ac1b67ea.jpg[/img]
按理来说,以上代码从这两个文件中取得数据,再去对比,肯定没有什么问题。
但是,一运行就是有问题,程序对比245.0与245.0就判定为不相等了。
[img]http://dl2.iteye.com/upload/attachment/0102/0417/0d0c107a-4694-3475-a70b-ad84274ab37a.jpg[/img]
当时,搞的我蒙,怎么会这样,浮点型与浮点型可以啊:
[img]http://dl2.iteye.com/upload/attachment/0102/0419/edea5262-389d-322f-835b-019fd0fd5a64.jpg[/img]
结果是
[img]http://dl2.iteye.com/upload/attachment/0102/0421/675024dc-2f9b-3bdc-8f01-5624122d90e4.jpg[/img]
后来,我又这样写,转成整数比较吧
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
结果同样让你心碎
[img]http://dl2.iteye.com/upload/attachment/0102/0423/0f26ab54-8fef-32cb-aaeb-dc052b7b8a03.jpg[/img]
转成int后,精度丢失了,245.0变成了249.9999,打了这么多年仗,我第一次感觉到这么无奈,这样折腾2,3个小时,算了,先出去走走吧,走到了外面,突然,我想到一个问题,Python的代码是用utf-8,而我的系统是XP,XP的默认编码是gbk,会不会因为这样的原因呢?
程序指定了utf-8
[img]http://dl2.iteye.com/upload/attachment/0102/0427/fc1d7d86-dd5d-316d-a6dc-8bd57d486419.jpg[/img]
我马上修改了一下程序,把从文件中读取的数据全部转成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
一运行程序,完美解决:
[img]http://dl2.iteye.com/upload/attachment/0102/0429/796705df-e747-39b0-a3be-599b1da17dd1.jpg[/img]
归根到底,就是因为编码问题造成的