mdoc version three
[unix-history] / usr / src / usr.bin / bc / bc.1
CommitLineData
2791ff57
KB
1.\" Copyright (c) 1990 The Regents of the University of California.
2.\" All rights reserved.
67aa4194 3.\"
2791ff57
KB
4.\" %sccs.include.proprietary.roff%
5.\"
8e21e06d 6.\" @(#)bc.1 6.8 (Berkeley) %G%
b5dc1377
CL
7.\"
8.Dd
9.Dt BC 1
10.Os ATT 7th
11.Sh NAME
8e21e06d 12.Nm \&bc
b5dc1377
CL
13.Nd arbitrary-precision arithmetic language and calculator
14.Sh SYNOPSIS
8e21e06d 15.Nm \&bc
b5dc1377
CL
16.Op Fl c
17.Op Fl l
18.Ar
19.Sh DESCRIPTION
8e21e06d 20.Nm \&Bc
67aa4194
KM
21is an interactive processor for a language which resembles
22C but provides unlimited precision arithmetic.
23It takes input from any files given, then reads
24the standard input.
8e21e06d
CL
25.Pp
26Options available:
27.Bl -tag -width flag
28.It Fl l
5325ced3 29allow specification
67aa4194 30of an arbitrary precision math library.
8e21e06d
CL
31.It Fl c
32.Nm \&Bc
b5dc1377 33is actually a preprocessor for
8e21e06d 34.Ar \&dc 1 ,
b5dc1377
CL
35which it invokes automatically, unless the
36.Fl c
37compile only.
38option is present.
39In this case the
8e21e06d 40.Ar \&dc
b5dc1377 41input is sent to the standard output instead.
8e21e06d 42.El
b5dc1377
CL
43.Pp
44The syntax for
8e21e06d 45.Nm \&bc
67aa4194
KM
46programs is as follows;
47L means letter a-z,
48E means expression, S means statement.
b5dc1377 49.Pp
67aa4194 50Comments
8e21e06d 51.Bd -unfilled -offset indent -compact
5325ced3 52are enclosed in /* and */.
8e21e06d 53.Ed
b5dc1377 54.Pp
67aa4194 55Names
8e21e06d 56.Bd -unfilled -offset indent -compact
5325ced3
CL
57simple variables: L
58array elements: L [ E ]
67aa4194 59The words `ibase', `obase', and `scale'
8e21e06d 60.Ed
b5dc1377 61.Pp
67aa4194 62Other operands
8e21e06d 63.Bd -unfilled -offset indent -compact
5325ced3
CL
64arbitrarily long numbers with optional sign and decimal point.
65\&( E \&)
66sqrt ( E )
67length ( E ) number of significant decimal digits
68scale ( E ) number of digits right of decimal point
69L ( E , ... , E )
8e21e06d 70.Ed
b5dc1377 71.Pp
67aa4194 72Operators
8e21e06d 73.Bd -unfilled -offset indent -compact
5325ced3
CL
74\&+ \- * / % ^ (% is remainder; ^ is power)
75\&++ \-\- (prefix and postfix; apply to names)
76\&== <= >= != < >
77\&= += \-= *= /= %= ^=
8e21e06d 78.Ed
b5dc1377 79.Pp
67aa4194 80Statements
8e21e06d 81.Bd -unfilled -offset indent -compact
67aa4194 82E
67aa4194 83{ S ; ... ; S }
67aa4194 84if ( E ) S
67aa4194 85while ( E ) S
67aa4194 86for ( E ; E ; E ) S
67aa4194 87null statement
67aa4194 88break
67aa4194 89quit
8e21e06d 90.Ed
b5dc1377 91.Pp
67aa4194 92Function definitions
8e21e06d 93.Bd -unfilled -offset indent -compact
67aa4194 94define L ( L ,..., L ) {
67aa4194 95 auto L, ... , L
67aa4194 96 S; ... S
67aa4194 97 return ( E )
67aa4194 98}
8e21e06d 99.Ed
b5dc1377
CL
100.Pp
101Functions in
102.Fl l
67aa4194 103math library
8e21e06d
CL
104.Bl -tag -width j(n,x) -offset indent -compact
105.It s(x)
b5dc1377 106sine
8e21e06d 107.It c(x)
b5dc1377 108cosine
8e21e06d 109.It e(x)
b5dc1377 110exponential
8e21e06d 111.It l(x)
b5dc1377 112log
8e21e06d 113.It a(x)
b5dc1377 114arctangent
8e21e06d 115.It j(n,x)
b5dc1377 116Bessel function
8e21e06d 117.El
b5dc1377 118.Pp
67aa4194 119All function arguments are passed by value.
b5dc1377 120.Pp
67aa4194
KM
121The value of a statement that is an expression is printed
122unless the main operator is an assignment.
123Either semicolons or newlines may separate statements.
124Assignment to
b5dc1377 125.Ar scale
67aa4194
KM
126influences the number of digits to be retained on arithmetic
127operations in the manner of
8e21e06d 128.Xr \&dc 1 .
67aa4194 129Assignments to
b5dc1377 130.Ar ibase
67aa4194 131or
b5dc1377 132.Ar obase
67aa4194 133set the input and output number radix respectively.
b5dc1377 134.Pp
67aa4194
KM
135The same letter may be used as an array, a function,
136and a simple variable simultaneously.
137All variables are global to the program.
138`Auto' variables are pushed down during function calls.
139When using arrays as function arguments
140or defining them as automatic variables
141empty square brackets must follow the array name.
b5dc1377 142.Pp
67aa4194 143For example
8e21e06d 144.Bd -literal -offset indent
67aa4194
KM
145scale = 20
146define e(x){
147 auto a, b, c, i, s
148 a = 1
149 b = 1
150 s = 1
151 for(i=1; 1==1; i++){
152 a = a*x
153 b = b*i
154 c = a/b
155 if(c == 0) return(s)
156 s = s+c
157 }
158}
8e21e06d 159.Ed
b5dc1377 160.Pp
67aa4194
KM
161defines a function to compute an approximate value of
162the exponential function and
b5dc1377
CL
163.Pp
164.Dl for(i=1; i<=10; i++) e(i)
165.Pp
67aa4194
KM
166prints approximate values of the exponential function of
167the first ten integers.
b5dc1377 168.Sh FILES
dd9e2e8d 169.\" /usr/lib/lib.b mathematical library
8e21e06d
CL
170.Bl -tag -width xxxxx -compact
171.It Xr \&dc 1
172Desk calculator Proper.
173.El
b5dc1377 174.Sh SEE ALSO
8e21e06d
CL
175.Xr \&dc 1
176.Rs
177.%A L. L. Cherry
178.%A R. Morris
179.%T "BC \- An arbitrary precision desk-calculator language"
180.Re
b5dc1377
CL
181.Sh HISTORY
182The
8e21e06d
CL
183.Nm \&bc
184command appeared in
185.At v6 .
b5dc1377
CL
186.Sh BUGS
187No &&, \(or\\(or, or ! operators.
8e21e06d 188.Pp
5325ced3 189.Ql For
67aa4194 190statement must have all three E's.
8e21e06d 191.Pp
5325ced3 192.Ql Quit
67aa4194 193is interpreted when read, not when executed.