Open In App

StrictMath ulp() Method In Java with Examples

Last Updated : 13 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




// Java program 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




// Java program 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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads