How is the math calculation of the module (%) done in JavaScript?

10

I'm trying to use "reverse engineering" to understand what the calculation is for the module ( % ), but I can not understand and wanted to understand and clarify myself, to clarify this part, before moving forward and start doing exercises with it.

Then I saw this example:

  
    

If we do 23 % 10, we divide 23 by 10 which equals 2 with 3 left over. So 23 % 10 evaluates to 3 .

  
     

More examples:
17 % 5 evaluates to 2
13 % 7 evaluates to 6

In this first example it says that we split 23 / 10 which is equal to 2 but in fact if we did that calculation would give 2,3 . So I thought, maybe what this means is:

  

23 % 10 equal to 2 with 3 out, should mean 23 / 3 equal to 2,3 , then this would be the ratio of equal to 2 and 3 out?!

But then when I applied this theory in the following examples, this theory did not apply ... I also thought it could be the division of the number 23 by 2 that rounded would be 10 10 leaving the 3 out, but this theory too would not have logic because otherwise it would be 23 % 2 instead of 23 % 10 .

    
asked by anonymous 18.10.2015 / 17:21

3 answers

11

The account is made up of integer values, just as it is in math. The algorithm would look like this:

var x = 23;
var y = 10;
var temp = Math.trunc(x / y); //pega a parte inteira, então o 2,3 vira 2
var modulo = x - temp * y; //pega o dividendo menos o maior valor inteiro divisível
document.body.innerHTML += "<br>" + modulo;
document.body.innerHTML += "<br>" + (17 - Math.trunc(17 / 5) * 5);
document.body.innerHTML += "<br>" + (13 - Math.trunc(13 / 7) * 7);
    
18.10.2015 / 17:31
10

The concept of "module" is similar to "Break of the [whole] division" :

Dividendo ou numerador: 23
Divisor ou denominador: 10
Quociente: 2
Resto: 3

It's not exactly the same concept (according to Wikipedia, most common programming languages - including JavaScript - implement the rest of the division in your % operator, not the module), but it's close enough, especially when all the numbers involved are integers and positives (as in their examples). However, this is not always the case:

// Em JavaScript, o dividendo determina o sinal do resto
log(23 % 10);   // 3
log(-23 % 10);  // -3
log(23 % -10);  // 3
log(-23 % -10); // -3

// Se o dividendo não for inteiro, o resto também não é inteiro
log(23.5 % 10); // 3.5

// Se o divisor não for inteiro, não importa, o quociente é que sempre tem de ser inteiro
log(23 % 10.5);   // 2
log(23.5 % 10.5); // 2.5

function log(x) { document.body.innerHTML += "<p>" + x + "</p>"; }

Other languages may follow different conventions or have different restrictions on the type of operands (I can not tell whether this is standardized or not). About how the calculation is done, I think the bigown answer already explains very well (being consistent with the % in all cases mentioned above).

On the module itself, if you are curious, it is an equivalence relation between several values according to a well defined criterion. It makes no sense to ask "what is the value of X module Y?", Actually X is equivalent to infinite numbers module Y:

23 ≡ 3 ≡ 13 ≡ -7 ≡ -17 ≡ ... (mod 10)

Only one of them - 3 - satisfies the 0 <= 3 < 10 relation, so the "canonical" value is commonly considered, but this only makes sense (and is useful) when the module ( 10 ) is positive and whole.

    
18.10.2015 / 17:58
4

Just to complement

Both bigown and mgibsonbr answers are quite good and complete, thanks for the clarification and additional information that instructed me more =). While researching more on this subject, I found here a example in the SO in this same subject, which I thought would be of value add here to the 'documentation'.

Calculating the following example: 16 % 6 = 4

16 / 6 = 2

Then multiply the quotient (result of division) by 6 (which is the divisor):

2 * 6 = 12

And finally the dividend is subtracted:

16 - 12 = 4

The result gives 4 . The number 4 is the rest, which is the same result of the division of the module that we will get, when doing 16 % 6 = 4 .

    
18.10.2015 / 19:04