# MOD function help

• 04-18-2006
dimaash
MOD function help
Hi everyone.

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.

thanks.
• 04-18-2006
Tonto
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.
• 04-19-2006
jafet
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.
Code:

```    24   ----- 4) 99     8     --     19     16     ---       3```
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.