I got wrong answer with this solution:
http://codeforces.me/contest/568/submission/12456152
And i got accepted with this solution:
http://codeforces.me/contest/568/submission/12456422
I think both of them should get accepted. Can someone help me?
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
I got wrong answer with this solution:
http://codeforces.me/contest/568/submission/12456152
And i got accepted with this solution:
http://codeforces.me/contest/568/submission/12456422
I think both of them should get accepted. Can someone help me?
Name |
---|
Well, comparing doubles like this leads to unpredictable results. Use integers whenever possible, and when not, use epsilon when comparing two doubles (or comparing double with integer).
http://codeforces.me/contest/568/submission/12457494
I know but i just changed the char string into a string. I didn't change anything about doubles and it got accepted. Why?
That's what unpredictable means.
I think it's so stupid.
Thinking so will not help you in the future. Instead, you should try to understand why floating-point numbers work the way they do (there are good reasons), and how they should actually be used.
So why do they work this way? Do you agree that in both cases the bits stored in these double variables are the same? And if the bits are the same, any operations must produce the same result, right? What am I missing? Don't tell me this logic is wrong.
You'd think that even though there are precision errors, doing the same thing should lead to the same results, but there's also top answer here: link
Why is
cos(x) != cos(y)
even thoughx == y
?Okay, got it, so instructions generated by compiler do different things from what the source code says. One more question: why this shit is turned on?
The very first sentence from the article above:
You expect it to produce exact results, while it only produces approximate results.
No, you did not "get it". The instructions generated by the compiler do exactly what the source says. The problem here is that you are reading the source incorrectly.
More precisely, you are making assumptions you should not make, such as "==(double,double) is a test whether two real numbers are equal".
Do read the article andreyv posted, including the previous two questions before the one with the cosines.
The big picture here is that the IEEE standard for floating-point numbers was designed to be platform-independent. This is necessary, because there have been, and to some extent there still are, wild differences between different platforms. The main thing the standard gives you are guarantees on the least amount of precision that will be used to perform your calculations. E.g., if you use a "double" anywhere in your code, you have the guarantee that all calculations with that value will be done with at least the 53 most significant bits of the number. The computer is allowed to use more precision whenever it's available, and that (along with rounding errors) is one of the sources of all this confusion.
All of this is actually a good thing: it allows the compilers to optimize your code better and to make it run faster, and it also makes well-written code portable to different architectures.
There are some niche cases (way more frequent in competitive programming than in "real life") where you don't want the default floating-point behavior. In those cases, you always have the option to avoid using them, and to replace them by your own implementation that is exact.
That is also a good advice for competitive programming. You can certainly use doubles in many tasks, if you really know what you are doing. But if you don't, it's much safer to avoid them and use integers only. (Integers can be used, for example, to represent exact fractions, and also to represent fixed-point arithmetic -- e.g., if the input are numbers like "$0.47", just count everything in cents instead of dollars.)
You are right, but it is hard to decide because
I used long long integer to avoid double problems but it got wrong answer because of overflow:
http://codeforces.me/contest/500/submission/9326220
When i used double it passed:
http://codeforces.me/contest/500/submission/9326317
================================================
Very nice, so I should make an assumption "==(double,double) is a test whether (two real numbers are equal) and (the moon is in its certain phase)", right? Even if they are the return values of the same function called with the same arguments? And have the same binary representation (I'm not talking about NaN-s)? Don't you find it illogical?
In the example with cosines, I read the code
cos(x) == cos(y)
as "Calculatecos(x)
as double (the function returns double, so I expect the result to be double), then calculatecos(y)
as double, then compare them", and it's the meaning everyone would think, probably except C++ compiler authors, who understand it as "calculatecos(x)
, store it as double, then calculatecos(y)
as 80-bit number (WTF?), then compare them".Why would you ever want to assume doubles can be precise? It'll screw you over sometimes and you can always replace equality checking by abs() < eps checking.
With sufficient lack of background knowledge, anything can seem illogical. In this case, if you know what the code appears to do, but not what it's translated to on compile time, then it will seem illogical — until you realise that equality checking in general has little meaning with doubles for unrelated reasons, anyway. Garbage in, garbage out.
The way I see it, you have 2 options (not counting throwing a tantrum):
You don't like how C++ compiler/s handle this? Make your own and make it popular.
Deal with it; it doesn't negatively affect your life and there are reasons for it.
Thanks anyway.
Твое решение не принято потому что неправильный ответ на тест 4 9. А второе принято потому что все правильные ответы
I tried the case 4 9 in g++ and it gives the right answer. You can try it too if you want.
Тогда не знаю попробуй g++.exe -static -DONLINE_JUDGE -lm -s -x c++ -Wl,--stack=268435456 -O2 -o {filename}.exe {file}
Это может зависить от всяких оптимизаций.
You can copy my code and try it on this page:
http://www.tutorialspoint.com/compile_cpp_online.php
Bera, in your code you use
long long int
. Where is differentlong long
orlong long int
...int
orlong
in C++?Thanks for advice.
There is no difference between long long and long long int. I wrote int then i realized that i should use long long and i added long long.
I cannot help you. So go away.
akulsareen wrote this :X