1%2 - Modulus Question

Dec 13, 2009 at 1:48am
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? .


Dec 13, 2009 at 2:07am
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
Dec 13, 2009 at 2:08am
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 Dec 13, 2009 at 2:09am
Topic archived. No new replies allowed.