xref: /freebsd-13.1/usr.bin/bc/bc.library (revision be848c7a)
1fdf1f88bSGabor Kovesdan/*	$FreeBSD$							*/
2*be848c7aSXin LI/*      $OpenBSD: bc.library,v 1.4 2012/03/14 07:35:53 otto Exp $	*/
3fdf1f88bSGabor Kovesdan
4fdf1f88bSGabor Kovesdan/*
5fdf1f88bSGabor Kovesdan * Copyright (C) Caldera International Inc.  2001-2002.
6fdf1f88bSGabor Kovesdan * All rights reserved.
7fdf1f88bSGabor Kovesdan *
8fdf1f88bSGabor Kovesdan * Redistribution and use in source and binary forms, with or without
9fdf1f88bSGabor Kovesdan * modification, are permitted provided that the following conditions
10fdf1f88bSGabor Kovesdan * are met:
11fdf1f88bSGabor Kovesdan * 1. Redistributions of source code and documentation must retain the above
12fdf1f88bSGabor Kovesdan *    copyright notice, this list of conditions and the following disclaimer.
13fdf1f88bSGabor Kovesdan * 2. Redistributions in binary form must reproduce the above copyright
14fdf1f88bSGabor Kovesdan *    notice, this list of conditions and the following disclaimer in the
15fdf1f88bSGabor Kovesdan *    documentation and/or other materials provided with the distribution.
16fdf1f88bSGabor Kovesdan * 3. All advertising materials mentioning features or use of this software
17fdf1f88bSGabor Kovesdan *    must display the following acknowledgement:
18fdf1f88bSGabor Kovesdan *      This product includes software developed or owned by Caldera
19fdf1f88bSGabor Kovesdan *      International, Inc.
20fdf1f88bSGabor Kovesdan * 4. Neither the name of Caldera International, Inc. nor the names of other
21fdf1f88bSGabor Kovesdan *    contributors may be used to endorse or promote products derived from
22fdf1f88bSGabor Kovesdan *    this software without specific prior written permission.
23fdf1f88bSGabor Kovesdan *
24fdf1f88bSGabor Kovesdan * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
25fdf1f88bSGabor Kovesdan * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
26fdf1f88bSGabor Kovesdan * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27fdf1f88bSGabor Kovesdan * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28fdf1f88bSGabor Kovesdan * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
29fdf1f88bSGabor Kovesdan * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30fdf1f88bSGabor Kovesdan * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31fdf1f88bSGabor Kovesdan * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32fdf1f88bSGabor Kovesdan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33fdf1f88bSGabor Kovesdan * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34fdf1f88bSGabor Kovesdan * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35fdf1f88bSGabor Kovesdan * POSSIBILITY OF SUCH DAMAGE.
36fdf1f88bSGabor Kovesdan */
37fdf1f88bSGabor Kovesdan
38fdf1f88bSGabor Kovesdan/*
39fdf1f88bSGabor Kovesdan *	@(#)bc.library	5.1 (Berkeley) 4/17/91
40fdf1f88bSGabor Kovesdan */
41fdf1f88bSGabor Kovesdan
42fdf1f88bSGabor Kovesdanscale = 20
43fdf1f88bSGabor Kovesdandefine e(x) {
44fdf1f88bSGabor Kovesdan	auto a, b, c, d, e, g, t, w, y, r
45fdf1f88bSGabor Kovesdan
46fdf1f88bSGabor Kovesdan	r = ibase
47fdf1f88bSGabor Kovesdan	ibase = A
48fdf1f88bSGabor Kovesdan	t = scale
49741e1fafSKevin Lo	scale = 0
50741e1fafSKevin Lo	if (x > 0) scale = (0.435*x)/1
518e30a6b4SKevin Lo	scale = scale + t + length(scale + t) + 1
52fdf1f88bSGabor Kovesdan
53fdf1f88bSGabor Kovesdan	w = 0
54fdf1f88bSGabor Kovesdan	if (x < 0) {
55fdf1f88bSGabor Kovesdan		x = -x
56fdf1f88bSGabor Kovesdan		w = 1
57fdf1f88bSGabor Kovesdan	}
58fdf1f88bSGabor Kovesdan	y = 0
59fdf1f88bSGabor Kovesdan	while (x > 2) {
60fdf1f88bSGabor Kovesdan		x = x/2
61fdf1f88bSGabor Kovesdan		y = y + 1
62fdf1f88bSGabor Kovesdan	}
63fdf1f88bSGabor Kovesdan
64fdf1f88bSGabor Kovesdan	a = 1
65fdf1f88bSGabor Kovesdan	b = 1
66fdf1f88bSGabor Kovesdan	c = b
67fdf1f88bSGabor Kovesdan	d = 1
68fdf1f88bSGabor Kovesdan	e = 1
69fdf1f88bSGabor Kovesdan	for (a = 1; 1 == 1; a++) {
70fdf1f88bSGabor Kovesdan		b = b*x
71fdf1f88bSGabor Kovesdan		c = c*a + b
72fdf1f88bSGabor Kovesdan		d = d*a
73fdf1f88bSGabor Kovesdan		g = c/d
74fdf1f88bSGabor Kovesdan		if (g == e) {
75fdf1f88bSGabor Kovesdan			g = g/1
76fdf1f88bSGabor Kovesdan			while (y--) {
77fdf1f88bSGabor Kovesdan				g = g*g
78fdf1f88bSGabor Kovesdan			}
79fdf1f88bSGabor Kovesdan			scale = t
80fdf1f88bSGabor Kovesdan			ibase = r
81fdf1f88bSGabor Kovesdan			if (w == 1) return (1/g)
82fdf1f88bSGabor Kovesdan			return (g/1)
83fdf1f88bSGabor Kovesdan		}
84fdf1f88bSGabor Kovesdan		e = g
85fdf1f88bSGabor Kovesdan	}
86fdf1f88bSGabor Kovesdan}
87fdf1f88bSGabor Kovesdan
88fdf1f88bSGabor Kovesdandefine l(x) {
89fdf1f88bSGabor Kovesdan	auto a, b, c, d, e, f, g, u, s, t, r
90fdf1f88bSGabor Kovesdan	r = ibase
91fdf1f88bSGabor Kovesdan	ibase = A
92fdf1f88bSGabor Kovesdan	if (x <= 0) {
93fdf1f88bSGabor Kovesdan		a = (1 - 10^scale)
94fdf1f88bSGabor Kovesdan		ibase = r
95fdf1f88bSGabor Kovesdan		return (a)
96fdf1f88bSGabor Kovesdan	}
97fdf1f88bSGabor Kovesdan	t = scale
98fdf1f88bSGabor Kovesdan
99fdf1f88bSGabor Kovesdan	f = 1
100741e1fafSKevin Lo	if (x < 1) {
101741e1fafSKevin Lo		s = scale(x)
102741e1fafSKevin Lo	} else {
103741e1fafSKevin Lo		s = length(x)-scale(x)
104741e1fafSKevin Lo	}
105741e1fafSKevin Lo	scale = 0
106741e1fafSKevin Lo	a = (2.31*s)/1 /* estimated integer part of the answer */
107741e1fafSKevin Lo	s = t + length(a) + 2 /* estimated length of the answer */
108fdf1f88bSGabor Kovesdan	while (x > 2) {
109741e1fafSKevin Lo		scale = 0
110741e1fafSKevin Lo		scale = (length(x) + scale(x))/2 + 1
111741e1fafSKevin Lo		if (scale < s) scale = s
112fdf1f88bSGabor Kovesdan		x = sqrt(x)
113fdf1f88bSGabor Kovesdan		f = f*2
114fdf1f88bSGabor Kovesdan	}
115fdf1f88bSGabor Kovesdan	while (x < .5) {
116741e1fafSKevin Lo		scale = 0
117741e1fafSKevin Lo		scale = scale(x)/2 + 1
118741e1fafSKevin Lo		if (scale < s) scale = s
119fdf1f88bSGabor Kovesdan		x = sqrt(x)
120fdf1f88bSGabor Kovesdan		f = f*2
121fdf1f88bSGabor Kovesdan	}
122fdf1f88bSGabor Kovesdan
1238e30a6b4SKevin Lo	scale = 0
1248e30a6b4SKevin Lo	scale = t + length(f) + length((1.05*(t+length(f))/1)) + 1
125fdf1f88bSGabor Kovesdan	u = (x - 1)/(x + 1)
126fdf1f88bSGabor Kovesdan	s = u*u
127741e1fafSKevin Lo	scale = t + 2
128fdf1f88bSGabor Kovesdan	b = 2*f
129fdf1f88bSGabor Kovesdan	c = b
130fdf1f88bSGabor Kovesdan	d = 1
131fdf1f88bSGabor Kovesdan	e = 1
132fdf1f88bSGabor Kovesdan	for (a = 3; 1 == 1 ; a = a + 2) {
133fdf1f88bSGabor Kovesdan		b = b*s
134fdf1f88bSGabor Kovesdan		c = c*a + d*b
135fdf1f88bSGabor Kovesdan		d = d*a
136fdf1f88bSGabor Kovesdan		g = c/d
137fdf1f88bSGabor Kovesdan		if (g == e) {
138fdf1f88bSGabor Kovesdan			scale = t
139fdf1f88bSGabor Kovesdan			ibase = r
140fdf1f88bSGabor Kovesdan			return (u*c/d)
141fdf1f88bSGabor Kovesdan		}
142fdf1f88bSGabor Kovesdan		e = g
143fdf1f88bSGabor Kovesdan	}
144fdf1f88bSGabor Kovesdan}
145fdf1f88bSGabor Kovesdan
146fdf1f88bSGabor Kovesdandefine s(x) {
147fdf1f88bSGabor Kovesdan	auto a, b, c, s, t, y, p, n, i, r
148fdf1f88bSGabor Kovesdan	r = ibase
149fdf1f88bSGabor Kovesdan	ibase = A
150fdf1f88bSGabor Kovesdan	t = scale
151fdf1f88bSGabor Kovesdan	y = x/.7853
152fdf1f88bSGabor Kovesdan	s = t + length(y) - scale(y)
153fdf1f88bSGabor Kovesdan	if (s < t) s = t
154fdf1f88bSGabor Kovesdan	scale = s
155fdf1f88bSGabor Kovesdan	p = a(1)
156fdf1f88bSGabor Kovesdan
157fdf1f88bSGabor Kovesdan	scale = 0
158fdf1f88bSGabor Kovesdan	if (x >= 0) n = (x/(2*p) + 1)/2
159fdf1f88bSGabor Kovesdan	if (x < 0) n = (x/(2*p) - 1)/2
160fdf1f88bSGabor Kovesdan	x = x - 4*n*p
161fdf1f88bSGabor Kovesdan	if (n % 2 != 0) x = -x
162fdf1f88bSGabor Kovesdan
163fdf1f88bSGabor Kovesdan	scale = t + length(1.2*t) - scale(1.2*t)
164fdf1f88bSGabor Kovesdan	y = -x*x
165fdf1f88bSGabor Kovesdan	a = x
166fdf1f88bSGabor Kovesdan	b = 1
167fdf1f88bSGabor Kovesdan	s = x
168fdf1f88bSGabor Kovesdan	for (i =3 ; 1 == 1; i = i + 2) {
169fdf1f88bSGabor Kovesdan		a = a*y
170fdf1f88bSGabor Kovesdan		b = b*i*(i - 1)
171fdf1f88bSGabor Kovesdan		c = a/b
172fdf1f88bSGabor Kovesdan		if (c == 0) {
173fdf1f88bSGabor Kovesdan			scale = t
174fdf1f88bSGabor Kovesdan			ibase = r
175fdf1f88bSGabor Kovesdan			return (s/1)
176fdf1f88bSGabor Kovesdan		}
177fdf1f88bSGabor Kovesdan		s = s + c
178fdf1f88bSGabor Kovesdan	}
179fdf1f88bSGabor Kovesdan}
180fdf1f88bSGabor Kovesdan
181fdf1f88bSGabor Kovesdandefine c(x) {
182fdf1f88bSGabor Kovesdan	auto t, r
183fdf1f88bSGabor Kovesdan	r = ibase
184fdf1f88bSGabor Kovesdan	ibase = A
185fdf1f88bSGabor Kovesdan	t = scale
186fdf1f88bSGabor Kovesdan	scale = scale + 1
187fdf1f88bSGabor Kovesdan	x = s(x + 2*a(1))
188fdf1f88bSGabor Kovesdan	scale = t
189fdf1f88bSGabor Kovesdan	ibase = r
190fdf1f88bSGabor Kovesdan	return (x/1)
191fdf1f88bSGabor Kovesdan}
192fdf1f88bSGabor Kovesdan
193fdf1f88bSGabor Kovesdandefine a(x) {
194fdf1f88bSGabor Kovesdan	auto a, b, c, d, e, f, g, s, t, r
195fdf1f88bSGabor Kovesdan	if (x == 0) return(0)
196fdf1f88bSGabor Kovesdan
197fdf1f88bSGabor Kovesdan	r = ibase
198fdf1f88bSGabor Kovesdan	ibase = A
199fdf1f88bSGabor Kovesdan	if (x == 1) {
200fdf1f88bSGabor Kovesdan		if (scale < 52) {
201fdf1f88bSGabor Kovesdan			 a = .7853981633974483096156608458198757210492923498437764/1
202fdf1f88bSGabor Kovesdan			 ibase = r
203fdf1f88bSGabor Kovesdan			 return (a)
204fdf1f88bSGabor Kovesdan		}
205fdf1f88bSGabor Kovesdan	}
206fdf1f88bSGabor Kovesdan	t = scale
207fdf1f88bSGabor Kovesdan	f = 1
208fdf1f88bSGabor Kovesdan	while (x > .5) {
209fdf1f88bSGabor Kovesdan		scale = scale + 1
210fdf1f88bSGabor Kovesdan		x = -(1 - sqrt(1. + x*x))/x
211fdf1f88bSGabor Kovesdan		f = f*2
212fdf1f88bSGabor Kovesdan	}
213fdf1f88bSGabor Kovesdan	while (x < -.5) {
214fdf1f88bSGabor Kovesdan		scale = scale + 1
215fdf1f88bSGabor Kovesdan		x = -(1 - sqrt(1. + x*x))/x
216fdf1f88bSGabor Kovesdan		f = f*2
217fdf1f88bSGabor Kovesdan	}
218fdf1f88bSGabor Kovesdan	s = -x*x
219fdf1f88bSGabor Kovesdan	b = f
220fdf1f88bSGabor Kovesdan	c = f
221fdf1f88bSGabor Kovesdan	d = 1
222fdf1f88bSGabor Kovesdan	e = 1
223fdf1f88bSGabor Kovesdan	for (a = 3; 1 == 1; a = a + 2) {
224fdf1f88bSGabor Kovesdan		b = b*s
225fdf1f88bSGabor Kovesdan		c = c*a + d*b
226fdf1f88bSGabor Kovesdan		d = d*a
227fdf1f88bSGabor Kovesdan		g = c/d
228fdf1f88bSGabor Kovesdan		if (g == e) {
229fdf1f88bSGabor Kovesdan			ibase = r
230fdf1f88bSGabor Kovesdan			scale = t
231fdf1f88bSGabor Kovesdan			return (x*c/d)
232fdf1f88bSGabor Kovesdan		}
233fdf1f88bSGabor Kovesdan		e = g
234fdf1f88bSGabor Kovesdan	}
235fdf1f88bSGabor Kovesdan}
236fdf1f88bSGabor Kovesdan
237fdf1f88bSGabor Kovesdandefine j(n,x) {
238fdf1f88bSGabor Kovesdan	auto a, b, c, d, e, g, i, s, k, t, r
239fdf1f88bSGabor Kovesdan
240fdf1f88bSGabor Kovesdan	r = ibase
241fdf1f88bSGabor Kovesdan	ibase = A
242fdf1f88bSGabor Kovesdan	t = scale
243fdf1f88bSGabor Kovesdan	k = 1.36*x + 1.16*t - n
244fdf1f88bSGabor Kovesdan	k = length(k) - scale(k)
245fdf1f88bSGabor Kovesdan	if (k > 0) scale = scale + k
246fdf1f88bSGabor Kovesdan
247fdf1f88bSGabor Kovesdan	s = -x*x/4
248fdf1f88bSGabor Kovesdan	if (n < 0) {
249fdf1f88bSGabor Kovesdan		n = -n
250fdf1f88bSGabor Kovesdan		x = -x
251fdf1f88bSGabor Kovesdan	}
252fdf1f88bSGabor Kovesdan	a = 1
253fdf1f88bSGabor Kovesdan	c = 1
254fdf1f88bSGabor Kovesdan	for (i = 1; i <= n; i++) {
255fdf1f88bSGabor Kovesdan		a = a*x
256fdf1f88bSGabor Kovesdan		c = c*2*i
257fdf1f88bSGabor Kovesdan	}
258fdf1f88bSGabor Kovesdan	b = a
259fdf1f88bSGabor Kovesdan	d = 1
260fdf1f88bSGabor Kovesdan	e = 1
261fdf1f88bSGabor Kovesdan	for (i = 1; 1; i++) {
262fdf1f88bSGabor Kovesdan		a = a*s
263fdf1f88bSGabor Kovesdan		b = b*i*(n + i) + a
264fdf1f88bSGabor Kovesdan		c = c*i*(n + i)
265fdf1f88bSGabor Kovesdan		g = b/c
266fdf1f88bSGabor Kovesdan		if (g == e) {
267fdf1f88bSGabor Kovesdan			ibase = r
268fdf1f88bSGabor Kovesdan			scale = t
269fdf1f88bSGabor Kovesdan			return (g/1)
270fdf1f88bSGabor Kovesdan		}
271fdf1f88bSGabor Kovesdan		e = g
272fdf1f88bSGabor Kovesdan	}
273fdf1f88bSGabor Kovesdan}
274741e1fafSKevin Lo/* vim: set filetype=bc shiftwidth=8 noexpandtab: */
275