Tor*_*res 67 javascript math operators
我有一些JavaScript代码:
<script type="text/javascript">
$(document).ready(function(){
$('#calcular').click(function() {
var altura2 = ((($('#ddl_altura').attr("value"))/100)^2);
var peso = $('#ddl_peso').attr("value");
var resultado = Math.round(parseFloat(peso / altura2)*100)/100;
if (resultado > 0) {
$('#resultado').html(resultado);
$('#imc').show();
};
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
什么是^(尖)运算符在Javascript中是什么意思?
Gum*_*mbo 76
该^操作是按位异或运算符.要平方值,请使用Math.pow:
var altura2 = Math.pow($('#ddl_altura').attr("value")/100, 2);
Run Code Online (Sandbox Code Playgroud)
Rin*_*g Ø 34
^ 例如,正在执行异或(XOR)
6是110二进制的,3是011二进制的,和
6 ^ 3,意思是110 XOR 011给出101(5).
110 since 0 ^ 0 => 0
011 0 ^ 1 => 1
--- 1 ^ 0 => 1
101 1 ^ 1 => 0
Run Code Online (Sandbox Code Playgroud)
Math.pow(x,2)计算x²但是对于square,你最好使用x*xMath.pow使用对数,你会得到更多的近似误差.(x² ~ exp(2.log(x)))
它称为按位异或。让我解释一下:
你有 :
Decimal Binary
0 0
1 01
2 10
3 11
Run Code Online (Sandbox Code Playgroud)
现在我们想要3^2=?那么我们有11^10=?
11
10
---
01
---
Run Code Online (Sandbox Code Playgroud)
所以11^10=01
01十进制是1.
所以我们可以说3^2=1;