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
154
votes
20 answers

Why does 2 mod 4 = 2?

I'm embarrassed to ask such a simple question. My term does not start for two more weeks so I can't ask a professor, and the suspense would kill me. Why does 2 mod 4 = 2?
NewToThis
  • 3,197
  • 5
  • 22
  • 10
153
votes
12 answers

How does the modulo (%) operator work on negative numbers in Python?

Exactly how does the % operator work in Python, particularly when negative numbers are involved? For example, why does -5 % 4 evaluate to 3, rather than, say, -1?
facha
  • 11,862
  • 14
  • 59
  • 82
133
votes
7 answers

Best way to make Java's modulus behave like it should with negative numbers?

In java when you do a % b If a is negative, it will return a negative result, instead of wrapping around to b like it should. What's the best way to fix this? Only way I can think is a < 0 ? b + a : a % b
fent
  • 17,861
  • 15
  • 87
  • 91
128
votes
4 answers

Is there a modulus (not remainder) function / operation?

In Rust (like most programming languages), the % operator performs the remainder operation, not the modulus operation. These operations have different results for negative numbers: -21 modulus 4 => 3 -21 remainder 4 => -1 println!("{}", -21 % 4);…
Kapichu
  • 3,346
  • 4
  • 19
  • 38
119
votes
19 answers

How Does Modulus Divison Work

I don't really understand how modulus division works. I was calculating 27 % 16 and wound up with 11 and I don't understand why. I can't seem to find an explanation in layman's terms online. Can someone elaborate on a very high level as to what's…
NSWOA
  • 1,201
  • 2
  • 9
  • 5
104
votes
15 answers

How does java do modulus calculations with negative numbers?

Am I doing modulus wrong? Because in Java -13 % 64 evaluates to -13 but I want to get 51.
Jakir00
  • 2,023
  • 4
  • 20
  • 32
101
votes
6 answers

How can I calculate divide and modulo for integers in C#?

How can I calculate division and modulo for integer numbers in C#?
kartal
  • 17,436
  • 34
  • 100
  • 145
91
votes
16 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

One of my pet hates of C-derived languages (as a mathematician) is that (-1) % 8 // comes out as -1, and not 7 fmodf(-1,8) // fails similarly What's the best solution? C++ allows the possibility of templates and operator overloading, but both…
P i
  • 29,020
  • 36
  • 159
  • 267
88
votes
6 answers

The modulo operator (%) gives a different result for different .NET versions in C#

I am encrypting the user's input for generating a string for password. But a line of code gives different results in different versions of the framework. Partial code with value of key pressed by user: Key pressed: 1. Variable ascii is 49. Value of…
Rajiv Bhardwaj
  • 293
  • 3
  • 9
84
votes
2 answers

How to make a modulo operation in objective-c / cocoa touch?

I have two CGFloat values, and want to calculate the modulo result. Or in other words: I want to know what's left if valueA is placed as much as possible into valueB. So I just tried: CGFloat moduloResult = valueB % valueA; the compiler complains…
HelloMoon
80
votes
10 answers

How to calculate modulus of large numbers?

How to calculate modulus of 5^55 modulus 221 without much use of calculator? I guess there are some simple principles in number theory in cryptography to calculate such things.
Priyank Bolia
  • 14,077
  • 14
  • 61
  • 82
77
votes
8 answers

What does the percentage sign mean in Python

In the tutorial there is an example for finding prime numbers: >>> for n in range(2, 10): ... for x in range(2, n): ... if n % x == 0: ... print(n, 'equals', x, '*', n//x) ... break ... else: ... #…
Lonnie Price
  • 10,265
  • 7
  • 24
  • 13
75
votes
6 answers

C and Python - different behaviour of the modulo (%) operation

I have found that the same mod operation produces different results depending on what language is being used. In Python: -1 % 10 produces 9 In C it produces -1 ! Which one is the right modulo? How to make mod operation in C to be the same like in…
psihodelia
  • 29,566
  • 35
  • 108
  • 157
75
votes
19 answers

How does a modulo operation work when the first number is smaller?

I'm messing with the modulo operation in python and I understand that it will spit back what the remainder is. But what if the first number is smaller than the second? for instance 2 % 5 the answer is 2. How does that work? 2/5 = .4
Pete
  • 5,791
  • 3
  • 19
  • 10
75
votes
4 answers

Why does C++ output negative numbers when using modulo?

Math: If you have an equation like this: x = 3 mod 7 x could be ... -4, 3, 10, 17, ..., or more generally: x = 3 + k * 7 where k can be any integer. I don't know of a modulo operation is defined for math, but the factor ring certainly…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958