AppDividend
Latest Code Tutorials

Java Math nextDown() Function: Complete Guide

The java.lang.Math.nextDown() is a built-in method that is used to find the immediate previous(closest to negative infinity) floating-point value that exists before the value is passed as a parameter.

Java Math nextDown()

The java.lang.Math.nextDown() is a built-in math method that returns the floating-point value adjacent to a user specified parameter (d) in the direction of negative infinity. The nextDown() method is equivalent to nextAfter(d, Double.NEGATIVE_INFINITY) method.

This method comes in handy because floating-point numbers are not equidistant and are finite in number. It is to be noted that a nextDown implementation of the equivalent nextAfter implementation may run faster.

Syntax

```public static float nextDown(float a)
public static double nextDown(double a)
```

Parameter(s)

A floating-point value (float or double).

Return Value

The immediate previous(closest to negative infinity) floating-point value exists before the passed value.

Note

1. This method is the semantic equivalent of nextAfter(d, Double.NEGATIVE_INFINITY) [or nextAfter(f, Float.NEGATIVE_INFINITY)].
2. If the argument is NaN, then this method returns NaN.
3. If the argument is zero, then this method returns -Double.MIN_VALUE(for double type) or -Float.MIN_VALUE(for float type).
4. If the argument is negative infinity, this method returns negative infinity.

Consider the following examples.

Example1.java: The following example demonstrates the use of this method.

See the following code.

```public class Example1 {
public static void main(String[] args) {
float a = 13.4f;
double b = 13.4;

System.out.println(Math.nextDown(a));
System.out.println(Math.nextDown(b));
}
}
```

Output

```->javac Example1.java
->java Example1
13.399999
13.399999999999999
```

Example2.java: The following example demonstrates the use of this method.

The following example demonstrates the use of this method on negative floating-point values.

```public class Example2 {
public static void main(String[] args) {
float a = -13.4f;
double b = -13.4;

System.out.println(Math.nextDown(a));
System.out.println(Math.nextDown(b));
}
}
```

Output

```->javac Example2.java
->java Example2
-13.400001
-13.400000000000002

```

Example3.java: The following example demonstrates the relation between nextDown and nextAfter methods.

See the following code.

```public class Example3 {
public static void main(String[] args) {
float a = 104.451f;
double b = 736.147;

System.out.println(Math.nextDown(a));
System.out.println(Math.nextAfter(a, Float.NEGATIVE_INFINITY));

System.out.println(Math.nextDown(b));
System.out.println(Math.nextAfter(b, Double.NEGATIVE_INFINITY));
}
}
```

Output

```->javac Example3.java
->java Example3
104.45099
104.45099
736.1469999999999
736.1469999999999

```

Example4.java: The following example demonstrates the situation of passing NaN.

See the following code.

```public class Example4 {
public static void main(String[] args) {
float a = Float.NaN;
double b = Double.NaN;

System.out.println(Math.nextDown(a));
System.out.println(Math.nextDown(b));
}
}
```

Output

```->javac Example4.java
->java Example4
NaN
NaN

```

Example5.java: The following example demonstrates the situation of passing NaN.

The following example demonstrates the situation of passing zero.

```public class Example5 {
public static void main(String[] args) {
float a = 0.0f;
double b = 0.0;

System.out.println(Math.nextDown(a));
System.out.println(-Float.MIN_VALUE);
System.out.println(Math.nextDown(b));
System.out.println(-Double.MIN_VALUE);
}
}
```

Output

```->javac Example5.java
->java Example5
-1.4E-45
-1.4E-45
-4.9E-324
-4.9E-324
```

Example6.java: The following example demonstrates the situation of passing negative infinity.

See the following code.

```public class Example6 {
public static void main(String[] args) {
float a = Float.NEGATIVE_INFINITY;
double b = Double.NEGATIVE_INFINITY;

System.out.println(Math.nextDown(a));
System.out.println(Math.nextDown(b));
}
}
```

Output

```->javac Example6.java
->java Example6
-Infinity
-Infinity
```