我在執行二進制文件寫入時在python中打包和解壓二進制浮點數時遇到了一些麻煩。下面是我做了什麼:在python中打包和解壓縮二進制浮點數
import struct
f = open('file.bin', 'wb')
value = 1.23456
data = struct.pack('f',value)
f.write(data)
f.close()
f = open('file.bin', 'rb')
print struct.unpack('f',f.read(4))
f.close()
結果我得到的是以下幾點:
(1.2345600128173828,)
這是怎麼回事與額外的數字?這是一個舍入錯誤?這個怎麼用?
是的,浮點數本質上是不精確的。 – 2013-04-23 09:16:35
有關完整* why *的信息,請參閱[每位計算機科學家應瞭解的浮點算術知識](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html)。 – 2013-04-23 09:17:12
您遇到的Python教程[總結表示問題](http://docs.python.org/2/tutorial/floatingpoint.html#representation-error)。 – 2013-04-23 09:20:28