Bug in codeforces c++ compiler?

Today I was solving one problem from codeforces past contests. And I found that codeforces and vscode are giving different output for same code.
Problem link is Problem - 576A - Codeforces
My submission link is Submission #93032729 - Codeforces
Codeforces is showing wrong answer on test case 2.

Annotation 2020-09-17 151226

But vscode and other reliable online compilers are giving output same as of jury’s.
Codechef IDE

Ideone

Can anybody tell me why codeforces is giving different output for same code ? Is this bug or Am I missing something.

Precision error for sqrt() function may be the case.

1 Like

Thanks… sqrt() function was creating problem. But why only on codeforces, sqrt() function was creating problem?

1 Like

It’s because sqrt() returns double and the way these floats are stored in the bits create precision error while doing direct comparison with ==.

There are many problems based on the precision only.
For a fun activity you can run the following program. It’ll print 0 in C++.
double a = 0.1;
double b = 0.2;
cout << ((a+b) == 0.3);

You can learn more about it on google.
Float representation
Gfg article

2 Likes

Ok, now I understand. I will take care of this thing in future. Thanks for help

1 Like

Np. I was unaware of this thing for a long time.

For comparison, suppose if you want to check if a equals b, then rather than doing plain a == b, you can check if their absolute difference is less than a very minimal quantity, say EPS = 1e^-6. So basically it’s same as abs(a-b) < EPS.

And you can enable all warnings in your working environment to remind you for the same. Specifically just add -Wall or -Wfloat-equal to your VSCode settings and if you are using CodeRunner extenstion, then to its running command. (Y)

2 Likes