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.
public static int floorMod(data_type a, data_type b)
Parameters: The function accepts two parameters.
- a: This refers to the dividend value.
- b: This refers to the divisor value.
The parameters can be of data-type int or long.
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.
0 23 -27 27
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)
- Java.math.BigInteger.modInverse() method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java Math exp() method with Example
- Java Math log() method with example
- Math pow() method in Java with Example
- Java Math incrementExact(int x) method
- Java Math min() method with Examples
- Java Math abs() method with Examples
- Java Math round() method with Example
- Java Math negateExact() method
- Java Math getExponent() method with Example
- Java Math expm1() method with Example
- Math fma() method in Java with Examples
- Java Math ulp() method with Examples
- Java Math cos() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.