Home » Java programming language

Java StrictMath nextUp() Method with Example

StrictMath Class nextUp() method: Here, we are going to learn about the nextUp() method of StrictMath Class with its syntax and example.
Submitted by Preeti Jain, on December 27, 2019

StrictMath Class nextUp() method

Syntax:

    public static float nextUp(float fl);
    public static double nextUp(double do);
  • nextUp() method is available in java.lang package.
  • nextUp(float fl) method is used to return the float floating-point number adjacent to the given argument (fl) in the direction of the path of infinity.
  • nextUp(double do) method is used to return the double floating-point number adjacent to the given argument (do) in the direction of the path of infinity.
  • 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 fl/ double do – it represents the initial or starting floating-point value of float or double type.

Return value:

The return type of this method is float / double – it returns the floating-point number adjacent to the given parameter which is nearby infinity.

Note:

  • If we pass NaN, the method returns NaN.
  • If we pass a positive infinity, the methods returns the same (i.e. a positive infinity).
  • If we pass 0 (positive or negative), the method returns Float.MIN_VALUE / Double.MIN_VALUE.

Example:

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

public class NextUp {
    public static void main(String[] args) {
        // variable declarations
        float f1 = -0.0f;
        float f2 = 0.0f;
        float f3 = -7.0f / 0.0f;
        float f4 = 7.0f / 0.0f;

        double d1 = -0.0;
        double d2 = 0.0;
        double d3 = -7.0 / 0.0;
        double d4 = 7.0 / 0.0;

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

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

        System.out.println();
        System.out.println("nextUp(float): ");

        // Here , we will get (Float.MIN_VALUE) because we are
        // passing parameter whose value is (-0.0f)
        System.out.println("StrictMath.nextUp (f1): " + StrictMath.nextUp(f1));

        // Here , we will get (Float.MIN_VALUE) and we are
        // passing parameter whose value is (0.0f)
        System.out.println("StrictMath.nextUp (f2): " + StrictMath.nextUp(f2));

        // Here , we will get (Infinity) and we are 
        // passing parameter whose value is (7.0f/0.0f)
        System.out.println("StrictMath.nextUp (f4): " + StrictMath.nextUp(f4));

        System.out.println();
        System.out.println("nextUp(float): ");


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

        // Here , we will get (Double.MIN_VALUE) and we are 
        // passing parameter whose value is (0.0)
        System.out.println("StrictMath.nextUp (d2): " + StrictMath.nextUp(d2));

        // Here , we will get (Infinity) and we are 
        // passing parameter whose value is (7.0/0.0)
        System.out.println("StrictMath.nextUp (d4): " + StrictMath.nextUp(d4));
    }
}

Output

f1: -0.0
f2: 0.0
f3: -Infinity
f4:  Infinity
d1: -0.0
d2: 0.0
d3: -Infinity
d4: Infinity

nextUp(float): 
StrictMath.nextUp (f1): 1.4E-45
StrictMath.nextUp (f2): 1.4E-45
StrictMath.nextUp (f4): Infinity

nextUp(float): 
StrictMath.nextUp (d1): 4.9E-324
StrictMath.nextUp (d2): 4.9E-324
StrictMath.nextUp (d4): Infinity





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.