Open In App

Java Math nextDown() Method

Last Updated : 12 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

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

Syntax of nextDown() Method

public static double Math.nextDown(double d)

public static float Math.nextDown(float f)

  • Parameters: d or f: The floating-point number that is double or float, for which we want to find the next lower representable value.
  • Returns: It returns the adjacent floating-point value closer to negative infinity.

Special Values:

  • If the input is NaN, we will get result NaN.
  • If the input is 0.0, we will get the result as the smallest negative value representable, for example, -Double.MIN_VALUE or -Float.MIN_VALUE.
  • If the input is negative infinity, the result is also negative infinity.

Examples of Java Math nextDown() Method

Example 1: In this example, we will see how nextDown() method works in Java.

Java
// Java program to demonstrate Math.nextDown() method
import java.lang.Math;

public class Geeks {
    public static void main(String[] args) {
        double x = 50.5;
        float y = 19.2f;

        System.out.println("Next down of 50.5 (double): " + Math.nextDown(x));
        System.out.println("Next down of 19.2f (float): " + Math.nextDown(y));
    }
}

Output
Next down of 50.5 (double): 50.49999999999999
Next down of 19.2f (float): 19.199999


Example 2: In this example, we are going to try with special values like NaN, 0.0, and negative infinity.

Java
// Java program to demonstrate nextDown() 
// with special floating-point values
import java.lang.Math;

public class Geeks {
    
    public static void main(String[] args) {
        
        System.out.println("Next down of NaN: " + Math.nextDown(Double.NaN));
        System.out.println("Next down of 0.0f: " + Math.nextDown(0.0f));
        System.out.println("Next down of -Infinity: " + Math.nextDown(Double.NEGATIVE_INFINITY));
    }
}

Output
Next down of NaN: NaN
Next down of 0.0f: -1.4E-45
Next down of -Infinity: -Infinity


Important Points:

  • This method works with both float and double.
  • It returns the closest representable value which is smaller than the input.
  • This method id useful for numeriacal methods or any kind of comparison operations.

Practice Tags :

Similar Reads