1%2 - Modulus Question

I am getting differing opinions on the answer to 1%2. Some say you should round and the answer is 1 and some say the answer is 0 because modulus only deals with integer values in C++. I used a sample program to test this in the compiler and it seems to be rounding the answer because it says the value is 1.

Anyone out there have insight and can help us settle this? .


There is no rounding to be done.

% gives you the remainder of integer division. 1%2 is 1 because 1 divided by 2 is 0 remainder 1.

% can only be used with ?nonnegative? integer types.

Pretty much it works like so:

if:
A / B = d
A % B = r

then:
(d * B) + r = A
a%b behaves like follows (i consider a and b to be greater than 0) :

Compose a into a = k*b + rest, where k - the largets integer that can appear here. The rest is the answer (it will be strictly less than b and greater or equal 0).
Last edited on
Topic archived. No new replies allowed.