35. Computing the floor of a division and modulus

Let's assume that we have the following division:

double z = (double)222/14;

This will initialize z with the result of this division, that is, 15.85, but our problem requests the floor of this division, which is 15 (this is the largest integer value that is less than or equal to the algebraic quotient). A solution to obtain this desired result will consist of applying Math.floor(15.85), which is 15.

However, 222 and 14 are integers, and so this preceding division is written as follows:

int z = 222/14;

This time, z will be equal to 15, which is exactly the expected result (the / operator returns the integer closest to zero). There is no need to apply Math.floor(z). Moreover, if the divisor is 0, then 222/0 will throw ArithmeticException.

The conclusion so far is that the floor of a division for two integers that have the same sign (both are positive or negative) can be obtained via the / operator.

Okay, so far, so good, but let's assume that we have the following two integers (opposite signs; the dividend is negative and the divisor is positive, and vice versa):

double z = (double) -222/14;

This time, z will be equal to -15.85. Again, by applying Math.floor(z), the result will be -16, which is correct (this is the largest integer value that is less than or equal to the algebraic quotient).

Let's go over the same problem again with int:

int z = -222/14;

This time, z will be equal to -15. This is incorrect and Math.floor(z) will not help us in this case since Math.floor(-15) is -15. So, this is a problem that should be considered.

From JDK 8 onward, all of these cases have been covered and exposed via the Math.floorDiv() method. This method takes two integers representing the dividend and the divisor as arguments and returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient:

int x = -222;
int y = 14;

// x is the dividend, y is the divisor
int z = Math.floorDiv(x, y); // -16

The Math.floorDiv() method comes in three flavors: floorDiv(int x, int y), floorDiv(long x, int y), and floorDiv(long x, long y).

After Math.floorDiv(), JDK 8 came with Math.floorMod(), which returns the floor modulus of the given arguments. This is computed as the result of x - (floorDiv(x, y) * y), and so it will return the same result as the % operator for arguments with the same sign and a different result for arguments that don't have the same sign.

Rounding up the result of dividing two positive integers (a/b) can be accomplished quickly as follows:

long result = (a + b - 1) / b;

The following is one example of this (we have 4 / 3 = 1.33 and we want 2):

long result = (4 + 3 - 1) / 3; // 2

The following is another example of this (we have 17 / 7 = 2.42 and we want 3):

long result = (17 + 7 - 1) / 7; // 3

If the integers are not positive, then we can rely on Math.ceil():

long result = (long) Math.ceil((double) a/b);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.129.90