The problem here is that in Python the % operator returns the modulus and in Java it returns the remainder. These functions give the same values for positive arguments, but the modulus always returns positive results for negative input, whereas the remainder may give negative results. There's some more information about it in this question.

You can find the positive value by doing this:

int i = (((-1 % 2) + 2) % 2)

or this:

int i = -1 % 2;
if (i<0) i += 2;

(obviously -1 or 2 can be whatever you want the numerator or denominator to be)

Answer from andrewmu on Stack Overflow
Discussions

Trying to understand modulus with negative numbers. Can't seem to grasp negative numbers. Can someone help?
If i%n is j, then (i+1) will either be j+1 or 0 (if j == n-1). Similarly, (i-1) % n will either be j-1 or n-1 (if j==0). i | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | i%3 | 2 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 1 If -1%3 were 1, there'd be a break in the pattern when going from -1 to 0. PS: Note that there is a not-insignificant number of languages where -1%3 is -1, which does introduce a break in the pattern, but at least it's still an ascending sequence until it wraps it around. More on reddit.com
๐ŸŒ r/computerscience
16
8
June 21, 2024
Mod with negative numbers gives a negative result in Java and C - Stack Overflow
Does either ANSI C or ISO C specify ... negative numbers, Why is the behavior of the modulo operator (%) different between C and Ruby for negative integers? ... C89 has this text: "If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator." C99 and later have made quotient and modulus perfectly ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Java Tip: modulo with negative numbers
Came across this issue of running a modulo command on a negative number when working on an extension. Java does not handle it correctly. 11 % 7 = 4 >> correct -11 % 7 = -4 >> wrong instead use: Math.floorMod(-11,7) = 3 >> correct ref More on community.appinventor.mit.edu
๐ŸŒ community.appinventor.mit.edu
0
2
March 28, 2020
modulo - Best way to make Java's modulus behave like it should with negative numbers? - Stack Overflow
There's no "right" modulus behaviour when dealing with negative numbers - a lot of languages do it this way, a lot of languages do it different, and a few languages do something completely different. At least the first two have their pros and cons. ... it is. but the title of that question should be renamed. i wouldn't click that question if i was searching for this one because i already know how java modulus works. ... I just renamed it to that from "Why is -13 % 64 = 51?", which would never in ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
March 17, 2025
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ modulus-on-negative-numbers
Modulus on Negative Numbers - GeeksforGeeks
July 23, 2025 - Thus, the C/C++ code gives 2 as remainder because it uses truncate divison to find modulus. Let's see the result of -7 mod -5 in different programming languages: ... #include <iostream> using namespace std; int main() { // a and b both negative int a = -7, b = -5; cout << a % b; return 0; } ... #include <stdio.h> int main() { // a and b both negative int a = -7, b = -5; printf("%d", a % b); return 0; } ... /*package whatever //do not write package name here */ import java.io.*; class GFG { public static void main (String[] args) { int a=-7,b=-5; System.out.println(a%b); } } // This code is contributed by ksrikanth0498.
๐ŸŒ
Reddit
reddit.com โ€บ r/computerscience โ€บ trying to understand modulus with negative numbers. can't seem to grasp negative numbers. can someone help?
r/computerscience on Reddit: Trying to understand modulus with negative numbers. Can't seem to grasp negative numbers. Can someone help?
June 21, 2024 -

In the below examples, the modulus with the positive integers makes sense. I've almost been programmed my whole life with division and remainders. However, the negative numbers don't make sense. I've looked at difference formulas, but I can't seem to make them work in my head or on paper. (Using the Window calculator for results)

