5 ms·
Its because they don't do the same thing Math.floor() favors the number equal to/less than the parameter, Math.floor(-15.5) is -16 while (-15.5 | 0) is -15 Al
by Stormcaller 11y ago
Its because they don't do the same thing
Math.floor() favors the number equal to/less than the parameter, Math.floor(-15.5) is -16 while (-15.5 | 0) is -15
Also because the returned value is int32[0],
Math.floor(2147483648.5) is 2147483648 while (2147483648.5 | 0) is -2147483648
You are fine if you know the input is less than (2^31) + 1 and you want to truncate, rather than floor.
[0]: http://www.ecma-international.org/ecma-262/6.0/#sec-binary-bitwise-operators-runtime-semantics-evaluation http://www.ecma-international.org/ecma-262/6.0/#sec-binary-b...
- nkron 11y agoECMAScript 2016 adds Math.trunc()[0] which should give the same value as | 0 for inputs that fit in 32 bits. [0] http://www.javascripture.com/Math#trunc http://www.javascripture.com/Math#trunc
- 4Data 11y agoHmm, pretty sure floor rounds a number downward to its nearest integer. So Math.floor(-15.5) would be 15. https://developer.mozilla.org/pt-PT/docs/Web/JavaScript/Reference/Global_Objects/Math/floor https://developer.mozilla.org/pt-PT/docs/Web/JavaScript/Refe...
- catchmrbharath 11y agoNope. Math.floor(-15.5) would be -16. It rounds a number downwards. -16 < -15.
- Stormcaller 11y agoLiterally the first example on that page: > Math.floor( 45.95); // 45 > Math.floor(-45.95); // -46 Its not like its hidden or anything... Its in the center of the page on my 1920x1080 screen.