Representing integers in doubles

前端 未结 5 1008
难免孤独
难免孤独 2020-11-29 08:18

Can a double (of a given number of bytes, with a reasonable mantissa/exponent balance) always fully precisely hold the range of an unsigned integer of half that number of by

5条回答
  •  醉话见心
    2020-11-29 09:13

    I wouldn't use the words "fully precisely" when talking about floating-point numbers. But yes, a double can represent a 32-bit integer.

    I do not know which other combinations of floats and ints that this is also true for.

    Practically speaking, you don't want to bother using floating point above what your machine supports, so just switch to rational arithmetic with bignums. That way, you're guaranteed precision.

提交回复
热议问题