SimpleRookie
SimpleRookie

Reputation: 305

What does % mean in C#?

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

Upvotes: 3

Views: 3134

Answers (7)

Lajos Arpad
Lajos Arpad

Reputation: 76414

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.

Upvotes: 3

Mr Lister
Mr Lister

Reputation: 46539

See MSDN:

Upvotes: 7

Darren
Darren

Reputation: 70718

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)

Upvotes: 3

MarcinJuraszek
MarcinJuraszek

Reputation: 125610

% Operator (C# Reference)

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

Upvotes: 12

Reinard
Reinard

Reputation: 3654

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

Upvotes: 3

Mathew Thompson
Mathew Thompson

Reputation: 56429

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

example:

int remainder = 10 % 3 //remainder is 1

Upvotes: 4

PVitt
PVitt

Reputation: 11740

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.

Upvotes: 4

Related Questions