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() Methodpublic 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() MethodExample 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)); } } Outputulp(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)); } } Outputulp(NaN): NaN ulp(-0.0f): 1.4E-45 ulp(-Infinity): Infinity ulp(Double.MAX_VALUE): 1.9958403095347198E292 Comment More infoAdvertise with us Next Article Month maxLength() method in Java N Niraj_Pandey Follow Improve Article Tags : Java Java-lang package java-math Practice Tags : Java Similar Reads Java Math ulp() method with Examples 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, w 3 min read Month maxLength() method in Java The maxLength() method is a built-in method of the Month ENUM which is used to get the maximum length of this month in number of days. For example, February can have both 28 days and 29 days depending on whether this year is a leap year or not. Therefore, this method will return 29 for February as m 1 min read Java Math nextUp() method with Examples The Math.nextUp() method in Java comes under java.lang.Math package. This method returns the floating-point value that is next greater than the given input and moving in the direction of positive infinity. This method is very useful when we need to find the smallest representable number that is grea 3 min read JavaTuple toList() method The toList() method in org.javatuples is used to convert the values in TupleClass into a List. This List is of Object type, so that it can take all values. It is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns List formed with t 2 min read Java Math exp() method with Example The Math.exp() method is a part of the java.lang.Math class. This method is used to calculate the value of Euler's number e raised to the power of a specified exponent. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.Sp 2 min read Month plus() method in Java The plus() method is a built-in method of the Month ENUM which is used to get a month after the current month by a specific number of months. That is, this method returns the month after the specified number of months from this month. Syntax: public Month plus(long months) Parameters: This method ac 1 min read JavaTuples with() method The with() method in org.javatuples is used to instantiate a tuple in a semantically elegant way, with the values given as parameters. This method can be used for any tuple class object of the javatuples library. This method is a static function in each javatuple class and it returns the tuple class 3 min read BigDecimal ulp() Method in Java The java.math.BigDecimal.ulp() is an inbuilt method in Java that returns the size of an ulp(unit in the last place) of this BigDecimal. An ulp of a nonzero BigDecimal value is defined as the positive distance between this value and the BigDecimal value next larger in magnitude with the same number o 2 min read LinkedList pop() Method in Java In Java, the pop() method of the LinkedList class is used to remove and return the top element from the stack represented by the LinkedList. The method simply pops out an element present at the top of the stack. This method is similar to the removeFirst method in LinkedList.Example 1: Here, we use t 2 min read Math multiplyFull() method in Java with Examples The multiplyFull(int x, int y) method of Math class is used to return the exact mathematical product of the two arguments. In the parameters, two integer values are provided and the product of both numbers expressed in long type returned by this method. Syntax: public static long multiplyFull(int x 2 min read Like