3

Sorry for a possible repeat question the % symbol doesn't coincide with searchability.

What does % mean? I can't seem to stick this one down.

Example:

rotation = value % MathHelper.TwoPi;

is a specific instance.

But I have found code that uses % more often. Modulus I 'think' it is called, but I am not positive.

Previous Post:

With well thought out awnser

Community
  • 1
  • 1
SimpleRookie
  • 305
  • 1
  • 3
  • 14
  • 5
    According to [Eric Lippert](http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx), most of the current answers are (subtly) wrong - it's remainder, not modulus. – Damien_The_Unbeliever Apr 13 '12 at 10:34
  • Thanks for all the links to helpful articles, folks. – SimpleRookie Apr 13 '12 at 10:37
  • 1
    possible duplicate of [What does the '%' operator mean?](http://stackoverflow.com/questions/3264524/what-does-the-operator-mean) – H H Apr 13 '12 at 12:08
  • Oh, you found a previous post! I hadn't seen that, thank you for finding it. This post can be deleted at mod leisure. – SimpleRookie Apr 13 '12 at 16:47

7 Answers7

12

% Operator (C# Reference)

The % operator computes the remainder after dividing its first operand by its second. All numeric types have predefined remainder operators.

MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
7

See MSDN:

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
4

It is the modulus operator. It computes the remainder after dividing its first operand by its second, e.g.

  1. 5 % 2 = 1
  2. 6 % 2 = 0
  3. 5 % 3 = 2.
PVitt
  • 11,500
  • 5
  • 51
  • 85
4

in C# it means modulus, which is basically a remainder of

example:

int remainder = 10 % 3 //remainder is 1
Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
  • Modulus and remainder are different things. Eric Lippert has very good post about it (http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx) – Nikolay Apr 13 '12 at 10:44
  • If you look at the article, it says that " The % operator is not the canonical modulus operator, it is the remainder operator.", although it is named modulus. So it is called the modulus operator, but it is actually the remainder operator – Mathew Thompson Apr 13 '12 at 10:47
  • Yes, you are right, i just wanted to emphasize that is not not canonical modulus operator. Upvoted you answer back. – Nikolay Apr 13 '12 at 10:50
3

Yes it' the modulus. http://en.wikipedia.org/wiki/Modulus_(algebraic_number_theory)

Reinard
  • 3,624
  • 10
  • 43
  • 61
  • Modulus and remainder are different things. Eric Lippert has very good post about it (http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx) – Nikolay Apr 13 '12 at 10:43
3

It is the modulus operator. It returns the remainder of an integer.

int remainder = 2 % 1; // (remainder variable is assigned to 0) 
int remainder2 = 3 % 2; // (remainder variable is assigned to 1)
Darren
  • 68,902
  • 24
  • 138
  • 144
  • Modulus and remainder are different things. Eric Lippert has very good post about it (http://blogs.msdn.com/b/ericlippert/archive/2011/12/05/what-s-the-difference-remainder-vs-modulus.aspx) – Nikolay Apr 13 '12 at 10:43
  • 1
    @Nikolay It clearly states on MSDN that % is the modulus operator and computes the remainder of an integer which is what I put in my answer: http://msdn.microsoft.com/en-us/library/0w4e0fzs(v=vs.80).aspx Why the downvote? If you have an issue with what is stated on MSDN you should take it up with them :) – Darren Apr 13 '12 at 10:47
  • Yes, you are right, i just wanted to emphasize that is not not canonical modulus operator. Upvoted you answer back. – Nikolay Apr 13 '12 at 10:50
  • It clearly states *in older documentation*. The 2010 and VS11 documentation has removed that error. So at best you could assert that "it was named..." – Damien_The_Unbeliever Apr 13 '12 at 10:51
3

Let's say that

x / y = z,
x, y, z being integers.

There is no guarantee that

z * y = x, because the "/" operator rounds down.

So we must add a remainder to our equation:

z * y = x + r.

z * y = x + r
z * (-y) = - (z * y) = -(x + r) = -x - r

This means that the result of the "%" operator can be negative, which means that the "%" or remainder operator differs from the modulo relation, because the result is not guaranteed to be canonical.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175