Java Java API
Class:
java.lang.Math
Methods:
public static int floorMod(int x,
int y)
Returns the floor modulus of the
int
arguments.
The floor modulus is
x - (floorDiv(x, y) * y)
, has the same sign as the divisor
y
, and is in the range of
-abs(y) < r < +abs(y)
.
The relationship between
floorDiv
and
floorMod
is such that:
-
floorDiv(x, y) * y + floorMod(x, y) == x
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.
If the signs of arguments are unknown and a positive modulus is needed it can be computed as
(floorMod(x, y) + abs(y)) % abs(y) .
- Parameters:
-
x
- the dividend
-
y
- the divisor
- Returns:
- the floor modulus
x - (floorDiv(x, y) * y)
- Throws:
-
java.lang.ArithmeticException,ArithmeticException
- if the divisor
y
is zero
- Since:
- 1.8
public static int floorMod(long x,
int y)
Returns the floor modulus of the
long
and
int
arguments.
The floor modulus is
x - (floorDiv(x, y) * y)
, has the same sign as the divisor
y
, and is in the range of
-abs(y) < r < +abs(y)
.
The relationship between
floorDiv
and
floorMod
is such that:
-
floorDiv(x, y) * y + floorMod(x, y) == x
- Parameters:
-
x
- the dividend
-
y
- the divisor
- Returns:
- the floor modulus
x - (floorDiv(x, y) * y)
- Throws:
-
java.lang.ArithmeticException,ArithmeticException
- if the divisor
y
is zero
- Since:
- 9
public static long floorMod(long x,
long y)
Returns the floor modulus of the
long
arguments.
The floor modulus is
x - (floorDiv(x, y) * y)
, has the same sign as the divisor
y
, and is in the range of
-abs(y) < r < +abs(y)
.
The relationship between
floorDiv
and
floorMod
is such that:
-
floorDiv(x, y) * y + floorMod(x, y) == x
- Parameters:
-
x
- the dividend
-
y
- the divisor
- Returns:
- the floor modulus
x - (floorDiv(x, y) * y)
- Throws:
-
java.lang.ArithmeticException,ArithmeticException
- if the divisor
y
is zero
- Since:
- 1.8
Examples
 package com.logicbig.example.math;
public class FloorModExample {
public static void main(String... args) { findFloorMod(61, 5); findFloorMod(61, -5); findFloorMod(-25, Integer.MIN_VALUE); findFloorMod(250, 0); findFloorMod(Integer.MAX_VALUE, -4); }
public static void findFloorMod(int x, int y) { System.out.println("-----------"); try { System.out.printf("%s %% %s = %s%n", x, y, x % y); System.out.printf("Math.floorMod(%s, %s) = %s%n", x, y, Math.floorMod(x, y)); } catch (ArithmeticException e) { System.out.println("error: " + e); } } }
Output----------- 61 % 5 = 1 Math.floorMod(61, 5) = 1 ----------- 61 % -5 = 1 Math.floorMod(61, -5) = -4 ----------- -25 % -2147483648 = -25 Math.floorMod(-25, -2147483648) = -25 ----------- error: java.lang.ArithmeticException: / by zero ----------- 2147483647 % -4 = 3 Math.floorMod(2147483647, -4) = -1
 package com.logicbig.example.math;
public class FloorModExample2 {
public static void main(String... args) { findFloorMod(35, -2); findFloorMod(Long.MIN_VALUE, -4); findFloorMod(-15, 3); findFloorMod(120, 0); findFloorMod(Long.MAX_VALUE, -5); }
public static void findFloorMod(long x, long y) { System.out.println("-----------"); try { System.out.printf("%s %% %s = %s%n", x, y, x % y); System.out.printf("Math.floorMod(%s, %s) = %s%n", x, y, Math.floorMod(x, y)); } catch (ArithmeticException e) { System.out.println("error: " + e); } }
}
Output----------- 35 % -2 = 1 Math.floorMod(35, -2) = -1 ----------- -9223372036854775808 % -4 = 0 Math.floorMod(-9223372036854775808, -4) = 0 ----------- -15 % 3 = 0 Math.floorMod(-15, 3) = 0 ----------- error: java.lang.ArithmeticException: / by zero ----------- 9223372036854775807 % -5 = 2 Math.floorMod(9223372036854775807, -5) = -3
 package com.logicbig.example.math;
public class FloorModExample3 {
public static void main(String... args) { System.out.println("Prove that floorDiv(x, y) * y + floorMod(x, y) == x"); values(23, 3); values(31, -15); values(-15, Long.MIN_VALUE); values(300, 0); values(Long.MAX_VALUE, -4); }
public static void values(long x, long y) {
try { System.out.println("-----------"); long floorDivision = Math.floorDiv(x, y); long floorModulus = Math.floorMod(x, y); System.out.printf("floorDiv(%s,%s) x %s + floorMod(%s,%s) == %s%n", x, y, y, x, y, x); System.out.printf("==> %s == %s%n", floorDivision * y + floorModulus, x); } catch (ArithmeticException e) { System.out.println("error: " + e); }
} }
OutputProve that floorDiv(x, y) * y + floorMod(x, y) == x ----------- floorDiv(23,3) x 3 + floorMod(23,3) == 23 ==> 23 == 23 ----------- floorDiv(31,-15) x -15 + floorMod(31,-15) == 31 ==> 31 == 31 ----------- floorDiv(-15,-9223372036854775808) x -9223372036854775808 + floorMod(-15,-9223372036854775808) == -15 ==> -15 == -15 ----------- error: java.lang.ArithmeticException: / by zero ----------- floorDiv(9223372036854775807,-4) x -4 + floorMod(9223372036854775807,-4) == 9223372036854775807 ==> 9223372036854775807 == 9223372036854775807
|
|