biggest integer that can be stored in a double

前端 未结 7 1917
清歌不尽
清歌不尽 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.

提交回复
热议问题