xref: /dragonfly/usr.bin/bc/bc.1 (revision c37c9ab3)
1.\"	$OpenBSD: bc.1,v 1.22 2007/05/31 19:20:07 jmc Exp $
2.\"
3.\" Copyright (C) Caldera International Inc.  2001-2002.
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code and documentation must retain the above
10.\"    copyright notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed or owned by Caldera
17.\"	International, Inc.
18.\" 4. Neither the name of Caldera International, Inc. nor the names of other
19.\"    contributors may be used to endorse or promote products derived from
20.\"    this software without specific prior written permission.
21.\"
22.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
27.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33.\" POSSIBILITY OF SUCH DAMAGE.
34.\"
35.\"	@(#)bc.1	6.8 (Berkeley) 8/8/91
36.\"
37.Dd November 14, 2009
38.Dt BC 1
39.Os
40.Sh NAME
41.Nm bc
42.Nd arbitrary-precision arithmetic language and calculator
43.Sh SYNOPSIS
44.Nm
45.Op Fl cl
46.Op Fl e Ar expression
47.Op Ar file ...
48.Sh DESCRIPTION
49.Nm
50is an interactive processor for a language which resembles
51C but provides unlimited precision arithmetic.
52It takes input from any expressions on the command line and
53any files given, then reads the standard input.
54.Pp
55Options available:
56.Bl -tag -width Ds
57.It Fl c
58.Nm
59is actually a preprocessor for
60.Xr dc 1 ,
61which it invokes automatically, unless the
62.Fl c
63.Pq compile only
64option is present.
65In this case the generated
66.Xr dc 1
67instructions are sent to the standard output,
68instead of being interpreted by a running
69.Xr dc 1
70process.
71.It Fl e Ar expression
72Evaluate
73.Ar expression .
74If multiple
75.Fl e
76options are specified, they are processed in the order given,
77separated by newlines.
78.It Fl l
79Allow specification of an arbitrary precision math library.
80The definitions in the library are available to command line
81expressions.
82.El
83.Pp
84The syntax for
85.Nm
86programs is as follows:
87.Sq L
88means letter a-z;
89.Sq E
90means expression;
91.Sq S
92means statement.
93As a non-portable extension, it is possible to use long names
94in addition to single letter names.
95A long name is a sequence starting with a lowercase letter
96followed by any number of lowercase letters and digits.
97The underscore character
98.Pq Sq _
99counts as a letter.
100.Pp
101Comments
102.Bd -unfilled -offset indent -compact
103are enclosed in /* and */
104are enclosed in # and the next newline
105.Ed
106.Pp
107The newline is not part of the line comment,
108which in itself is a non-portable extension.
109.Pp
110Names
111.Bd -unfilled -offset indent -compact
112simple variables: L
113array elements: L [ E ]
114The words `ibase', `obase', and `scale'
115The word `last' or a single dot
116.Ed
117.Pp
118Other operands
119.Bd -unfilled -offset indent -compact
120arbitrarily long numbers with optional sign and decimal point
121( E )
122sqrt ( E )
123length ( E )	number of significant decimal digits
124scale ( E )	number of digits right of decimal point
125L ( E , ... , E )
126.Ed
127.Pp
128The sequence
129.Sq \e<newline><whitespace>
130is ignored within numbers.
131.Pp
132Operators
133.Pp
134The following arithmetic and logical operators can be used.
135The semantics of the operators is the same as in the C language.
136They are listed in order of decreasing precedence.
137Operators in the same group have the same precedence.
138.Bl -column "= += \-= *= /= %= ^=" "Associativity" "multiply, divide, modulus" -offset indent
139.It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description"
140.It "++ \-\-" Ta "none" Ta "increment, decrement"
141.It "\-" Ta "none" Ta "unary minus"
142.It "^" Ta "right" Ta "power"
143.It "* / %" Ta "left" Ta "multiply, divide, modulus"
144.It "+ \-" Ta "left" Ta "plus, minus"
145.It "= += -= *= /= %= ^=" Ta "right" Ta "assignment"
146.It "== <= >= != < >" Ta "none" Ta "relational"
147.It "!" Ta "none" Ta "boolean not"
148.It "&&" Ta "left" Ta "boolean and"
149.It "||" Ta "left" Ta "boolean or"
150.El
151.Pp
152Note the following:
153.Bl -bullet -offset indent
154.It
155The relational operators may appear in any expression.
156The
157.St -p1003.2
158standard only allows them in the conditional expression of an
159.Sq if ,
160.Sq while
161or
162.Sq for
163statement.
164.It
165The relational operators have a lower precedence than the assignment
166operators.
167This has the consequence that the expression
168.Sy a = b < c
169is interpreted as
170.Sy (a = b) < c ,
171which is probably not what the programmer intended.
172.It
173In contrast with the C language, the relational operators all have
174the same precedence, and are non-associative.
175The expression
176.Sy a < b < c
177will produce a syntax error.
178.It
179The boolean operators (!, && and ||) are non-portable extensions.
180.It
181The boolean not
182(!) operator has much lower precedence than the same operator in the
183C language.
184This has the consequence that the expression
185.Sy !a < b
186is interpreted as
187.Sy !(a < b) .
188Prudent programmers use parentheses when writing expressions involving
189boolean operators.
190.El
191.Pp
192Statements
193.Bd -unfilled -offset indent -compact
194E
195{ S ; ... ; S }
196if ( E ) S
197if ( E ) S else S
198while ( E ) S
199for ( E ; E ; E ) S
200null statement
201break
202continue
203quit
204a string of characters, enclosed in double quotes
205print E ,..., E
206.Ed
207.Pp
208A string may contain any character, except a double quote.
209The if statement with an else branch is a non-portable extension.
210All three E's in a for statement may be empty.
211This is a non-portable extension.
212The continue and print statements are also non-portable extensions.
213.Pp
214The print statement takes a list of comma-separated expressions.
215Each expression in the list is evaluated and the computed
216value is printed and assigned to the variable `last'.
217No trailing newline is printed.
218The expression may also be a string enclosed in double quotes.
219Within these strings the following escape sequences may be used:
220.Sq \ea
221for bell (alert),
222.Sq \eb
223for backspace,
224.Sq \ef
225for formfeed,
226.Sq \en
227for newline,
228.Sq \er
229for carriage return,
230.Sq \et
231for tab,
232.Sq \eq
233for double quote and
234.Sq \e\e
235for backslash.
236Any other character following a backslash will be ignored.
237Strings will not be assigned to `last'.
238.Pp
239Function definitions
240.Bd -unfilled -offset indent
241define L ( L ,..., L ) {
242	auto L, ... , L
243	S; ... S
244	return ( E )
245}
246.Ed
247.Pp
248As a non-portable extension, the opening brace of the define statement
249may appear on the next line.
250The return statement may also appear in the following forms:
251.Bd -unfilled -offset indent
252return
253return ()
254return E
255.Ed
256.Pp
257The first two are equivalent to the statement
258.Dq return 0 .
259The last form is a non-portable extension.
260Not specifying a return statement is equivalent to writing
261.Dq return (0) .
262.Pp
263Functions available in the math library, which is loaded by specifying the
264.Fl l
265flag on the command line
266.Pp
267.Bl -tag -width j(n,x) -offset indent -compact
268.It s(x)
269sine
270.It c(x)
271cosine
272.It e(x)
273exponential
274.It l(x)
275log
276.It a(x)
277arctangent
278.It j(n,x)
279Bessel function
280.El
281.Pp
282All function arguments are passed by value.
283.Pp
284The value of a statement that is an expression is printed
285unless the main operator is an assignment.
286The value printed is assigned to the special variable `last'.
287This is a non-portable extension.
288A single dot may be used as a synonym for `last'.
289Either semicolons or newlines may separate statements.
290Assignment to
291.Ar scale
292influences the number of digits to be retained on arithmetic
293operations in the manner of
294.Xr dc 1 .
295Assignments to
296.Ar ibase
297or
298.Ar obase
299set the input and output number radix respectively.
300.Pp
301The same letter may be used as an array, a function,
302and a simple variable simultaneously.
303All variables are global to the program.
304`Auto' variables are pushed down during function calls.
305When using arrays as function arguments
306or defining them as automatic variables,
307empty square brackets must follow the array name.
308.Pp
309For example
310.Bd -literal -offset indent
311scale = 20
312define e(x){
313	auto a, b, c, i, s
314	a = 1
315	b = 1
316	s = 1
317	for(i=1; 1==1; i++){
318		a = a*x
319		b = b*i
320		c = a/b
321		if(c == 0) return(s)
322		s = s+c
323	}
324}
325.Ed
326.Pp
327defines a function to compute an approximate value of
328the exponential function and
329.Pp
330.Dl for(i=1; i<=10; i++) e(i)
331.Pp
332prints approximate values of the exponential function of
333the first ten integers.
334.Bd -literal -offset indent
335$ bc -l -e 'scale = 500; 2 * a(2^10000)' -e quit
336.Ed
337prints an approximation of pi.
338.Sh FILES
339.Bl -tag -width /usr/share/misc/bc.library -compact
340.It Pa /usr/share/misc/bc.library
341math library, read when the
342.Fl l
343option is specified on the command line.
344.El
345.Sh SEE ALSO
346.Xr dc 1
347.Rs
348.%A "Lorinda Cherry"
349.%A "Robert Morris"
350.%B "4.4BSD Users's Supplementary Documents (USD)"
351.%T "BC \- An Arbitrary Precision Desk-Calculator Language"
352.Re
353.Sh STANDARDS
354The
355.Nm
356utility is compliant with the
357.St -p1003.1-2004
358specification.
359.Pp
360The flags
361.Op Fl ce
362are extensions to that specification.
363.Sh HISTORY
364The
365.Nm
366command first appeared in
367.At v6 .
368A complete rewrite of the
369.Nm
370command first appeared in
371.Ox 3.5 .
372.Sh AUTHORS
373.An -nosplit
374The original version of the
375.Nm
376command was written by
377.An Robert Morris
378and
379.An Lorinda Cherry .
380The current version of the
381.Nm
382utility was written by
383.An Otto Moerbeek .
384.Sh BUGS
385.Ql Quit
386is interpreted when read, not when executed.
387.Pp
388Some non-portable extensions, as found in the GNU version of the
389.Nm
390utility are not implemented (yet).
391