7

How do I perform a mod operation between two integers in C++?

1
  • 5
    And.. click here if you don't mind stackoverflow being the ultimate resource, even for easy questions. Commented Feb 4, 2013 at 20:20

6 Answers 6

16

C++ has the % operator, occasionally and misleadingly named "the modulus" operator. In particular the STL has the modulus<> functor in the <functional> header. That's not the mathematical modulus operator, mind you, because in modulus arithmetics a mod b by definition evaluates to a non-negative value for any value of a and any positive value of b. In C++ the sign of the result of a % b is implementation-defined if either of the arguments is negative. So, we would more appropriately name the % operator the remainder operator.

That said, if you truly want the mathematical modulus operator then you can define a function to do just that:

template<typename V> V mod(const V& a, const V& b) { return (a % b + b) % b; } 

So long as b is a positive value a call to the above function will yield a non-negative result.

Sign up to request clarification or add additional context in comments.

3 Comments

No one else mentions how % is essentially a remainder operator in C++, and fail to provide a modulus implementation that wraps around properly given a negative input value for a. This is the best answer.
This is the only correct answer. If you want remainder, not modulo, use %
This has not been implementation defined since CWG issue 614 was resolved. The current behavior is that (a / b) * b + a % b == a
13

In c++, use % operator

More Help

Comments

10

As the other answers have stated, you can use the C++ % operator. But be aware that there's a wrinkle no one has mentioned yet: in the expression a % b, what if a is negative? Should the result of this operation be positive or negative? The C++ standard leaves this up to the implementation. So if you want to handle negative inputs portably, you should probably do something like r = abs(a) % b, then fix up the sign of r to match your requirements.

1 Comment

That's assuming you want the implied div operation to be round-towards-zero. If you want round-towards-negative-infinity, then you'll want r = (unsigned(a) + offset * b) % b, where offset is big enough for a + offset * b to always be positive.
9

Like this: x=y%z

8 Comments

Except that the result is negative for a negative dividend.
@Potatocorn maybe, maybe not. That's implementation-defined.
@wilhelmtell: 5.6/4: "(a/b)*b + a%b is equal to a", so round-toward-zero (the overwhelmingly popular implementation, for better or worse) implies that, and it is mandated by C++0x.
Round towards zero is mandated by c++0x, but not c++03. In both standards, your formula must hold true, but the sign of a%b depends on how integral division is implemented. The sign is well defined as non-negative only if a and b are both non-negative.
In any case, it won't map negative integers onto a en.wikipedia.org/wiki/Modular_arithmetic modular arithmetic ring. If it does, you are very lucky and your program is very unportable.
|
0

Using the modulus % operator :

int modulus_a_b = a % b; 

4 Comments

a and b are integers... so why double? modulus_a_b should be the same type as a and b.
@Mike: well, a % b will be either int, or else the same type as at least one of a and b. So you have a few choices for the type of modulus_a_b, depending on context :-)
@Steve: But only one of those choices is the type that the % operator returns. All the others imply a typecast, er, static_cast<>. double is certainly one of the latter. Also, using double means using the slowest math available (unless there's a long double type, dog forbid)...
The reason I said a lot of choice is because for instance the result of short % short is an int, not a short, but depending on context it probably makes more sense to use it as a short. So there's a conflict between "the same type as a and b" vs "avoiding an implicit conversion".
0

if you use double variable, you should use;

double x; double y; double result = fmod(x, y); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.