0

I have learned a lot from my last question hopefully I don't make the same mistakes again:)

This stems from a previous question. Here is what I THINK I know:

For ints in java (I assume in all languages but I’m asking in JAVA specifically):

1/3 = 0
1%3 = 1

I was stumped as to why i%j = i when i < j and a previous poster explained how this worked and also stated that "First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics...."

Their explanation was perfect for what I needed. However, I was confused by their quote because I was always taught that in mathematics modular == remainder division.

How does one execute modular division in JAVA and are there pitfalls to watch for when trying to use % as a modulus operator?

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
FredCarston
  • 41
  • 1
  • 7
  • 2
    Possible duplicate of [What's the difference between “mod” and “remainder”?](http://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder) – timbre timbre May 15 '16 at 15:18
  • `%` is the arithmetic remainder operator. [This thread](http://stackoverflow.com/questions/2947044/how-do-i-use-modulus-for-float-double) is also interesting. – Darius May 15 '16 at 15:20

2 Answers2

1

You may be confused by the "modulo operator" in arithmetic, which is the same as the % operator in Java and similar languages, I don't think there is such thing as "modular division". The % operator in java will always return the integer remainder from repeated division between two numbers. Just like in arithmetic, (i % j) = i where i < j and i >= 0. The result of the operation is less than j.

SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
1

mathematicaly, modulo is division with remainder.

7 mod 4 = 1 R3

see:

n = a * m + r

The modulo operator in Java (like in most other languages) gives only the remainder part and not i dont know, if it works with negative numbers correct.

In Detail, mathematicaly the modulo is allways positive. That is the differece to the modulo operator in java.

a mod n = b, if there is a number k existing with a = b + kn
                    and 0 <= b < n 

That means, if you take -14 mod 4:

 -14 = b + k * 4    //lets take -3 for k
 -14 = b + -3 * 4
 -14 = b - 12
 -2 = b

that would be wrong (mathematically) becaouse b is negative. so we need to take -4 for k

 -14 = b + -4 * 4
 -14 = b + 16
  2 = b

that is the correct answer. In this case only the sign is the difference, but if you take -15 mod 4 you will get -3 in java and most other languages, but the mathematically correct answer would be 1 (-15 + 16)

using java, you will get the negative values.

Jarlik Stepsto
  • 1,667
  • 13
  • 18
  • modulo is not only the remainder, if you read my answer, i postet 7 mod 4 is 1 R3. the r3 stands for the remainder of 3 – Jarlik Stepsto May 15 '16 at 15:24
  • I know, but the mod operation returns the remainder, not the result of division, so the result is just 3. 7 divided by 4 would be 1 with a remainder of 3. – SamTebbs33 May 15 '16 at 21:14