What is the difference between System.Math.DivRem()
and the %
operator?
3 Answers
%
gives you the remainder of a division and discards the quotient altogether, while DivRem()
calculates and returns both the quotient and the remainder.
If you're only concerned about the remainder of a division between two integers, use %
:
int remainder = 10 % 3;
Console.WriteLine(remainder); // 1
If you need to know how many times 10 was divided by 3 before having a remainder of 1, use DivRem()
, which returns the quotient and stores the remainder in an out parameter:
int quotient, remainder;
quotient = Math.DivRem(10, 3, out remainder);
Console.WriteLine(quotient); // 3
Console.WriteLine(remainder); // 1

- 700,868
- 160
- 1,392
- 1,356
-
If you only need the quotient of two integers, you can simply do integer division using the `/` operator instead of `%`: `int quotient = 10 / 3;` – BoltClock Aug 08 '11 at 20:59
-
2And in VB.NET the integer division operator is \: `Dim quotient As Integer = 10 \ 3` – BoltClock Aug 08 '11 at 21:06
-
Thanks guys. I may actually work with Decimal.Divide() since I do not want an whole integer returned. – brandeded Aug 09 '11 at 12:56
-
1@mbrownnyc re "*I do not want a whole integer returned*": convert either or both values to a non-integer type *before* dividing: `double result = a / (double)b;` That is the *non-integer* result of a division of two integers. – ToolmakerSteve Apr 08 '17 at 15:42
It is an optimization. Some processors are able to compute both values at the same time. Other processors can't divide in hardware (and must use, very slow, software routines).
In either case (unless you have a smart compiler) you can end up computing the same division twice. Since divisions are never fast, on any processor (even when implemented in hardware), then using Math.DivRem gives the JIT a nice 'hint' to compute the values only once.
iirc Mono does not implement this optimization, and I'm not even sure if MS does.

- 43,413
- 6
- 77
- 174
-
Thanks for explaining the underlying processes. I am coding in c#, and obviously using .NET framework; but it's very good to expand. Hey... I just finished reading this: http://www.infoworld.com/print/169171 – brandeded Aug 09 '11 at 12:54
It could have been an optimization, but unfortunately produces the same IL as a division PLUS a mod operation.
On typical architectures (x86 & friends) the two can be obtained from a single operation, but .NET JIT seems not to optimize that (in my tests).
So the following two are equivalent:
quotient = Math.DivRem(10, 3, out remainder);
VS:
quotient = 10 / 3;
remainder = 10 % 3;
Except the later is more readable.
For the record this ugly option is faster on x86 & x64:
quotient = 10 / 3;
remainder = 10 - (3*quotient);
Hopefully the JIT will improve some day to optimize the two operations into a single one, since it is trivial.

- 149
- 2
- 2