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