mkagenius's blog

By mkagenius, 13 years ago, In English

I think it has happened a while ago on codeforces itself, but I forgot the reason why this happens:
See this code and code below.
http://ideone.com/N7vzQ

and added 1 "cout"  to the code at 119 and result changes:
http://ideone.com/OXHpO

By adding cout <<" "; should not change 39 to 2. :|


My guess is , its compilers problem.
Thanks in Advance.

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

13 years ago, # |
  Vote: I like it +5 Vote: I do not like it

Do you have -O2 enabled?
If yes, it's very common effect. Optimizer is guilty. This cout 'foribs' optimization of some code. It can change execution result, if you have arrays overflow or something like this.
Try to compile the first one without optimization and compare results.


  • 13 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it
    Thanks.
    Yes , you are right , Optimizer is guilty.

    In that case, if we are participating in online contest(where we cannot modify compiler-options) , shall we use "eps" as Alias told?

    But that means whenever I will compare doubles , I will have to use eps,
    that sounds like a headache. :(
    • 13 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it
      You always need to use eps. All floating point numbers are saved with some error. In computer 0.333333333333333333 can be equal to 1.0 / 3.0. It's not really good idea to hope that this error is less than type's precision and we don't need eps.