What are the INFINITY constants in Java, really?

前端 未结 3 1354
萌比男神i
萌比男神i 2020-12-25 12:22

I just recently ran across the constants in the primitive type wrapper classes like Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY. In the

相关标签:
3条回答
  • 2020-12-25 12:44

    The IEEE Standard 754 Floating Point Numbers states:

    "The values +infinity and -infinity are denoted with an exponent of all 1s and a fraction of all 0s. The sign bit distinguishes between negative infinity and positive infinity. Being able to denote infinity as a specific value is useful because it allows operations to continue past overflow situations. Operations with infinite values are well defined in IEEE floating point."

    Also: "Operations on special numbers are well-defined by IEEE. In the simplest case, any operation with a NaN yields a NaN result. Other operations are as follows:"

          Operation         Result
            n ÷ ±Infinity   0
    ±Infinity × ±Infinity   ±Infinity
    ±nonzero  ÷ 0           ±Infinity
    Infinity  + Infinity    Infinity
           ±0 ÷ ±0          NaN
     Infinity - Infinity    NaN
    ±Infinity ÷ ±Infinity   NaN
    ±Infinity × 0           NaN
    
    0 讨论(0)
  • 2020-12-25 12:46

    They do indeed represent positive and negative infinity, which are clearly defined concepts in the IEEE floating point standard. For example, dividing a positive floating point number by zero yields positive infinity. As for the bit pattern itself, it is just a pattern that has been chosen to represent infinity.

    0 讨论(0)
  • 2020-12-25 12:54

    Java floating point is based on the IEEE 754 binary floating point standard Floating Point Standard, the first version of which was issued in about 1985, so it is much older than Java. Given widespread hardware implementation of IEEE 754 by the time Java was being defined, the Java creators had little choice.

    Each IEEE 754 floating point number has three components, a sign bit, an exponent, and a mantissa. Simplifying considerably, the magnitude of a normal number is:

     mantissa * (2 ** exponent)
    

    where "**" represents power.

    The leading bit is the sign bit. In doubles, the next 11 bits are the exponent.

    The bit patterns with all the exponent bits on are reserved for infinities and NaNs. All normal numbers have at least one zero bit in the exponent. The two infinities are represented by having all exponent bits on, and all mantissa bits zero. The leading sign bit distinguishes positive and negative infinity.

    The choice of all exponent bits one for the special cases is not arbitrary. It is easier to chop off one of the extremes than to deal with a gap in the middle of a range of numbers, especially for hardware implementations. Taking the all bits off exponent for special cases would have prevented encoding zero with the all bits off pattern, and would have given the largest absolute magnitude values, the infinities, the smallest exponent, which would have also made hardware more complicated. The all bits on exponent is definitely the best choice for the infinities.

    The two infinities are both used to represent two things, actually infinite results and results that are too large in absolute magnitude to represent in the normal number system, numbers larger than Double.MAX_VALUE or smaller than -Double.MAX_VALUE. 1.0/0.0 is infinite. So is 2*Double.MAX_VALUE.

    There are some algorithms that can be simplified, with fewer special cases, by allowing intermediate results to be infinite, in either sense. Doing so also allows e.g. even a line parallel to the y-axis to have a storable gradient that can be used in calculations.

    0 讨论(0)
提交回复
热议问题