StrictMath.floorMod(long, int) in Java

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

Syntax:

int java.lang.StrictMath.floorMod(long x, int y)

This method takes two arguments one of type long and another of type int as its parameters. This method returns the floor modulus of the long and int arguments. The floor modulus is x - (floorDiv(x, y) * y), has the same sign as the  divisor y, and is in the range of -abs(y) < r < +abs(y).

Parameters: Two parameters are required for this method.

x: the dividend.

y: the divisor.

Returns: the floor modulus x - (floorDiv(x, y) * y).

Throws:

ArithmeticException - if the divisor y is zero.

Approach 1: When no exceptions.

Java

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

        long x = 191991;
        int y = 18;
        System.out.println(StrictMath.floorMod(x, y));
    }
}

Output:

3


Approach 2: ArithmeticException 

Java

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

        long x = 191991;
        int y = 0;
        System.out.println(StrictMath.floorMod(x, y));
    }
}


Output:

Exception in thread "main" java.lang.ArithmeticException: / by zero at java.base/java.lang.Math.floorMod(Math.java:1345) at java.base/java.lang.Math.floorMod(Math.java:1319) at java.base/java.lang.StrictMath.floorMod(StrictMath.java:1092)


No comments:

Post a Comment