Problem Link : 1995C - Squaring
I have tried to implement the float method from the editorial.
I am using base 2 for log here. In the first code, I am calculating the difference of b[i-1] and b[i], tmp is the number of times log(2)=1 has to be added into b[i]. If b[i-1]-b[i]>0, We are adding it's ceil value to the total operations ( ops ) and also to b[i] itself.
However this solution gives wrong answer on test-case 2, my guess is my outputs are bigger than answer expected.
Note: I have removed input templates from codes to make them short, rest is intact. You can also refer to the submission links.
Code with wrong answer
submission : 272991530
While this code works
submission : 272991530
Using direct floating point arithmetic is a gamble if you don't understand what you are doing.
Then what is the solution, how to avoid such errors.
I am really curious about how the second approach avoids floating point errors.
I don't trust FP in this problem either. I'd rather do a full integer-based solution.
For this one, the
eps
used in AC solution helped a little bit in circumventing. TL;DR there might be cases where the "equal" comparison for FP numbers aren't accurate, somehow making two identical number having slight errors (turning $$$0$$$ into $$$10^{-15}$$$ or something that similarly low, pushingceil
to $$$1$$$).eps
sets a threshold for ignored error.