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

Автор DorelBarbu, история, 8 лет назад, По-английски

Hello, everybody! I was solving http://codeforces.net/contest/319/problem/C which uses the "Convex Hull Trick". After many failed attempts I peeked at the AC solutions.I found a very interesting thing. This gets AC. However, if I replace the explicit conversion to double, with an explicit conversion to long long (which seems more suitable in this case, since we're not dealing with floating point numbers) I get WA, like this. Now, forgive me if it is a dumb question but I really didn't come across something like this before. Can someody help me figure it out? Thank you!

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

»
8 лет назад, # |
  Проголосовать: нравится +6 Проголосовать: не нравится

cout << (double)1000000000*1000000000 << " " << 1000000000*1000000000 << endl;

Try this and you will understand.

  • »
    »
    8 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

    I believe it's more like

    cout << (double)1000000000*1000000000 << " " << (long long)1000000000*1000000000 << endl;

    Because in the code he's referenced, the instruction was cast to long long on its both sides

    • »
      »
      »
      8 лет назад, # ^ |
      Rev. 2   Проголосовать: нравится +6 Проголосовать: не нравится

      I hope, now it will be better for you.

      long long x = (long long)1e+18;
      cout << (double)x*x << " " << (long long)x*x << endl;
      
      • »
        »
        »
        »
        8 лет назад, # ^ |
          Проголосовать: нравится +5 Проголосовать: не нравится

        This is indeed much more clearer. Learning by example is great. But what is the exact reason for this? Can a double hold bigger integers than long long?

      • »
        »
        »
        »
        8 лет назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится

        Oh thank you!

        That was useful. :D