var suit = i / 13 | 0;
That's such a clean way to get a 0, 1, 2 or 3 from each card's `i` and I never would have thought of it.
var suit = i / 13 | 0;
That's such a clean way to get a 0, 1, 2 or 3 from each card's `i` and I never would have thought of it.
suit = int(i / 13)
suit = (int)(i / 13)
suit = math.floor(i / 13)
Are much easier to understand for someone not familiar with the code.And on the flip side with JS's lack of number types, any code that looks like it's doing type casts (rather than math) looks out of place. If one must mess around with JS number types then all you can really do is give hints to the optimizing compiler, in which case "|0" is the standard way to hint that you want a small int.
Obviously what looks clean is a matter of opinion, but FWIW.
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-b...
https://developer.mozilla.org/pt-PT/docs/Web/JavaScript/Refe...
> 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.
Chrome 45 on Linux
It's odd that a bitwise operator should have the effect of truncating the float (since X|0 == X)? I'm guessing there's an implicit type conversion to int in the middle?
This all to save a fraction of a microsecond on an operation that's called 60 times on the page.
But yeah, I agree about googling part: I remember having Googled "tilde javascript" and "pipe javascript".. :)
Tilde is useful with indexOf:
if (~array.indexOf(item)) {}
..equals to:
if (array.indexOf(item) > -1) {}
i/13|undefined
has that "this was originally written in javascript, not ported halfheartedly from some other language" feel. (Well, maybe PHP.)Math.floor(value) can return some weirder values such as: NaN (for anything that isn't a number), and Infinity, -Infinity, -0. And Math.floor(-1e-300); is -1 so care needs to be taken with floating values near zero.
4 and 13 are relatively prime, so each number between 1 and 52 can be uniquely represented by the function (x) -> (x%13, x%4).
which has the advantage of working with any size of deck. important since in brazillian truco you leave most of the numbers out ;)
Use (int), like C.
I just tested it in PHP and it seems it is 20% faster in PHP compared to casting to int. Maybe someone out there will have a weird use case for that, or someone doesn't want to put more effort into writing code into terminal and doesn't care about his code being unreadable. I think its useful to know it exists.
Btw, do you or someone else know why its faster in php too? Its a bitwise operation and I assume under the hood its casting to int, its weird to have it faster than casting it to int, why does this happen? It makes sense in javascript to that its faster than functions, but why it is faster than a cast in PHP?
It is worse because people are even less familiar with this in PHP. It's not idiomatic.
> I just tested it in PHP and it seems it is 20% faster in PHP compared to casting to int.
How rigorous was your testing? Which version of PHP were you using?
> It makes sense in javascript to that its faster than functions, but why it is faster than a cast in PHP?
It shouldn't be. It might save an allocation in older PHP versions, I'm not sure.
~~(i/13)Similar to seeing `!!` and thinking true/false coercion.