kome_pi.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
void now(void)
{
time_t t = time(NULL);
struct tm *local = localtime(&t);
printf("%04d/", local->tm_year + 1900);
printf("%02d/", local->tm_mon + 1);
printf("%02d", local->tm_mday);
printf(" ");
printf("%02d:", local->tm_hour);
printf("%02d:", local->tm_min);
printf("%02d\n", local->tm_sec);
fflush(stdout);
}
int main(int argc,char *argv[])
{
unsigned long long n=8 ;
if(argc > 1 )
{
n=atol( argv[1] ) ;
}
double i,k ,all,en;
long len = n ;
for(i=0.0f;i<len;i+=1.0f) n *= 2.0f;
printf("2^%dまで計算可能,start=%ld \n",sizeof(n)*8,n );
now();
while(1)
{
double ai = 2.0f / (float)n;
en = 0.0f ;
for(i=-1.0f ; i<=1.0f ; i+=ai)
{
for(k=-1.0f ; k<=1.0f ; k += ai)
{
if( i*i + k*k <= 1.0f) en+=1.0f ;
}
}
all = (float)n ;
all *= all ;
printf("π=%.20f ,{%d},",4.0f* en / all ,len);
now();
n *= 2 ;
len++ ;
}
return 0;
}