0

Why do I get 2 and not -2 in the following?

System.out.println(11%-3);

Considering that:

System.out.println(-11%3);

returns -2..

Thanks in advance.

Rollerball
  • 12,618
  • 23
  • 92
  • 161
  • 1
    Because % is the **remainder** after the two operands have been divided. – Oliver Charlesworth Aug 29 '13 at 07:43
  • @OliCharlesworth since 11/-3 and -11/3 returns the same results, why do I get a different result using % – Rollerball Aug 29 '13 at 07:45
  • Because r = n - q*d, where n is numerator, d is denominator, q is quotient and r is remainder. – Oliver Charlesworth Aug 29 '13 at 07:47
  • Because the remainder of the the numerator. If I divide `-3` into `11` the minus is somewhat irrelevant. Whereas if I divide `3` into `-11` then the remaining amount is from the `-11` and so is negative. – Boris the Spider Aug 29 '13 at 07:48
  • This answer has the logic(formula) behind it : http://stackoverflow.com/questions/4412179/best-way-to-make-javas-modulus-behave-like-it-should-with-negative-numbers/4412200#4412200 – blackSmith Aug 29 '13 at 07:50
  • possible duplicate of [How does java do modulus calculations with negative numbers?](http://stackoverflow.com/questions/4403542/how-does-java-do-modulus-calculations-with-negative-numbers) – Oliver Charlesworth Aug 29 '13 at 07:55

5 Answers5

3

Quoting JLS:

The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.


It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor.

(Emphasis mine.)

Additionally, refer to Modulo operation for information about modulo operators in various programming languages.

devnull
  • 118,548
  • 33
  • 236
  • 227
1

Rules are specified in Jls 15.17.3 Remainder Operator % -

It follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor.

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

Becaue the sign of the result equals the sign of the dividend. http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.17.3

KocsisLaci
  • 59
  • 3
0

First, "remainder()" means: if a=b*c+r, a,b,c,r are integers. Then a%b = c

But, how to choose c, different Programming Language has different implementation.

To C and Java, remainder was calculated as truncating:

-11 % 3 = 3*(-3)+(-2) 11 % (-3) = (-3)*(-3)+2

The key point is why choose -3? Because:

truncate(-11/3)=truncate(-3.66)=-3

"truncate()" get the int part close to 0

In other programming language,like Python, use floor division. Which means that in Python: -11 % 3 = 3 * (floor(-11/3))+2

Please refer to: http://python-history.blogspot.sg/2010/08/why-pythons-integer-division-floors.html

James Liu
  • 269
  • 1
  • 11
-3

This is a math problem.

1st, -11%3 and 11%-3 are the same.

2nd, if you want a positive number, please remove 'minus'. try 11%3 or -11%-3

53iScott
  • 827
  • 1
  • 13
  • 18