xref: /original-bsd/usr.bin/bc/bc.library (revision 6219b5e8)
1/*	bc.library	4.1	83/04/02	*/
2
3scale = 20
4define e(x){
5	auto a, b, c, d, e, g, w, y
6
7	t = scale
8	scale = t + .434*x + 1
9
10	w = 0
11	if(x<0){
12		x = -x
13		w = 1
14	}
15	y = 0
16	while(x>2){
17		x = x/2
18		y = y + 1
19	}
20
21	a=1
22	b=1
23	c=b
24	d=1
25	e=1
26	for(a=1;1==1;a++){
27		b=b*x
28		c=c*a+b
29		d=d*a
30		g = c/d
31		if(g == e){
32			g = g/1
33			while(y--){
34				g = g*g
35			}
36			scale = t
37			if(w==1) return(1/g)
38			return(g/1)
39		}
40		e=g
41	}
42}
43
44define l(x){
45	auto a, b, c, d, e, f, g, u, s, t
46	if(x <=0) return(1-10^scale)
47	t = scale
48
49	f=1
50	scale = scale + scale(x) - length(x) + 1
51	s=scale
52	while(x > 2){
53		s = s + (length(x)-scale(x))/2 + 1
54		if(s>0) scale = s
55		x = sqrt(x)
56		f=f*2
57	}
58	while(x < .5){
59		s = s + (length(x)-scale(x))/2 + 1
60		if(s>0) scale = s
61		x = sqrt(x)
62		f=f*2
63	}
64
65	scale = t + length(f) - scale(f) + 1
66	u = (x-1)/(x+1)
67
68	scale = scale + 1.1*length(t) - 1.1*scale(t)
69	s = u*u
70	b = 2*f
71	c = b
72	d = 1
73	e = 1
74	for(a=3;1==1;a=a+2){
75		b=b*s
76		c=c*a+d*b
77		d=d*a
78		g=c/d
79		if(g==e){
80			scale = t
81			return(u*c/d)
82		}
83		e=g
84	}
85}
86
87define s(x){
88	auto a, b, c, s, t, y, p, n, i
89	t = scale
90	y = x/.7853
91	s = t + length(y) - scale(y)
92	if(s<t) s=t
93	scale = s
94	p = a(1)
95
96	scale = 0
97	if(x>=0) n = (x/(2*p)+1)/2
98	if(x<0) n = (x/(2*p)-1)/2
99	x = x - 4*n*p
100	if(n%2!=0) x = -x
101
102	scale = t + length(1.2*t) - scale(1.2*t)
103	y = -x*x
104	a = x
105	b = 1
106	s = x
107	for(i=3; 1==1; i=i+2){
108		a = a*y
109		b = b*i*(i-1)
110		c = a/b
111		if(c==0){scale=t; return(s/1)}
112		s = s+c
113	}
114}
115
116define c(x){
117	auto t
118	t = scale
119	scale = scale+1
120	x = s(x+2*a(1))
121	scale = t
122	return(x/1)
123}
124
125define a(x){
126	auto a, b, c, d, e, f, g, s, t
127	if(x==0) return(0)
128	if(x==1)
129		if(scale<52)
130return(.7853981633974483096156608458198757210492923498437764/1)
131	t = scale
132	f=1
133	while(x > .5){
134		scale = scale + 1
135		x= -(1-sqrt(1.+x*x))/x
136		f=f*2
137	}
138	while(x < -.5){
139		scale = scale + 1
140		x = -(1-sqrt(1.+x*x))/x
141		f=f*2
142	}
143	s = -x*x
144	b = f
145	c = f
146	d = 1
147	e = 1
148	for(a=3;1==1;a=a+2){
149		b=b*s
150		c=c*a+d*b
151		d=d*a
152		g=c/d
153		if(g==e){
154			scale = t
155			return(x*c/d)
156		}
157		e=g
158	}
159}
160
161define j(n,x){
162auto a,b,c,d,e,g,i,s,k,t
163
164	t = scale
165	k = 1.36*x + 1.16*t - n
166	k = length(k) - scale(k)
167	if(k>0) scale = scale + k
168
169s= -x*x/4
170if(n<0){
171	n= -n
172	x= -x
173	}
174a=1
175c=1
176for(i=1;i<=n;i++){
177	a=a*x
178	c = c*2*i
179	}
180b=a
181d=1
182e=1
183for(i=1;1;i++){
184	a=a*s
185	b=b*i*(n+i) + a
186	c=c*i*(n+i)
187	g=b/c
188	if(g==e){
189		scale = t
190		return(g/1)
191		}
192	e=g
193	}
194}
195