Please read the new rule regarding the restriction on the use of AI tools. ×

kakhatake's blog

By kakhatake, history, 6 years ago, In English

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!!

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

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

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

code

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

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

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

    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 years ago, # ^ |
        Vote: I like it +13 Vote: I do not like it

      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 years ago, # ^ |
        Vote: I like it +16 Vote: I do not like it

      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 years ago, # |
Rev. 2   Vote: I like it -24 Vote: I do not like it

[Deleted] Wrong Info