absExact() long in Java

absExact():This function returns the mathematical absolute value of an long value if it is exactly representable as an long, throwing ArithmeticException if the result overflows the positive long range. The mathematical absolute value of Long.MIN_VALUE overflows the positive long range, so an exception is thrown for that argument. 

Parameters: One parameter is required for this function.

arg: The argument whose absolute value is to be determined. 

It returns the absolute value of the argument, unless overflow occurs.

Throws: ArithmeticException - if the argument is Long.MIN_VALUE

Syntax:

Math.absExact(arg)

For Example:

Math.absExact(-199993393) = > It return 199993393.

Approach

Java

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

        long a = -199993393;
        System.out.println(Math.absExact(a));

    }
}


No comments:

Post a Comment