rohit_1402's blog

By rohit_1402, 5 years ago, In English

In educational round 88 Problem C I have submitted my solution in GNU C++17 (64) it gives WA on test 4 at the time of contest. But today I have submitted my same code in GNU C++ 17 and it is accepted.

Code submitted in GNU C++ 17 (64)

Code submitted in GNU C++ 17

Both are the same code

Can anyone tell me why it is so??

UPD: Now I have use long double instead of double and submitted the same code using GNU C++17 (64) compiler and it is AC. But by just using double why it was giving WA using GNU C++17 (64) and AC using GNU C++17 ?

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

same happened with me.

»
5 years ago, # |
Rev. 2   Vote: I like it +4 Vote: I do not like it

Try to use abs(a - b) < EPS instead of a == b.

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

    From the next time I will use it. But I am asking that why my code gives WA using GNU C++ 17 (64).

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by rohit_1402 (previous revision, new revision, compare).

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

Try running this code using g++ 17 and g++ 17 (64)

Sample_code

You can read the 4th point here to understand the details of what is happening here.

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

    I'll summarize why you had that error.

    The problem is that according to the standard a C++ compiler can sometimes do the internal calculations using a larger data type. And indeed, g++ sometimes internally uses long doubles instead of doubles to achieve larger precision. The value stored is only typecast to double when necessary. Source (Last Rumor)

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It is not compiler's bug. Read http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html to understand that floating-point arithmetic has rules on its own.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Raid works wonders against bugs.

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

this causes me to -75 in the last contest.....

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

I just posted a blog on the subject https://codeforces.net/blog/entry/78161 . In 32 bit g++ all floating point arithmetic is done using (80 bit) long double, which creates a kind of excess precision. However in 64 bit g++ it does the floating point arithmetic using each corresponding type. This results in less precision but also more "deterministic" behavior.

It possible to turn on excess precision even on 64 bit g++, this is done with the flag -mfpmath=387. With it your code gets AC in 64 bit g++ 81987643. It is also possible to make you get WA in 32 bit g++ by turning off excess precision using -mfpmath=sse -msse2 81995764.