Saturday, 18 June 2022

java: math: floorDiv: Get largest integer less than or equal to the algebraic quotient

 

java math provides ‘floorDiv’ method, which returns the largest (closest to positive infinity) int value that is less than or equal to the algebraic quotient.

 

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.

 

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.

 

Argument x specifies the dividend and y specifies the divisor.

 

Example

Math.floorDiv(5, 3); // Return 1

Math.floorDiv(-5, 3); // Return -2

 

Special cases to consider

a. If the dividend is the Integer.MIN_VALUE and the divisor is -1, then integer overflow occurs and the result is equal to the Integer.MIN_VALUE

 

If the dividend is the Long.MIN_VALUE and the divisor is -1, then integer overflow occurs and the result is equal to the Long.MIN_VALUE.

 

b. If the signs of the arguments are the same, the results of floorDiv and the / operator are the same.

 

c. 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(-5, 3) == -2, whereas (-5 / 3) == -1.

 


Find the below working application.

 

FloorDivDemo.java

package com.sample.app;

public class FloorDivDemo {

	public static void main(String[] args) {
		System.out.printf("Math.floorDiv(%d, %d) is %d\n", 5, 3, Math.floorDiv(5, 3));
		System.out.printf("Math.floorDiv(%d, %d) is %d\n", -5, 3, Math.floorDiv(-5, 3));

		System.out.printf("Math.floorDiv(%d, %d) is %d\n", 5l, 3l, Math.floorDiv(5l, 3l));
		System.out.printf("Math.floorDiv(%d, %d) is %d\n", -5l, 3l, Math.floorDiv(-5l, 3l));

		// If the dividend is the Integer.MIN_VALUE and the divisor is -1, then integer
		// overflow occurs and the result is equal to the Integer.MIN_VALUE
		// If the dividend is the Long.MIN_VALUE and the divisor is -1, then integer
		// overflow occurs and the result is equal to the Long.MIN_VALUE.
		System.out.printf("Math.floorDiv(%d, %d) is %d\n", Integer.MIN_VALUE, -1, Math.floorDiv(Integer.MIN_VALUE, -1));
		System.out.printf("Math.floorDiv(%d, %d) is %d\n", Long.MIN_VALUE, -1l, Math.floorDiv(Long.MIN_VALUE, -1l));

		// If the signs of the arguments are the same, the results of floorDiv and the /
		// operator are the same.
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", 5, 3, Math.floorDiv(5, 3), 5, 3, (5 / 3));
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", -5, -3, Math.floorDiv(-5, -3), -5, -3, (-5 / -3));
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", 5l, 3l, Math.floorDiv(5l, 3l), 5l, 3l,(5l / 3l));
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", -5, -3l, Math.floorDiv(5l, 3l), -5l, -3l,(-5l / -3l));

		// 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.
		System.out.printf("Math.floorDiv(%d, %d) is %d and (%d/%d) = %d\n", -5, 3, Math.floorDiv(-5, 3), -5, 3,(-5 / 3));
	}

}

 

Output

Math.floorDiv(5, 3) is 1
Math.floorDiv(-5, 3) is -2
Math.floorDiv(5, 3) is 1
Math.floorDiv(-5, 3) is -2
Math.floorDiv(-2147483648, -1) is -2147483648
Math.floorDiv(-9223372036854775808, -1) is -9223372036854775808
Math.floorDiv(5, 3) is 1 and (5/3) = 1
Math.floorDiv(-5, -3) is 1 and (-5/-3) = 1
Math.floorDiv(5, 3) is 1 and (5/3) = 1
Math.floorDiv(-5, -3) is 1 and (-5/-3) = 1
Math.floorDiv(-5, 3) is -2 and (-5/3) = -1

 

 

 

Previous                                                 Next                                                 Home

No comments:

Post a Comment