Open In App

Java Math ulp() Method

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, the Math.ulp() method returns the size of the unit of least precision (ulp) of a given number. This method is used to find the smallest difference between a given floating-point number and the next larger number it can represent. It works with both float and double types.

In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.

Note: Unit of least precision (ulp) is the smallest difference between two numbers that can be represented in floating point format.

Special Cases:

  • If the argument is NaN, the method will return NaN.
  • If the argument is a positive zero, then the method will return a Double.MIN_VALUE.
  • If the argument is negative zero, the method returns Float.MIN_VALUE for float values, and Double.MIN_VALUE for double values respectively.
  • If the argument is positive or negative infinity, the method returns positive infinity because there is no representable floating-point number beyond infinity.
  • If the argument value is close to large number, then the method will return a large value, showing the maximum precision that can be represented.

These special cases make sure that the Math.ulp() methods work correctly.

Syntax of ulp() Method

public static double ulp(double d);

public static float ulp(float f);

Note: This method can work with both float and double types.

  • Parameter: This method take a single parameter, for which the ulp is calculated.
  • Return Type: This method returns the size of an ulp of the argument.

Now, we are going to discuss some examples for better understanding.


Examples of Java Math ulp() Method

Example 1: In this example, we will see the basic usage of ulp() with positive and negative Double value.

Java
// Java program to demonstrate 
// the working of Math.ulp() method
import java.lang.Math;

class Geeks {
    
    public static void main(String args[]) {
        
        double a = 34.543;
        
        // Positive double value
        System.out.println("ulp(a): " + Math.ulp(a));
        
        // Negative double value
        System.out.println("ulp(-a): " + Math.ulp(-a));
    }
}

Output
ulp(a): 7.105427357601002E-15
ulp(-a): 7.105427357601002E-15

Explanation: Here, we are calculating the ulp for both positive and negative double value.

Example 2: In this example, we will see how the ulp() method handles edge cases.

Java
// Handling edge cases
import java.lang.Math;

class Geeks {
    
    public static void main(String args[]) {
        
         // NaN value
        double b = 0.0 / 0;
        // Negative Zero
        float c = -0.0f;   
        // Negative Infinity
        float d = -1.0f / 0; 
        // Max Double value
        double e = Double.MAX_VALUE; 
        
        // NaN case
        System.out.println("ulp(NaN): " + Math.ulp(b));
        
        // Negative zero case
        System.out.println("ulp(-0.0f): " + Math.ulp(c));
        
        // Negative infinity case
        System.out.println("ulp(-Infinity): " + Math.ulp(d));
        
        // Max double value case
        System.out.println("ulp(Double.MAX_VALUE): " + Math.ulp(e));
    }
}

Output
ulp(NaN): NaN
ulp(-0.0f): 1.4E-45
ulp(-Infinity): Infinity
ulp(Double.MAX_VALUE): 1.9958403095347198E292

Practice Tags :

Similar Reads