Math floorMod() Method in Java
Last Updated :
20 Jun, 2018
The java.lang.Math.floorMod() is a built-in math function in java which 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) < t < +abs(b).
Relationship between floorDiv and floorMod is:
floorDiv(a, b) * b + floorMod(a, b) == a
The difference in values between floorMod and the % operator is due to the difference between floorDiv that returns the integer less than or equal to the quotient and the / operator that returns the integer closest to zero.
Syntax :
public static int floorMod(data_type a, data_type b)
Parameters: The function accepts two parameters.
Exception: It throws an ArithmeticException if the divisor is zero.
Return Value: This method returns the floor modulus x – (floorDiv(x, y) * y).
Below programs are used to illustrate the working of java.lang.Math.floorMod() method.
Program 1:
import java.lang.Math;
class Gfg1 {
public static void main(String args[])
{
int a = 25 , b = 5 ;
System.out.println(Math.floorMod(a, b));
int c = 123 , d = 50 ;
System.out.println(Math.floorMod(c, d));
int e = 123 , f = - 50 ;
System.out.println(Math.floorMod(e, f));
int g = - 123 , h = 50 ;
System.out.println(Math.floorMod(g, h));
}
}
|
Program 2:
import java.lang.Math;
class Gfg2 {
public static void main(String args[])
{
int x = 200 ;
int y = 0 ;
System.out.println(Math.floorMod(x, y));
}
}
|
Output:
Runtime Error :
Exception in thread "main" java.lang.ArithmeticException: / by zero
at java.lang.Math.floorDiv(Math.java:1052)
at java.lang.Math.floorMod(Math.java:1139)
at Gfg2.main(File.java:13)
Reference: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-int-int-
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...