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

Автор nine.nine, 4 года назад, По-английски

In one of the recent problems (Link), I got the right (expected) output in VS Code compiler but WA on cf (Link). I think this is due to comparing the long double values. I even tried using

(cur - check > 1e-15) and (fabs(cur - check) > 1e-15)

instead of

cur > check

but still did not work. Can someone please help? Thanks in advance.

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

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

Consider below big case

Testcase
Correct answer

In your binary search, you set a higher limit(variable right) as 1e9 which is not high enough. even after setting a limit high enough. Your code is giving wrong answer on the custom invocation

Your answer

maybe try to do it without using doubles. Correct me if I am wrong anywhere

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

    Thanks for the heads up! I did think of the test case here, But even 1e-15 didn't work out as you rightly pointed. Also, changing the data type from long double to double doesn't turn up too. Any generalized conclusion for working with floating-point numbers especially during their comparison?

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

      The way you compared floating-point numbers was right. I don't know if there is any better way. Just try to avoid using float/double whenever you can.

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

        Got it. So you mean using 'modulo' instead of division and checking?

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

          $$$a/b >= c/d$$$ instead just check $$$a * d >= b * c$$$(careful about overflow).

          this is just an example. there might be any other ways to avoid doubles in different cases.

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

You aren't clearing vector<ld> v, before appending data to it in fun()