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.

6
  • 1
    Because % is the remainder after the two operands have been divided. Commented Aug 29, 2013 at 7:43
  • @OliCharlesworth since 11/-3 and -11/3 returns the same results, why do I get a different result using % Commented Aug 29, 2013 at 7:45
  • Because r = n - q*d, where n is numerator, d is denominator, q is quotient and r is remainder. Commented Aug 29, 2013 at 7: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. Commented Aug 29, 2013 at 7:48
  • This answer has the logic(formula) behind it : stackoverflow.com/questions/4412179/… Commented Aug 29, 2013 at 7:50

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

Comments

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

Comments

-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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.