×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# I could use an explanation of MODULUS2

## I could use an explanation of MODULUS

(OP)
I have read a few definitions, and seen many examples.
Part of them do make sense, but not completely.
So far the best definition I've seen is this one:

#### Quote (developer.mozilla.org)

"The remainder operator returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend, not the divisor. It uses a built-in modulo function to produce the result, which is the integer remainder of dividing var1 by var2 — for example — var1 modulo var2"
...returns the remainder left over... (of a division)

What "remainder left over" ?
How can there be such thing on the first place?
Maybe I'm not looking at it from the right angle.

I was hoping someone could offer a different definition/approach.

### RE: I could use an explanation of MODULUS

What do you get if you divide 10 by 3?
=>3 and a remainder of 1.

This works for whole numbers only. If one number is a multiple of the other, the modulo is 0. Example: 8 divided by 2 gives 4. No remainder.
9 divided by 2 gives 4, remainder 1. <= This is your modulo right there.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

### RE: I could use an explanation of MODULUS

(OP)
Excellent!
This is what I needed!
In another words: Fit the smaller number as many times as possible into the larger, and what's left is the remainder.

However, one of the examples at Mozilla, is this one:

2 % 3 // 2

Based on the what I understood from your example, I would have thought that: (2 fits once into 3, with a left over of 1). But seen the example above I can infer I am not getting it.

### RE: I could use an explanation of MODULUS

You need to pay attention to the *order* in which the numbers are presented. In this example it is how often does "3 fit into 2"? => 0 times, the 2 remains.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

### RE: I could use an explanation of MODULUS

(OP)
Now I see!
So, if the right number won't fit into the left one, then the left one remains.

Thank you so much MakeItSo :)

### RE: I could use an explanation of MODULUS

Just be aware that the javascript modulus operator, like many of the programming languages have the 1950's Fortran bug - it gives the incorrect answer on negative numbers.

-10 % 3 should be 2, not -1

### RE: I could use an explanation of MODULUS

(OP)
Mmmm, that's a good one!
Thanks a bunch for that xwb :)

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!