The Wayback Machine - https://web.archive.org/web/20240118165946/https://www.geeksforgeeks.org/java-math-nextdown-method-examples/
Open In App
Related Articles

Java Math nextDown() method with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The java.lang.Math.nextDown() is a built-in math function in java which returns the floating-point value adjacent to the parameter provided in the direction of negative infinity.nextDown() implementation may run faster than its equivalent nextAfter() call. nextDown() method is overloaded which means that we have more than one method with the same name under the Math class.Two overloaded method of the nextDown() :

  • double type : nextDown(double d)
  • float type : nextDown(float f)

Note :

  • If the argument is NaN, the result is NaN.
  • If the argument is zero, the result is – Double.MIN_VALUE if we are dealing with
    double and if it’s float then the result is – Float.MIN_VALUE.
  • If the argument is negative infinity, the result is negative infinity.

Syntax :

public static dataType nextDown(dataType g)
Parameter :
 g : an input for starting floating-point value.
Return :
The nextDown() method returns the adjacent floating-point value closer to 
 negative infinity.

Example :To show working of java.lang.Math.nextDown() method.




// Java program to demonstrate working
// of java.lang.Math.nextDown() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    public static void main(String args[])
    {
        double g = 23.44;
  
        // Input double value, Output adjacent floating-point
        System.out.println(Math.nextDown(g));
  
        float gf = 28.1f;
  
        // Input float value, Output adjacent floating-point
        System.out.println(Math.nextDown(gf));
  
        double a = 0.0 / 0;
  
        // Input NaN, Output NaN
        System.out.println(Math.nextDown(a));
  
        float b = 0.0f;
  
        // Input zero, Output - Float.MIN_VALUE for float
        System.out.println(Math.nextDown(b));
  
        double c = -1.0 / 0;
  
        // Input negative infinity, Output negative infinity
        System.out.println(Math.nextDown(c));
    }
}


Output:

23.439999999999998
28.099998
NaN
-1.4E-45
-Infinity
Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 16 Apr, 2018
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials