Java Math ulp() method with Examples
The java.lang.Math.ulp() is a built-in java method which returns the size of an ulp of the argument.An ulp stands for unit of least precision.It calculates the distance between the given double or float value and the
double or float value next larger in magnitude.
Arguments can be of two types :
- ulp(float f) :It takes input of float type.
- ulp(double d) :It takes input of double type.
Note :
- If the argument is NaN, Output is NaN.
- If the argument is positive or negative double or float value, Output for ulp(-arg) and ulp(arg) is same.
- If the argument is positive or negative Zero, Output will be Double.MIN_VALUE or Float.MIN_VALUE.
- If the argument is positive or negative infinity, Output is positive infinity.
- If the argument is positive or negative Double.MAX_VALUE or Float.MAX_VALUE, Output will be 2971 for double type and 2104 for float type.
Syntax :
public static dataType ulp(dataType g) Parameter : g: argument whose ulp is to be returned. Return : This method returns the size of an ulp of the argument.
Example :To show working of java.lang.Math.ulp() method.
// Java program to demonstrate working // of java.lang.Math.ulp() method import java.lang.Math; class Gfg { // driver code public static void main(String args[]) { double a = 34.543; // Input positive double value // Output ulp(a) System.out.println(Math.ulp(a)); // Input negative double value // Output ulp(-a)==ulp(a) System.out.println(Math.ulp(-a)); double b = 0.0 / 0; // Input NaN, Output Nan System.out.println(Math.ulp(b)); float c = -0.0f; // Input negative zero // Output Float.MIN_VALUE. System.out.println(Math.ulp(c)); float d = -1.0f / 0; // Input negative infinity // Output positive infinity. System.out.println(Math.ulp(d)); double e = Double.MAX_VALUE; System.out.println(Math.ulp(e)); } } |
chevron_right
filter_none
Output:
7.105427357601002E-15 7.105427357601002E-15 NaN 1.4E-45 Infinity 1.9958403095347198E292
Recommended Posts:
- Java Math tan() method with Examples
- Math fma() method in Java with Examples
- Java Math min() method with Examples
- Java Math sin() method with Examples
- Java Math abs() method with Examples
- Java Math cos() method with Examples
- Java Math max() method with Examples
- Java Math cosh() method with Examples
- Math multiplyFull() method in Java with Examples
- Java Math nextDown() method with Examples
- Java Math sinh() method with Examples
- Java Math tanh() method with Examples
- Java Math nextUp() method with Examples
- Math multiplyHigh() method in Java with Examples
- Java Math acos() method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



