Las*_*Cho 5 python floating-point
我在python解释器上创建了一个简单的代码并运行它.
Python 3.5.3 (v3.5.3:1880cb95a742, Jan 16 2017, 16:02:32) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> x=np.array([0,1])
>>> w=np.array([0.5,0.5])
>>> b=-0.7
>>> np.sum(w*x)+b
-0.19999999999999996
Run Code Online (Sandbox Code Playgroud)
结果-0.19999999999999996很奇怪.我认为......它是由IEEE 754规则引起的.但是当我尝试按文件运行几乎相同的代码时,结果会有很大不同.
import numpy as np
x = np.array([0,1])
w = np.array([0.5,0.5])
b = -0.7
print(np.sum(w * x) + b)
Run Code Online (Sandbox Code Playgroud)
结果是"-0.2".IEEE 754规则不会影响结果.
基于文件的运行和基于解释器的运行有什么区别?
差异是由于解释器如何显示输出.
该print函数将尝试使用对象的__str__方法,但解释器将使用对象的方法__repr__.
如果你在翻译中写道:
...
z = np.sum(w*x)+b
print(z)
Run Code Online (Sandbox Code Playgroud)
你会看到(这是你在代码中所做的)-0.2.
同样,如果在您的代码中,您写道:
print(repr(np.sum(w * x) + b))
Run Code Online (Sandbox Code Playgroud)
你会看到(这是你在翻译中所做的) -0.19999999999999996
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |