辛普森公式之求椭圆面积
程序员文章站
2022-04-02 10:03:00
...
#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
const double eps=1e-7;
double a,b;
double f(double x)
{
return b*sqrt(1-x*x/(a*a));
}
double simpson(double l,double r)
{
double mid=(l+r)/2;
return (r-l)*(f(l)+f(r)+4*f(mid))/6;
}
double solve(double l,double r)
{
double mid=(l+r)/2;
if(fabs(simpson(l,r)-simpson(l,mid)-simpson(mid,r))<=eps)
{
return simpson(l,r);
}
return solve(l,mid)+solve(mid,r);
}
int main()
{
int t;
scanf("%d",&t);
double l,r;
while(t--)
{
scanf("%lf%lf%lf%lf",&a,&b,&l,&r);
printf("%.3lf\n",2*solve(l,r));
}
}