public static double nextAfter(double d, double direction) {

        // handling of some important special cases
        if (Double.isNaN(d) || Double.isNaN(direction)) {
            return Double.NaN;
        } else if (d == direction) {
            return direction;
        } else if (Double.isInfinite(d)) {
            return (d < 0) ? -Double.MAX_VALUE : Double.MAX_VALUE;
        } else if (d == 0) {
            return (direction < 0) ? -Double.MIN_VALUE : Double.MIN_VALUE;
        }
        // special cases MAX_VALUE to infinity and  MIN_VALUE to 0
        // are handled just as normal numbers

        final long bits = Double.doubleToLongBits(d);
        final long sign = bits & 0x8000000000000000L;
        if ((direction < d) ^ (sign == 0L)) {
            return Double.longBitsToDouble(sign | ((bits & 0x7fffffffffffffffL) + 1));
        } else {
            return Double.longBitsToDouble(sign | ((bits & 0x7fffffffffffffffL) - 1));
        }

    }
public static double nextAfter ( double d , double direction ) { if ( Double . isNaN ( d ) || Double . isNaN ( direction ) ) { return Double . NaN ; } else if ( d == direction ) { return direction ; } else if ( Double . isInfinite ( d ) ) { return ( d < 0 ) ? - Double . MAX_VALUE : Double . MAX_VALUE ; } else if ( d == 0 ) { return ( direction < 0 ) ? - Double . MIN_VALUE : Double . MIN_VALUE ; } final long bits = Double . doubleToLongBits ( d ) ; final long sign = bits & 0x8000000000000000L ; if ( ( direction < d ) ^ ( sign == 0L ) ) { return Double . longBitsToDouble ( sign | ( ( bits & 0x7fffffffffffffffL ) + 1 ) ) ; } else { return Double . longBitsToDouble ( sign | ( ( bits & 0x7fffffffffffffffL ) - 1 ) ) ; } }
Get the next machine representable number after a number, moving
in the direction of another number.
<p>
The ordering is as follows (increasing):
<ul>
<li>-INFINITY</li>
<li>-MAX_VALUE</li>
<li>-MIN_VALUE</li>
<li>-0.0</li>
<li>+0.0</li>
<li>+MIN_VALUE</li>
<li>+MAX_VALUE</li>
<li>+INFINITY</li>
<li></li>
<p>
If arguments compare equal, then the second argument is returned.
<p>
If {@code direction} is greater than {@code d},
the smallest machine representable number strictly greater than
{@code d} is returned; if less, then the largest representable number
strictly less than {@code d} is returned.</p>
<p>
If {@code d} is infinite and direction does not
bring it back to finite numbers, it is returned unchanged.</p>

@param d base number
@param direction (the only important thing is whether
{@code direction} is greater or smaller than {@code d})
@return the next machine representable number in the specified direction