alpercakan's blog

By alpercakan, history, 9 years ago, In English

For the problem UVa-579 I get AC if I use ((h*60)*0.5)+(m*0.5)-(m*6) for calculation, but if I use (h*30+m/2)-(m*6) I get WA. I know that it's something about precision problem (like comparison), but answer is printed with 3 digits after decimal point; so I didn't think this kind of change in calculation would be a problem. I don't know what's the best way for this kind of calculations. How should I calculate?

  • Vote: I like it
  • 0
  • Vote: I do not like it

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

Do you read h and m as integers or as floats?

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

In the second formula m/2 would evaluate to an integer if m is of integer type. To avoid that either change the type of m or use m/2.0 which will show that you require floating type result :)

  • »
    »
    9 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    Sorry, I typed that wrong. Actually I use 2.0 but I get WA

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

      Can you post the actual program. That might help making it clear what the issue is.

      • »
        »
        »
        »
        9 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it
        #include <cstdio>
        #include <vector>
        
        using namespace std;
        
        int main()
        {
        	int h, m;
        	vector<double> r;
        	while (scanf(" %d:%d", &h, &m) == 2)
        	{
        		if (h == 0 && m == 0) break;
        		
        		double d1 = 30*h + m/2.0, d2 = (6 * m);
        
        		d1 -= d2;
        
        		if (d1 < 0) d1 *= -1;
        		if (d1 > 180) d1 = 360 - d1;
                        ///if (d1 == 180) d1 = 0;
        
        		r.push_back(d1);
        	}
        
        	for (int i = 0; i < r.size(); ++i)
        		printf("%.3f\n", r[i]);
        
        	return 0;
        }
        
        • »
          »
          »
          »
          »
          9 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Can you also post the code that got AC?

          • »
            »
            »
            »
            »
            »
            9 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it
            double diff = ((h*60)*0.5)+(m*0.5)-(m*6);
            
            if (diff < 0) diff *= -1;
            if (diff > 180) diff = 360 - diff ;
            ///if (diff == 180) diff = 0;
            
            r.push_back(diff );
            

            Rest is the same.

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

              You must have had a mistake somewhere else. I get AC for both versions of your code.

              Also, there is no reason for the code to be imprecise -- each value is either an integer or an integer_and_a_half, and those can be represented exactly in a double. There are no precision errors anywhere.

              My best guess at the moment is that Enchom was right and that the version that got WA actually used integer division by accident.

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

          can u try this one (h*30+(m*0.5))-(m*6) , I wonder