Блог пользователя DCSekhara

Автор DCSekhara, история, 22 месяца назад, По-английски

Suppose we are working in a setting where we only have integers and the operations that we are doing on them are multiply, add, subtract and comparisons between them

In such a scenario is it safe to replace all integers with doubles? (Safe in the sense of precision issues)

  • Проголосовать: нравится
  • +3
  • Проголосовать: не нравится

»
22 месяца назад, # |
  Проголосовать: нравится -23 Проголосовать: не нравится

I think it is safe, since I've never had any problems. Only comparisons may be the problem because of decimal places, but you'll never have those decimal places anyway since you are working just with integers.

»
22 месяца назад, # |
Rev. 2   Проголосовать: нравится +18 Проголосовать: не нравится

There are so-called safe integers--the integers which are exactly representable in a double type. The name comes from ECMAScript, which does not even know what an integer is and always uses floating-point numbers (barring some corner cases). All integers from $$$-2^{53}$$$ to $$$2^{53}$$$ are safe, which means you can treat double just like an integral type as long as you stay within these bounds. For a 80-bit IEEE 754 type, that is, long double on *nix GCC, the range is $$$-2^{64}$$$ to $$$2^{64}$$$.

»
22 месяца назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится

Hmm probably depends on what kind of integers and what kind of doubles we're talking about right?

Because double has gaps between very big integers and for sufficiently big integers we could probably use that.

»
22 месяца назад, # |
  Проголосовать: нравится +16 Проголосовать: не нравится

Why would you want to do this though

  • »
    »
    22 месяца назад, # ^ |
      Проголосовать: нравится +4 Проголосовать: не нравится

    I saw a question which had to deal with 100 bit integers. I did it using a custom BigINT class but some other solution that just used double passed. So hence I got the doubt..

»
22 месяца назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

nice blog