pai.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
const float pp = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679f;
float p = pp ;
long n=4 ;
if(argc > 1 )
{
n=atol( argv[1] ) ;
}
printf("TIMES=%ld\n",n);
//----
int i;
int seis[n];
for(i=0; i<n ; i++)
{
seis [i] =(int)( p ); //あんま良くないけど
p -= (float)seis[i] ;
p = 1.0f / p ;
// printf("%d\n",seis[i]);
}
long long bnc = 1.0f ;
long long bnb =(long)p ;
long long t ;
printf("bnc=%f\n",p);
for(i=n-1 ; i >=0 ; i--)
{ t = bnc ;
bnc = bnb ;
bnb = t;
bnc += seis[i] * bnb ;
p = 1.0f / p ;
}
printf("%ld / %ld -> %.10f \n",bnc , bnb , (float)(1.0F *bnc/bnb) );
printf("gosa = %.10f\n",(float)(1.0f*bnc/bnb)-pp);
return 0;
}
約分プログラムは後で計算する