Java .nextAfter()
Published Oct 20, 2022Updated Oct 20, 2022
Contribute to Docs
The Math.nextAfter() method returns the floating-point number next to the first argument in the direction of the second argument.
Syntax
Math.nextAfter(start, direction)
- The first argument
startcan be of typedoubleorfloat. - The second argument
directioncan only be of typedouble. - The return type for
.nextAfter()is ofdoubleorfloat, and matches the type of the first argument. - If
direction>start, then return result is >start. - If
direction<start, then return result is <start. - If
direction==start, then value of direction isreturned.
Some special cases for .nextAfter() include:
- If one or both arguments are
NaN, the result isNaN. - If both arguments are signed zero, the direction of the result is unchanged.
- If the first argument is positive or negative
MIN_VALUE, and the second argument has a value which would return a result with a smaller magnitude, zero is returned with the same sign as the first argument. - If the first argument is infinity and the second argument has a value which would return a result with a smaller magnitude,
MAX_VALUEis returned with the same sign as the first argument. - If the first argument is positive or negative
MAX_VALUE, and the second argument has a value which would return a result with a larger magnitude, infinity is returned with the same sign as the first argument.
Example
The following example demonstrates using .nextAfter():
// Test.javapublic class Test {public static void main(String args[]) {float start = 1.15f;double direction = 5.37;System.out.println(Math.nextAfter(start, direction));}}
This results in the following output of type float:
1.1500001
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
- Beginner Friendly.17 hours