219

My friend said that there are differences between "mod" and "remainder".

If so, what are those differences in C and C++? Does '%' mean either "mod" or "rem" in C?

5
  • 2
    It probably is ill-defined for negative operands. Commented Dec 3, 2012 at 12:46
  • @BasileStarynkevitch : Do you means that difference depend on implementations when occur negative operands ? I need "yes" or "no" because this question make a trouble to me. Thanks!
    – songhir
    Commented Dec 3, 2012 at 12:50
  • 1
    % is remainder. Answer details here -> blogs.msdn.com/b/ericlippert/archive/2011/12/05/…
    – wim
    Commented Dec 3, 2012 at 12:52
  • 21
    @David: the question is about the meanings of the terms. If you say that the question has no meaning, despite several people understanding it in the way that the questioner intended, then I think you have to be more specific what you mean by the word "mean" ;-) Commented Dec 3, 2012 at 13:53
  • 3
    @SteveJessop There are competing meanings for these terms. If the question can specify which of those competing meanings is to be used, then it will be possible to say how they differ. Commented Dec 3, 2012 at 13:55

7 Answers 7

204

There is a difference between modulus (Euclidean division) and remainder (C's % operator). For example:

-21 mod 4 is 3 because -21 + 4 x 6 is 3.

But -21 divided by 4 with truncation towards 0 (as C's / operator)
gives -5 with a remainder (C -21 % 4) of -1.

For positive values, there is no difference between Euclidean and truncating division.

See https://en.wikipedia.org/wiki/Euclidean_division#Other_intervals_for_the_remainder - C's choice of truncating the remainder towards 0 (required since C99) forces a negative remainder range for negative quotients. Even in C89, when Euclidean division was allowed by the standard for /

If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.

(-21/4) * 4 + (-21%4) == -21; C99 and later require (-5) * 4 + (-1), not Euclidean -6 and 3.

13
  • 27
    @Jinxiao: in C89 it was implementation-defined: % was always the remainder, but it might also be the modulus (i.e. always positive), because in C89 integer division was permitted to round towards negative infinity instead of towards 0. So in C89, -5 / 2 could be -2 with remainder -1, or -3 with remainder 1, the implementation just had to document which. C99 removed the flexibility, so now -5 / 2 is always -2. Commented Dec 3, 2012 at 13:23
  • 5
    Actually, it is not clear what modulus is. There seem to be many different definitions, depending on the context and the language. See the wikipedia article about modulo_operation. In some contexts, it is actually the same as remainder. Commented Jun 8, 2014 at 17:44
  • 19
    Can someone explain the steps in the first calculation? How -21 mod 4 is 3? Why the calculation is -21 + 4 x 6?
    – Oz Edri
    Commented Dec 5, 2015 at 20:23
  • 19
    @OzEdri To get some number mod 4, you add whatever integer multiple of 4 it takes to get a number between 0 and 3. For -21, that integer is 6 because -21 + 4 x 6 is between 0 and 3. Commented Dec 6, 2015 at 6:53
  • 13
    Actually this is wrong. By definition (see en.wikipedia.org/wiki/Euclidean_division) the remainder is a positive number, so -21 divided by 4 gives -6 with a remainder of 3
    – marcosh
    Commented Feb 8, 2018 at 10:45
75

Does '%' mean either "mod" or "rem" in C?

In C, % is the remainder1.

..., the result of the / operator is the algebraic quotient with any fractional part discarded ... (This is often called "truncation toward zero".) C11dr §6.5.5 6

The operands of the % operator shall have integer type. C11dr §6.5.5 2

The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder ... C11dr §6.5.5 5


What's the difference between “mod” and “remainder”?

C does not define a "mod" nor "modulo" operator/function, such as the integer modulus function used in Euclidean division or other modulo.

C defines remainder.

Let us compare "remainder" per the % operator to the Euclidean "mod".

"Euclidean mod" differs from C's a%b operation when a is negative.

 // a % b, the remainder after an integer division that truncates toward 0.
 7 %  3 -->  1  
 7 % -3 -->  1  
-7 %  3 --> -1  
-7 % -3 --> -1   

"Mod" or modulo as in Euclidean division. The result is always 0 or positive.

 7 modulo  3 -->  1
 7 modulo -3 -->  1
-7 modulo  3 -->  2
-7 modulo -3 -->  2

Candidate modulo code:

int modulo_Euclidean(int a, int b) {
  int m = a % b;
  if (m < 0) {
    // m += (b < 0) ? -b : b; // avoid this form: it is UB when b == INT_MIN
    m = (b < 0) ? m - b : m + b;
  }
  return m;
}

Note about floating point: double fmod(double x, double y), even though called "fmod", it is not the same as Euclidean division "mod", but similar to C integer remainder:

The fmod functions compute the floating-point remainder of x/y. C11dr §7.12.10.1 2

fmod( 7,  3) -->  1.0
fmod( 7, -3) -->  1.0
fmod(-7,  3) --> -1.0
fmod(-7, -3) --> -1.0

Disambiguation: C also has a similar named function double modf(double value, double *iptr) which breaks the argument value into integral and fractional parts, each of which has the same type and sign as the argument. This has little to do with the "mod" discussion here except name similarity.


[Edit Dec 2020]

For those who want proper functionality in all cases, an improved modulo_Euclidean() that 1) detects mod(x,0) and 2) a good and no UB result with modulo_Euclidean2(INT_MIN, -1). Inspired by 4 different implementations of modulo with fully defined behavior.

