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.
Consider below big case
1
100 1
1 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
99999999999
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
99999990000
maybe try to do it without using doubles. Correct me if I am wrong anywhere
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?
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.
Got it. So you mean using 'modulo' instead of division and checking?
$$$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.
You aren't clearing
vector<ld> v
, before appending data to it infun()