StrictMath.floorDiv(long, long) in Java

StrictMath.floorDiv(long, long): This method is available in java.lang.StrictMath class of Java.

Syntax:

long java.lang.StrictMath.floorDiv(long x, long y)

This method takes two arguments of type long as its parameters. This method returns the largest (closest to positive infinity) long value that is less than or equal to the algebraic quotient.

Note:

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

Parameters: Two parameters are required for this method.

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:

ArithmeticException - if the divisor y is zero.

Approach 1: When no exceptions.

Java

public class StrictMathfloorDivlong {
    public static void main(String[] args) {

        long x = 1919119, y = 1919;
        System.out.println(StrictMath.floorDiv(x, y));
    }
}

Output:

1000


Approach 2: ArithmeticException

Java

public class StrictMathfloorDivlong {
    public static void main(String[] args) {

        long x = 1919119, y = 0;
        System.out.println(StrictMath.floorDiv(x, y));
    }
}


Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero at java.base/java.lang.Math.floorDiv(Math.java:1233) at java.base/java.lang.StrictMath.floorDiv(StrictMath.java:1037)


No comments:

Post a Comment