### CF.BC - Continued fraction experimentation # Create a continued fraction representation of x in the global array cf[] define cf(x) { auto sign, i, s3 s3=int(scale/2); sign=sgn(x);x*=sign for(i=0;i<=s3;i++) { cf[i]=int(x) if(cf[i]>10^s3)break; x-=cf[i] if(x==0){i+=1;break} x=1/x } cf[i]=0 } # Create a continued fraction representation of x in the global array cf[] # using alternating signs define cf2(x) { auto sign, i, s3 s3=int(scale/2); sign=sgn(x);x*=sign for(i=0;i<=s3;i++) { cf[i]=1+int(x) if(cf[i]>10^s3)break; x-=cf[i] if(i!=2*int(i/2))cf[i]*=-1 if(x==0){i+=1;break} x=1/-x } cf[i]=0 } # Output the global array cf[] formatted as a continued fraction define printcf() { auto i; print "[", cf[0], "; "; i=1;if(cf[i]!=0)for(i=1;cf[i+1]!=0;i++)print cf[i], ", "; print cf[i], "]\n"; } # Convert global array cf[] back into a number define cf2num() { auto n, i; for(i=0;cf[i]!=0;i++){} n=cf[--i] for(;i>=0;i--)n=1/n+cf[i] return(n); } # Turn the alternating signed continued fraction representation of x back # into a number by first taking the absolute value of the chain define cfflip(x) { cf2(x) for(i=0;cf[i]!=0;i++)cf[i]=abs(cf[i]) return(cf2num()); } # Turn the alternating signed continued fraction representation of x back # into a number by first taking the absolute value less one of the chain define cfflip1(x) { cf2(x) for(i=0;cf[i]!=0;i++)cf[i]=abs(cf[i])-1 return(cf2num()); } # Turn the binary representation of x into a continued fraction-like # structure using global array cf[]. # e.g. 0.1001000011111101110111 -> [0;1,2,1,4,6,1,3,1,3] define bincf(x) { auto i,b,bb,n,j; x=abs(x);if(x>1)x=frac(x) x*=2;b=int(x);x-=b;n=1;j=1;cf[0]=0 for(i=0;i 0.11110111101111011110... define cfbin() { auto n,i,b,x; #cf[0]=0; for(i=1;cf[i]!=0;i++){} n=cf[--i];b=1;x=0 for(;i>=0;i--){ for(j=0;j