# StrictMath ulp() Method In Java with Examples

### ulp(double num)

The ulp(double num) is an inbuilt method of StrictMath class in java which is used to get the size of an ulp of the given argument. An ulp of a double value is the positive distance between this floating point value and the double value which is next larger in magnitude.

Some special cases are:

• The result is NaN, if the argument is NaN.
• The result is positive infinity, if the argument is positive or negative infinity.
• The result is Double.MIN_VALUE if the argument is positive or negative zero.
• The result is equal to 2^971 if the argument is ±Double.MAX_VALUE.

Syntax:

`public static double ulp(double num)`

Parameters: The method accepts one parameter num of double type the floating-point value whose ulp is to be returned.

Return Value: The ulp method returns the size of an ulp of the argument.

Examples:

```Input: num = 5.7
Output: 8.881784197001252E-16

Input: num = 0.0
Output: 4.9E-324
```

Below program illustrates the java.lang.StrictMath.ulp() method:

Program 1:

 `// Java praogram to illustrate the ` `// java.lang.StrictMath.ulp() ` ` `  `import` `java.lang.*; ` ` `  `public` `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``double` `num1 = ``9.7``, num2 = ``4.9``; ` ` `  `        ``// Get the size of an ulp of the argument ` `        ``// using ulp() method ` `        ``double` `ulp_Value = StrictMath.ulp(num1); ` ` `  `        ``// Print the size of the ulp ` `        ``System.out.println(``" The Size of ulp of "` `                           ``+ num1 + ``" = "` `                           ``+ ulp_Value); ` ` `  `        ``// Get the size of an ulp of the argument ` `        ``// using ulp() method ` `        ``ulp_Value = StrictMath.ulp(num2); ` ` `  `        ``// Print the size of the ulp ` `        ``System.out.println(``" The Size of ulp of "` `                           ``+ num2 + ``" = "` `                           ``+ ulp_Value); ` `    ``} ` `} `

Output:

``` The Size of ulp of 9.7 = 1.7763568394002505E-15
The Size of ulp of 4.9 = 8.881784197001252E-16
```

### ulp(float num)

The ulp(float num) is an inbuilt method of StrictMath class in java which is used to get the size of an ulp of the given argument. An ulp of a float value is the positive distance between this floating point value and the float value which is next larger in magnitude.

Some special cases are:

• The result is NaN, if the argument is NaN.
• The result is positive infinity, if the argument is positive or negative infinity.
• The result is Float.MIN_VALUE if the argument is positive or negative zero.
• The result is equal to 2^104 if the argument is ±Float.MAX_VALUE.

Syntax:

`public static float ulp(float num)`

Parameters: The method accepts one parameter num of float type the floating-point value whose ulp is to be returned.

Return Value: The ulp method returns the size of an ulp of the argument.

Examples:

```Input: num = 5.7
Output: 8.881784197001252E-16

Input: num = 0.0
Output: 4.9E-324
```

Below program illustrates the java.lang.StrictMath.ulp() method:

Program 1:

 `// Java praogram to illustrate the ` `// java.lang.StrictMath.ulp() ` ` `  `import` `java.lang.*; ` ` `  `public` `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``float` `num1 = ``2``.7f, num2 = -``4``.5f; ` ` `  `        ``// Get the size of an ulp of the argument ` `        ``// using ulp() method ` `        ``float` `ulp_Value = StrictMath.ulp(num1); ` ` `  `        ``// Print the size of the ulp ` `        ``System.out.println(``" The Size of ulp of "` `                           ``+ num1 + ``" = "` `                           ``+ ulp_Value); ` ` `  `        ``// Get the size of an ulp of the argument ` `        ``// using ulp() method ` `        ``ulp_Value = StrictMath.ulp(num2); ` ` `  `        ``// Print the size of the ulp ` `        ``System.out.println(``" The Size of ulp of "` `                           ``+ num2 + ``" = "` `                           ``+ ulp_Value); ` `    ``} ` `} `

Output:

``` The Size of ulp of 2.7 = 2.3841858E-7
The Size of ulp of -4.5 = 4.7683716E-7
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.