1202719Sgabor/* $FreeBSD: releng/10.2/usr.bin/bc/bc.library 265533 2014-05-07 08:06:54Z delphij $ */ 2265533Sdelphij/* $OpenBSD: bc.library,v 1.4 2012/03/14 07:35:53 otto Exp $ */ 3202719Sgabor 4202719Sgabor/* 5202719Sgabor * Copyright (C) Caldera International Inc. 2001-2002. 6202719Sgabor * All rights reserved. 7202719Sgabor * 8202719Sgabor * Redistribution and use in source and binary forms, with or without 9202719Sgabor * modification, are permitted provided that the following conditions 10202719Sgabor * are met: 11202719Sgabor * 1. Redistributions of source code and documentation must retain the above 12202719Sgabor * copyright notice, this list of conditions and the following disclaimer. 13202719Sgabor * 2. Redistributions in binary form must reproduce the above copyright 14202719Sgabor * notice, this list of conditions and the following disclaimer in the 15202719Sgabor * documentation and/or other materials provided with the distribution. 16202719Sgabor * 3. All advertising materials mentioning features or use of this software 17202719Sgabor * must display the following acknowledgement: 18202719Sgabor * This product includes software developed or owned by Caldera 19202719Sgabor * International, Inc. 20202719Sgabor * 4. Neither the name of Caldera International, Inc. nor the names of other 21202719Sgabor * contributors may be used to endorse or promote products derived from 22202719Sgabor * this software without specific prior written permission. 23202719Sgabor * 24202719Sgabor * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA 25202719Sgabor * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR 26202719Sgabor * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27202719Sgabor * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28202719Sgabor * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, 29202719Sgabor * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 30202719Sgabor * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 31202719Sgabor * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32202719Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 33202719Sgabor * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 34202719Sgabor * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35202719Sgabor * POSSIBILITY OF SUCH DAMAGE. 36202719Sgabor */ 37202719Sgabor 38202719Sgabor/* 39202719Sgabor * @(#)bc.library 5.1 (Berkeley) 4/17/91 40202719Sgabor */ 41202719Sgabor 42202719Sgaborscale = 20 43202719Sgabordefine e(x) { 44202719Sgabor auto a, b, c, d, e, g, t, w, y, r 45202719Sgabor 46202719Sgabor r = ibase 47202719Sgabor ibase = A 48202719Sgabor t = scale 49232994Skevlo scale = 0 50232994Skevlo if (x > 0) scale = (0.435*x)/1 51233121Skevlo scale = scale + t + length(scale + t) + 1 52202719Sgabor 53202719Sgabor w = 0 54202719Sgabor if (x < 0) { 55202719Sgabor x = -x 56202719Sgabor w = 1 57202719Sgabor } 58202719Sgabor y = 0 59202719Sgabor while (x > 2) { 60202719Sgabor x = x/2 61202719Sgabor y = y + 1 62202719Sgabor } 63202719Sgabor 64202719Sgabor a = 1 65202719Sgabor b = 1 66202719Sgabor c = b 67202719Sgabor d = 1 68202719Sgabor e = 1 69202719Sgabor for (a = 1; 1 == 1; a++) { 70202719Sgabor b = b*x 71202719Sgabor c = c*a + b 72202719Sgabor d = d*a 73202719Sgabor g = c/d 74202719Sgabor if (g == e) { 75202719Sgabor g = g/1 76202719Sgabor while (y--) { 77202719Sgabor g = g*g 78202719Sgabor } 79202719Sgabor scale = t 80202719Sgabor ibase = r 81202719Sgabor if (w == 1) return (1/g) 82202719Sgabor return (g/1) 83202719Sgabor } 84202719Sgabor e = g 85202719Sgabor } 86202719Sgabor} 87202719Sgabor 88202719Sgabordefine l(x) { 89202719Sgabor auto a, b, c, d, e, f, g, u, s, t, r 90202719Sgabor r = ibase 91202719Sgabor ibase = A 92202719Sgabor if (x <= 0) { 93202719Sgabor a = (1 - 10^scale) 94202719Sgabor ibase = r 95202719Sgabor return (a) 96202719Sgabor } 97202719Sgabor t = scale 98202719Sgabor 99202719Sgabor f = 1 100232994Skevlo if (x < 1) { 101232994Skevlo s = scale(x) 102232994Skevlo } else { 103265533Sdelphij s = length(x)-scale(x) 104232994Skevlo } 105232994Skevlo scale = 0 106232994Skevlo a = (2.31*s)/1 /* estimated integer part of the answer */ 107232994Skevlo s = t + length(a) + 2 /* estimated length of the answer */ 108202719Sgabor while (x > 2) { 109265533Sdelphij scale = 0 110232994Skevlo scale = (length(x) + scale(x))/2 + 1 111232994Skevlo if (scale < s) scale = s 112202719Sgabor x = sqrt(x) 113202719Sgabor f = f*2 114202719Sgabor } 115202719Sgabor while (x < .5) { 116232994Skevlo scale = 0 117232994Skevlo scale = scale(x)/2 + 1 118232994Skevlo if (scale < s) scale = s 119202719Sgabor x = sqrt(x) 120202719Sgabor f = f*2 121202719Sgabor } 122202719Sgabor 123233121Skevlo scale = 0 124233121Skevlo scale = t + length(f) + length((1.05*(t+length(f))/1)) + 1 125202719Sgabor u = (x - 1)/(x + 1) 126202719Sgabor s = u*u 127232994Skevlo scale = t + 2 128202719Sgabor b = 2*f 129202719Sgabor c = b 130202719Sgabor d = 1 131202719Sgabor e = 1 132202719Sgabor for (a = 3; 1 == 1 ; a = a + 2) { 133202719Sgabor b = b*s 134202719Sgabor c = c*a + d*b 135202719Sgabor d = d*a 136202719Sgabor g = c/d 137202719Sgabor if (g == e) { 138202719Sgabor scale = t 139202719Sgabor ibase = r 140202719Sgabor return (u*c/d) 141202719Sgabor } 142202719Sgabor e = g 143202719Sgabor } 144202719Sgabor} 145202719Sgabor 146202719Sgabordefine s(x) { 147202719Sgabor auto a, b, c, s, t, y, p, n, i, r 148202719Sgabor r = ibase 149202719Sgabor ibase = A 150202719Sgabor t = scale 151202719Sgabor y = x/.7853 152202719Sgabor s = t + length(y) - scale(y) 153202719Sgabor if (s < t) s = t 154202719Sgabor scale = s 155202719Sgabor p = a(1) 156202719Sgabor 157202719Sgabor scale = 0 158202719Sgabor if (x >= 0) n = (x/(2*p) + 1)/2 159202719Sgabor if (x < 0) n = (x/(2*p) - 1)/2 160202719Sgabor x = x - 4*n*p 161202719Sgabor if (n % 2 != 0) x = -x 162202719Sgabor 163202719Sgabor scale = t + length(1.2*t) - scale(1.2*t) 164202719Sgabor y = -x*x 165202719Sgabor a = x 166202719Sgabor b = 1 167202719Sgabor s = x 168202719Sgabor for (i =3 ; 1 == 1; i = i + 2) { 169202719Sgabor a = a*y 170202719Sgabor b = b*i*(i - 1) 171202719Sgabor c = a/b 172202719Sgabor if (c == 0) { 173202719Sgabor scale = t 174202719Sgabor ibase = r 175202719Sgabor return (s/1) 176202719Sgabor } 177202719Sgabor s = s + c 178202719Sgabor } 179202719Sgabor} 180202719Sgabor 181202719Sgabordefine c(x) { 182202719Sgabor auto t, r 183202719Sgabor r = ibase 184202719Sgabor ibase = A 185202719Sgabor t = scale 186202719Sgabor scale = scale + 1 187202719Sgabor x = s(x + 2*a(1)) 188202719Sgabor scale = t 189202719Sgabor ibase = r 190202719Sgabor return (x/1) 191202719Sgabor} 192202719Sgabor 193202719Sgabordefine a(x) { 194202719Sgabor auto a, b, c, d, e, f, g, s, t, r 195202719Sgabor if (x == 0) return(0) 196202719Sgabor 197202719Sgabor r = ibase 198202719Sgabor ibase = A 199202719Sgabor if (x == 1) { 200202719Sgabor if (scale < 52) { 201202719Sgabor a = .7853981633974483096156608458198757210492923498437764/1 202202719Sgabor ibase = r 203202719Sgabor return (a) 204202719Sgabor } 205202719Sgabor } 206202719Sgabor t = scale 207202719Sgabor f = 1 208202719Sgabor while (x > .5) { 209202719Sgabor scale = scale + 1 210202719Sgabor x = -(1 - sqrt(1. + x*x))/x 211202719Sgabor f = f*2 212202719Sgabor } 213202719Sgabor while (x < -.5) { 214202719Sgabor scale = scale + 1 215202719Sgabor x = -(1 - sqrt(1. + x*x))/x 216202719Sgabor f = f*2 217202719Sgabor } 218202719Sgabor s = -x*x 219202719Sgabor b = f 220202719Sgabor c = f 221202719Sgabor d = 1 222202719Sgabor e = 1 223202719Sgabor for (a = 3; 1 == 1; a = a + 2) { 224202719Sgabor b = b*s 225202719Sgabor c = c*a + d*b 226202719Sgabor d = d*a 227202719Sgabor g = c/d 228202719Sgabor if (g == e) { 229202719Sgabor ibase = r 230202719Sgabor scale = t 231202719Sgabor return (x*c/d) 232202719Sgabor } 233202719Sgabor e = g 234202719Sgabor } 235202719Sgabor} 236202719Sgabor 237202719Sgabordefine j(n,x) { 238202719Sgabor auto a, b, c, d, e, g, i, s, k, t, r 239202719Sgabor 240202719Sgabor r = ibase 241202719Sgabor ibase = A 242202719Sgabor t = scale 243202719Sgabor k = 1.36*x + 1.16*t - n 244202719Sgabor k = length(k) - scale(k) 245202719Sgabor if (k > 0) scale = scale + k 246202719Sgabor 247202719Sgabor s = -x*x/4 248202719Sgabor if (n < 0) { 249202719Sgabor n = -n 250202719Sgabor x = -x 251202719Sgabor } 252202719Sgabor a = 1 253202719Sgabor c = 1 254202719Sgabor for (i = 1; i <= n; i++) { 255202719Sgabor a = a*x 256202719Sgabor c = c*2*i 257202719Sgabor } 258202719Sgabor b = a 259202719Sgabor d = 1 260202719Sgabor e = 1 261202719Sgabor for (i = 1; 1; i++) { 262202719Sgabor a = a*s 263202719Sgabor b = b*i*(n + i) + a 264202719Sgabor c = c*i*(n + i) 265202719Sgabor g = b/c 266202719Sgabor if (g == e) { 267202719Sgabor ibase = r 268202719Sgabor scale = t 269202719Sgabor return (g/1) 270202719Sgabor } 271202719Sgabor e = g 272202719Sgabor } 273202719Sgabor} 274232994Skevlo/* vim: set filetype=bc shiftwidth=8 noexpandtab: */ 275