Traversing Floating-Point Numbers with nextDown() Method in Java: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-04 04:28:16   6061  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Description

In this tuorial we will be showing a java example on how to use the nextDown() method of Math Class. The nextDown() returns the floating-point value adjacent to method argument in the direction of negative infinity. Make a note that the nextDown() method is overloaded which means that we have more than one method with the same name under the Math class. Below are the two overloaded method of the nextDown(). 

  • public static double nextDown(double d)
  • public static float nextDown(float f)

The two overloaded methods are basically the same, it’s just that they deal with different data type either float or double.

Most of the methods of the Math class is static and the nextDown() method is no exception. Thus don’t forget that in order to call this method, you don’t have to create a new object. Use the method in the format Math.nextDown(value).

Notes:

  • If the argument is NaN, the result is NaN.
  • If the argument is negative infinity, the result is negative infinity.
  • 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.

Method Syntax

public static double nextDown(double d)
public static float nextDown(float f)

Method Returns

The nextDown() method returns the adjacent floating-point value closer to negative infinity.

Compatibility

Requires Java 1.8 and up

Example

Below is a java code demonstrates the use of nextDown() method of Math class. The example presented might be simple however it shows the behavior of the nextDown() method.


import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 * nextDown() method of Math class
 */

public class MathNexDown {

	public static void main(String[] args) {

		// Ask for user input
		System.out.print("start:");

		// use scanner to read the console input
		Scanner scan = new Scanner(System.in);

		// Assign the input to String variable
		String start = scan.nextLine();


		// close the scanner object
		scan.close();

		// convert the values to double
		double doubleStart = Double.parseDouble(start);


		// get the result of nextDown() method
		double result = Math.nextDown(doubleStart);
		System.out.println("Result of the operation:"+result);
	}

}

output

Below is the sample output when you run the above example.

start:15
Result of the operation:14.999999999999998
Press any key to continue . . .

The above java example source code demonstrates the use of nextDown() method of Math class. We simply ask for user input and we use the Scanner class to parse it. Since we have used the nextLine() method to get the console value which is having a return data type of String thus we have used the Double.parseDouble() to transform it into double. Alternatively if the requirements is to use float then you must use the Float.parseFloat() instead. This conversion is required because the argument for nextDown() method only accepts either float or double.