# How does java do modulus calculations with negative numbers?

Am I doing modulus wrong? Because in Java -13 % 64 is supposed to evaluate to -13 but I get 51.

Am I forgetting something very basic about the modulus operator? [duplicate]

This question already has an answer here: How does java do modulus calculations with negative numbers? 11 answers Mod in Java produces negative numbers 3 answers is something wrong with my

Modulus to account for negative numbers

I’m trying to write a code for modulus, but when I do it for negative numbers I don’t get the right result, my code: double mod (double X, double Y) { double result = X; if (X>0){ do { result = res

How does Java calculate negative numbers?

I use the ~ operation for bit manipulation, and I’m just wondering how Java calculates the negative number? I checked the Java documentation: The unary bitwise complement operator ~ inverts a bit

how to generate positive and negative numbers in Java [duplicate]

Possible Duplicate: How to generate random positive and negative numbers in java Hello I am trying to create a method in Java to create negative and positive values in Java. the problem is that I do

How do I Turn negative numbers red?

I am working with a dataset, and will be returning both positive and negative currency figures. I already have a parenthesis around it. How do I get only the negative numbers in the gridview to show u

Best way to make Java’s modulus behave like it should with negative numbers?

In java when you do a % b If a is negative, it will return a negative result, instead of wrapping around to b like it should. What’s the best way to fix this? Only way I can think is a < 0 ? b + a

How does modulus work exactly? [duplicate]

This question already has an answer here: How Does Modulus Divison Work 15 answers I know what modulus (%) does, it gives you the remainder. But how does it do that? I saw that it uses the form

Modulus with negative numbers in C++

I have been writing a program for the following recurrence relation: An = 5An-1 – 2An-2 – An-3 + An-4 The output should be the Answer modulus 10^9 + 7.. I wrote a brute force approach for this one as

How do you recursively count the number of negative numbers in an array (Java)?

I need to use this method: public static int countNegative(double[] numbers, int count){ } to count the number of negative numbers in a double array. I could easily do it if I could include a 3rd par

modulus of negative numbers in Python

23 % -5 = -2 23 % 5 = 3 Can someone explain to me how I can understand this because I have an exam tomorrow. I want to say its because -5 * -5 =25 then 25 -2 = 23 which is how they get the 23. Is thi

Since “mathematically” both are correct:

```-13 = -13 (in modulus 64)
-13 = 51 (in modulus 64)
```

One of the options had to be chosen by Java language developers and it was decided that:

…, the sign of the result equals the sign of the dividend.

Says it in Java specs:

http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.17.3

Both definitions of modulus of negative numbers are in use – some languages use one definition and some the other.

If you want to get a negative number for negative inputs then you can use this:

```int r = x % n;
if (r > 0 && x < 0)
{
r -= n;
}
```

Likewise if you were using a language that returns a negative number on a negative input and you would prefer positive:

```int r = x % n;
if (r < 0)
{
r += n;
}
```

Modulo arithmetic with negative operands is defined by the language designer, who might leave it to the language implementation, who might defer the definition to the CPU architecture.

I wasn’t able to find a Java language definition.
Thanks Ishtar, Java Language Specification for the Remainder Operator % says that the sign of the result is the same as the sign of the numerator.

x = x + m = x – m in modulus m.
so -13 = -13 + 64 in modulus 64 and -13 = 51 in modulus 64.
assume Z = X * d + r, if 0 < r < X then in division Z/X we call r the remainder.
Z % X returns the remainder of Z/X.

you can use

```(x % n) - (x < 0 ? n : 0);
```

It says, that in Java the sign on modulo operation is the same as that of dividend. and since we’re talking about the rest of the division operation is just fine, that it returns -13 in your case, since -13/64 = 0. -13-0 = -13.

EDIT: Sorry, misunderstood your question…You’re right, java should give -13. Can you provide more surrounding code?

To overcome this, you could add 64 (or whatever your modulus base is) to the negative value until it is positive

```int k = -13;
int modbase = 64;

while (k < 0) {
k += modbase;
}

int result = k % modbase;
```

The result will still be in the same equivalence class.

The mod function is defined as the amount by which a number exceeds the largest integer multiple of the divisor that is not greater than that number. So in your case of

```-13 % 64
```

the largest integer multiple of 64 that does not exceed -13 is -64. Now, when you subtract -13 from -64 it equals 51 -13 – (-64) = -13 + 64 = 51

Your result is wrong for Java. Please provide some context how you arrived at it (your program, implementation and version of Java).

From the Java Language Specification

15.17.3 Remainder Operator %

[…]

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.

15.17.2 Division Operator /

[…]

Integer division rounds toward 0.

Since / is rounded towards zero (resulting in zero), the result of % should be negative in this case.

Are you sure you are working in Java? ’cause Java gives -13 % 64 = -13 as expected. The sign of dividend!

I don’t think Java returns 51 in this case. I am running Java 8 on a Mac and I get:

```-13 % 64 = -13
```

Program:

```public class Test {
public static void main(String[] args) {
int i = -13;
int j = 64;
System.out.println(i % j);
}
}
```

In my version of Java JDK 1.8.0_05 -13%64=-13

you could try -13-(int(-13/64)) in other words do division cast to an integer to get rid of the fraction part then subtract from numerator So numerator-(int(numerator/denominator)) should give the correct remainder & sign

In Java latest versions you get -13%64 = -13. The answer will always have sign of numerator.

According to section 15.17.3 of the JLS, “The remainder operation for operands that are integers after binary numeric promotion produces a result value such that (a/b)*b+(a%b) is equal to a. This identity holds even in the special case that the dividend is the negative integer of largest possible magnitude for its type and the divisor is -1 (the remainder is 0).”

Hope that helps.