2012-12-12 185 views
0

正如我在示例中看到的,~~Math.floor的功能是相同的。他们两人的轮数下降(AM我认为正确的吗?)~~与Math.floor之间的区别()

我也应该提到根据这个测试~~Math.floor快:jsperf.com/math-round-vs

所以我想知道,有没有~~任何区别和Math.floor

+0

可能重复(http://stackoverflow.com/questions/5971645/what-is-the-double-tilde-operator-in-javascript) –

回答

3

是的,按位运算符通常不能很好地运用负数。 f.ex:

~~-6.8 == -6 // doesn’t round down, simply removes the decimals 

Math.floor(-6.8) == -7 

而你也可以得到0,而不是为NaN,f.ex:什么是 “双波浪”(~~)在JavaScript操作]的

~~'a' == 0 

Math.floor('a') == NaN 
+0

你也可以做'~~ n - (n <0)',但那只是残酷。 – Blender

相关问题