Javascript 整数除法,或者 Math.floor(x) 等于 x | 0 表示 x >= 0?
看下面的示例,看起来 Math.floor(x)
相当于 x | 0
,对于x >= 0
。这是真的吗?如果是,为什么? (或者如何计算x | 0
?)
x = -2.9; console.log(Math.floor(x) + ", " + (x | 0)); // -3, -2
x = -2.3; console.log(Math.floor(x) + ", " + (x | 0)); // -3, -2
x = -2; console.log(Math.floor(x) + ", " + (x | 0)); // -2, -2
x = -0.5; console.log(Math.floor(x) + ", " + (x | 0)); // -1, 0
x = 0; console.log(Math.floor(x) + ", " + (x | 0)); // 0, 0
x = 0.5; console.log(Math.floor(x) + ", " + (x | 0)); // 0, 0
x = 2; console.log(Math.floor(x) + ", " + (x | 0)); // 2, 2
x = 2.3; console.log(Math.floor(x) + ", " + (x | 0)); // 2, 2
x = 2.9; console.log(Math.floor(x) + ", " + (x | 0)); // 2, 2
x = 3.1; console.log(Math.floor(x) + ", " + (x | 0)); // 3, 3
这对于在Javascript中执行整数除法很有用:(5 / 3) | 0
而不是 Math.floor(5 / 3)
。
Looking at the following examples, it looks like Math.floor(x)
is equivalent to x | 0
, for x >= 0
. Is that really true? If yes, why? (or how x | 0
is calculated?)
x = -2.9; console.log(Math.floor(x) + ", " + (x | 0)); // -3, -2
x = -2.3; console.log(Math.floor(x) + ", " + (x | 0)); // -3, -2
x = -2; console.log(Math.floor(x) + ", " + (x | 0)); // -2, -2
x = -0.5; console.log(Math.floor(x) + ", " + (x | 0)); // -1, 0
x = 0; console.log(Math.floor(x) + ", " + (x | 0)); // 0, 0
x = 0.5; console.log(Math.floor(x) + ", " + (x | 0)); // 0, 0
x = 2; console.log(Math.floor(x) + ", " + (x | 0)); // 2, 2
x = 2.3; console.log(Math.floor(x) + ", " + (x | 0)); // 2, 2
x = 2.9; console.log(Math.floor(x) + ", " + (x | 0)); // 2, 2
x = 3.1; console.log(Math.floor(x) + ", " + (x | 0)); // 3, 3
This can be useful to perform integer division in Javascript: (5 / 3) | 0
rather than Math.floor(5 / 3)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
位运算符将数字转换为 32 位序列。因此,您建议的替代方案仅适用于带正号的 32 位浮点数,即从
0
到+2,147,483,647
(2^31-1
) 的数字代码>)。另一个区别:如果 x 不是数字,则 x | 的结果0 可能与
Math.floor(x)
不同。除此之外,只要使用正数,结果应该与 Math.floor() 类似。
以下是更多示例+性能测试:http://jsperf.com/rounding-numbers-down
Bitwise operators convert numbers to a 32-bit sequence. So the alternatives you’re suggesting will only work with positive signed 32-bit floats, i.e. numbers from
0
to+2,147,483,647
(2^31-1
).Another difference: if
x
is not a number, the result ofx | 0
might be different than that ofMath.floor(x)
.Other than that, the result should be similar to that of
Math.floor()
, as long as a positive number is used.Here are some more examples + performance tests: http://jsperf.com/rounding-numbers-down
根据 ECMAScript 规范,§11.10 二进制按位运算符:
这是如何
x | y
的计算方法:x
和y
被解析为Int32
,然后对它们应用|
运算符。As per ECMAScript spec, §11.10 Binary Bitwise Operators:
This is how
x | y
is calculated:x
andy
are parsed toInt32
and then apply the|
operator to them.JS中的按位运算是32位的,也就是说float首先被“cast”成“int”。
<代码>“2.6”| 0 = 2 表明正在调用
parseInt
。Bitwise operations in JS are 32bit, that is the float is first "cast" into an "int".
"2.6" | 0 = 2
suggests thatparseInt
is being called.竖线是按位或运算符。由于 0 的位全为零,因此 x|0 理论上是空操作。但为了计算它,操作数必须是整数,因此 x 必须首先从浮点转换为整数。转换是通过消除小数部分来完成的,所以是的,对于某些 x >= 0,我们有
x|0
==Math.floor(x)
。请注意,结果取决于内部整数类型的大小和符号。例如你得到:
The vertical bar is the bitwise-or operator. Since the bits of 0 are all zero,
x|0
is in theory a no-op. But in order to evaluate it the operands must be integers, sox
must be converted from floating point into an integer first. The conversion is made by eliminating the fractional part, so yes, for some x >= 0 we havex|0
==Math.floor(x)
.Note that the result depends on the size and signness of the internal integer type. For example you get:
(x | 0) 删除了“.”之后的位,因此我们可以得到下一个真关系:
x >>> 0 与 x | 具有相同的效果0,所以:
(x | 0) removes the bits after the ".", so we can get the next true relation:
x >> 0 has the same effect as x | 0, so :