1
### CF.BC - Continued fraction experimentation for GNU bc
3
# Create a continued fraction representation of x in the global array cf[]
18
# Create a continued fraction representation of x in the global array cf[]
19
# using alternating signs
28
if(i!=2*int(i/2))cf[i]*=-1
35
# Output the global array cf[] formatted as a continued fraction
38
print "[", cf[0], "; ";
39
i=1;if(cf[i]!=0)for(i=1;cf[i+1]!=0;i++)print cf[i], ", ";
43
# Convert global array cf[] back into a number
46
for(i=0;cf[i]!=0;i++){}
48
for(;i>=0;i--)n=1/n+cf[i]
52
# Turn the alternating signed continued fraction representation of x back
53
# into a number by first taking the absolute value of the chain
56
for(i=0;cf[i]!=0;i++)cf[i]=abs(cf[i])
60
# Turn the alternating signed continued fraction representation of x back
61
# into a number by first taking the absolute value less one of the chain
64
for(i=0;cf[i]!=0;i++)cf[i]=abs(cf[i])-1
68
# Turn the binary representation of x into a continued fraction-like
69
# structure using global array cf[].
70
# e.g. 0.1001000011111101110111 -> [0;1,2,1,4,6,1,3,1,3]
73
x=abs(x);if(x>1)x=frac(x)
74
x*=2;b=int(x);x-=b;n=1;j=1;cf[0]=0
77
if(bb==b){n+=1}else{cf[j]=n;j+=1;n=1}
80
if(n!=0){cf[j]=n;j+=1}
84
# Read a continued fraction as a representation of alternating groups of
85
# bits in a binary number.
86
# e.g. [1;4,1,4,1,4,1,4,1...] -> 0.11110111101111011110...
90
for(i=1;cf[i]!=0;i++){}
93
for(j=0;j<n;j++)x=x/2+b