naevis's blog

By naevis, history, 6 years ago, In English

If double can store larger value than long long, then why everyone rarely used double?

  • Vote: I like it
  • +11
  • Vote: I do not like it

»
6 years ago, # |
  Vote: I like it +71 Vote: I do not like it

Because double can't accurately store integers whose absolute value exceeds $$$2^{53}$$$. Take a look at this:

cout << (long long)(double)123451234512345677LL << '\n';

This will not print what you might expect. To avoid such situations, double should be avoided in favor of long long when an exact algorithm is needed and all input/output/intermediate variables can be represented as integers. Also, integer types tend to be significantly faster.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    This is not entirely true, double can store accurately integers over 2^53 as long as the integer can be represented with 53 bits multiplied by a power of two (within 11 bits). For example, 2^100 has only 1 bit and a power of two of 100 that can fit with 11 bits, so it can be stored without a loss of precision and can be tested for direct equality without abs(num1-num2)<epsilon trick.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +32 Vote: I do not like it

      I didn't say that all such integers can't be represented, the point is that some can't.

      • »
        »
        »
        »
        6 years ago, # ^ |
        Rev. 4   Vote: I like it -17 Vote: I do not like it

        Downvote me, please, but choose one :)

        • "double can't accurately store integers whose absolute value exceeds 2^53"

        • "I didn't say that all such integers can't be represented"

        Formally, your first statement states that if an integer is above 2^53, it can't be represented correctly, so any such integer being represented correctly contradicts your statement, so the "all" is implied here.

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it +11 Vote: I do not like it

          If you can't accurately hit a target that doesn't mean that you will never hit it. The negation of accurate is not "always misses" but "does not always hit" — look up negation of logic quantifiers.

          • »
            »
            »
            »
            »
            »
            6 years ago, # ^ |
            Rev. 4   Vote: I like it -8 Vote: I do not like it

            accuracy (n) — the quality or state of being correct or precise.

            not accurate — not being correct or precise, NOT low % of being correct or precise :)

            • »
              »
              »
              »
              »
              »
              »
              6 years ago, # ^ |
                Vote: I like it +8 Vote: I do not like it

              I give up, check back in a year

              • »
                »
                »
                »
                »
                »
                »
                »
                6 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                Appreciated, thanks, you gave hope to all the blues :)

            • »
              »
              »
              »
              »
              »
              »
              6 years ago, # ^ |
                Vote: I like it +3 Vote: I do not like it

              That's where you're wrong. Let accuracy(n) be $$$A$$$ and the quality or state of being correct be $$$C$$$. Then $$$A \rightarrow C$$$ $$$\equiv$$$ $$$\neg C \rightarrow \neg A$$$, but it is not the same as $$$\neg A \rightarrow \neg C$$$.

              That is, accuracy implies it is correct, and not correct implies that is is not accurate, but not accurate does not imply it is not correct.

              It may be correct or may not be, but it is in no way a guarentee.

              • »
                »
                »
                »
                »
                »
                »
                »
                6 years ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                You are mistaking definition (iff) with implication.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  6 years ago, # ^ |
                  Rev. 2   Vote: I like it +3 Vote: I do not like it

                  The thing is it is not a bi-implication(iff), but an implication. If it is correct, it in no way means it is accurate. Maybe it was correct with very low probability on that experiment, and thus has low accuracy, ie not accurate.

                • »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  »
                  6 years ago, # ^ |
                  Rev. 2   Vote: I like it 0 Vote: I do not like it

                  So in your logic if something is not white, it may or may not be white :D

                  I'm giving up now too, back in a year.