-4 mod 3 = 2 but 4 mod 3 = 1
-5 mod 3 = 1 but 5 mod 3 = 2
๐ŸŒ
Medium
medium.com โ€บ @jihoo.kang.kor โ€บ java-how-the-modulo-operator-is-executed-for-negative-numbers-in-java-7c4888157390
[JAVA] How the modulo operator is executed for negative numbers in JAVA? | by Jihoo Kang | Medium
November 20, 2023 - My expectation was 11/-3 = -4 and -11/3 = -4 since the rounding result of -3.66667 is -4. However, the result is different, and thus JAVA might apply the floor() method without considering any sign of numbers.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ the modulo operator in java
The Modulo Operator in Java | Baeldung
October 16, 2019 - Using the modulo operator, we prevent writeIndex from falling out of the boundaries of the array; therefore, weโ€™ll never get an ArrayIndexOutOfBoundsException. However, once we insert more than QUEUE_CAPACITY items, the next item will overwrite the first. In cases where the dividend is negative, Java returns a negative remainder:
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 401943 โ€บ java โ€บ isn-mod
% isn't really mod, is it? (Beginning Java forum at Coderanch)
January 3, 2006 - Originally posted by Adam Price: ... java always returns the remainder instead of modular arithmetic. For negative numbers, java returns the greatest negative congruent element, and for positive numbers, java returns the smallest positive congruent element....
Find elsewhere
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ mod of negative numbers in java
Mod of Negative Numbers in Java | Delft Stack
December 10, 2010 - The % operator is the conventional choice for modulus calculations in Java. It determines the remainder when one number is divided by another. While it performs admirably in many scenarios, it may yield unexpected results when dealing with negative ...
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-manage-modulo-with-negative-numbers-431119
How to manage modulo with negative numbers | LabEx
December 18, 2024 - By mastering these nuanced scenarios, developers can write more robust and predictable code when working with modulo operations involving negative numbers. public class ModuloOptimization { // Faster modulo for power of 2 divisors public static int fastModulo(int number, int divisor) { return number & (divisor - 1); } public static void main(String[] args) { // Demonstrates bitwise modulo optimization System.out.println("8 % 4 = " + (8 % 4)); // Standard System.out.println("Bitwise: " + fastModulo(8, 4)); // Optimized } }
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ java-modulus-negative
Understanding Java Modulus with Negative Numbers โ€” javaspring.net
January 18, 2023 - In Java, the modulus operator with negative numbers behaves in a way that the sign of the result is the same as the sign of the dividend. Understanding this behavior is crucial when using the modulus operator in your programs.
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ understanding-modulus-calculations-with-negative-numbers-in-java
Understanding Modulus Calculations with Negative Numbers in Java - CodingTechRoom
3 weeks ago - ... public class ModulusExample ...intln("Modulus result: " + result); // Output: 51 } } In Java, the sign of the result of a modulus operation is the same as the sign of the dividend (the number being divided)....
Top answer
1 of 4
6

The % operator is treated as a remainder operator, so the sign of the result is the same as that of the dividend.

If you want a modulo function, you can do something like this:

int mod(int a, int b)
{
    int ret = a % b;
    if (ret < 0)
        ret += b;
    return ret;
}
2 of 4
2

Why is this happening?

It's defined that way in C. % is the remainder operator. Per 6.5.5 Multiplicative Operators, paragraph 6 of the C Standard:

When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a ; otherwise, the behavior of both a/b and a%b is undefined.

It's also defined that way in Java. Per 15.17.3. Remainder Operator % of the Java 8 specification:

The binary % operator is said to yield the remainder of its operands from an implied division; the left-hand operand is the dividend and the right-hand operand is the divisor.

In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands.

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.

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).

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.

So the remainder - the result of the % operator - must have the same sign as the dividend - the first value, before the % operator.

๐ŸŒ
Torstencurdt
torstencurdt.com โ€บ tech โ€บ posts โ€บ modulo-of-negative-numbers
Modulo of Negative Numbers
February 21, 2023 - Only two languages take a different stance: Dart and in particular Zig, which distinguishes both cases as @rem(a,b) and @mod(a,b) and errors out on a negative divisor. So if you use the modulo operator to ensure correct bounds for accessing a collection, beware that some languages need a little more diligence. A simple and efficient way is to check the sign. int mod(a, b) { c = a % b return (c < 0) ? c + b : c } As another option, you could also apply the modulo twice. ... Another pitfall to watch out for is when testing whether a number is odd or even using the modulo operator.
๐ŸŒ
MIT App Inventor
community.appinventor.mit.edu โ€บ open source development โ€บ extension development
Java Tip: modulo with negative numbers - Extension Development - MIT App Inventor Community
March 28, 2020 - Came across this issue of running a modulo command on a negative number when working on an extension. Java does not handle it correctly. 11 % 7 = 4 >> correct -11 % 7 = -4 >> wrong instead use: Math.floorMod(-11,7) = 3 >> correct ref
๐ŸŒ
Omni Calculator
omnicalculator.com โ€บ math โ€บ modulo-of-negative-numbers
Modulo Operations with Negative Numbers
November 14, 2023 - In most popular programming languages (such as Java, C, C++, Python), there are separate functions that can calculate the modulus according to both these definitions. There are also languages where only one of these operations is available. In case of doubt, consult the documentation before writing any code. Now, we'll discuss how these two approaches work depending on whether the dividend or the divisor (or both) is negative...
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ java
What is Modulo/Modulus/Remainder Operator In Java?
July 23, 2025 - For instance, 5 % 2 would return ... lead to confusion. In Java, the modulo operator is used with the following syntax: operand1 % operand2....
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ best-way-to-make-java-s-modulus-behave-like-it-should-with-negative-numbers
Best Way to Fix Java Modulus with Negative Numbers: Make It Behave Correctly โ€” javathinking.com
For example, -10 / 3 in Java is -3 (truncated towards zero), so the remainder is -10 - (3 * -3) = -1. Modulus: The result of division where the quotient is truncated towards negative infinity.