blob: 571943409974219573793fea8ab07880d25ccfb6 [file] [log] [blame]
rsc058b0112005-01-03 06:40:20 +00001.TH BC 1
2.SH NAME
3bc \- arbitrary-precision arithmetic language
4.SH SYNOPSIS
5.B bc
6[
7.B -c
8]
9[
10.B -l
11]
12[
13.B -s
14]
15[
16.I file ...
17]
18.SH DESCRIPTION
19.I Bc
20is an interactive processor for a language that resembles
21C but provides arithmetic on numbers of arbitrary length with up
22to 100 digits right of the decimal point.
23It takes input from any files given, then reads
24the standard input.
25The
26.B -l
27argument stands for the name
28of an arbitrary precision math library.
29The
30.B -s
31argument suppresses the automatic display
32of calculation results; all output is via the
33.B print
34command.
35.PP
36The following syntax for
37.I bc
38programs is like that of C;
39.I L
40means letter
41.BR a - z ,
42.I E
43means expression,
44.I S
45means statement.
46.TF length(E)
47.TP
48Lexical
49.RS
50.HP
51comments are enclosed in
52.B /* */
53.HP
54newlines end statements
55.RE
56.TP
57Names
58.IP
59simple variables:
60.I L
61.br
62array elements:
63.IB L [ E ]
64.br
65The words
66.BR ibase ,
67.BR obase ,
68and
69.B scale
70.TP
71Other operands
72.IP
73arbitrarily long numbers with optional sign and decimal point.
74.RS
75.TP
76.BI ( E )
77.TP
78.BI sqrt( E )
79.TP
80.BI length( E )
81number of significant decimal digits
82.TP
83.BI scale( E )
84number of digits right of decimal point
85.TP
86.IB L ( E , ... ,\fIE\fP)
87function call
88.RE
89.TP
90Operators
91.RS
92.HP
93.B "+ - * / % ^\ "
94.RB ( %
95is remainder;
96.B ^
97is power)
98.HP
99.B "++ --\ "
100.TP
101.B "== <= >= != < >"
102.TP
103.B "= += -= *= /= %= ^="
104.RE
105.TP
106Statements
107.RS
108.br
109.I E
110.br
111.B {
112.I S
113.B ;
114\&...
115.B ;
116.I S
117.B }
118.br
119.B "print"
120.I E
121.br
122.B "if ("
123.I E
124.B )
125.I S
126.br
127.B "while ("
128.I E
129.B )
130.I S
131.br
132.B "for ("
133.I E
134.B ;
135.I E
136.B ;
137.I E
138.B ")"
139.I S
140.br
141null statement
142.br
143.B break
144.br
145.B quit
146.br
147\fL"\fRtext\fL"\fR
148.RE
149.TP
150Function definitions
151.RS
152.br
153.B define
154.I L
155.B (
156.I L
157.B ,
158\&...
159.B ,
160.I L
161.B ){
162.PD0
163.br
164.B auto
165.I L
166.B ,
167\&...
168.B ,
169.I L
170.br
171.I S
172.B ;
173\&...
174.B ;
175.I S
176.br
177.B return
178.I E
179.LP
180.B }
181.RE
182.TP
183Functions in
184.B -l
185math library
186.RS
187.TP
188.BI s( x )
189sine
190.TP
191.BI c( x )
192cosine
193.TP
194.BI e( x )
195exponential
196.TP
197.BI l( x )
198log
199.TP
200.BI a( x )
201arctangent
202.TP
203.BI j( "n, x" )
204Bessel function
205.RE
206.PP
207.DT
208All function arguments are passed by value.
209.PD
210.PP
211The value of an expression at the top level is printed
212unless the main operator is an assignment or the
213.B -s
214command line argument is given.
215Text in quotes, which may include newlines, is always printed.
216Either semicolons or newlines may separate statements.
217Assignment to
218.B scale
219influences the number of digits to be retained on arithmetic
220operations in the manner of
221.IR dc (1).
222Assignments to
223.B ibase
224or
225.B obase
226set the input and output number radix respectively.
227.PP
228The same letter may be used as an array, a function,
229and a simple variable simultaneously.
230All variables are global to the program.
231Automatic variables are pushed down during function calls.
232In a declaration of an array as a function argument
233or automatic variable
234empty square brackets must follow the array name.
235.PP
236.I Bc
237is actually a preprocessor for
238.IR dc (1),
239which it invokes automatically, unless the
240.B -c
241(compile only)
242option is present.
243In this case the
244.I dc
245input is sent to the standard output instead.
246.SH EXAMPLE
247Define a function to compute an approximate value of
248the exponential.
249Use it to print 10 values.
250(The exponential function in the library gives better answers.)
251.PP
252.EX
253scale = 20
254define e(x) {
255 auto a, b, c, i, s
256 a = 1
257 b = 1
258 s = 1
259 for(i=1; 1; i++) {
260 a *= x
261 b *= i
262 c = a/b
263 if(c == 0) return s
264 s += c
265 }
266}
267for(i=1; i<=10; i++) print e(i)
268.EE
269.SH FILES
rscc8b63422005-01-13 04:49:19 +0000270.B \*9/lib/bclib
rsc058b0112005-01-03 06:40:20 +0000271mathematical library
272.SH SOURCE
rscc3674de2005-01-11 17:37:33 +0000273.B \*9/src/cmd/bc.y
rsc058b0112005-01-03 06:40:20 +0000274.SH "SEE ALSO"
275.IR dc (1),
276.IR hoc (1)
277.SH BUGS
278No
279.LR && ,
280.LR || ,
281or
282.L !
283operators.
rscc8b63422005-01-13 04:49:19 +0000284.PP
rsc058b0112005-01-03 06:40:20 +0000285A
286.L for
287statement must have all three
288.LR E s.
rscc8b63422005-01-13 04:49:19 +0000289.PP
rsc058b0112005-01-03 06:40:20 +0000290A
291.L quit
292is interpreted when read, not when executed.