public static double nextAfter ( double start , double direction ) { /* * The cases: * * nextAfter(+infinity, 0) == MAX_VALUE * nextAfter(+infinity, +infinity) == +infinity * nextAfter(-infinity, 0) == -MAX_VALUE * nextAfter(-infinity, -infinity) == -infinity * * are naturally handled without any additional testing */ // First check for NaN values if ( Double . isNaN ( start ) || Double . isNaN ( direction ) ) { // return a NaN derived from the input NaN(s) return start + direction ; } else if ( start == direction ) { return direction ; } else { // start > direction or start < direction // Add +0.0 to get rid of a -0.0 (+0.0 + -0.0 => +0.0) // then bitwise convert start to integer. long transducer = Double . doubleToRawLongBits ( start + 0.0d ) ; /* * IEEE 754 floating-point numbers are lexicographically * ordered if treated as signed- magnitude integers . * Since Java's integers are two's complement, * incrementing" the two's complement representation of a * logically negative floating-point value *decrements* * the signed-magnitude representation. Therefore, when * the integer representation of a floating-point values * is less than zero, the adjustment to the representation * is in the opposite direction than would be expected at * first . */ if ( direction > start ) { // Calculate next greater value transducer = transducer + ( transducer >= 0L ? 1L : - 1L ) ; } else { // Calculate next lesser value assert direction < start ; if ( transducer > 0L ) -- transducer ; else if ( transducer < 0L ) ++ transducer ; /* * transducer==0, the result is -MIN_VALUE * * The transition from zero (implicitly * positive) to the smallest negative * signed magnitude value must be done * explicitly. */ else transducer = DoubleConsts . SIGN_BIT_MASK | 1L ; } return Double . longBitsToDouble ( transducer ) ; } }
Returns the floating - point number adjacent to the first argument in the direction of the second argument . If both arguments compare as equal the second argument is returned .