英文:
How to find empty space in a rectangle which has 2 circles in it?
问题
我在 Toph 上解决了一个问题。在这个问题中,我需要找出一个矩形中两个相等圆的空白空间。
#include <stdio.h>
float pi=3.1416;
int main()
{
int i,t;
float r,rest;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%f",&r);
rest=(4*r*2*r)-(2*pi*r*r);
printf("Case %d: %.2f\n",i,rest);
}
return 0;
}
这是我的解决方案。它对第一个测试案例返回了正确的值,但未能解决第二个问题。
问题是什么?
英文:
I'm solving a problem in Toph . In this problem I've to find out the empty space of a rectangle which has 2 equal circles in it.
#include <stdio.h>
float pi=3.1416;
int main()
{
int i,t;
float r,rest;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%f",&r);
rest=(4*r*2*r)-(2*pi*r*r);
printf("Case %d: %.2f\n",i,rest);
}
return 0;
Here is my solve. It returns a correct value for first test case but it fails to solve the second one.
What's the problem???
答案1
得分: 1
<strike>```float pi=3.1416;``` 是问题的原因。在数学头文件 (```#include <math.h>```) 下有一个常数 ```M_PI```,请使用它代替。</strike>
Edit:
抱歉,没有仔细阅读,显然问题出在浮点精度上。如果你将所有的 float 值改成 double,应该就可以工作。
```c
#include <stdio.h>
double pi=3.1416;
int main()
{
int i,t;
double r,rest;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%lf",&r);
rest=(4*r*2*r)-(2*pi*r*r);
printf("Case %d: %.2lf\n",i,rest);
}
return 0;
}
<details>
<summary>英文:</summary>
<strike>```float pi=3.1416;``` is the cause of the problem. Under the math header file (```#include <math.h>```) there is a constant ```M_PI``` use it instead.</strike>
Edit:
Sorry, didn't read thoroughly, apparently the problem is in the floating point precision. If you change all float values into double it should work.
```c
#include <stdio.h>
double pi=3.1416;
int main()
{
int i,t;
double r,rest;
scanf("%d",&t);
for(i=1;i<=t;i++)
{
scanf("%lf",&r);
rest=(4*r*2*r)-(2*pi*r*r);
printf("Case %d: %.2lf\n",i,rest);
}
return 0;
}
答案2
得分: 0
不同于2和8,double
更准确的原因是因为float
无法像输入值一样表示3.1416:
3.1416 -> 3.1415998935699462890625
40.082 -> 40.082000732421875
85.8 -> 85.8000030517578125
这里简单来说,精度不够(请注意,IEEE-754 float
,这是绝大多数情况下用于float
的标准,它以2为底存储)。很可能,后面的数字可能是有意生成的,以便无法通过测试用例。如果想了解更多,请参考不要将其存储在浮点数中和每位计算机科学家都应该了解的浮点数算术知识。
数值常数是1.7168
,假设是他们版本的圆周率乘以r*r
的结果是准确的。而使用单精度精度的最佳结果是1.7167999744415283203125
,误差为2.55584716796875E-8
。使用double
的结果是1.71680000000000010373923942097
,误差为1.0373923942097E-16
,再加上输入的值。
英文:
Unlike 2 and 8, the reason double
is more accurate is because float
cannot represent 3.1416 as well as the input values:
3.1416 -> 3.1415998935699462890625
40.082 -> 40.082000732421875
85.8 -> 85.8000030517578125
There's simply not enough precision, (note that IEEE-754 float
, which is overwhelmingly used for float
, stores it in base-2.) Most probably, the later numbers were probably specifically generated in order to fail the test cases. If one wants to know more, Don’t Store That in a Float, and What Every Computer Scientist Should Know About Floating-Point Arithmetic.
The numerical constant is 1.7168
, which is exact assuming their version of pi, (times r*r
.) The best one could with single-point precision is 1.7167999744415283203125
, which is off by 2.55584716796875E-8
. With a double
, it's 1.71680000000000010373923942097
, off by 1.0373923942097E-16
, plus the values input.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论