ulp() double in Java

ulp(): This method is available in the Math class of Java.

Syntax:

double java.lang.Math.ulp(double d)

This method takes one argument of type double as its parameter. This method returns the size of an ulp of the argument. An ulp, unit in the last place, of a double value, is the positive distance between this floating-point value and the double value next larger in magnitude. 

Note: For non-NaN x,ulp(-x) == ulp(x).

Special Cases: 

1. If the argument is NaN, then the result is NaN.

2. If the argument is positive or negative infinity, then the result is positive infinity.

3. If the argument is positive or negative zero, then the result is Double.MIN_VALUE.

4. If the argument is ±Double.MAX_VALUE, then the result is equal to 2971.

Parameters: One parameter is required for this method.

d: the floating-point value whose ulp is to be returned.

Returns: the size of an ulp of the argument.

For Example:

Math.ulp(199.9) = > It returns 2.8421709430404007E-14

Approach

Java

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

        double d = 199.9;
        System.out.println(Math.ulp(d));

    }
}

Output:

2.8421709430404007E-14

No comments:

Post a Comment