欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

求π的近似值

程序员文章站 2022-07-07 18:36:41
...
#include<bits/stdc++.h>
double funpi(double n)
{
    double w = 0,item = 1,flag=1,denominator=1;

    while(fabs(item)>=n){
        item = flag*1.0/denominator;
        w = w+item;
        flag = -flag;
        denominator = denominator + 2;
    }
    return 4*w;
}

int main()
{
    double pi,e;
    scanf("%lf",&e);
    pi = funpi(e);
    printf("pi = %lf",pi);
    return 0;
}

相关标签: 笔记 c++