我的 Javascript 和 Python 代码是因为指数还是其他原因给我不同的答案?
Are my Javascript and Python code giving me different answers because of exponents, or something else?
我已经将一个简单的算法从 Python3 移植到 Javascript,令人惊讶的是,我得到了不同的答案。 Python 代码按预期工作,我不知道为什么 Javascript 的行为不同。
这是Python:
def foo():
theArray = [1,2,3,4,5,6]
a = theArray[0]
b = theArray[1]
c = theArray[2]
d = theArray[3]
e = theArray[4]
f = theArray[5]
res = (((a**2 + b**2 - (d - e)^3 + (b//e)^2)/ ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + (c//d)^2))*0.75)
return res
Python3 结果:0.32..
这是 Javascript 代码:
function foo() {
theArray = [1,2,3,4,5,6]
var a, b, c, d, e, f
a = theArray[0]
b = theArray[1]
c = theArray[2]
d = theArray[3]
e = theArray[4]
f = theArray[5]
res = (((a**2 + b**2 - (d - e)**3 + (b/e)**2)/ ((a**2 + b**2 - (d - e)**3) + c**2 + (f-4)**2 + (c/d)^2))*0.75)
return res
}
Javascript 结果:0.27..
在 Javascript 代码中使用 Math.pow() 没有任何改变。
我注意到的第一件事是您有时使用“**”表示求幂,有时使用“^”。前者应该用于 python 和 javascript(或者至少根据 w3 学校,但无论如何它们对我产生不同的结果)
我假设您不是要使用按位 "XOR" 运算符,而是在编写“^”时使用指数,因为您在 python 和 js 中都输入了不同的符号……
当我将计算机上的所有“^”实例更改为“**”时,两种算法 return 0.2361
请注意,它们不是相同的公式。
在您的 python 代码中您有:
res = (((a**2 + b**2 - (d - e)^3 + (b//e)^2) / ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + (c//d)^2))*0.75
其中有(d - e)^3
和(b//e)^2
和(f-4)^2
在你的 js 代码中你有:
res = (((a**2 + b**2 - (d - e)**3 + (b/e)**2) / ((a**2 + b**2 - (d - e)**3) + c**2 + (f-4)**2 + (c/d)^2))*0.75)
其中有 (d - e)**3
和 (b//e)**2
和 (f-4)**2
XOR 运算与指数运算有很大不同。
此外,请注意 python 中有很多整数除法。在 javascript 中,等效项类似于:
(Math.floor(b/e))^2
所以正确的js公式应该是:
res = (((a**2 + b**2 - (d - e)^3 + Math.floor(b/e)^2) / ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + Math.floor(c/d)^2))*0.75
我已经将一个简单的算法从 Python3 移植到 Javascript,令人惊讶的是,我得到了不同的答案。 Python 代码按预期工作,我不知道为什么 Javascript 的行为不同。
这是Python:
def foo():
theArray = [1,2,3,4,5,6]
a = theArray[0]
b = theArray[1]
c = theArray[2]
d = theArray[3]
e = theArray[4]
f = theArray[5]
res = (((a**2 + b**2 - (d - e)^3 + (b//e)^2)/ ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + (c//d)^2))*0.75)
return res
Python3 结果:0.32..
这是 Javascript 代码:
function foo() {
theArray = [1,2,3,4,5,6]
var a, b, c, d, e, f
a = theArray[0]
b = theArray[1]
c = theArray[2]
d = theArray[3]
e = theArray[4]
f = theArray[5]
res = (((a**2 + b**2 - (d - e)**3 + (b/e)**2)/ ((a**2 + b**2 - (d - e)**3) + c**2 + (f-4)**2 + (c/d)^2))*0.75)
return res
}
Javascript 结果:0.27..
在 Javascript 代码中使用 Math.pow() 没有任何改变。
我注意到的第一件事是您有时使用“**”表示求幂,有时使用“^”。前者应该用于 python 和 javascript(或者至少根据 w3 学校,但无论如何它们对我产生不同的结果)
我假设您不是要使用按位 "XOR" 运算符,而是在编写“^”时使用指数,因为您在 python 和 js 中都输入了不同的符号……
当我将计算机上的所有“^”实例更改为“**”时,两种算法 return 0.2361
请注意,它们不是相同的公式。
在您的 python 代码中您有:
res = (((a**2 + b**2 - (d - e)^3 + (b//e)^2) / ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + (c//d)^2))*0.75
其中有(d - e)^3
和(b//e)^2
和(f-4)^2
在你的 js 代码中你有:
res = (((a**2 + b**2 - (d - e)**3 + (b/e)**2) / ((a**2 + b**2 - (d - e)**3) + c**2 + (f-4)**2 + (c/d)^2))*0.75)
其中有 (d - e)**3
和 (b//e)**2
和 (f-4)**2
XOR 运算与指数运算有很大不同。
此外,请注意 python 中有很多整数除法。在 javascript 中,等效项类似于:
(Math.floor(b/e))^2
所以正确的js公式应该是:
res = (((a**2 + b**2 - (d - e)^3 + Math.floor(b/e)^2) / ((a**2 + b**2 - (d - e)^3) + c**2 + (f-4)^2 + Math.floor(c/d)^2))*0.75