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

Asked

Viewed 4,953 times

12

I am trying to use "reverse engineering" to understand what is the calculation done by the module (%), but I’m not getting it and I wanted to understand and clarify, to make this part clear, before I go ahead 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 he says that we divide 23 / 10 which is equal to 2 but in fact if we did this calculation would give 2,3. So I thought, maybe what this means is:

23 % 10 equal to 2 with 3 outside, shall mean 23 / 3 equal to 2,3, then this would be the reason equal to 2 and 3 outsider?!

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

3 answers

12


The account is made over integer values, exactly as it is in mathematics. The algorithm would be more or less like this:

let x = 23;
let y = 10;
let temp = Math.trunc(x / y); //pega a parte inteira, então o 2,3 vira 2
let modulo = x - temp * y; //pega o dividendo menos o maior valor inteiro divisível
console.log(modulo);
console.log((17 - Math.trunc(17 / 5) * 5));
console.log((13 - Math.trunc(13 / 7) * 7));

I put in the Github for future reference.

  • 3

    Warning: this truncation function is part of ES6 and not available on Math in any browser.

  • 1

    Yeah, good warning. in that case it was more to demonstrate the functioning, no one will make that formula if they have the operator ready.

11

The concept of "module" is similar to that of "rest of the division [entire]":

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

It’s not exactly the same concept (according to Wikipedia, the most common programming languages - including Javascript - implement the rest of the split in their operator %, and not the module), but it is close enough, especially when all the numbers involved are integer and positive (as in your examples). However, it 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’t tell if this is standardized or not). About how the calculation is done, I believe that the maniero’s response already explains very well (being consistent with the operator % in all cases cited above).

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

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

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

4

Just to complement

Both the answers of Maniero and mgibsonbr are quite good and complete, thanks for the clarification and additional information that instructed me more =) . While researching further on this subject, I found here a example in the English OS on this same subject, which I thought would be worth adding 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 subtract the dividend:

16 - 12 = 4

The result is 4. Number 4 is the rest, which is the same result of splitting the module that we will get by doing 16 % 6 = 4.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.