biggest integer that can be stored in a double

前端 未结 7 1929
清歌不尽
清歌不尽 2020-11-22 01:09

What is the biggest \"no-floating\" integer that can be stored in an IEEE 754 double type without losing precision ?

相关标签:
7条回答
  • 2020-11-22 01:18

    The biggest/largest integer that can be stored in a double without losing precision is the same as the largest possible value of a double. That is, DBL_MAX or approximately 1.8 × 10308 (if your double is an IEEE 754 64-bit double). It's an integer. It's represented exactly. What more do you want?

    Go on, ask me what the largest integer is, such that it and all smaller integers can be stored in IEEE 64-bit doubles without losing precision. An IEEE 64-bit double has 52 bits of mantissa, so I think it's 253:

    • 253 + 1 cannot be stored, because the 1 at the start and the 1 at the end have too many zeros in between.
    • Anything less than 253 can be stored, with 52 bits explicitly stored in the mantissa, and then the exponent in effect giving you another one.
    • 253 obviously can be stored, since it's a small power of 2.

    Or another way of looking at it: once the bias has been taken off the exponent, and ignoring the sign bit as irrelevant to the question, the value stored by a double is a power of 2, plus a 52-bit integer multiplied by 2exponent − 52. So with exponent 52 you can store all values from 252 through to 253 − 1. Then with exponent 53, the next number you can store after 253 is 253 + 1 × 253 − 52. So loss of precision first occurs with 253 + 1.

    0 讨论(0)
  • 2020-11-22 01:23

    You need to look at the size of the mantissa. An IEEE 754 64 bit floating point number (which has 52 bits, plus 1 implied) can exactly represent integers with an absolute value of less than or equal to 2^53.

    0 讨论(0)
  • 2020-11-22 01:29

    DECIMAL_DIG from <float.h> should give at least a reasonable approximation of that. Since that deals with decimal digits, and it's really stored in binary, you can probably store something a little larger without losing precision, but exactly how much is hard to say. I suppose you should be able to figure it out from FLT_RADIX and DBL_MANT_DIG, but I'm not sure I'd completely trust the result.

    0 讨论(0)
  • 2020-11-22 01:30

    Wikipedia has this to say in the same context with a link to IEEE 754:

    On a typical computer system, a 'double precision' (64-bit) binary floating-point number has a coefficient of 53 bits (one of which is implied), an exponent of 11 bits, and one sign bit.

    2^53 is just over 9 * 10^15.

    0 讨论(0)
  • 2020-11-22 01:31

    1.7976931348623157 × 10^308

    http://en.wikipedia.org/wiki/Double_precision_floating-point_format

    0 讨论(0)
  • 2020-11-22 01:36

    The largest integer that can be represented in IEEE 754 double (64-bit) is the same as the largest value that the type can represent, since that value is itself an integer.

    This is represented as 0x7FEFFFFFFFFFFFFF, which is made up of:

    • The sign bit 0 (positive) rather than 1 (negative)
    • The maximum exponent 0x7FE (2046 which represents 1023 after the bias is subtracted) rather than 0x7FF (2047 which indicates a NaN or infinity).
    • The maximum mantissa 0xFFFFFFFFFFFFF which is 52 bits all 1.

    In binary, the value is the implicit 1 followed by another 52 ones from the mantissa, then 971 zeros (1023 - 52 = 971) from the exponent.

    The exact decimal value is:

    179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368

    This is approximately 1.8 x 10308.

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