xref: /freebsd/usr.bin/bc/bc.1 (revision 7899f917b1c0ea178f1d2be0cfb452086d079d23)
1.\"	$OpenBSD: bc.1,v 1.32 2015/11/17 05:45:35 mmcc 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.Dd November 21, 2015
36.Dt BC 1
37.Os
38.Sh NAME
39.Nm bc
40.Nd arbitrary-precision arithmetic language and calculator
41.Sh SYNOPSIS
42.Nm bc
43.Op Fl chlv
44.Op Fl e Ar expression
45.Op Ar file ...
46.Sh DESCRIPTION
47.Nm
48is an interactive processor for a language which resembles
49C but provides unlimited precision arithmetic.
50It takes input from any expressions on the command line and
51any files given, then reads the standard input.
52.Pp
53Options available:
54.Bl -tag -width Ds
55.It Fl c
56.Nm
57is actually a preprocessor for
58.Xr dc 1 ,
59which it invokes automatically, unless the
60.Fl c
61.Pq compile only
62option is present.
63In this case the generated
64.Xr dc 1
65instructions are sent to the standard output,
66instead of being interpreted by a running
67.Xr dc 1
68process.
69.It Fl e Ar expression , Fl Fl expression Ar expression
70Evaluate
71.Ar expression .
72If multiple
73.Fl e
74options are specified, they are processed in the order given,
75separated by newlines.
76.It Fl h , Fl Fl help
77Prints usage information.
78.It Fl l , Fl Fl mathlib
79Allow specification of an arbitrary precision math library.
80The definitions in the library are available to command line
81expressions.
82.It Fl v , Fl Fl version
83Prints version information.
84.El
85.Pp
86The syntax for
87.Nm
88programs is as follows:
89.Sq L
90means letter a-z;
91.Sq E
92means expression;
93.Sq S
94means statement.
95As a non-portable extension, it is possible to use long names
96in addition to single letter names.
97A long name is a sequence starting with a lowercase letter
98followed by any number of lowercase letters and digits.
99The underscore character
100.Pq Sq _
101counts as a letter.
102.Pp
103Comments
104.Bd -unfilled -offset indent -compact
105are enclosed in /* and */
106are enclosed in # and the next newline
107.Ed
108.Pp
109The newline is not part of the line comment,
110which in itself is a non-portable extension.
111.Pp
112Names
113.Bd -unfilled -offset indent -compact
114simple variables: L
115array elements: L [ E ]
116The words `ibase', `obase', and `scale'
117The word `last' or a single dot
118.Ed
119.Pp
120Other operands
121.Bd -unfilled -offset indent -compact
122arbitrarily long numbers with optional sign and decimal point
123( E )
124sqrt ( E )
125length ( E )	number of significant decimal digits
126scale ( E )	number of digits right of decimal point
127L ( E , ... , E )
128.Ed
129.Pp
130The sequence
131.Sq \e<newline><whitespace>
132is ignored within numbers.
133.Pp
134Operators
135.Pp
136The following arithmetic and logical operators can be used.
137The semantics of the operators is the same as in the C language.
138They are listed in order of decreasing precedence.
139Operators in the same group have the same precedence.
140.Bl -column "= += \-= *= /= %= ^=" "Associativity" "multiply, divide, modulus" -offset indent
141.It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description"
142.It "++ \-\-" Ta "none" Ta "increment, decrement"
143.It "\-" Ta "none" Ta "unary minus"
144.It "^" Ta "right" Ta "power"
145.It "* / %" Ta "left" Ta "multiply, divide, modulus"
146.It "+ \-" Ta "left" Ta "plus, minus"
147.It "= += -= *= /= %= ^=" Ta "right" Ta "assignment"
148.It "== <= >= != < >" Ta "none" Ta "relational"
149.It "!" Ta "none" Ta "boolean not"
150.It "&&" Ta "left" Ta "boolean and"
151.It "||" Ta "left" Ta "boolean or"
152.El
153.Pp
154Note the following:
155.Bl -bullet -offset indent
156.It
157The relational operators may appear in any expression.
158The
159.St -p1003.1-2008
160standard only allows them in the conditional expression of an
161.Sq if ,
162.Sq while
163or
164.Sq for
165statement.
166.It
167The relational operators have a lower precedence than the assignment
168operators.
169This has the consequence that the expression
170.Sy a = b < c
171is interpreted as
172.Sy (a = b) < c ,
173which is probably not what the programmer intended.
174.It
175In contrast with the C language, the relational operators all have
176the same precedence, and are non-associative.
177The expression
178.Sy a < b < c
179will produce a syntax error.
180.It
181The boolean operators (!, && and ||) are non-portable extensions.
182.It
183The boolean not
184(!) operator has much lower precedence than the same operator in the
185C language.
186This has the consequence that the expression
187.Sy !a < b
188is interpreted as
189.Sy !(a < b) .
190Prudent programmers use parentheses when writing expressions involving
191boolean operators.
192.El
193.Pp
194Statements
195.Bd -unfilled -offset indent -compact
196E
197{ S ; ... ; S }
198if ( E ) S
199if ( E ) S else S
200while ( E ) S
201for ( E ; E ; E ) S
202null statement
203break
204continue
205quit
206a string of characters, enclosed in double quotes
207print E ,..., E
208.Ed
209.Pp
210A string may contain any character, except double quote.
211The if statement with an else branch is a non-portable extension.
212All three E's in a for statement may be empty.
213This is a non-portable extension.
214The continue and print statements are also non-portable extensions.
215.Pp
216The print statement takes a list of comma-separated expressions.
217Each expression in the list is evaluated and the computed
218value is printed and assigned to the variable `last'.
219No trailing newline is printed.
220The expression may also be a string enclosed in double quotes.
221Within these strings the following escape sequences may be used:
222.Sq \ea
223for bell (alert),
224.Sq \eb
225for backspace,
226.Sq \ef
227for formfeed,
228.Sq \en
229for newline,
230.Sq \er
231for carriage return,
232.Sq \et
233for tab,
234.Sq \eq
235for double quote and
236.Sq \e\e
237for backslash.
238Any other character following a backslash will be ignored.
239Strings will not be assigned to `last'.
240.Pp
241Function definitions
242.Bd -unfilled -offset indent
243define L ( L ,..., L ) {
244	auto L, ... , L
245	S; ... S
246	return ( E )
247}
248.Ed
249.Pp
250As a non-portable extension, the opening brace of the define statement
251may appear on the next line.
252The return statement may also appear in the following forms:
253.Bd -unfilled -offset indent
254return
255return ()
256return E
257.Ed
258.Pp
259The first two are equivalent to the statement
260.Dq return 0 .
261The last form is a non-portable extension.
262Not specifying a return statement is equivalent to writing
263.Dq return (0) .
264.Pp
265Functions available in the math library, which is loaded by specifying the
266.Fl l
267flag on the command line
268.Pp
269.Bl -tag -width j(n,x) -offset indent -compact
270.It s(x)
271sine
272.It c(x)
273cosine
274.It e(x)
275exponential
276.It l(x)
277log
278.It a(x)
279arctangent
280.It j(n,x)
281Bessel function
282.El
283.Pp
284All function arguments are passed by value.
285.Pp
286The value of a statement that is an expression is printed
287unless the main operator is an assignment.
288The value printed is assigned to the special variable `last'.
289This is a non-portable extension.
290A single dot may be used as a synonym for `last'.
291Either semicolons or newlines may separate statements.
292Assignment to
293.Ar scale
294influences the number of digits to be retained on arithmetic
295operations in the manner of
296.Xr dc 1 .
297Assignments to
298.Ar ibase
299or
300.Ar obase
301set the input and output number radix respectively.
302.Pp
303The same letter may be used as an array, a function,
304and a simple variable simultaneously.
305All variables are global to the program.
306`Auto' variables are pushed down during function calls.
307When using arrays as function arguments
308or defining them as automatic variables,
309empty square brackets must follow the array name.
310.Pp
311For example
312.Bd -literal -offset indent
313scale = 20
314define e(x){
315	auto a, b, c, i, s
316	a = 1
317	b = 1
318	s = 1
319	for(i=1; 1==1; i++){
320		a = a*x
321		b = b*i
322		c = a/b
323		if(c == 0) return(s)
324		s = s+c
325	}
326}
327.Ed
328.Pp
329defines a function to compute an approximate value of
330the exponential function and
331.Pp
332.Dl for(i=1; i<=10; i++) e(i)
333.Pp
334prints approximate values of the exponential function of
335the first ten integers.
336.Bd -literal -offset indent
337$ bc -l -e 'scale = 500; 2 * a(2^10000)' -e quit
338.Ed
339.Pp
340prints an approximation of pi.
341.Sh COMMAND LINE EDITING
342.Nm
343supports interactive command line editing, via the
344.Xr editline 3
345library.
346It is enabled by default if input is from a tty.
347Previous lines can be recalled and edited with the arrow keys,
348and other GNU Emacs-style editing keys may be used as well.
349.Pp
350The
351.Xr editline 3
352library is configured with a
353.Pa .editrc
354file \- refer to
355.Xr editrc 5
356for more information.
357.Sh FILES
358.Bl -tag -width /usr/share/misc/bc.library -compact
359.It Pa /usr/share/misc/bc.library
360math library, read when the
361.Fl l
362option is specified on the command line.
363.El
364.Sh COMPATIBILITY
365The
366.Fl q
367and
368.Fl Fl quiet
369options are no-ops for compatibility with some other implementations of
370.Nm
371and their use is discouraged.
372.Sh SEE ALSO
373.Xr dc 1
374.Sh STANDARDS
375The
376.Nm
377utility is compliant with the
378.St -p1003.1-2008
379specification.
380.Pp
381The flags
382.Op Fl ce ,
383as well as the parts noted above,
384are extensions to that specification.
385.Sh HISTORY
386The
387.Nm
388command first appeared in
389.At v6 .
390A complete rewrite of the
391.Nm
392command first appeared in
393.Ox 3.5 .
394.Sh AUTHORS
395.An -nosplit
396The original version of the
397.Nm
398command was written by
399.An Robert Morris
400and
401.An Lorinda Cherry .
402The current version of the
403.Nm
404utility was written by
405.An Otto Moerbeek .
406.Sh BUGS
407The
408.Ql quit
409statement is interpreted when read, not when executed.
410.Pp
411Some non-portable extensions, as found in the GNU version of the
412.Nm
413utility are not implemented (yet).
414