I'm seeing this in some code, and I have no idea what it does:
var jdn = function(y, m, d) { var tmp = (m <= 2 ? -1 : 0); return ~~((1461 * (y + 4800 + tmp)) / 4) + ~~((367 * (m - 2 - 12 * tmp)) / 12) - ~~((3 * ((y + 4900 + tmp) / 100)) / 4) + d - 2483620; };
What's the ~~
operator do?
The “double tilde” (~~) operator is a double NOT Bitwise operator. Use it as a substitute for Math. floor(), since it's faster.
Another approximation symbol is the double tilde ≈, meaning "approximately equal to". The tilde is also used to indicate congruence of shapes by placing it over an = symbol, thus ≅.
The (~) tilde operator takes any number and inverts the binary digits, for example, if the number is (100111) after inversion it would be (011000).
Tilde is a R's "Primitive Function" that does not evaluate its argument, and it is normally used to create a formula object as an inner-DSL role. I hijack this functionality to make an anounymous function. Double-tilde with a two-dots symbol, .. , makes an anonymous function in which two-dots plays a placeholder.
That ~~
is a double NOT bitwise operator.
It is used as a faster substitute for Math.floor()
for positive numbers. It does not return the same result as Math.floor()
for negative numbers, as it just chops off the part after the decimal (see other answers for examples of this).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With