nine.nine's blog

By nine.nine, 4 years ago, In English

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.

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

| Write comment?
»
4 years ago, # |
Rev. 2   Vote: I like it +16 Vote: I do not like it

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 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    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 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      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 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

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

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

          $$$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 years ago, # |
  Vote: I like it +1 Vote: I do not like it

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