yash_coder's blog

By yash_coder, 13 years ago, In English

In codeforces round 106 Div 2, for problem B I submitted a code which failed on pretest 1 but when I executed the same code for that pretest the output was correct and the output of my program as shown by the judge was different from the one that I was getting on my system. That is I was getting right output but the one shown by the judge for my program on that test case was wrong. Kindly, look into this matter and figure out what's wrong ? My code id 1167715.

  • Vote: I like it
  • +4
  • Vote: I do not like it

»
13 years ago, # |
Rev. 3   Vote: I like it +11 Vote: I do not like it

You are using function pow() — calculation is doing in float numbers, so results may differ from machine to machine depending on compile flags.

  • »
    »
    13 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    But even after typecasting it to (int) isn't working. Do I need to use something else other than pow() function for the same ?

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

      In fact, when you work with floats/doubles (in spite of compiler), you should remember that any number (including integers) can be stored like X+-EPS. So, pow(3, 3) can be 26.9999999999999. If you cast it to int, it becomes 26, not 27. It's better to use something like (int)(pow(3, 3) + 1e-9) here. There is a very good article on TopCoder about floating point arithmetics.

      You can try MSVC++ compiler instead of GCC. GCC's optimizer can give very strange results with floating point numbers, and they think that it's well-known not bug