Questions tagged [modulo]

The modulo (sometimes called modulus) operation finds the remainder of division of one number by another. It's typically represented by the percent ('%') character in programming languages.

The modulo (sometimes called modulus) operation finds the remainder of division of one number by another. It's typically represented by the percent ('%') character in programming languages.

For example, assuming % as the character representing this operation, the expresssion 8 % 3 would yield a result of 2: the remainder after dividing 8 by 3.

1648 questions
1336
votes
19 answers

How to perform an integer division, and separately get the remainder, in JavaScript

In JavaScript, how do I get: The whole number of times a given integer goes into another? The remainder?
Yarin
  • 173,523
  • 149
  • 402
  • 512
540
votes
17 answers

How does a hash table work?

I'm looking for an explanation of how a hash table works - in plain English for a simpleton like me! For example, I know it takes the key, calculates the hash (I am looking for an explanation how) and then performs some kind of modulo to work out…
Arec Barrwin
  • 61,343
  • 9
  • 29
  • 25
370
votes
13 answers

JavaScript % (modulo) gives a negative result for negative numbers

According to Google Calculator (-13) % 64 is 51. According to Javascript (see this JSBin) it is -13. How do I fix this?
Alec Gorge
  • 17,110
  • 10
  • 59
  • 71
328
votes
11 answers

Why do people say there is modulo bias when using a random number generator?

I have seen this question asked a lot but never seen a true concrete answer to it. So I am going to post one here which will hopefully help people understand why exactly there is "modulo bias" when using a random number generator, like rand() in…
user1413793
  • 9,057
  • 7
  • 30
  • 42
284
votes
13 answers

Modulo operation with negative numbers

In a C program I was trying the below operations (Just to check the behavior) x = 5 % (-3); y = (-5) % (3); z = (-5) % (-3); printf("%d ,%d ,%d", x, y, z); It gave me output as (2, -2 , -2) in gcc. I was expecting a positive result every…
Alva
  • 2,882
  • 3
  • 14
  • 8
265
votes
20 answers

What is the result of % in Python?

What does the % in a calculation? I can't seem to work out what it does. Does it work out a percent of the calculation for example: 4 % 2 is apparently equal to 0. How?
orange
  • 5,297
  • 12
  • 50
  • 71
255
votes
15 answers

Mod of negative number is melting my brain

I'm trying to mod an integer to get an array position so that it will loop round. Doing i % arrayLength works fine for positive numbers but for negative numbers it all goes wrong. 4 % 3 == 1 3 % 3 == 0 2 % 3 == 2 1 % 3 == 1 0 % 3 == 0 -1 % 3…
gormenghastly
251
votes
17 answers

What's the syntax for mod in java

As an example in pseudocode: if ((a mod 2) == 0) { isEven = true; } else { isEven = false; }
Bob
  • 2,649
  • 2
  • 16
  • 6
239
votes
5 answers

Can't use modulus on doubles?

I have a program in C++ (compiled using g++). I'm trying to apply two doubles as operands to the modulus function, but I get the following error: error: invalid operands of types 'double' and 'double' to binary 'operator%' Here's the code: int…
Bhaxy
  • 5,346
  • 10
  • 39
  • 41
225
votes
6 answers

How to use mod operator in bash?

I'm trying a line like this: for i in {1..600}; do wget http://example.com/search/link $i % 5; done; What I'm trying to get as output is: wget http://example.com/search/link0 wget http://example.com/search/link1 wget…
Eric
  • 2,900
  • 2
  • 19
  • 20
221
votes
13 answers

Find the division remainder of a number

How could I go about finding the division remainder of a number in Python? For example: If the number is 26 and divided number is 7, then the division remainder is 5. (since 7+7+7=21 and 26-21=5.) For simple divisibility testing, see How do you…
Bob
  • 10,427
  • 24
  • 63
  • 71
194
votes
30 answers

How to get the separate digits of an int number?

I have numbers like 1100, 1002, 1022 etc. I would like to have the individual digits, for example for the first number 1100 I want to have 1, 1, 0, 0. How can I get it in Java?
Edy Moore
  • 2,129
  • 3
  • 14
  • 10
177
votes
9 answers

How do you check whether a number is divisible by another number?

I need to test whether each number from 1 to 1000 is a multiple of 3 or a multiple of 5. I tried this code in Python 2.x: n = 0 s = 0 while (n < 1001): x = n/3 if isinstance(x, (int, long)): print 'Multiple of 3!' s = s + n …
Taimur
  • 3,171
  • 8
  • 32
  • 38
162
votes
12 answers

Find if variable is divisible by 2

How do I figure out if a variable is divisible by 2? Furthermore I need do a function if it is and do a different function if it is not.
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180
154
votes
4 answers

Division ( / ) not giving my answer in postgresql

I have a table software and columns in it as dev_cost, sell_cost. If dev_cost is 16000 and sell_cost is 7500, how do I find the quantity of software to be sold in order to recover the dev_cost? I have queried as below: select dev_cost / sell_cost…
zeewagon
  • 1,835
  • 4
  • 18
  • 22
1
2 3
99 100