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