What types of numbers are representable in binary floating-point?

后端 未结 4 1032
广开言路
广开言路 2020-11-30 02:35

I\'ve read a lot about floats, but it\'s all unnecessarily involved. I think I\'ve got it pretty much understood, but there\'s just one thing I\'d like to know for

4条回答
  •  情书的邮戳
    2020-11-30 02:57

    A finite number can be represented in the common IEEE 754 double-precision format if and only if it equals M•2e for some integers M and e such that -253 < M < 253 and -1074 ≤ e ≤ 971.

    For single precision, -224 < M < 224 and -149 ≤ e ≤ 104.

    For double-precision, these are consequences of the facts that the double-precision format uses 52 bits to store a significand (which normally has 53 bits due to an implicit 1) and uses 11 bits to store an exponent. 11 bits encodes numbers from 0 to 2047, but 0 and 2047 are excluded for special purposes, and the encoded number is biased by 1023, so it represents unbiased exponents from -1022 to 1023. However, these unbiased exponents are for significands in the interval [1, 2), and those significands have fractions. To express the significand as an integer, I adjusted the exponent range by 52. Single-precision is similar, with 23 bits to store a 24-bit significand, 8 bits for the exponent, and a bias of 127.

    Expressing the representable numbers using an integer times a power of two rather than the more common fractional significand simplifies some number theory and other reasoning about floating-point properties. I used it in this answer because it allows the set of representable values to be expressed concisely.

提交回复
热议问题