.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
start
can be of typedouble
orfloat
. - The second argument
direction
can only be of typedouble
. - The return type for
.nextAfter()
is ofdouble
orfloat
, 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_VALUE
is 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.