ulp() float in Java

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

Syntax:

float java.lang.Math.ulp(float f)

This method takes one argument of type float as its parameter. This method returns the size of an ulp of the argument. An ulp, unit in the last place, of a float value, is the positive distance between this floating-point value and the float 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 Float.MIN_VALUE.

4. If the argument is ±Float.MAX_VALUE, then the result is equal to 2104. 

Parameters: One parameter is required for this method.

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

Returns: the size of an ulp of the argument.

For Example:

Math.ulp(6.7) = > It returns 4.7683716E-7

Approach

Java

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

        float f = (float6.7;
        System.out.println(Math.ulp(f));

    }
}

Output:

4.7683716E-7

No comments:

Post a Comment