Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя kakhatake

Автор kakhatake, история, 6 лет назад, По-английски

Hey there, I was solving 789B and I had this very simple and short code but it just fails on test 3 although it works perfectly on my machine. I have read that undefined behavior can induce this but I can't recognize what's wrong with what I typed. Here's the code:

code

Thank you!!

  • Проголосовать: нравится
  • -8
  • Проголосовать: не нравится

»
6 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I've added outputting an empty string before the if and got AC. I don't know why it works.

code

»
6 лет назад, # |
  Проголосовать: нравится +13 Проголосовать: не нравится

You compare doubles using ==. Don't do that.

  • »
    »
    6 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    In general, yes, you're right. But why do r1 and r2 differ in this case (for the input data 6 6)?

    If x is 6 and y is 6, then surely r1 == r2 is true:

    r1 = x*log10(y);
    r2 = y*log10(x);
    if(r1 == r2) cout << "=";
    

    Why adding cout<<""; changes the result of the test (see my comment above)?

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +13 Проголосовать: не нравится

      In short, r1 == r2 is true if they are both 64bit floats, compiler may calculate and store one of them in 80bit register (for optimization), and 80bit logarithm != 64bit logarithm. And cout disables this optimization.

      This issue was discussed here (and not here) million times, but every time there is a person who didn't read these discussions.

    • »
      »
      »
      6 лет назад, # ^ |
        Проголосовать: нравится +16 Проголосовать: не нравится

      Actually, not. Even for the following example:

      double x, y;
      cin >> y;
      x = y;
      ... // x and y are not changed
      if (x == y) // there is no guarantees that it's true
      
»
6 лет назад, # |
Rev. 2   Проголосовать: нравится -24 Проголосовать: не нравится

[Deleted] Wrong Info