使用C++,求pi的近似值

问题描述:

使用C++,求pi的近似值
用下面的公式求pi的近似值
pi/4 = 1-1/3+1/5-1/7+.
直到最后一项的绝对值小于10的-7次方为止
如下式我自己编的
#include "stdafx.h"
#include "iostream"
#include "cmath"
#include "conio.h"
using namespace std;
int _tmain(int argc,_TCHAR* argv[])
{
int n = 1;
double P = 0,pi;
for(n=1;1/(2*n-1) < 10^(-7);++n)
{
P += ((-1)^(n+1))*(1/(2*n-1));
}
pi = P*4;
cout
1个回答 分类:综合 2014-12-01

问题解答:

我来补答
才疏学浅,你的程序好多看不到,我挑主要的for循环中的说:
#include "iostream"
#include "math.h"
using namespace std;
void main()
{
int n = 1;
double P = 0,pi;
for(n=1;pow((2*n-1),(-1))>pow(10,(-7));n++) //pow()的返回值是double的,1/n返回的应该是取整的数吧,那就应该是0了.还有条件应该是大于,要不根本不循环下去.
{
P+=(pow((-1),(n+1))*pow((2*n-1),(-1)));
}
pi = P*4;
cout
 
 
展开全文阅读
剩余:2000
上一页:函数急