Annotation of src/usr.bin/bc/bc.library, Revision 1.2
1.2 ! otto 1: /* $OpenBSD: bc.library,v 1.1 2003/09/25 19:34:22 otto Exp $ */
1.1 otto 2:
3: /*
4: * Copyright (C) Caldera International Inc. 2001-2002.
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code and documentation must retain the above
11: * copyright notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed or owned by Caldera
18: * International, Inc.
19: * 4. Neither the name of Caldera International, Inc. nor the names of other
20: * contributors may be used to endorse or promote products derived from
21: * this software without specific prior written permission.
22: *
23: * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24: * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27: * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28: * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33: * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34: * POSSIBILITY OF SUCH DAMAGE.
35: */
36:
37: /*
38: * @(#)bc.library 5.1 (Berkeley) 4/17/91
39: */
40:
41: scale = 20
1.2 ! otto 42: define e(x) {
1.1 otto 43: auto a, b, c, d, e, g, t, w, y
44:
45: t = scale
46: scale = t + .434*x + 1
47:
48: w = 0
1.2 ! otto 49: if (x < 0) {
1.1 otto 50: x = -x
51: w = 1
52: }
53: y = 0
1.2 ! otto 54: while (x > 2) {
1.1 otto 55: x = x/2
56: y = y + 1
57: }
58:
1.2 ! otto 59: a = 1
! 60: b = 1
! 61: c = b
! 62: d = 1
! 63: e = 1
! 64: for (a = 1; 1 == 1; a++) {
! 65: b = b*x
! 66: c = c*a + b
! 67: d = d*a
1.1 otto 68: g = c/d
1.2 ! otto 69: if (g == e) {
1.1 otto 70: g = g/1
1.2 ! otto 71: while (y--) {
1.1 otto 72: g = g*g
73: }
74: scale = t
1.2 ! otto 75: if (w == 1) return(1/g)
1.1 otto 76: return(g/1)
77: }
1.2 ! otto 78: e = g
1.1 otto 79: }
80: }
81:
1.2 ! otto 82: define l(x) {
1.1 otto 83: auto a, b, c, d, e, f, g, u, s, t
1.2 ! otto 84: if (x <= 0) return (1 - 10^scale)
1.1 otto 85: t = scale
86:
1.2 ! otto 87: f = 1
1.1 otto 88: scale = scale + scale(x) - length(x) + 1
1.2 ! otto 89: s = scale
! 90: while (x > 2) {
! 91: s = s + (length(x) - scale(x))/2 + 1
! 92: if (s > 0) scale = s
1.1 otto 93: x = sqrt(x)
1.2 ! otto 94: f = f*2
1.1 otto 95: }
1.2 ! otto 96: while (x < .5) {
! 97: s = s + (length(x) - scale(x))/2 + 1
! 98: if (s > 0) scale = s
1.1 otto 99: x = sqrt(x)
1.2 ! otto 100: f = f*2
1.1 otto 101: }
102:
103: scale = t + length(f) - scale(f) + 1
1.2 ! otto 104: u = (x - 1)/(x + 1)
1.1 otto 105:
106: scale = scale + 1.1*length(t) - 1.1*scale(t)
107: s = u*u
108: b = 2*f
109: c = b
110: d = 1
111: e = 1
1.2 ! otto 112: for (a = 3; 1 == 1 ; a = a + 2) {
! 113: b = b*s
! 114: c = c*a + d*b
! 115: d = d*a
! 116: g = c/d
! 117: if (g == e) {
1.1 otto 118: scale = t
1.2 ! otto 119: return (u*c/d)
1.1 otto 120: }
1.2 ! otto 121: e = g
1.1 otto 122: }
123: }
124:
1.2 ! otto 125: define s(x) {
1.1 otto 126: auto a, b, c, s, t, y, p, n, i
127: t = scale
128: y = x/.7853
129: s = t + length(y) - scale(y)
1.2 ! otto 130: if (s < t) s = t
1.1 otto 131: scale = s
132: p = a(1)
133:
134: scale = 0
1.2 ! otto 135: if (x >= 0) n = (x/(2*p) + 1)/2
! 136: if (x < 0) n = (x/(2*p) - 1)/2
1.1 otto 137: x = x - 4*n*p
1.2 ! otto 138: if (n % 2 != 0) x = -x
1.1 otto 139:
140: scale = t + length(1.2*t) - scale(1.2*t)
141: y = -x*x
142: a = x
143: b = 1
144: s = x
1.2 ! otto 145: for (i =3 ; 1 == 1; i = i + 2) {
1.1 otto 146: a = a*y
1.2 ! otto 147: b = b*i*(i - 1)
1.1 otto 148: c = a/b
1.2 ! otto 149: if (c == 0) {
! 150: scale = t
! 151: return (s/1)
! 152: }
! 153: s = s + c
1.1 otto 154: }
155: }
156:
1.2 ! otto 157: define c(x) {
1.1 otto 158: auto t
159: t = scale
1.2 ! otto 160: scale = scale + 1
! 161: x = s(x + 2*a(1))
1.1 otto 162: scale = t
1.2 ! otto 163: return (x/1)
1.1 otto 164: }
165:
1.2 ! otto 166: define a(x) {
1.1 otto 167: auto a, b, c, d, e, f, g, s, t
1.2 ! otto 168: if (x == 0) return(0)
! 169: if (x == 1) {
! 170: if (scale < 52) {
1.1 otto 171: return(.7853981633974483096156608458198757210492923498437764/1)
172: }
173: }
174: t = scale
1.2 ! otto 175: f = 1
! 176: while (x > .5) {
1.1 otto 177: scale = scale + 1
1.2 ! otto 178: x = -(1 - sqrt(1. + x*x))/x
! 179: f = f*2
1.1 otto 180: }
1.2 ! otto 181: while (x < -.5) {
1.1 otto 182: scale = scale + 1
1.2 ! otto 183: x = -(1 - sqrt(1. + x*x))/x
! 184: f = f*2
1.1 otto 185: }
186: s = -x*x
187: b = f
188: c = f
189: d = 1
190: e = 1
1.2 ! otto 191: for (a = 3; 1 == 1; a = a + 2) {
! 192: b = b*s
! 193: c = c*a + d*b
! 194: d = d*a
! 195: g = c/d
! 196: if (g == e) {
1.1 otto 197: scale = t
1.2 ! otto 198: return (x*c/d)
1.1 otto 199: }
1.2 ! otto 200: e = g
1.1 otto 201: }
202: }
203:
1.2 ! otto 204: define j(n,x) {
1.1 otto 205: auto a,b,c,d,e,g,i,s,k,t
206:
207: t = scale
208: k = 1.36*x + 1.16*t - n
209: k = length(k) - scale(k)
1.2 ! otto 210: if (k > 0) scale = scale + k
1.1 otto 211:
1.2 ! otto 212: s = -x*x/4
! 213: if (n < 0) {
! 214: n = -n
! 215: x = -x
! 216: }
! 217: a = 1
! 218: c = 1
! 219: for (i = 1; i <= n; i++) {
! 220: a = a*x
1.1 otto 221: c = c*2*i
222: }
1.2 ! otto 223: b = a
! 224: d = 1
! 225: e = 1
! 226: for (i = 1; 1; i++) {
! 227: a = a*s
! 228: b = b*i*(n + i) + a
! 229: c = c*i*(n + i)
! 230: g = b/c
! 231: if (g == e) {
1.1 otto 232: scale = t
1.2 ! otto 233: return (g/1)
1.1 otto 234: }
1.2 ! otto 235: e = g
1.1 otto 236: }
237: }