Java Math floorMod() Method
Last Updated :
13 May, 2025
The Math.floorMod() is a built-in math function in Java that returns the floor modulus of the integer arguments passed to it. Therefore, floor modulus is (a - (floorDiv(a, b) * b)), has the same sign as the divisor b, and is in the range of -abs(b) < result < +abs(b). The relationship between floorDiv() and floorMod() is:
floorDiv(a, b) * b + floorMod(a, b) == a
The difference in results between floorMod and the % operator is due to the difference between floorDiv() which returns the integer less than or equal to the quotient, and the "/ " operator that returns the integer closest to zero.
Syntax of floorMod() Method
public static int floorMod(int a, int b)
public static long floorMod(long a, long b)
- Parameters:
- a: the dividend (number to be divided)
- b: the divisor
- Return Value: It returns the remainder after division, where the result always carries the sign of the divisor
- Exception: If the divisor is 0, it throws an ArithmeticException.
Examples of Java Math floorMod() Method
Example 1: In this example, we are going see how floorMod() method handles different sign cases.
Java
// Java program to demonstrate Math.floorMod()
import java.lang.Math;
public class Geeks {
public static void main(String[] args) {
int a = 25;
int b = 5;
System.out.println("Result 1: " + Math.floorMod(a, b));
int c = 123;
int d = 50;
System.out.println("Result 2: " + Math.floorMod(c, d));
int e = 123;
int f = -50;
System.out.println("Result 3: " + Math.floorMod(e, f));
int g = -123;
int h = 50;
System.out.println("Result 4: " + Math.floorMod(g, h));
}
}
OutputResult 1: 0
Result 2: 23
Result 3: -27
Result 4: 27
Explanation: In this example,
- In the first case, we start with 25 mod 5 and for this, the remainder is 0.
- In the second case, we use 123 mod 50. So, 123 / 50 = 2 with a remainder of 23.
- In the third case, we use 123 mod -50. This returns -27 because it follows the negative sign of the divisor.
- In the fourth example, we use -123 mod 50. The result is 27 because it follows the divisor's sign.
Example 2: In this example, we will perform the division by zero case.
Java
// Java program to show ArithmeticException with floorMod()
import java.lang.Math;
public class Geeks {
public static void main(String[] args) {
int x = 200;
int y = 0;
System.out.println("Result: "
+ Math.floorMod(x, y));
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
Explanation: In this example, we try to divide 200 by 0, which is not allowed in Java. So the output of the program throws an ArithmeticException.
When to Use floorMod() Instead of %
We should use floorMod() when:
- We want the result to match the sign of the divisor.
- We are dealing with negative numbers and want consistent behavior.
- We are implementing modular arithmetic such as clock or calendar calculations.
Similar Reads
Math floorMod() Method in Java The Math.floorMod() is a built-in math function in Java that returns the floor modulus of the integer arguments passed to it. Therefore, floor modulus is (a - (floorDiv(a, b) * b)), has the same sign as the divisor b, and is in the range of -abs(b) < result < +abs(b). The relationship between
3 min read
Math floorDiv() method in Java The Math.floorDiv() is a built-in math function in Java that returns the largest (closest to negative infinity) integer value that is less than or equal to the algebraic quotient of two numbers. As floorDiv() is static, object creation is not required. The floorDiv() method performs division that ro
3 min read
Java Math decrementExact() method The java.strictmath.lang.decrementExact() is a built-in function in java which returns the argument decremented by one, throwing an exception if the result overflows the specified datatype either long or int depending on which data type has been used on the method argument. Since this is decrement,
2 min read
Java Math incrementExact() method The incrementExact() is a built-in function in Java provided by the Math class, which returns the argument incremented by one. It throws an exception if the result overflows the specified datatype, either long or int, depending on which data type has been used on the method argument.This method is h
2 min read
Java floor() method with Examples The java.lang.Math.floor() returns the double value that is less than or equal to the argument and is equal to the nearest mathematical integer. Note: If the argument is Integer, then the result is Integer.If the argument is NaN or an infinity or positive zero or negative zero, then the result is th
2 min read
Java Guava | floorPowerOfTwo() method IntMath Class The floorPowerOfTwo() method of Guava's IntMath class accepts a parameter and returns the largest power of two less than the value passed in the parameter. This is equivalent to: checkedPow(2, log2(x, FLOOR)). Syntax : public static int floorPowerOfTwo(int x) Parameters: This method accepts a single
2 min read