0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

πの近似値の分数を計算する【C言語】

Posted at
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;
}

約分プログラムは後で計算する

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?