Java Tutorial Java Advanced Java References

Java Math - ulp() Method



The Java ulp() method returns the size of the unit of least precision (ulp) of the argument. An ulp is the positive distance between the given double/float value and the double/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 Double.MIN_VALUE or Float.MIN_VALUE.
  • If the argument is ±Double.MAX_VALUE, then the result is equal to 2971
  • If the argument is ±Float.MAX_VALUE, then the result is equal to 2104.

Syntax

public static double ulp(double arg)
public static float ulp(float arg)

Parameters

arg 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.

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

The output of the above code will be:

3.552713678800501E-15
3.552713678800501E-15
7.105427357601002E-15
1.9073486E-6

❮ Java Math Methods