本文目录一览:
- 1、求用三种方法计算圆周率(C语言)
- 2、C语言求圆周率
- 3、C语言计算圆周率
- 4、计算圆周率的C语言程序
求用三种方法计算圆周率(C语言)
给你个C程序吧:
#include stdio.h
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("%04d",e+d/a),e=d%a)
for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
}
PI=3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185...
C语言求圆周率
#include stdio.h
int main()
{
float f;
double pi,i,sign;
while(scanf("%f",f)==1)
{
pi=0;
i=1;
sign=1;
do
{
pi+=sign*1.0/i;
}while(1.0/i=f(sign=-sign)(i+=2));
printf("%lf\n",pi*4.0);
}
return 0;
}
C语言计算圆周率
计算溢出了。你的 fact 和 multi 都使用整数保存计算结果,参数稍大一点就超出整数表示范围了,于是溢出变成负数。
你把这两个函数改成 double 类型,内部变量 res 也声明成 double,就能算出正确结果了。
计算圆周率的C语言程序
#include stdio.h
#define L 10000 //求10000位PI值
#define N L/4+1
// L 为位数,N是array长度
/*圆周率后的小数位数是无止境的,如何使用电脑来计算这无止境的小数是一些数学家与程式设计师所感兴趣的,在这边介绍一个公式配合 大数运算,可以计算指定位数的圆周率。
John Wallis的圆周率公式:
//详细看网站介绍:
PI = [16/5 - 16 / (3*53) + 16 / (5*55) - 16 / (7*57) + ......] - [4/239 - 4/(3*2393) + 4/(5*2395) - 4/(7*2397) + ......]
*/
void add ( int*, int*, int* );
void sub ( int*, int*, int* );
void div ( int*, int, int* );
int main ( void )
{
int s[N+3] = {0};
int w[N+3] = {0};
int v[N+3] = {0};
int q[N+3] = {0};
int n = ( int ) ( L/1.39793 + 1 );
int k;
w[0] = 16*5;
v[0] = 4*239;
for ( k = 1; k = n; k++ )
{
// 套用公式
div ( w, 25, w );
div ( v, 239, v );
div ( v, 239, v );
sub ( w, v, q );
div ( q, 2*k-1, q );
if ( k%2 ) // 奇数项
add ( s, q, s );
else // 偶数项
sub ( s, q, s );
}
printf ( "%d.", s[0] );
for ( k = 1; k N; k++ )
printf ( "%04d", s[k] );
printf ( "\n" );
return 0;
}
void add ( int *a, int *b, int *c )
{
int i, carry = 0;
for ( i = N+1; i = 0; i-- )
{
c[i] = a[i] + b[i] + carry;
if ( c[i] 10000 )
carry = 0;
else // 进位
{
c[i] = c[i] - 10000;
carry = 1;
}
}
}
void sub ( int *a, int *b, int *c )
{
int i, borrow = 0;
for ( i = N+1; i = 0; i-- )
{
c[i] = a[i] - b[i] - borrow;
if ( c[i] = 0 )
borrow = 0;
else // 借位
{
c[i] = c[i] + 10000;
borrow = 1;
}
}
}
void div ( int *a, int b, int *c ) // b 为除数
{
int i, tmp, remain = 0;
for ( i = 0; i = N+1; i++ )
{
tmp = a[i] + remain;
c[i] = tmp / b;
remain = ( tmp % b ) * 10000;
}
}