Library contents

Java Math.nextDown

Move to the next representable floating-point value toward negative infinity.

A simple example

Math.nextDown

  1. Start with 1.0
  2. Do this Take one double-precision step downward
  3. Result 0.9999999999999999
The result is the closest representable double below 1.0.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.nextDown(1.0));
    }
}
Output
0.9999999999999999

Syntax

Java syntax
Math.nextDown(limit)
JDK 21 method signatures (2)
public static double nextDown(double d)
public static float nextDown(float f)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

Test just below a measurement limit

A calibration routine needs to test the nearest representable value on one side of a limit. Move by one floating-point step so the boundary test uses a value the computer can actually store.

From the programdouble limit = 1.0
Result0.9999999999999999
Main.java

class Main {
    public static void main(String[] args) throws Exception {
        double limit = 1.0;
        System.out.println(Math.nextDown(limit));
    }
}
Output
0.9999999999999999

The printed value is the closest double smaller than one.

Advertisements

Test the scale just below 2

A scale switches display ranges at 2. The tester needs the nearest representable double below this threshold to exercise the boundary.

Given2.0 · one step below
Worked result1.9999999999999998

There is no intervening double between the threshold and this result.

Test the exposure meter just below 8

A exposure meter switches display ranges at 8. The tester needs the nearest representable double below this threshold to exercise the boundary.

Given8.0 · one step below
Worked result7.999999999999999

There is no intervening double between the threshold and this result.

What to watch for

The gap below a power of two can differ from the gap above it.

Documentation for Math.nextDown