The remainder of the quotient of two numbers (usually integers).
Questions tagged [modulus]
847 questions
127
votes
5 answers
What does "% is unavailable: Use truncatingRemainder instead" mean?
I get the following error when using code for an extension, I'm not sure if they're asking to just use a different operator or modify the values in the expression based on an internet search.
Error: % is unavailable: Use truncatingRemainder…

Laurence Wingo
- 3,912
- 7
- 33
- 61
96
votes
10 answers
Understanding The Modulus Operator %
I understand the Modulus operator in terms of the following expression:
7 % 5
This would return 2 due to the fact that 5 goes into 7 once and then gives the 2 that is left over, however my confusion comes when you reverse this statement to read:
5…

Peter Featherstone
- 7,835
- 4
- 32
- 64
63
votes
11 answers
checking if a number is divisible by 6 PHP
I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.
how can I do that ?

Utku Dalmaz
- 9,780
- 28
- 90
- 130
59
votes
4 answers
Selecting rows where remainder (modulo) is 1 after division by 2?
There is a column in options that hold an integer. I want to select the row only if that value % 2 = 1.
I know this can be done in 2 queries but is it possible to do it in 1?

Carlos
- 681
- 2
- 6
- 4
47
votes
8 answers
Fast way to calculate n! mod m where m is prime?
I was curious if there was a good way to do this. My current code is something like:
def factorialMod(n, modulus):
ans=1
for i in range(1,n+1):
ans = ans * i % modulus
return ans % modulus
But it seems quite slow!
I also…

John Smith
- 11,678
- 17
- 46
- 51
45
votes
8 answers
C# modulus operator
I can write the program
int a = 3;
int b = 4;
Console.WriteLine(a % b);
The answer I get is 3. How does 3 mod 4 = 3???
I can't figure out how this is getting computed this way.

Ben Ziegler
- 895
- 1
- 7
- 18
45
votes
6 answers
Reverse Modulus Operator
Over 3 years after asking the question I found the solution. I have included it as an answer.
I have an expression with modulus in it that needs to be put in terms of x.
(a + x) mod m = b
I can't figure out what to do with the modulus. Is there a…

Michael Hogenson
- 1,292
- 1
- 15
- 31
39
votes
8 answers
Why does modulus operator return fractional number in javascript?
Why does 49.90 % 0.10 in JavaScript return 0.09999999999999581? I expected it to be 0.

Edgar
- 4,348
- 4
- 40
- 59
37
votes
5 answers
How does the modulus operator work?
Let's say that I need to format the output of an array to display a fixed number of elements per line. How do I go about doing that using modulus operation?
Using C++, the code below works for displaying 6 elements per line but I have no idea how…

Tex Qas
- 371
- 1
- 3
- 4
36
votes
6 answers
What is the modulus operator (%) in swift 3?
I have this line:
randomIndex = Int(drand48() % Double(alphabetColors.count))
And Xcode 8 (Swift 3) tells me:
'%' is unavailable: Use truncatingRemainder instead
Is there no operator anymore? How should I convert my code?

Kalzem
- 7,320
- 6
- 54
- 79
36
votes
7 answers
Faster modulus in C/C#?
Is there a trick for creating a faster integer modulus than the standard % operator for particular bases?
For my program, I'd be looking for around 1000-4000 (e.g. n%2048). Is there a quicker way to perform n modulus 2048 than simply: n%2048?

Dan W
- 3,520
- 7
- 42
- 69
34
votes
6 answers
How to test whether a float is a whole number in Go?
I originally tried this, however the % operator isn't defined for float64.
func main(){
var a float64
a = 1.23
if a%1 == 0{
fmt.Println("yay")
}else{
fmt.Println("you fail")
}
}

John Calder
- 619
- 3
- 9
- 13
33
votes
4 answers
Find multiples of a number in PHP
I want to find all muliples of a number in PHP.
I'm using something like this
if($count != 20 )
to work out if $count is not equal to 20.
But I also need this script to check if $count is not equal to 20, 40, 60, 80, 100, 120, 140, 160 etc.
Any…

dotty
- 40,405
- 66
- 150
- 195
28
votes
10 answers
How do I divide so I get a decimal value?
I want to know how to get remainder and quotient in single value in Java.
Example:
3/2 I should get value as 1.5.
If I use the / operator I get only the quotient. If I user the % operator I get only the remainder. How do I get both at a time in…
Raja
26
votes
5 answers
Why doesn't a negative number modulo a vector size give a negative number?
#include
#include
#include
using namespace std;
int main()
{
vector v = {1, 2, 3, 4, 5, 6, 7};
int i = -4;
cout << i << endl;
cout << v.size() << endl;
cout << i % v.size() << endl;
cout << -4 % 7 <<…

George Zhang
- 377
- 2
- 3