absolute-mess's blog

By absolute-mess, 2 years ago, In English

Hello Coders!

I was doing this question High School: Become Human. I had to calculate x^y and y^x and take log on both sides...x(logy) and y(logx). When I submitted my solution, it got WA on TC3...where the TC was n=6 and m=6. The answer should be '=,' but the compiler showed '<'...Code in C++17 WA on 3. So why does 6(log6) != 6(log6), according to C++17, when I submitted the code for checking the value of 6log6 in this TC. It surprised me coz now the answer was showing '='... Code in C++17 for checking. But it got failed due to the value of n1 and m1 displayed. When I tried submitting the first code using C++20, it was accepted. AC on C++20.

I tried using Long double also, but it was of no use. https://codeforces.com/contest/987/submission/140238196

Can you somehow tell me why log, C++17, and C++20 behave in this strange manner and give weird output? I don't know why people are downvoting this, but if someone has the answer, do answer this.

Thanks!

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

| Write comment?
»
2 years ago, # |
  Vote: I like it -9 Vote: I do not like it

Bahut bura hua bro !!!

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

Log always behave strange, because it's double arithmetic

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

Different compilers will work with floating point numbers differently. This is the reason why one should avoid using them if possible. That's all I know.

»
2 years ago, # |
  Vote: I like it +9 Vote: I do not like it

I have previously written a blog about exactly this weirdness: Explanation of weird/strange floating point behaviour in C++

The difference in floating point behaviour comes from the difference of compiling in 32 bit vs 64 bit.

WA in C++17 (32 bit) 140239565

AC in C++17 (64 bit) 140305850

AC in C++17 (32 bit) if you use a pragma to turn off most of the weird floating point behaviour 140305892

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

    I read your blog and got my solution. Thanks a lot!

    One doubt: Will turning off weird floating-point behavior using pragma, will affect in some other way?

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

      I'm really not sure what you are asking.

      I didn't show that pragma to tell you to use it. What you should do to avoid weird floating point issues in the future is to only use 64 bit C++. 64 bit C++ is superior in pretty much every way. To be honest, the only reason cf still supports 32 bit C++ is because of backwards compability.