MOD function help
I am confused a bit with a mod function, cause my calculator, the C % mod operator and some other calculator are all giving me different results for some numbers.
Can any1 please explain how it works. As far as i understand mod function returns the remainder after division of first number by second.
So if i divide 3/2=1.5, where the remainder is 5, shouldnt i get 5 as output? Why am i getting 1?
I know it is a pretty stupid question, but i've never worked with mod before (only heard about it), thus i need to know for sure how it works for real.
3/2 performs integer division, and the decimal is truncated.
In addition, a modulus gives the remainder. Which is 1. 2 goes into 3 one time, with one left over.
The remainder isn't 5! That is called the fractional part of the number.
Modulo is only defined for integers. It takes the remainder after division.
What is the remainder?
99 / 4 == 24 with 3 left over.
The number left over at the bottom is 3; the remainder is 3.
More "programmatically", x%y is also defined as
x - (x / y * y)
for any two integers x and y. Note that x/y does integer division and kills off the fractional part.