diff options
-rw-r--r-- | usr.bin/bc/bc.1 | 221 | ||||
-rw-r--r-- | usr.bin/bc/bc.library | 234 |
2 files changed, 455 insertions, 0 deletions
diff --git a/usr.bin/bc/bc.1 b/usr.bin/bc/bc.1 new file mode 100644 index 00000000000..161908fb6f6 --- /dev/null +++ b/usr.bin/bc/bc.1 @@ -0,0 +1,221 @@ +.\" $OpenBSD: bc.1,v 1.1 2003/09/25 19:34:22 otto Exp $ +.\" +.\" Copyright (C) Caldera International Inc. 2001-2002. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code and documentation must retain the above +.\" copyright notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed or owned by Caldera +.\" International, Inc. +.\" 4. Neither the name of Caldera International, Inc. nor the names of other +.\" contributors may be used to endorse or promote products derived from +.\" this software without specific prior written permission. +.\" +.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA +.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR +.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, +.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGE. +.\" +.\" @(#)bc.1 6.8 (Berkeley) 8/8/91 +.\" +.Dd August 8, 1991 +.Dt BC 1 +.Sh NAME +.Nm \&bc +.Nd arbitrary-precision arithmetic language and calculator +.Sh SYNOPSIS +.Nm \&bc +.Op Fl c +.Op Fl l +.Ar +.Sh DESCRIPTION +.Nm \&Bc +is an interactive processor for a language which resembles +C but provides unlimited precision arithmetic. +It takes input from any files given, then reads +the standard input. +.Pp +Options available: +.Bl -tag -width flag +.It Fl l +allow specification +of an arbitrary precision math library. +.It Fl c +.Nm \&Bc +is actually a preprocessor for +.Ar \&dc 1 , +which it invokes automatically, unless the +.Fl c +compile only. +option is present. +In this case the +.Ar \&dc +input is sent to the standard output instead. +.El +.Pp +The syntax for +.Nm \&bc +programs is as follows; +L means letter a-z, +E means expression, S means statement. +.Pp +Comments +.Bd -unfilled -offset indent -compact +are enclosed in /* and */. +.Ed +.Pp +Names +.Bd -unfilled -offset indent -compact +simple variables: L +array elements: L [ E ] +The words `ibase', `obase', and `scale' +.Ed +.Pp +Other operands +.Bd -unfilled -offset indent -compact +arbitrarily long numbers with optional sign and decimal point. +\&( E \&) +sqrt ( E ) +length ( E ) number of significant decimal digits +scale ( E ) number of digits right of decimal point +L ( E , ... , E ) +.Ed +.Pp +Operators +.Bd -unfilled -offset indent -compact +\&+ \- * / % ^ (% is remainder; ^ is power) +\&++ \-\- (prefix and postfix; apply to names) +\&== <= >= != < > +\&= += \-= *= /= %= ^= +.Ed +.Pp +Statements +.Bd -unfilled -offset indent -compact +E +{ S ; ... ; S } +if ( E ) S +while ( E ) S +for ( E ; E ; E ) S +null statement +break +quit +.Ed +.Pp +Function definitions +.Bd -unfilled -offset indent -compact +define L ( L ,..., L ) { + auto L, ... , L + S; ... S + return ( E ) +} +.Ed +.Pp +Functions in +.Fl l +math library +.Bl -tag -width j(n,x) -offset indent -compact +.It s(x) +sine +.It c(x) +cosine +.It e(x) +exponential +.It l(x) +log +.It a(x) +arctangent +.It j(n,x) +Bessel function +.El +.Pp +All function arguments are passed by value. +.Pp +The value of a statement that is an expression is printed +unless the main operator is an assignment. +Either semicolons or newlines may separate statements. +Assignment to +.Ar scale +influences the number of digits to be retained on arithmetic +operations in the manner of +.Xr \&dc 1 . +Assignments to +.Ar ibase +or +.Ar obase +set the input and output number radix respectively. +.Pp +The same letter may be used as an array, a function, +and a simple variable simultaneously. +All variables are global to the program. +`Auto' variables are pushed down during function calls. +When using arrays as function arguments +or defining them as automatic variables +empty square brackets must follow the array name. +.Pp +For example +.Bd -literal -offset indent +scale = 20 +define e(x){ + auto a, b, c, i, s + a = 1 + b = 1 + s = 1 + for(i=1; 1==1; i++){ + a = a*x + b = b*i + c = a/b + if(c == 0) return(s) + s = s+c + } +} +.Ed +.Pp +defines a function to compute an approximate value of +the exponential function and +.Pp +.Dl for(i=1; i<=10; i++) e(i) +.Pp +prints approximate values of the exponential function of +the first ten integers. +.Sh FILES +.\" /usr/lib/lib.b mathematical library +.Bl -tag -width xxxxx -compact +.It Xr \&dc 1 +Desk calculator Proper. +.El +.Sh SEE ALSO +.Xr \&dc 1 +.Rs +.%A L. L. Cherry +.%A R. Morris +.%T "BC \- An arbitrary precision desk-calculator language" +.Re +.Sh HISTORY +The +.Nm \&bc +command appeared in +.At v6 . +.Sh BUGS +No &&, \(or\\(or, or ! operators. +.Pp +.Ql For +statement must have all three E's. +.Pp +.Ql Quit +is interpreted when read, not when executed. diff --git a/usr.bin/bc/bc.library b/usr.bin/bc/bc.library new file mode 100644 index 00000000000..7f7289c8b63 --- /dev/null +++ b/usr.bin/bc/bc.library @@ -0,0 +1,234 @@ +/* $OpenBSD: bc.library,v 1.1 2003/09/25 19:34:22 otto Exp $ */ + +/* + * Copyright (C) Caldera International Inc. 2001-2002. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code and documentation must retain the above + * copyright notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed or owned by Caldera + * International, Inc. + * 4. Neither the name of Caldera International, Inc. nor the names of other + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA + * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT, + * INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * @(#)bc.library 5.1 (Berkeley) 4/17/91 + */ + +scale = 20 +define e(x){ + auto a, b, c, d, e, g, t, w, y + + t = scale + scale = t + .434*x + 1 + + w = 0 + if(x<0){ + x = -x + w = 1 + } + y = 0 + while(x>2){ + x = x/2 + y = y + 1 + } + + a=1 + b=1 + c=b + d=1 + e=1 + for(a=1;1==1;a++){ + b=b*x + c=c*a+b + d=d*a + g = c/d + if(g == e){ + g = g/1 + while(y--){ + g = g*g + } + scale = t + if(w==1) return(1/g) + return(g/1) + } + e=g + } +} + +define l(x){ + auto a, b, c, d, e, f, g, u, s, t + if(x <=0) return(1-10^scale) + t = scale + + f=1 + scale = scale + scale(x) - length(x) + 1 + s=scale + while(x > 2){ + s = s + (length(x)-scale(x))/2 + 1 + if(s>0) scale = s + x = sqrt(x) + f=f*2 + } + while(x < .5){ + s = s + (length(x)-scale(x))/2 + 1 + if(s>0) scale = s + x = sqrt(x) + f=f*2 + } + + scale = t + length(f) - scale(f) + 1 + u = (x-1)/(x+1) + + scale = scale + 1.1*length(t) - 1.1*scale(t) + s = u*u + b = 2*f + c = b + d = 1 + e = 1 + for(a=3;1==1;a=a+2){ + b=b*s + c=c*a+d*b + d=d*a + g=c/d + if(g==e){ + scale = t + return(u*c/d) + } + e=g + } +} + +define s(x){ + auto a, b, c, s, t, y, p, n, i + t = scale + y = x/.7853 + s = t + length(y) - scale(y) + if(s<t) s=t + scale = s + p = a(1) + + scale = 0 + if(x>=0) n = (x/(2*p)+1)/2 + if(x<0) n = (x/(2*p)-1)/2 + x = x - 4*n*p + if(n%2!=0) x = -x + + scale = t + length(1.2*t) - scale(1.2*t) + y = -x*x + a = x + b = 1 + s = x + for(i=3; 1==1; i=i+2){ + a = a*y + b = b*i*(i-1) + c = a/b + if(c==0){scale=t; return(s/1)} + s = s+c + } +} + +define c(x){ + auto t + t = scale + scale = scale+1 + x = s(x+2*a(1)) + scale = t + return(x/1) +} + +define a(x){ + auto a, b, c, d, e, f, g, s, t + if(x==0) return(0) + if(x==1) { + if(scale<52) { + return(.7853981633974483096156608458198757210492923498437764/1) + } + } + t = scale + f=1 + while(x > .5){ + scale = scale + 1 + x= -(1-sqrt(1.+x*x))/x + f=f*2 + } + while(x < -.5){ + scale = scale + 1 + x = -(1-sqrt(1.+x*x))/x + f=f*2 + } + s = -x*x + b = f + c = f + d = 1 + e = 1 + for(a=3;1==1;a=a+2){ + b=b*s + c=c*a+d*b + d=d*a + g=c/d + if(g==e){ + scale = t + return(x*c/d) + } + e=g + } +} + +define j(n,x){ + auto a,b,c,d,e,g,i,s,k,t + + t = scale + k = 1.36*x + 1.16*t - n + k = length(k) - scale(k) + if(k>0) scale = scale + k + + s= -x*x/4 + if(n<0){ + n= -n + x= -x + } + a=1 + c=1 + for(i=1;i<=n;i++){ + a=a*x + c = c*2*i + } + b=a + d=1 + e=1 + for(i=1;1;i++){ + a=a*s + b=b*i*(n+i) + a + c=c*i*(n+i) + g=b/c + if(g==e){ + scale = t + return(g/1) + } + e=g + } +} |