int modulo_Euclidean2(int a, int b) {
  if (b == 0) TBD_Code(); // perhaps return -1 to indicate failure?
  if (b == -1) return 0; // This test needed to prevent UB of `INT_MIN % -1`.
  int m = a % b;
  if (m < 0) {
    // m += (b < 0) ? -b : b; // avoid this form: it is UB when b == INT_MIN
    m = (b < 0) ? m - b : m + b;
  }
  return m;
}

1 Prior to C99, C's definition of % was still the remainder from division, yet then / allowed negative quotients to round down rather than "truncation toward zero". See Why do you get different values for integer division in C89?. Thus with some pre-C99 compilation, % code can act just like the Euclidean division "mod". The above modulo_Euclidean() will work with this alternate old-school remainder too.

[Edit Apr 2024]
No sign C2X will offer any help here.

1
18

sign of remainder will be same as the divisible and the sign of modulus will be same as divisor.

Remainder is simply the remaining part after the arithmetic division between two integer number whereas Modulus is the sum of remainder and divisor when they are oppositely signed and remaining part after the arithmetic division when remainder and divisor both are of same sign.

Example of Remainder:

10 % 3 = 1 [here divisible is 10 which is positively signed so the result will also be positively signed]

-10 % 3 = -1 [here divisible is -10 which is negatively signed so the result will also be negatively signed]

10 % -3 = 1 [here divisible is 10 which is positively signed so the result will also be positively signed]

-10 % -3 = -1 [here divisible is -10 which is negatively signed so the result will also be negatively signed]

Example of Modulus:

5 % 3 = 2 [here divisible is 5 which is positively signed so the remainder will also be positively signed and the divisor is also positively signed. As both remainder and divisor are of same sign the result will be same as remainder]

-5 % 3 = 1 [here divisible is -5 which is negatively signed so the remainder will also be negatively signed and the divisor is positively signed. As both remainder and divisor are of opposite sign the result will be sum of remainder and divisor -2 + 3 = 1]

5 % -3 = -1 [here divisible is 5 which is positively signed so the remainder will also be positively signed and the divisor is negatively signed. As both remainder and divisor are of opposite sign the result will be sum of remainder and divisor 2 + -3 = -1]

-5 % -3 = -2 [here divisible is -5 which is negatively signed so the remainder will also be negatively signed and the divisor is also negatively signed. As both remainder and divisor are of same sign the result will be same as remainder]

I hope this will clearly distinguish between remainder and modulus.

2
6

