GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java Math ulp() Method - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java Math ulp() Method

Last Updated : 14 May, 2025

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
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.