Home » Java programming language

Java StrictMath ulp() Method with Example

StrictMath Class ulp() method: Here, we are going to learn about the ulp() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on January 05, 2020

StrictMath Class ulp() method

Syntax:

    public static double ulp(double do);
    public static float ulp(float fl);
  • ulp() Method is available in java.lang package.
  • ulp(double do) Method is used to return the size of an ulp of the given argument in the method. In this method, an ulp of the given double value parameter is the positive distance between double floating-point value and the given argument double value next larger in magnitude.
  • ulp(float fl) Method is used to return the size of an ulp of the given argument in the method. In this method, an ulp of the given float value parameter is the positive distance between float floating-point value and the given argument float value next larger in magnitude.
  • These methods don't throw an exception.
  • These are static methods, it is accessible with the class name and, if we try to access these methods with the class object then we will not get any error.

Parameter(s):

  • float / double – represents the value represents the double floating point value whose ulp is to be returned.

Return value:

The return type of the method is float / double, it returns the size of an ulp of the given parameter and return value is of float / double type.

Note:

  • If we pass NaN, the method returns the same value (i.e. NaN).
  • If we pass an infinity (positive or negative), the method returns the positive infinity.
  • If we pass zero (positive or negative), the method returns the Float.MIN_VALUE / Double.MIN_VALUE.
  • If we pass Float.MAX_VALUE, the method returns the 2 raised to the power of 104 and if we pass Double.MAX_VALUE, the method returns the 2 raised to the power of 971.

Example:

// Java program to demonstrate the example 
// of signum() method of StrictMath class

public class Ulp {
    public static void main(String[] args) {
        // variable declarations
        double d1 = 0.0;
        double d2 = -0.0;
        double d3 = 7.0 / 0.0;
        double d4 = -7.0 / 0.0;
        double d5 = 1285.45;

        float f1 = 0.0f;
        float f2 = -0.0f;
        float f3 = 7.0f / 0.0f;
        float f4 = -7.0f / 0.0f;
        float f5 = 1285.45f;

        System.out.println();
        System.out.println("ulp(double d:)");

        // Display previous value of d1,d2,d3 ,d4and d5
        System.out.println("d1:" + d1);
        System.out.println("d2: " + d2);
        System.out.println("d3: " + d3);
        System.out.println("d4: " + d4);
        System.out.println("d5: " + d5);

        // Display previous value of f1,f2,f3 ,f4and d5
        System.out.println("f1: " + f1);
        System.out.println("f2: " + f2);
        System.out.println("f3: " + f3);
        System.out.println("f4: " + f4);
        System.out.println("f5: " + f5);


        // Here , we will get (Double.MIN_VALUE) because
        // we are passing parameter (0.0)
        System.out.println("StrictMath.ulp(d1): " + StrictMath.ulp(d1));

        // Here , we will get (Double.MIN_VALUE) because
        // we are passing parameter (-0.0) 
        System.out.println("StrictMath.ulp(d2): " + StrictMath.ulp(d2));

        // Here , we will get (Infinity) because
        // we are passing parameter (7.0/0.0) 
        System.out.println("StrictMath.ulp(d2): " + StrictMath.ulp(d3));

        // Here , we will get (Infinity) because 
        // we are passing parameter (-7.0/0.0)
        System.out.println("StrictMath.ulp(d2): " + StrictMath.ulp(d4));

        // Here , we will get (2 raised to the power of 971) because
        // we are passing parameter (1285.45)
        System.out.println("StrictMath.ulp(d5): " + StrictMath.ulp(d5));

        System.out.println();
        System.out.println("ulp(float fl:)");

        // Here , we will get (Float.MIN_VALUE) because 
        // we are passing parameter (0.0)
        System.out.println("StrictMath.ulp(f1): " + StrictMath.ulp(f1));

        // Here , we will get (Float.MIN_VALUE) because 
        // we are passing parameter (-0.0) 
        System.out.println("StrictMath.ulp(f2): " + StrictMath.ulp(f2));

        // Here , we will get (Infinity) because 
        // we are passing parameter (7.0/0.0) 
        System.out.println("StrictMath.ulp(f3): " + StrictMath.ulp(f3));

        // Here , we will get (Infinity) because 
        // we are passing parameter (-7.0/0.0) 
        System.out.println("StrictMath.ulp(f4): " + StrictMath.ulp(f4));

        // Here , we will get (2 raised to the power of 971) because
        // we are passing parameter (1285.45) 
        System.out.println("StrictMath.ulp(f5): " + StrictMath.ulp(f5));
    }
}

Output


ulp(double d:)
d1:0.0
d2: -0.0
d3: Infinity
d4: -Infinity
d5: 1285.45
f1: 0.0
f2: -0.0
f3: Infinity
f4: -Infinity
f5: 1285.45
StrictMath.ulp(d1): 4.9E-324
StrictMath.ulp(d2): 4.9E-324
StrictMath.ulp(d2): Infinity
StrictMath.ulp(d2): Infinity
StrictMath.ulp(d5): 2.2737367544323206E-13

ulp(float fl:)
StrictMath.ulp(f1): 1.4E-45
StrictMath.ulp(f2): 1.4E-45
StrictMath.ulp(f3): Infinity
StrictMath.ulp(f4): Infinity
StrictMath.ulp(f5): 1.2207031E-4



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.