Java nextUp Function

The Java nextUp Function is a Math function which is to return the floating-point value adjacent to the specified user argument in the direction of positive infinity.

This Java nextUp function is semantically equal to nextAfter(number, Double.POSITIVE_INFINITY). Let us see how to use Math.nextUp, with an example.

Java nextUp Function syntax

The basic syntax of the Math.nextUp Function in Java Programming language is

Math.nextUp(data_type number);
  • number: Please specify the floating-point value here.

The nextUp function will not return the expected output for the below-specified cases:

  • If the number argument is not a number, the function will return NaN.
  • If the number argument is positive infinity, Math.nextUp function will return Positive Infinity.
  • When the number argument is zero, Math.nextUp function will return Double.MIN_VALUE

Java Programming provides two different Math.nextUp Java functions to find the adjacent floating-point value in the direction of positive infinity. The following function accepts a positive or negative double value as an argument and returns the result.

static double nextUp(double number); 

// In order to use in program: 
Math.nextUp(double number);

The following function accepts positive or negative float value as an argument. It returns the adjacent floating value of the argument in the direction of positive infinity.

static float nextUp(float number); //Return Type is float

// In order to use in program: 
Math.nextUp(float number);

Java nextUp Function Example

The Java Math.nextUp function returns the floating-point value adjacent to the first argument in the direction of positive infinity. In this example, We are going to find the same of different data types and display the output.

package MathFunctions;

public class NextUpMethod {
	public static void main(String[] args) {
		double a = 14.9666, b = -95.474;
		float c = 65.23f, d = -3.54f;
		
		System.out.println("NextUp result of Positive Values: " + Math.nextUp(10));
		System.out.println("NextUp result of Negative Values: " + Math.nextUp(-10));
			
		System.out.println("\nNextUp result of Positive Doubles: " + Math.nextUp(a));
		System.out.println("NextUp result of Negative Double Values: " + Math.nextUp(b));

		
		System.out.println("\nNextUp result of Positive Float Values: " + Math.nextUp(c));
		System.out.println("NextUp result of Negative Float Values: " + Math.nextUp(d));
	}
}
Java nextUp Function 1

First, We declared variables of type Double and Float to display the functionality of nextUp function on different data types. Please refer to nextAfter Function to understand the nextAfter function.

double a = 14.9666, b = -95.474;
float c = 65.23f, d = -3.54f;

Next, We used the Math.nextUp Function directly on both the Positive and negative values.

System.out.println("NextUp result of Positive Values: " + Math.nextUp(10));
System.out.println("NextUp result of Negative Values: " + Math.nextUp(-10));

Next, We used the Java Math.nextUp Function on variables a and b (they belong to double type). The following statements will call the nextUp method of double type ( static double nextUp(double number) ) to return the adjacent floating value in the direction of positive infinity.

System.out.println("\nNextUp result of Positive Doubles: " + Math.nextUp(a));
System.out.println("NextUp result of Negative Double Values: " + Math.nextUp(b));

Next, We used the nextAfter Math function on variables c and d (they belong to float type). The following statements will call the nextUp method of float type ( static float nextUp(float number) ) to return the adjacent floating value.

System.out.println("\nNextUp result of Positive Float Values: " + Math.nextUp(c));
System.out.println("NextUp result of Negative Float Values: " + Math.nextUp(d));

Java nextUp Function on Array example

In this Java program, we show how to find the adjacent floating-point values of bulk data. Here, we are going to declare an array of double type and find the adjacent floating-point value of each element in an array in the direction of positive infinity.

package MathFunctions;

public class NextUpOnArrays {
	public static void main(String[] args) {
		
		double [] myArray = {-5.02, 120.08, 124.21, -3.00, 6.10, 4.01};

		for (int i = 0; i < myArray.length; i++) {
			System.out.println("NextUp Result of Array Element = " + Math.nextUp(myArray[i]));
		}
	}
}
NextUp Result of Array Element = -5.019999999999999
NextUp Result of Array Element = 120.08000000000001
NextUp Result of Array Element = 124.21000000000001
NextUp Result of Array Element = -2.9999999999999996
NextUp Result of Array Element = 6.1000000000000005
NextUp Result of Array Element = 4.010000000000001

First, We declared an Array of double types and assigned some random values.

double [] myArray = {-5.02, 120.08, 124.21, -3.00, 6.10, 4.01};

Next, We used Java For Loop to iterate the Array. Within the Java For Loop, we initialized the i value as 0. Next, the compiler will check the condition (i < myArray.length).

TIP: myArray.length finds the length of an array.

for (int i = 0; i < myArray.length; i++) {

It calls the static double nextUp(double number) method to find the adjacent floating-point value of corresponding values in the direction of positive infinity and prints the output.

System.out.println("NextUp Result of Array Element = " + Math.nextUp(myArray[i]));

NOTE: If you want to find the adjacent floating point value of a single item, then use: Math.nextUp(myArray[index_position])