1
    x=-10 % -4;
    System.out.println("-10% -4 : "+x); //-2 second row

The output '-2' why in the answer is a negative value?

fedup
  • 1,209
  • 11
  • 26
  • I also do not see anything wrong with the result. What are you expecting? – Spangle Nov 05 '19 at 04:31
  • Possible duplicate of [Why doesn't the % operator work the same in Python and Java?](https://stackoverflow.com/q/8237469/5221149) – Andreas Nov 05 '19 at 04:33
  • 1
    @Deadpool Because Java has *remainder operator*, not modulo operator, see Java Language Specification, section [15.17.3. **Remainder Operator** `%`](https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.17.3) – Andreas Nov 05 '19 at 04:56
  • Possible duplicate of [Why doesn't the % operator work the same in Python and Java?](https://stackoverflow.com/questions/8237469/why-doesnt-the-operator-work-the-same-in-python-and-java) – phuclv Nov 05 '19 at 05:22

2 Answers2

3

% is remainder division. It is the amount left over after the integer division.

x = -10 / -4; // == 2

and

x = -10 % -4; // == -2

The later can be thought of as -10 divided by -4 (which is 2) with a remainder of -2.

It might be easier to see if both answers were not the same absolute value.

x = -10 / -3; // == 3

and

x = -10 % -3;  == -1
fedup
  • 1,209
  • 11
  • 26
  • 2
    More specifically, with `dividend = -10` and `quotient = -3`, we calculate **`divisor = dividend / quotient`** `= -10 / -3 = 3` *(truncated integer math)* and **`remainder = dividend - divisor * quotient`** `= -10 - 3 * -3 = -10 - -9 = -10 + 9 = -1` – Andreas Nov 05 '19 at 04:45
1

Unfortunately that is the way the Java modulus operator works with negative numbers. If you want only positive remainder values then do a simple conversion like this:

if(x < 0){
    x = x * -1;
}
allkenang
  • 1,511
  • 15
  • 12
  • 3
    *"that is the way the Java modulus operator works"* Except Java doesn't have a *modulus* operator, only a *remainder* operator. --- Reference 1: Java Language Specification, section [15.17.3. Remainder Operator %](https://docs.oracle.com/javase/specs/jls/se13/html/jls-15.html#jls-15.17.3). --- Reference 2: [What's the difference between “mod” and “remainder”?](https://stackoverflow.com/q/13683563/5221149) – Andreas Nov 05 '19 at 04:50
  • If you want only positive values, use [`Math.abs()`](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#abs-int-). – Andreas Nov 05 '19 at 04:54
  • Not sure why I was downvoted for this. The fact that most sites describe the operator '%' as a modulus operator instead of a remainder operator is widely understood and to argue otherwise is a little pedantic imho. – allkenang Nov 05 '19 at 06:21
  • Also go look up the implementation of Math.abs (http://developer.classpath.org/doc/java/lang/Math-source.html). The implementation is pretty similar to what I suggested. public static int abs(int i) 118: { 119: return (i < 0) ? -i : i; 120: } 121: – allkenang Nov 05 '19 at 06:22
  • 1
    It's a pedantic business. The difference between remainder and modulo is the answer to the question. The JLS describes it as a remainder operator, *punto basta.* – user207421 Nov 05 '19 at 07:34
  • Computers are very pedantic, they do exactly what you tell them. If you tell them wrong, they will do it wrong. As a programmer, you need to learn to be exact. *(FYI: I'm not the down-voter)* – Andreas Nov 05 '19 at 09:09
  • 1
    These complainers are both right and wrong. In mathematics both java's "%" and python's "%" are examples of remainder-like operators. Or you can call them modulus operators without being wrong. In math the [remainder is always >= 0](https://math.stackexchange.com/a/801988/552), which is how python's % operator operates. Sadly computer scientists have always felt free to redefine the settled meaning of math terms. So at some point someone rather arbitrarily decided that the way java (and some others) does "%" is called a remainder operator. But that's the convention, so get used to it. – President James K. Polk Nov 05 '19 at 17:21
  • instead of `x = x * -1` it should be better to do `x = -x` – phuclv Nov 17 '19 at 05:01