I have started to learn Javascript. From book http://eloquentjavascript.net: The
ID: 646960 • Letter: I
Question
I have started to learn Javascript. From book http://eloquentjavascript.net:
There is one more arithmetic operator which is probably less familiar to you. The % symbol is used to represent the remainder operation. X % Y is the remainder of dividing X by Y. For example 314 % 100 is 14, 10 % 3 is 1, and 144 % 12 is 0. Remainder has the same precedence as multiplication and division.
I don't understand, why it multiply the remainder fraction by divisor (after division). It is more complicated or less accurate.
10 / 3 = 3.3333 (a recurring decimal number). 10 % 3 = 1 (0.333 * 3 = 0.999 (not 1 )
What is purpose of this strange remainders ?
Thanks
Explanation / Answer
It's the modulo operator. It's not unique to JavaScript. It's a Discrete Math concept. And it's very useful in a lot of ways. Of particular interest in computer science, it's an important operation in cryptography.
But, and maybe this is more what you're looking for, it doesn't correlate perfectly with the "normal" math operations because it's a discrete math function. (Which generally means you're dealing with integers.)