Java.lang Package Classes

Java StrictMath - ulp() Method



The java.lang.StrictMath.ulp() method returns the size of the unit of least precision (ulp) of the argument. An ulp is the positive distance between the given float value and the float value next larger in magnitude. In special cases it returns the following:

  • If the argument is NaN, then the result is NaN.
  • If the argument is positive or negative infinity, then the result is positive infinity.
  • If the argument is positive or negative zero, then the result is Float.MIN_VALUE.
  • If the argument is ±Float.MAX_VALUE, then the result is equal to 2104.

Syntax

public static float ulp(float f)

Parameters

f Specify a floating-point value whose ulp need to be returned.

Return Value

Returns the size of an ulp of the argument.

Exception

NA.

Example:

In the example below, ulp() method returns the size of an ulp of the argument.

import java.lang.*;

public class MyClass {
 public static void main(String[] args) {
  System.out.println(StrictMath.ulp(25.0f)); 
  System.out.println(StrictMath.ulp(30.238f)); 
  System.out.println(StrictMath.ulp(35.5f)); 
  System.out.println(StrictMath.ulp(-25f));     
 }
}

The output of the above code will be:

1.9073486E-6
1.9073486E-6
3.8146973E-6
1.9073486E-6

❮ Java.lang - StrictMath