vc++问题!解决精度问题!!!!

能告诉我怎么改进一下程序,增加小数后多一位精度#include <iostream.h>#include <iomanip>#include <cmath>int main(){ double n,pi=0,pj=0; for(n=1;;n+=4) { if( fabs(1.0/n)>1e-7 )pi=pi+1/n; else break; } cout<<pi<<endl; for(double j=3.0;;j+=4) { if( fabs(1.0/j)>1e-7 ) { pj=pj-1/j; } else break; } cout<<pj<<endl; pi=(pi+pj)*4.0;cout<<"pi="<<pi<<endl; return 0;}输出结果:4.73981 -3.95442 pi=3.14159将pi的输出改为输出3.1415921e-7 改为 更小的数如1e-8 的话不行啊,没输出什么
2026年09月26日 12:26
有2个网友回答
网友(1):

计算圆周率800位的程序

#include
long a=10000, b, c=2800, d, e, f[2801], g;
void main(){
for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}

==================
你的程序应该吧 1e-7 改为 更小的数就行了,比如1e-10

网友(2):

在输出最后一句之前,加上
cout.precision(7);
7表示小数点后保留7位