Most languages have built-in or library functions to get the next or previous single-point (32-bit) and / or two-point (64-bit) numbers.
For users of 32-bit and 64-bit floating-point arithmetic, a reasonable understanding of the basic constructions is very useful to prevent some dangers with them. The IEEE standard is applied uniformly, but still leaves a number of details to the performers. Therefore, a universal platform solution based on bit manipulations of machine word representations can be problematic and may depend on issues such as endian, etc. Although understanding all the details about how it can or should work at the bit level can demonstrate intellectual skill, it's better to use an internal or library solution designed for each platform and having a universal API on supported platforms.
I noticed solutions for C # and C ++. Here are some of them for Java:
Math.nextUp:
public static double nextUp (double d):
- Returns a floating point value adjacent to d in the direction of positive infinity. This method is semantically equivalent to nextAfter (d, Double.POSITIVE_INFINITY); however, the nextUp implementation may be faster than the equivalent of the nextAfter call.
Special Occasions:
- If the argument is NaN, the result is NaN.
- If the argument is positive infinity, the result is positive infinity.
- If the argument is zero, the result will be Double.MIN_VALUE
Options:
- d - start of floating point value
Return:
- The closest floating point value is closer to positive infinity.
public static float nextUp (float f):
- Returns a floating point value adjacent to f in the direction of positive infinity. This method is semantically equivalent to nextAfter (f, Float.POSITIVE_INFINITY); however, the nextUp implementation may be faster than the equivalent of the nextAfter call.
Special Occasions:
- If the argument is NaN, the result is NaN.
- If the argument is positive infinity, the result is positive infinity.
- If the argument is zero, the result will be Float.MIN_VALUE
Options:
- f - start floating point value
Return:
- The closest floating point value is closer to positive infinity.
The following two are more difficult to use. However, the direction to zero or to positive or negative infinity seems more likely and useful. Another use is to see an intermediate value between two values. You can determine how many exist between two values using a loop and a counter. Also, it looks like they, along with the following methods, may be useful for incrementing / decrementing for loops.
Math.nextAfter:
public static double nextAfter (double start, double direction)
- Returns a floating point number adjacent to the first argument in the direction of the second argument. If both arguments are compared as equal to the second argument.
Special Occasions:
- If any argument is NaN, NaN is returned.
- If both arguments coincide with zeros, the direction is returned unchanged (which is implied by the requirement to return the second argument if the arguments are compared as equal).
- If the beginning is ± Double.MIN_VALUE and the direction is such that the result should be smaller, then zero with the same sign when the beginning is returned.
- If the beginning is infinite, and the direction is so important that the result should be smaller, Double.MAX_VALUE with the same sign as the beginning is returned.
- If the start is ± Double.MAX_VALUE, and the direction matters so that the result should be large, infinity with the same sign as start.
Options:
- start - start a floating point value
- direction - a value indicating which of the starting neighbors or start should be returned
Return:
- A floating point number adjacent to the direction beginning in the direction.
public static float nextAfter (floating point launch, double direction)
- Returns a floating point number adjacent to the first argument in the direction of the second argument. If both arguments are compared as equal to the value equivalent to the second argument.
Special Occasions:
- If any argument is NaN, then NaN is returned.
- If both arguments mean zeros, a value equivalent to the direction is returned.
- If start is ± Float.MIN_VALUE and the direction is such that the result should be smaller, then zero with the same sign when the beginning returns.
- If the beginning is infinite, and the direction is such that the result should be smaller, Float.MAX_VALUE with the same sign as the beginning is returned.
- If the beginning is ± Float.MAX_VALUE, and the direction is such that the result should be large, infinity with the same sign when the beginning returns.
Options:
- start - start a floating point value
- direction - a value indicating which of the start neighbors or start should be returned
Return:
- A floating point number adjacent to the beginning in the direction of the direction.
Jim Jul 10 '12 at 21:10 2012-07-10 21:10
source share