How does Javascript handle a negative modulo? (`-5 % 20`)

JavaScript uses a remainder operator, not a modulo operator - which is why it’s incorrect to refer to it as the latter. Programming languages use one or the other. This typically does not cause an issue, as the result when dividing positive numbers is the same for both. If using negative numbers, then the remainder and modulo operators will each produce a different result:

6 Likes

It matters very much to know whether one is using a remainder operator or a modulo operator. JS uses remainder and thinking it will produce the same results as modulo operator will cause bugs that are hard to trace in a program if using negative numbers. If modulo results are needed within JS, a specific operation must be used to obtain them.

2 Likes

Hello @bit5009167409
Case1: If the dividend is less than divisor, remainder is always the divisor. For example: -5 / 20 => Remainder(-5) OR 5/20 => Remainder(5)
Case2: If the dividend is equal to divisor remainder is always zero. For example: -6 / 6 => Remainder(0)
OR 6/-6 => Remainder(0)

Case3: If the dividend is greater than divisor just simply divide it and get the remainder. For example: -18/5 => Remainder(-3) OR 20/3 = > Remainder(2)

Note: In case 3 consider the negative value as a positive value and then add negative sign after the result.

Also note that the decimal fraction of the quotient times the divisor gives us the remainder of integer division.

20 ) -5   | -q => 0
20 ) -50  | -0.2
20 ) -100 | -0.25

20 * -0.25 => -5

In integer division, 20 goes into neither 5, nor negative 5 so the quotient is zero, and the remainder is the number we start with, the dividend. So R = D when q == 0.

Hey just for the record, you made a little mistake here.

Case 1:
101/ 11 = 99 remainder 2
or
101 = 11 * 99 + 2

It should be

101/ 11 = 9 remainder 2
and 101 = 11 * 9 + 2
respectively, right?

Good catch! That only took a year and a half to be pointed out.

% tells us the remainder after the number on the right is divided into the number on the left. As 20 cannot be divided into -5 and give us a positive number, it is technically 0. So, the remainder would be the original number on the left (-5).

Hello,
Thank you for these examples.
Just a little correction for the -6 / 6 ! I think the remainder should be -0 (instead of 0) as it always takes the sign of the dividend.

Zero is undefined so has no sense of direction. There is no such thing as negative zero.

1 Like

But what ever you put there its the same -5.
even I put
console.log(-5%17);
output is still > -5

So everytime we use negative number on the left will always return the same negative number?

Wikipedia article(may or may not be reliable):
https://en.wikipedia.org/wiki/Modulo_operation

1 Like

When writing a % n, n is always positive (or zero). a can be either positive or negative.

We won’t be dealing with a negative a anywhere in this course, and should consider modulo to mean “remainder of integer division” for the most part. In all the exercises, it is the remainder we are concerned with.

%  =>  remainder operator

Were it me teaching this class we would start with pencil and paper and long division, to get a feel for what the remainder is, and how we arrive at it. Of course, that would also entail review of the terminology of division…

divisor )  dividend  |  quotient  =>  d, q = divisor, quotient
           -  q * d
           ---------
           remainder

That gives us,

dividend = quotient X divisor + remainder

We’ll have seen me write this as,

D - R = q * d

It’s only when we are clear in our minds what the remainder is that we free ourselves of the confusing aspects of modulo (%). Suggest anybody who has not worked with long division to take the time to study and practice it, on paper, until such time as division becomes simple and our results are reliable. No cheating. Hide the calculator or phone and don’t use the computer, either. Pencil and paper.

234 ) 4567 |  q
1 Like

I might be too late for this and this topic quite caught my interest while learning JavaScript, but are you referring to the rule where:

a % b is the remainder from the division of a by b; and
If |a| < b and a < 0 then a % b = a + b

Hence,

-5 % 20
= -5 + 20
= 15

1 Like

the reason for this is because Javascript looks for the closest possible remainder to 0. javascript doesn’t consider that by definition, a remainder cannot be negative unless it is polynomial division. If you want it to calculate the remainder the correct way, write a function which checks that the remainder isnt negative.

i think you ae correct -5%20 simply means that 20 cant go into -5 then the answer is -5. but they are getting different answer probably because they are using a different programming language

so are saying because is 0 , the denominator will remain??

In conventional longhand division, that is correct.