Java.lang Package Classes

Java Math - ulp() Method



The java.lang.Math.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 value and the double 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.
  • If the argument is ±Double.MAX_VALUE, then the result is equal to 2971.

Syntax

public static double ulp(double d)

Parameters

d 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(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.lang - Math