The remainder of the quotient of two numbers (usually integers).
Questions tagged [modulus]
847 questions
25
votes
1 answer
Why do programming contests want answers modulo some large prime?
I have been testing the waters of competitive programming and I have already seen this statement mentioned a lot of times:
Print the result modulo 109 + 7
Now I can figure out that this is some way of preventing overflow of digits when dealing…

Pawan
- 1,480
- 2
- 18
- 27
23
votes
3 answers
Why is operator% referred to as the "modulus" operator instead of the "remainder" operator?
Today at work I had an interesting discussion with one of my coworkers. He was surprised when he had the following happen to him:
assert(-1 % 10 == -1) //Expecting 9
So when he came to ask me about it, I told him "well, that makes sense. When…

Chad La Guardia
- 5,088
- 4
- 24
- 35
23
votes
3 answers
What are the rules for modular arithmetic in C?
In earlier classes, I was taught that n % d = r and to think about it as n = d*q + r, where d is the divisor, q is the quotient, and r is the remainder (noting that the remainder can never be negative).
So for instance, -111 mod 11 is 10, because…

Theo Chronic
- 1,623
- 2
- 15
- 16
22
votes
2 answers
Why does the compiler evaluate remainder MinValue % -1 different than runtime?
I think this looks like a bug in the C# compiler.
Consider this code (inside a method):
const long dividend = long.MinValue;
const long divisor = -1L;
Console.WriteLine(dividend % divisor);
It compiles with no errors (or warnings). Seems like a…

Jeppe Stig Nielsen
- 60,409
- 11
- 110
- 181
18
votes
7 answers
Modulus in a PHP loop
I'm currently checking whether an entry in a loop is the third iteration or not, with the following code:

Udders
- 6,914
- 24
- 102
- 194
18
votes
4 answers
How to get the Modulus in LISP
I am learning LISP right now and I haven't found anything on how to get the modulus in LISP. Is there someway to get it inside of a function? I know other languages like Java use % in order to find the modulus, but what does LISP use?

Nick Welki
- 435
- 4
- 8
- 13
18
votes
2 answers
PHP float modulus not working
I wrote a function to add commas and zeros to a number if necessary, but I've gotten stuck at the modulus function. According to my PHP:
float(877.5) % 1 == 0 //true
Shouldn't 877.5 % 1 == 0.5?

Liftoff
- 24,717
- 13
- 66
- 119
16
votes
3 answers
What is the modulo operator for longs in Java?
How do I find the modulo (%) of two long values in Java? My code says 'Integer number too large' followed by the number I'm trying to mod. I tried casting it to a long but it didn't work. Do I have to convert it to a BigInteger and use the remainder…

Descartes
- 503
- 2
- 7
- 22
15
votes
2 answers
Modulus operator nim
What is the modulus operator in Nim?
tile % 9 == 0 results in undeclared identifier: '%'
Googling or searching SO doesn't bring up an answer.

Lex
- 4,749
- 3
- 45
- 66
15
votes
4 answers
Modulus with doubles in Java
How do you deal with Java's weird behaviour with the modulus operator when using doubles?
For example, you would expect the result of 3.9 - (3.9 % 0.1) to be 3.9 (and indeed, Google says I'm not going crazy), but when I run it in Java I get…

Andy
- 2,764
- 6
- 24
- 33
14
votes
3 answers
Split down a number in seconds to days, hours, minutes, and seconds?
I've heard that it's possible to accomplish this using the modulus % operator present in most programming languages. The real question is, how? I'm unfamiliar with how modulus works, so I've had difficulties using it in the past. Given the present…

Naftuli Kay
- 87,710
- 93
- 269
- 411
14
votes
1 answer
Modulus gives wrong outcome?
Could anyone tell me why these two modulus calculations yield two different outcomes? I just need to blame someone or something but me for all those hours I lost finding this bug.
public void test1()
{
int stepAmount = 100;
float t = 0.02f;
…

Madmenyo
- 8,389
- 7
- 52
- 99
14
votes
2 answers
Java - Is there a method for Euclidean or floored modulo
Java modulo operator % is based on the truncated division (see Wikipedia: Modulo operation).
5%3 produces 2 (note that 5/3 produces 1)
5%(-3) produces 2 (note that 5/(-3) produces -1)
(-5)%3 produces -2 (note that (-5)/3 produces -1)
(-5)%(-3)…

boumbh
- 2,010
- 1
- 19
- 21
13
votes
5 answers
How is -13 % 64 = -13 in PHP?
Derived from this question : (Java) How does java do modulus calculations with negative numbers?
Anywhere to force PHP to return positive 51?
update
Looking for a configuration setting to fix, instead hard-guessing
Or other math function like…

ajreal
- 46,720
- 11
- 89
- 119
13
votes
5 answers
Repeating letters like excel columns?
I want to create a list of string that resemble the column letters in Microsoft Excel. For example, after 26 columns, the next columns become AA, AB, AC, etc.
I have tried using the modulus operator, but I just end up with AA, BB, CC, etc...
import…

blacksite
- 12,086
- 10
- 64
- 109