近似计算

Posted Aftersoon_sun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了近似计算相关的知识,希望对你有一定的参考价值。

计算:PI/4=1-1/3+1/5-1/7+...,直达最后一项小于10-6

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
    double sum=0;
    for(int i=0;;i++)
    {
        double term=1.0/(i*2+1);
        if(i%2==0)
            sum+=term;
        else
            sum-=term;
        if(term<1e-6)
            break;
    }
    printf("%.6f\n",sum);
    return 0;
}

以上是关于近似计算的主要内容,如果未能解决你的问题,请参考以下文章