![]() |
VOOZH | about |
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:
These special cases make sure that the Math.ulp() methods work correctly.
public static double ulp(double d);
public static float ulp(float f);
Note: This method can work with both float and double types.
Now, we are going to discuss some examples for better understanding.
Example 1: In this example, we will see the basic usage of ulp() with positive and negative Double value.
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.
ulp(NaN): NaN ulp(-0.0f): 1.4E-45 ulp(-Infinity): Infinity ulp(Double.MAX_VALUE): 1.9958403095347198E292