Name: Anonymous 2011-12-20 13:47
Why is math.Nextafter(0, 1) = 5e-324?
1353 public static double nextAfter(double start, double direction) {
1354 return sun.misc.FpUtils.nextAfter(start, direction);
1355 }
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 (isNaN(start) || 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);
}
}
long transducer = Double.doubleToRawLongBits(start + 0.0d);
transducer = transducer + (transducer >= 0L ? 1L : -1L);
return Double.longBitsToDouble(transducer);
public static native long doubleToRawLongBits(double value);
public static native double longBitsToDouble(long bits);
/**
* 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.
*
* <p>
* Special cases:
* <ul>
* <li> If either argument is a NaN, then NaN is returned.
*
* <li> If both arguments are signed zeros, <code>direction</code>
* is returned unchanged (as implied by the requirement of
* returning the second argument if the arguments compare as
* equal).
*
* <li> If <code>start</code> is
* ±<code>Double.MIN_VALUE</code> and <code>direction</code>
* has a value such that the result should have a smaller
* magnitude, then a zero with the same sign as <code>start</code>
* is returned.
*
* <li> If <code>start</code> is infinite and
* <code>direction</code> has a value such that the result should
* have a smaller magnitude, <code>Double.MAX_VALUE</code> with the
* same sign as <code>start</code> is returned.
*
* <li> If <code>start</code> is equal to ±
* <code>Double.MAX_VALUE</code> and <code>direction</code> has a
* value such that the result should have a larger magnitude, an
* infinity with same sign as <code>start</code> is returned.
* </ul>
*
* @param start starting floating-point value
* @param direction value indicating which of
* <code>start</code>'s neighbors or <code>start</code> should
* be returned
* @return The floating-point number adjacent to <code>start</code> in the
* direction of <code>direction</code>.
* @author Joseph D. Darcy
*/