Java Math floorDiv() Method

The Math.floorDiv() method in Java is used to perform integer division and return the largest (closest to positive infinity) value that is less than or equal to the algebraic quotient.

Table of Contents

  1. Introduction
  2. floorDiv() Method Syntax
  3. Overloaded floorDiv() Methods
  4. Examples
    • floorDiv(int x, int y)
    • floorDiv(long x, int y)
    • floorDiv(long x, long y)
  5. Real-World Use Case
  6. Conclusion

Introduction

The Math.floorDiv() method provides a way to perform integer division and ensure that the result is the largest integer that is less than or equal to the algebraic quotient. This method is useful in scenarios where you need to perform division and ensure that the result is rounded towards negative infinity.

floorDiv() Method Syntax

The syntax for the floorDiv() method varies depending on the types of the arguments:

floorDiv(int x, int y)

public static int floorDiv(int x, int y)

floorDiv(long x, int y)

public static long floorDiv(long x, int y)

floorDiv(long x, long y)

public static long floorDiv(long x, long y)

Parameters:

  • x: The dividend.
  • y: The divisor.

Returns:

  • The largest integer value that is less than or equal to the algebraic quotient.

Throws:

  • ArithmeticException if the divisor y is zero.

Overloaded floorDiv() Methods

The Math.floorDiv() method is overloaded to handle different combinations of primitive data types: int and long. Each version returns the floor value of the division.

Examples

floorDiv(int x, int y)

The floorDiv(int x, int y) method returns the largest integer value that is less than or equal to the algebraic quotient of two int values.

Example

public class FloorDivIntExample {
    public static void main(String[] args) {
        int x1 = 7, y1 = 3;
        int x2 = -7, y2 = 3;

        int result1 = Math.floorDiv(x1, y1);
        int result2 = Math.floorDiv(x2, y2);

        System.out.println("Floor division of " + x1 + " / " + y1 + " is " + result1);
        System.out.println("Floor division of " + x2 + " / " + y2 + " is " + result2);
    }
}

Output:

Floor division of 7 / 3 is 2
Floor division of -7 / 3 is -3

floorDiv(long x, int y)

The floorDiv(long x, int y) method returns the largest integer value that is less than or equal to the algebraic quotient of a long and an int value.

Example

public class FloorDivLongIntExample {
    public static void main(String[] args) {
        long x1 = 10L, y1 = 3;
        long x2 = -10L, y2 = 3;

        long result1 = Math.floorDiv(x1, y1);
        long result2 = Math.floorDiv(x2, y2);

        System.out.println("Floor division of " + x1 + " / " + y1 + " is " + result1);
        System.out.println("Floor division of " + x2 + " / " + y2 + " is " + result2);
    }
}

Output:

Floor division of 10 / 3 is 3
Floor division of -10 / 3 is -4

floorDiv(long x, long y)

The floorDiv(long x, long y) method returns the largest integer value that is less than or equal to the algebraic quotient of two long values.

Example

public class FloorDivLongLongExample {
    public static void main(String[] args) {
        long x1 = 20L, y1 = 4L;
        long x2 = -20L, y2 = 4L;

        long result1 = Math.floorDiv(x1, y1);
        long result2 = Math.floorDiv(x2, y2);

        System.out.println("Floor division of " + x1 + " / " + y1 + " is " + result1);
        System.out.println("Floor division of " + x2 + " / " + y2 + " is " + result2);
    }
}

Output:

Floor division of 20 / 4 is 5
Floor division of -20 / 4 is -5

Real-World Use Case

Calculating Page Numbers in Pagination

In real-world scenarios, the Math.floorDiv() method can be used to calculate page numbers in pagination, ensuring that the result is always rounded towards negative infinity.

Example

public class PaginationExample {
    public static void main(String[] args) {
        int totalItems = 23;
        int itemsPerPage = 5;

        int totalPages = Math.floorDiv(totalItems, itemsPerPage) + (totalItems % itemsPerPage == 0 ? 0 : 1);

        System.out.println("Total pages needed: " + totalPages);
    }
}

Output:

Total pages needed: 5

Conclusion

The Math.floorDiv() method in Java provides a way to perform integer division with rounding towards negative infinity, ensuring that the result is the largest integer less than or equal to the algebraic quotient. 

By understanding how to use this method and its overloaded versions, you can handle various division operations and solve problems that require floor division. 

Whether you are working with integers or long integers, the floorDiv() method offers a reliable tool for ensuring correct division results.

Comments