WA to AC by changing datatypes.

Given below are the two submissions for the problem http://www.codechef.com/problems/PRAC. The first submission gives a WA whereas changing the datatype of the variables yields AC.Anyone willing to explain?

The first code got WA because float datatype wasn’t able to hold the answer for some input values. In the second one you used double instead of float. Now, since the size of double (in terms of range and precision) is greater than that of float datatype, the solution got accepted.

More on Datatypes

The range and precision of values that a double can hold is far more than the range and precision of values a float can hold. In the above cases, you must have lost precision of values during computation.

You are loosing precision in case of float which is maintained in case of double.

Single precision (float) gives you 23 bits of significand, 8 bits of exponent, and 1 sign bit.

Double precision (double) gives you 52 bits of significand, 11 bits of exponent, and 1 sign bit.

For example if a=5.814797187197 and b=6.9841984716 and we need to calculate a*b then,

  1. if a and b are defined as float => a*b = 40.611700
  2. if a and b are defined as double => a*b = 40.611698

See that the last two digits are different.

Edit 1: Ohh the question has been answered, didn’t see that.

Now for this question the constarints are below 500 and most probably an a value like 4.00,9.00,198.00 and not 18.073838.Still I think I have got my answer…Thanks a lot…:slight_smile: