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)
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 ?
same happened with me.
Try to use
abs(a - b) < EPS
instead ofa == b
.From the next time I will use it. But I am asking that why my code gives WA using GNU C++ 17 (64).
Auto comment: topic has been updated by rohit_1402 (previous revision, new revision, compare).
Try running this code using g++ 17 and g++ 17 (64)
You can read the 4th point here to understand the details of what is happening here.
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)
Thank you so much bro!
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.
Raid works wonders against bugs.
this causes me to -75 in the last contest.....
I just posted a blog on the subject https://mirror.codeforces.com/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.