Close

Java - Math.floorDiv() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Methods:

public static int floorDiv(int x,
                           int y)

Returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient. There is one special case, if the dividend is the Integer.MIN_VALUE and the divisor is -1 , then integer overflow occurs and the result is equal to Integer.MIN_VALUE .

Normal integer division operates under the round to zero rounding mode (truncation). This operation instead acts under the round toward negative infinity (floor) rounding mode. The floor rounding mode gives different results from truncation when the exact result is negative.

  • If the signs of the arguments are the same, the results of floorDiv and the / operator are the same.
    For example, floorDiv(4, 3) == 1 and (4 / 3) == 1 .
  • If the signs of the arguments are different, the quotient is negative and floorDiv returns the integer less than or equal to the quotient and the / operator returns the integer closest to zero.
    For example, floorDiv(-4, 3) == -2 , whereas (-4 / 3) == -1 .
Parameters:
x - the dividend
y - the divisor
Returns:
the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.
Throws:
java.lang.ArithmeticException,ArithmeticException if the divisor y is zero
Since:
1.8



public static long floorDiv(long x,
                            int y)

Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient. There is one special case, if the dividend is the Long.MIN_VALUE and the divisor is -1 , then integer overflow occurs and the result is equal to Long.MIN_VALUE .

Normal integer division operates under the round to zero rounding mode (truncation). This operation instead acts under the round toward negative infinity (floor) rounding mode. The floor rounding mode gives different results from truncation when the exact result is negative.

Parameters:
x - the dividend
y - the divisor
Returns:
the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.
Throws:
java.lang.ArithmeticException,ArithmeticException - if the divisor y is zero
Since:
9



public static long floorDiv(long x,
                            long y)

Returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient. There is one special case, if the dividend is the Long.MIN_VALUE and the divisor is -1 , then integer overflow occurs and the result is equal to Long.MIN_VALUE .

Normal integer division operates under the round to zero rounding mode (truncation). This operation instead acts under the round toward negative infinity (floor) rounding mode. The floor rounding mode gives different results from truncation when the exact result is negative.

For examples, see int),floorDiv(int, int) .

Parameters:
x - the dividend
y - the divisor
Returns:
the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.
Throws:
java.lang.ArithmeticException,ArithmeticException - if the divisor y is zero
Since:
1.8


Examples


package com.logicbig.example.math;

public class FloorDivExample {

public static void main(String... args) {
doFloorDiv(61, 5);
doFloorDiv(61, -5);
doFloorDiv(-25, 2);
doFloorDiv(250, 0);
doFloorDiv(8234, -4);
}

public static void doFloorDiv(int x, int y) {
try {
System.out.printf("%s / %s = %s%n", x, y, x / y);
int floorDivision = Math.floorDiv(x, y);
System.out.printf("Math.floorDiv(%s, %s) = %s%n", x, y, floorDivision);
} catch (ArithmeticException e) {
System.out.println("error: " + e);
}
System.out.println("-----------");
}
}

Output

61 / 5 = 12
Math.floorDiv(61, 5) = 12
-----------
61 / -5 = -12
Math.floorDiv(61, -5) = -13
-----------
-25 / 2 = -12
Math.floorDiv(-25, 2) = -13
-----------
error: java.lang.ArithmeticException: / by zero
-----------
8234 / -4 = -2058
Math.floorDiv(8234, -4) = -2059
-----------




package com.logicbig.example.math;

public class FloorDivExample2 {

public static void main(String... args) {
values(35, -2);
values(Long.MIN_VALUE, -4);
values(-15, 3);
values(120, 0);
values(Long.MAX_VALUE, -5);
}

public static void values(long x, int y) {
System.out.println("-----------");
System.out.println("Result without Math.floorDiv: ");

try {
System.out.printf("%s / %s = %s%n", x, y, x / y);
System.out.println("Result with Math.floorDiv: ");
long floorDivision = Math.floorDiv(x, y);
System.out.println(floorDivision);
} catch (ArithmeticException e) {
System.out.println("error: " + e);
}
}
}

Output

-----------
Result without Math.floorDiv:
35 / -2 = -17
Result with Math.floorDiv:
-18
-----------
Result without Math.floorDiv:
-9223372036854775808 / -4 = 2305843009213693952
Result with Math.floorDiv:
2305843009213693952
-----------
Result without Math.floorDiv:
-15 / 3 = -5
Result with Math.floorDiv:
-5
-----------
Result without Math.floorDiv:
error: java.lang.ArithmeticException: / by zero
-----------
Result without Math.floorDiv:
9223372036854775807 / -5 = -1844674407370955161
Result with Math.floorDiv:
-1844674407370955162




package com.logicbig.example.math;

public class FloorDivExample3 {

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);
}

}
}

Output

Prove 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




See Also