next higher/lower IEEE double precision number

后端 未结 6 1843
我寻月下人不归
我寻月下人不归 2020-12-03 07:27

I am doing high precision scientific computations. In looking for the best representation of various effects, I keep coming up with reasons to want to get the next higher (

6条回答
  •  春和景丽
    2020-12-03 07:48

    Most languages have intrinsic or library functions for acquiring the next or previous single-precision (32-bit) and/or double-precision (64-bit) number.

    For users of 32-bit and 64-bit floating point arithmetic, a sound understanding of the basic constructs is very useful for avoiding some hazards with them. The IEEE standard applies uniformly, but still leaves a number of details up to implementers. Hence, a platform universal solution based on bit manipulations of the machine word representations may problematic and may depend on issues such as endian and so on. Whilst understanding all the gory details of how it could or should work at the bit level may demonstrate intellectual prowess, it is still better to use an intrinsic or library solution that is tailored for each platform and has a universal API across supported platforms.

    I noticed solutions for C# and C++. Here are some for Java:

    Math.nextUp:

    public static double nextUp(double d):

    • Returns the floating-point value adjacent to d in the direction of positive infinity. This method is semantically equivalent to nextAfter(d, Double.POSITIVE_INFINITY); however, a nextUp implementation may run faster than its equivalent nextAfter call.

    Special Cases:

    • 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 is Double.MIN_VALUE

    Parameters:

    • d - starting floating-point value

    Returns:

    • The adjacent floating-point value closer to positive infinity.

    public static float nextUp(float f):

    • Returns the floating-point value adjacent to f in the direction of positive infinity. This method is semantically equivalent to nextAfter(f, Float.POSITIVE_INFINITY); however, a nextUp implementation may run faster than its equivalent nextAfter call.

    Special Cases:

    • 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 is Float.MIN_VALUE

    Parameters:

    • f - starting floating-point value

    Returns:

    • The adjacent floating-point value closer to positive infinity.

    The next two are a bit more complex to use. However, a direction towards zero or towards either positive or negative infinity seem the more likely and useful uses. Another use is to see an intermediate value exists between two values. One can determine how many exist between two values with a loop and counter. Also, it seems they, along with the nextUp methods, might be useful for increment/decrement in for loops.

    Math.nextAfter:

    public static double nextAfter(double start, double direction)

    • 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.

    Special cases:

    • If either argument is a NaN, then NaN is returned.
    • If both arguments are signed zeros, direction is returned unchanged (as implied by the requirement of returning the second argument if the arguments compare as equal).
    • If start is ±Double.MIN_VALUE and direction has a value such that the result should have a smaller magnitude, then a zero with the same sign as start is returned.
    • If start is infinite and direction has a value such that the result should have a smaller magnitude, Double.MAX_VALUE with the same sign as start is returned.
    • If start is equal to ± Double.MAX_VALUE and direction has a value such that the result should have a larger magnitude, an infinity with same sign as start is returned.

    Parameters:

    • start - starting floating-point value
    • direction - value indicating which of start's neighbors or start should be returned

    Returns:

    • The floating-point number adjacent to start in the direction of direction.

    public static float nextAfter(float start, double direction)

    • Returns the floating-point number adjacent to the first argument in the direction of the second argument. If both arguments compare as equal a value equivalent to the second argument is returned.

    Special cases:

    • If either argument is a NaN, then NaN is returned.
    • If both arguments are signed zeros, a value equivalent to direction is returned.
    • If start is ±Float.MIN_VALUE and direction has a value such that the result should have a smaller magnitude, then a zero with the same sign as start is returned.
    • If start is infinite and direction has a value such that the result should have a smaller magnitude, Float.MAX_VALUE with the same sign as start is returned.
    • If start is equal to ± Float.MAX_VALUE and direction has a value such that the result should have a larger magnitude, an infinity with same sign as start is returned.

    Parameters:

    • start - starting floating-point value
    • direction - value indicating which of start's neighbors or start should be returned

    Returns:

    • The floating-point number adjacent to start in the direction of direction.

提交回复
热议问题