| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Problem on HouseBoat(1005)I Think I Need a Houseboat
in this problem, I have this solution:
#include <stdio.h>
#include <math.h>
int main()
{
int t,p = 1;
scanf("%d",&t);
while(p <= t)
{
double x,y;
int i=1;
scanf("%lf%lf",&x,&y);
double a = (x*x + y*y)*M_PI_2;//please note this line.
while(a > i*50)
{
i++;
}
printf("Property %d: This property will begin eroding in year %d.\n",p++,i);
}
printf("END OF OUTPUT.");
return 0;
}
So, here: double a = (x*x + y*y)*M_PI_2
Obviously, this line calculates the area of the circle that pass through the point we are analyzing.
mathematically, if x*x + y*y = r*r and PI*r*r = A
then PI*(x*x + y*y)=A, right?
yet the result is not accepted by the judge. Instead, to be accepted, the code must look as follows:
#include <stdio.h>
#include <math.h>
int main()
{
int t,p = 1;
scanf("%d",&t);
while(p <= t)
{
double x,y;
int i=1;
scanf("%lf%lf",&x,&y);
double r = sqrt(x*x + y*y);
double a = r*r*1.57;
while(a > i*50)
{
i++;
}
printf("Property %d: This property will begin eroding in year %d.\n",p++,i);
}
printf("END OF OUTPUT.");
return 0;
}
1.57 being PI/2 where PI = 3.14
Now, the question is, how is it possible that using 3.14 as PI value, and calculating the square root of a number, and then squaring the result, we can come up with a more accurate result than just making a substitution in a formula and using the arbitrary precision M_PI_2 value provided by the C++ standard?
I mean, in my understanding, the lesser operations, the more accurate the result will be, wouldn磘?
I just don磘 see any reason why I should calculate the square root and then square the result to use the number, when I can directly use the number without that precision-losing calculations.
if anyone agrees with me, please let me know. And more importantly, if anyone disagrees with me, please tell me why am I wrong about this.
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator