#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#define MAX_Q 1000000000
// 返回最大下标
int calc_cfrac(double x, int a[], int p[], int q[], int max_idx)
{
double ipart;
int i;
if ( x>=1.0) // x 必须小于1
return 0;
p[0]=1;
q[0]=0;
p[1]= a[1]= 0;
q[1]=1;
for (i=2; i <= max_idx; i++)
{
x=1.0/x;
ipart=floor(x);
a[i]=(int)ipart;
if ( a[i] > (MAX_Q- q[i-2])/q[i-1] )
return i-1;
p[i]=a[i]*p[i-1] + p[i-2];
q[i]=a[i]*q[i-1] + q[i-2];
x-= ipart;
if ( x < 1e-8)
return i;
}
return i-1;
}
#define MSIZE 32
void test()
{
int a[MSIZE];
int p[MSIZE];
int q[MSIZE];
double log2=log(2.0)/log(10.0);
int c= calc_cfrac(log2, a, p, q, MSIZE-1);
assert(c<MSIZE);
printf("%.16lf\n",log2);
for (int i=1;i<=c;i++)
{
printf("%d/%d=%.16lf\n", p[i],q[i], (double)p[i]/(double)q[i] );
}
}
int main(int argc, char* argv[] )
{
test();
return 0;
}