我正在用 Javascript 编写一些涉及指数的计算,由于我习惯在 Microsoft Excel 中使用插入符号,所以我输入 x^2
在代码中。它仍然给了我答案,但这不是我想要的。 ^
是什么意思?在 JavaScript 中做什么?
alert(Math.pow(3,2)); //result is 9
alert(3^2); //result is 1
最佳答案
这是按位异或。使用Math.pow
对于指数。
如果你做了 3^2 你实际上在做:
3 XOR 2
计算机使用二进制,因此它们可以看到
11 XOR 10
将其放入表格中:
11 1 ^ 0 = 1
10 1 ^ 1 = 0
--
01
简单来说:
XOR will take the two numbers in binary, and for the bit to be true, they must be different
Here's a list of all operators like ^
Math.pow()
采用底数和指数:
Math.pow(3, 2)
--> 32 --> 9
Math.pow(5, 6)
--> 56 --> 15,625
Math.pow(7, 3)
--> 73 --> 343
关于javascript - (x^2) 在 JavaScript 中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29872035/