In C and C++ and many languages, % is the remainder NOT the modulus operator.

For example in the operation -21 / 4 the integer part is -5 and the decimal part is -.25. The remainder is the fractional part times the divisor, so our remainder is -1. JavaScript uses the remainder operator and confirms this

console.log(-21 % 4 == -1);

The modulus operator is like you had a "clock". Imagine a circle with the values 0, 1, 2, and 3 at the 12 o'clock, 3 o'clock, 6 o'clock, and 9 o'clock positions respectively. Stepping quotient times around the clock clock-wise lands us on the result of our modulus operation, or, in our example with a negative quotient, counter-clockwise, yielding 3.

Note: Modulus is always the same sign as the divisor and remainder the same sign as the quotient. Adding the divisor and the remainder when at least one is negative yields the modulus.

2
  • 2
    Did you mean that sign of the remainder is always the same sign as the dividend? The quotient of 7 / -3 = -2, but 7 rem -3 = 1.
    – dx_over_dt
    Commented Nov 9, 2020 at 16:52
  • "remainder the same sign as the quotient" --> No, not in C. 7/-3 has the quotient -2 and the 7%-3 has the remainder of 1. Commented Dec 12, 2022 at 8:43
3

Modulus, in modular arithmetic as you're referring, is the value left over or remaining value after arithmetic division. This is commonly known as remainder. % is formally the remainder operator in C / C++. Example:

7 % 3 = 1  // dividend % divisor = remainder

What's left for discussion is how to treat negative inputs to this % operation. Modern C and C++ produce a signed remainder value for this operation where the sign of the result always matches the dividend input without regard to the sign of the divisor input.

3
% is a remainder(leftover after dividend / divisor) NOT modulus. 

You could write your own modulus function using the remainder(%) by the relation

  ((n%m)+m)%m

  where `n` is the given number and `m` is the modulus

Find below the difference between the remainder and modulus values for the range n = (-7,7) and m = 3

n       -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  
------------------------------------------------------------------------- 
%(-m)   -1  0 -2 -1  0 -2 -1  0  1  2  0  1  2  0  1  =>  remainder
% m     -1  0 -2 -1  0 -2 -1  0  1  2  0  1  2  0  1  =>  remainder
mod m    2  0  1  2  0  1  2  0  1  2  0  1  2  0  1  =>  ((n%m)+m)%m
mod(-m) -1  0 -2 -1  0 -2 -1  0 -2 -1  0 -2 -1  0 -2  =>  ((n%m)+m)%m

Tips to remember:

n%(-m)   = +(remainder)
(-n)%(m) = -(remainder)
sign of 'm' doesn't matter

n mod (-m) = -(result)
(-n) mod m = +(result)
sign of 'n' doesn't matter

For +ve 'n' and '%(-m)' or '%m' or 'mod m' gives the same remainder
-4

In mathematics the result of the modulo operation is the remainder of the Euclidean division. However, other conventions are possible. Computers and calculators have various ways of storing and representing numbers; thus their definition of the modulo operation depends on the programming language and/or the underlying hardware.

 7 modulo  3 -->  1  
 7 modulo -3 --> -2 
-7 modulo  3 -->  2  
-7 modulo -3 --> -1 
3
  • 2
    The wiki Euclidean division asserts 0 ≤ r < |b| which means the remainder aka "modulo operation." is always at least 0. What definition are you using that results in -2 and -1? Commented Jun 14, 2016 at 18:51
  • sir, i dont no but i just google 7 modulo -3 --> -2 .and.-7 modulo -3 --> -1 please explain sir why this happened Commented Jun 15, 2016 at 7:17
  • 1
    Google uses a different definition of modulo (signed modulo?) than Wiki Euclidean division (as described by Raymond T. Boute). This discusses the differences more. Moral of the story: a%b and a modulo b have the same meaning when a,b are positive. C99 defines % precisely with negative values. C calls this "remainder'. "Modulo" has various definitions in the world concerning negative values. C spec only uses "modulo" in the context of positive numbers. Commented Jun 15, 2016 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.