-3

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

4

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.)

  • Unfortunately, programming languages favor using % to represent the remainder operator rather than the modulo operator. The modulo operator maps things into equivalence classes such that if x+y yields the arithmetical sum of x and y without overflow, ((x+y) mod base) will equal ((x mod base)+(y mod base). The remainder operator sacrifices that property for the property that ((-x) rem y)=(-(x rem y)). – supercat Mar 12 '14 at 6:39
4

It's one of those operators that you think you'll never use and then you find irreplaceable.

One example is looping through an indexed item:

i++;
if (i % 5 === 0) {
   i = 0;
}

Obviously, that can be simplified but that's the idea spelled out.

The result is: 1, 2, 3, 4, 0, 1, 2, 3, 4, etc...

[Edit]

Also, to answer the question I think that you might be asking...

It may help to think in fractions:

10 / 3 = 3 ⅓
⅓ * 3 = 1

See also : Euclidean Division

  • Your code can just be simplified to: i = (i + 1) % 5 – Servy Aug 16 '13 at 14:32
  • 1
    @Servy Oh, it can be simplified even farther, but that's exactly why I said "Obviously, that can be simplified but that's the idea spelled out." I knew someone would make some comment. This code is a teaching aid not code I would actually write. – Richard Aug 16 '13 at 14:32
  • I don't get that thing above Edit – Rapier Aug 16 '13 at 15:09
  • this: prntscr.com/1lll51 – Rapier Aug 16 '13 at 15:16
  • @Rapier Does that make more sense? This is basic if conditions and looping. – Richard Aug 16 '13 at 15:29
2

There are several answers possible to this question from different points of view.

Lets start with the practical approach. There are simply things that can only be divided in units of one. At the same time there may for a given problem be the constraint that all parts have to be equal. If you want to divide ten cents between three people and each of them to get an equal share then you are left with one cent. If you have a screen with 128 pixels width and a table with ten columns each one can have 12 pixels and you are left with 8 pixels. Divide those by two and you know where to place the table to center it.

From a mathematical view: Here it can simply be a matter of definition which number system you have to use (natural numbers, integers, rationals etc.). If your calculation is limited to natural numbers or integers then there are no decimal places. There is a clear definition for this in mathematics: Remainder (I am no mathematician so I hope this explanation isn't too much simplified)

For computers and programming languages there is often a strong difference between integers and floating point numbers because their binary representation is different. Today it doesn't make such a big difference any more but integer operations were faster than a floating point operation. And the number of bits that can be used to hold a number is limited, therefore recurring decimals are not possible (there are exceptions in some languages or libraries) which would lead to results of such a calculations not being exact. As you can see from Benjamin's link, in mathematics 0.999... would equal 1, for a computer this would not be the case.

Many programming languages have different data types for integers and floats. Javascript does not make this differentiation. But for many reasons (as in my examples above) it still has the modulo operator since there are practical applications as much as it simply completes the set of mathematical operators for a well defined operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.