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
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ mod of negative numbers in java
Mod of Negative Numbers in Java | Delft Stack
February 15, 2024 - Next, we apply the Math.floorMod method by passing in the dividend and divisor as arguments. This method ensures that the modulus result is always non-negative, addressing the quirks associated with negative numbers and modulus operations.
๐ŸŒ
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 7, 2022 - This post in Stack Overflow explains that JAVA multiplies -1 after any division occurs without considering the sign. This trait is different from other programming languages, which implies that the result of a modulo operation for negative numbers also differs.
๐ŸŒ
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....
๐ŸŒ
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
Find elsewhere
๐ŸŒ
Medium
thomaspoignant.medium.com โ€บ how-to-get-always-a-positive-modulo-remainder-9ac965361ff4
How to always get a positive modulo remainder | by Thomas Poignant | Medium
July 10, 2020 - Do the modulo who returns the negative value. (return -1) Add the value of the modulo (here 4, return 3). Re-do a second modulo, and now we are sure to have a positive value. (return 3) In our specific case, it returns 3 which is equals to Orientation.WEST ยท In Java you can do the same things, but since Java 8 there is a builtin feature in the JVM, so you just have to use Math.floorMod and it is doing it for you.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-how-to-manage-modulo-with-negative-numbers-431119
How to manage modulo with negative numbers | LabEx
Learn advanced Java modulo techniques for handling negative numbers, exploring efficient calculation methods and practical programming strategies for managing remainder operations.
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ java
What is Modulo/Modulus/Remainder Operator In Java?
November 20, 2023 - Modulo operations can sometimes yield unexpected results, particularly with negative numbers. In Java, the result of a modulo operation inherits the sign of the dividend, which can be counterintuitive. For example, -10 % 3 results in -1, not 2.
๐ŸŒ
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.
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ subarray-sums-divisible-by-k โ€บ discuss โ€บ 320139 โ€บ Why-Java-gives-negative-values-when-doing-negative-mod โ€บ 310475
Why Java gives negative values when doing negative mod?
Can you solve this real interview question? Subarray Sums Divisible by K - Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k. A subarray is a contiguous part of an array. Example 1: Input: nums = [4,5,0,-2,-3,1], k = 5 Output: ...
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ java-modulus-modulo
Java Modulus/Modulo: Your Guide to Remainder Operations
February 29, 2024 - If youโ€™re dealing with positive numbers or you want the sign of the result to match the dividend, the modulus operator is the way to go. If youโ€™re working with negative numbers and you want a positive result, floorMod might be a better choice. While the modulus operator in Java is quite straightforward, you might encounter some common errors or obstacles when using it.
๐ŸŒ
javaspring
javaspring.net โ€บ blog โ€บ java-modulus-negative
Understanding Java Modulus with Negative Numbers โ€” javaspring.net
In Java, the modulus operator (%) ... given by the following equation: ... When both a and b are positive, the result of a % b is the non - negative remainder of the division of a by b....
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ mod_in_java_produces_negative_numbers_duplicate
Mod in Java produces negative numbers
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
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
January 22, 2017 - In Java, however, its behavior with negative numbers often surprises developers. Unlike mathematical modulus, which typically returns a non-negative result, Javaโ€™s `%` operator can return negative values when the dividend (the number being divided) is negative.
๐ŸŒ
Java67
java67.com โ€บ 2014 โ€บ 11 โ€บ modulo-or-remainder-operator-in-java.html
How to use Modulo , Modulus, or Remainder Operator in Java? [Example] | Java67
What is mod here? are you using remainder operator in Java. do you mean 39.03/9 ?ReplyDelete ... Technically (n % m) is a remainder operator, but not a modulus operator. There's a difference. For nonnegative n and positive m, the remainder and modulus are the same thing. But for negative n, they are different.
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ java modulo
How to use the Java modulo operator - IONOS
December 18, 2024 - You can also use Java modulo to get the remainder when the dividend or divisor is negative.