xref: /openbsd-src/usr.bin/bc/bc.1 (revision 8e7768061bedd57d42860056678ff81f368bcb88)
1*8e776806Sjmc.\"	$OpenBSD: bc.1,v 1.36 2024/07/31 05:36:13 jmc Exp $
266f52024Sotto.\"
366f52024Sotto.\" Copyright (C) Caldera International Inc.  2001-2002.
466f52024Sotto.\" All rights reserved.
566f52024Sotto.\"
666f52024Sotto.\" Redistribution and use in source and binary forms, with or without
766f52024Sotto.\" modification, are permitted provided that the following conditions
866f52024Sotto.\" are met:
966f52024Sotto.\" 1. Redistributions of source code and documentation must retain the above
1066f52024Sotto.\"    copyright notice, this list of conditions and the following disclaimer.
1166f52024Sotto.\" 2. Redistributions in binary form must reproduce the above copyright
1266f52024Sotto.\"    notice, this list of conditions and the following disclaimer in the
1366f52024Sotto.\"    documentation and/or other materials provided with the distribution.
1466f52024Sotto.\" 3. All advertising materials mentioning features or use of this software
1566f52024Sotto.\"    must display the following acknowledgement:
1666f52024Sotto.\"	This product includes software developed or owned by Caldera
1766f52024Sotto.\"	International, Inc.
1866f52024Sotto.\" 4. Neither the name of Caldera International, Inc. nor the names of other
1966f52024Sotto.\"    contributors may be used to endorse or promote products derived from
2066f52024Sotto.\"    this software without specific prior written permission.
2166f52024Sotto.\"
2266f52024Sotto.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
2366f52024Sotto.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
2466f52024Sotto.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2566f52024Sotto.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2666f52024Sotto.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
2766f52024Sotto.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2866f52024Sotto.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2966f52024Sotto.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3066f52024Sotto.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
3166f52024Sotto.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
3266f52024Sotto.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3366f52024Sotto.\" POSSIBILITY OF SUCH DAMAGE.
3466f52024Sotto.\"
3566f52024Sotto.\"	@(#)bc.1	6.8 (Berkeley) 8/8/91
3666f52024Sotto.\"
37*8e776806Sjmc.Dd $Mdocdate: July 31 2024 $
3866f52024Sotto.Dt BC 1
39660b00acSschwarze.Os
4066f52024Sotto.Sh NAME
415db927c3Sjmc.Nm bc
4266f52024Sotto.Nd arbitrary-precision arithmetic language and calculator
4366f52024Sotto.Sh SYNOPSIS
445db927c3Sjmc.Nm bc
455db927c3Sjmc.Op Fl cl
46b1fdaab8Sotto.Op Fl e Ar expression
4787fcf138Sotto.Op Ar file ...
4866f52024Sotto.Sh DESCRIPTION
495db927c3Sjmc.Nm
5066f52024Sottois an interactive processor for a language which resembles
5166f52024SottoC but provides unlimited precision arithmetic.
52b1fdaab8SottoIt takes input from any expressions on the command line and
53b1fdaab8Sottoany files given, then reads the standard input.
5466f52024Sotto.Pp
5566f52024SottoOptions available:
565db927c3Sjmc.Bl -tag -width Ds
5766f52024Sotto.It Fl c
585db927c3Sjmc.Nm
5966f52024Sottois actually a preprocessor for
605db927c3Sjmc.Xr dc 1 ,
6166f52024Sottowhich it invokes automatically, unless the
6266f52024Sotto.Fl c
635db927c3Sjmc.Pq compile only
6466f52024Sottooption is present.
655db927c3SjmcIn this case the generated
665db927c3Sjmc.Xr dc 1
675db927c3Sjmcinstructions are sent to the standard output,
685db927c3Sjmcinstead of being interpreted by a running
695db927c3Sjmc.Xr dc 1
705db927c3Sjmcprocess.
71b1fdaab8Sotto.It Fl e Ar expression
72b1fdaab8SottoEvaluate
73b1fdaab8Sotto.Ar expression .
74b1fdaab8SottoIf multiple
75b1fdaab8Sotto.Fl e
76b1fdaab8Sottooptions are specified, they are processed in the order given,
77b1fdaab8Sottoseparated by newlines.
785db927c3Sjmc.It Fl l
79dde315d0SjmcInclude an arbitrary precision math library.
80dde315d0SjmcThe definitions in the library are available to command line expressions
81dde315d0Sjmcand are documented below.
8266f52024Sotto.El
8366f52024Sotto.Pp
8466f52024SottoThe syntax for
855db927c3Sjmc.Nm
865db927c3Sjmcprograms is as follows:
875db927c3Sjmc.Sq L
885db927c3Sjmcmeans letter a-z;
895db927c3Sjmc.Sq E
905db927c3Sjmcmeans expression;
915db927c3Sjmc.Sq S
925db927c3Sjmcmeans statement.
93701f09e9SottoAs a non-portable extension, it is possible to use long names
94701f09e9Sottoin addition to single letter names.
95701f09e9SottoA long name is a sequence starting with a lowercase letter
96701f09e9Sottofollowed by any number of lowercase letters and digits.
97701f09e9SottoThe underscore character
98701f09e9Sotto.Pq Sq _
99701f09e9Sottocounts as a letter.
10066f52024Sotto.Pp
10166f52024SottoComments
10266f52024Sotto.Bd -unfilled -offset indent -compact
1035db927c3Sjmcare enclosed in /* and */
104054414f2Sottoare enclosed in # and the next newline
10566f52024Sotto.Ed
10666f52024Sotto.Pp
107054414f2SottoThe newline is not part of the line comment,
108054414f2Sottowhich in itself is a non-portable extension.
109054414f2Sotto.Pp
11066f52024SottoNames
11166f52024Sotto.Bd -unfilled -offset indent -compact
11266f52024Sottosimple variables: L
11366f52024Sottoarray elements: L [ E ]
11466f52024SottoThe words `ibase', `obase', and `scale'
115e449161cSottoThe word `last' or a single dot
11666f52024Sotto.Ed
11766f52024Sotto.Pp
11866f52024SottoOther operands
11966f52024Sotto.Bd -unfilled -offset indent -compact
1205db927c3Sjmcarbitrarily long numbers with optional sign and decimal point
1215db927c3Sjmc( E )
12266f52024Sottosqrt ( E )
12366f52024Sottolength ( E )	number of significant decimal digits
12466f52024Sottoscale ( E )	number of digits right of decimal point
12566f52024SottoL ( E , ... , E )
12666f52024Sotto.Ed
12766f52024Sotto.Pp
128709db6e1SottoThe sequence
129709db6e1Sotto.Sq \e<newline><whitespace>
130709db6e1Sottois ignored within numbers.
131709db6e1Sotto.Pp
13266f52024SottoOperators
13323fb519eSotto.Pp
13423fb519eSottoThe following arithmetic and logical operators can be used.
13523fb519eSottoThe semantics of the operators is the same as in the C language.
13623fb519eSottoThey are listed in order of decreasing precedence.
13723fb519eSottoOperators in the same group have the same precedence.
138d6f92ecdSjmc.Bl -column "= += \-= *= /= %= ^=" "Associativity" "multiply, divide, modulus" -offset indent
13923fb519eSotto.It Sy "Operator" Ta Sy "Associativity" Ta Sy "Description"
14023fb519eSotto.It "++ \-\-" Ta "none" Ta "increment, decrement"
14123fb519eSotto.It "\-" Ta "none" Ta "unary minus"
14223fb519eSotto.It "^" Ta "right" Ta "power"
14323fb519eSotto.It "* / %" Ta "left" Ta "multiply, divide, modulus"
14423fb519eSotto.It "+ \-" Ta "left" Ta "plus, minus"
14523fb519eSotto.It "= += -= *= /= %= ^=" Ta "right" Ta "assignment"
14623fb519eSotto.It "== <= >= != < >" Ta "none" Ta "relational"
14723fb519eSotto.It "!" Ta "none" Ta "boolean not"
14823fb519eSotto.It "&&" Ta "left" Ta "boolean and"
14923fb519eSotto.It "||" Ta "left" Ta "boolean or"
15023fb519eSotto.El
15123fb519eSotto.Pp
15223fb519eSottoNote the following:
15323fb519eSotto.Bl -bullet -offset indent
15423fb519eSotto.It
15523fb519eSottoThe relational operators may appear in any expression.
15623fb519eSottoThe
1576090c863Sjmc.St -p1003.1-2008
15823fb519eSottostandard only allows them in the conditional expression of an
15923fb519eSotto.Sq if ,
16023fb519eSotto.Sq while
16123fb519eSottoor
16223fb519eSotto.Sq for
16323fb519eSottostatement.
16423fb519eSotto.It
16523fb519eSottoThe relational operators have a lower precedence than the assignment
16623fb519eSottooperators.
16723fb519eSottoThis has the consequence that the expression
16823fb519eSotto.Sy a = b < c
16923fb519eSottois interpreted as
17023fb519eSotto.Sy (a = b) < c ,
17123fb519eSottowhich is probably not what the programmer intended.
17223fb519eSotto.It
17323fb519eSottoIn contrast with the C language, the relational operators all have
17423fb519eSottothe same precedence, and are non-associative.
17523fb519eSottoThe expression
17623fb519eSotto.Sy a < b < c
17723fb519eSottowill produce a syntax error.
17823fb519eSotto.It
17923fb519eSottoThe boolean operators (!, && and ||) are non-portable extensions.
18023fb519eSotto.It
18123fb519eSottoThe boolean not
18223fb519eSotto(!) operator has much lower precedence than the same operator in the
18323fb519eSottoC language.
18400446a6cSjmcThis has the consequence that the expression
18523fb519eSotto.Sy !a < b
18600446a6cSjmcis interpreted as
18723fb519eSotto.Sy !(a < b) .
18823fb519eSottoPrudent programmers use parentheses when writing expressions involving
18923fb519eSottoboolean operators.
19023fb519eSotto.El
19166f52024Sotto.Pp
19266f52024SottoStatements
19366f52024Sotto.Bd -unfilled -offset indent -compact
19466f52024SottoE
19566f52024Sotto{ S ; ... ; S }
19666f52024Sottoif ( E ) S
1973c8226c9Sottoif ( E ) S else S
19866f52024Sottowhile ( E ) S
19966f52024Sottofor ( E ; E ; E ) S
20066f52024Sottonull statement
20166f52024Sottobreak
2026add5204Sottocontinue
20366f52024Sottoquit
2046add5204Sottoa string of characters, enclosed in double quotes
205709db6e1Sottoprint E ,..., E
20666f52024Sotto.Ed
20766f52024Sotto.Pp
208709db6e1SottoA string may contain any character, except double quote.
2093c8226c9SottoThe if statement with an else branch is a non-portable extension.
210de09b448SjmcAll three E's in a for statement may be empty.
211464c70f5SottoThis is a non-portable extension.
212709db6e1SottoThe continue and print statements are also non-portable extensions.
213709db6e1Sotto.Pp
214709db6e1SottoThe print statement takes a list of comma-separated expressions.
215709db6e1SottoEach expression in the list is evaluated and the computed
216709db6e1Sottovalue is printed and assigned to the variable `last'.
217709db6e1SottoNo trailing newline is printed.
218709db6e1SottoThe expression may also be a string enclosed in double quotes.
219709db6e1SottoWithin these strings the following escape sequences may be used:
220709db6e1Sotto.Sq \ea
221709db6e1Sottofor bell (alert),
222709db6e1Sotto.Sq \eb
223709db6e1Sottofor backspace,
224709db6e1Sotto.Sq \ef
225709db6e1Sottofor formfeed,
226709db6e1Sotto.Sq \en
227709db6e1Sottofor newline,
228709db6e1Sotto.Sq \er
229709db6e1Sottofor carriage return,
230709db6e1Sotto.Sq \et
231709db6e1Sottofor tab,
232709db6e1Sotto.Sq \eq
233709db6e1Sottofor double quote and
234709db6e1Sotto.Sq \e\e
235709db6e1Sottofor backslash.
236709db6e1SottoAny other character following a backslash will be ignored.
237709db6e1SottoStrings will not be assigned to `last'.
238464c70f5Sotto.Pp
23966f52024SottoFunction definitions
240d9a29923Sjmc.Bd -unfilled -offset indent
24166f52024Sottodefine L ( L ,..., L ) {
24266f52024Sotto	auto L, ... , L
24366f52024Sotto	S; ... S
24466f52024Sotto	return ( E )
24566f52024Sotto}
24666f52024Sotto.Ed
24766f52024Sotto.Pp
248054414f2SottoAs a non-portable extension, the opening brace of the define statement
249054414f2Sottomay appear on the next line.
250054414f2SottoThe return statement may also appear in the following forms:
251054414f2Sotto.Bd -unfilled -offset indent
252054414f2Sottoreturn
253054414f2Sottoreturn ()
254054414f2Sottoreturn E
255054414f2Sotto.Ed
256054414f2Sotto.Pp
257054414f2SottoThe first two are equivalent to the statement
258054414f2Sotto.Dq return 0 .
259054414f2SottoThe last form is a non-portable extension.
260054414f2SottoNot specifying a return statement is equivalent to writing
261054414f2Sotto.Dq return (0) .
262054414f2Sotto.Pp
263709db6e1SottoFunctions available in the math library, which is loaded by specifying the
264709db6e1Sotto.Fl l
265dde315d0Sjmcflag on the command line:
26687fcf138Sotto.Pp
26766f52024Sotto.Bl -tag -width j(n,x) -offset indent -compact
26866f52024Sotto.It s(x)
26966f52024Sottosine
27066f52024Sotto.It c(x)
27166f52024Sottocosine
27266f52024Sotto.It e(x)
27366f52024Sottoexponential
27466f52024Sotto.It l(x)
27566f52024Sottolog
27666f52024Sotto.It a(x)
27766f52024Sottoarctangent
27866f52024Sotto.It j(n,x)
27966f52024SottoBessel function
28066f52024Sotto.El
28166f52024Sotto.Pp
28266f52024SottoAll function arguments are passed by value.
28366f52024Sotto.Pp
28466f52024SottoThe value of a statement that is an expression is printed
28566f52024Sottounless the main operator is an assignment.
286e449161cSottoThe value printed is assigned to the special variable `last'.
287e449161cSottoThis is a non-portable extension.
288e449161cSottoA single dot may be used as a synonym for `last'.
28966f52024SottoEither semicolons or newlines may separate statements.
29066f52024SottoAssignment to
29166f52024Sotto.Ar scale
29266f52024Sottoinfluences the number of digits to be retained on arithmetic
29366f52024Sottooperations in the manner of
2945db927c3Sjmc.Xr dc 1 .
29566f52024SottoAssignments to
29666f52024Sotto.Ar ibase
29766f52024Sottoor
29866f52024Sotto.Ar obase
29966f52024Sottoset the input and output number radix respectively.
30066f52024Sotto.Pp
30166f52024SottoThe same letter may be used as an array, a function,
30266f52024Sottoand a simple variable simultaneously.
30366f52024SottoAll variables are global to the program.
30466f52024Sotto`Auto' variables are pushed down during function calls.
30566f52024SottoWhen using arrays as function arguments
3065db927c3Sjmcor defining them as automatic variables,
30766f52024Sottoempty square brackets must follow the array name.
30866f52024Sotto.Pp
30966f52024SottoFor example
31066f52024Sotto.Bd -literal -offset indent
31166f52024Sottoscale = 20
31266f52024Sottodefine e(x){
31366f52024Sotto	auto a, b, c, i, s
31466f52024Sotto	a = 1
31566f52024Sotto	b = 1
31666f52024Sotto	s = 1
31766f52024Sotto	for(i=1; 1==1; i++){
31866f52024Sotto		a = a*x
31966f52024Sotto		b = b*i
32066f52024Sotto		c = a/b
32166f52024Sotto		if(c == 0) return(s)
32266f52024Sotto		s = s+c
32366f52024Sotto	}
32466f52024Sotto}
32566f52024Sotto.Ed
32666f52024Sotto.Pp
32766f52024Sottodefines a function to compute an approximate value of
32866f52024Sottothe exponential function and
32966f52024Sotto.Pp
33066f52024Sotto.Dl for(i=1; i<=10; i++) e(i)
33166f52024Sotto.Pp
33266f52024Sottoprints approximate values of the exponential function of
33366f52024Sottothe first ten integers.
334b1fdaab8Sotto.Bd -literal -offset indent
3355d974d9eSotto$ bc -l -e 'scale = 500; 4 * a(1)' -e quit
336b1fdaab8Sotto.Ed
337b1fdaab8Sotto.Pp
338b1fdaab8Sottoprints an approximation of pi.
339ce0441ffSotto.Sh COMMAND LINE EDITING
340ce0441ffSotto.Nm
341ce0441ffSottosupports interactive command line editing, via the
342ce0441ffSotto.Xr editline 3
343ce0441ffSottolibrary.
344ce0441ffSottoIt is enabled by default if input is from a tty.
345ce0441ffSottoPrevious lines can be recalled and edited with the arrow keys,
346ce0441ffSottoand other GNU Emacs-style editing keys may be used as well.
347ce0441ffSotto.Pp
348ce0441ffSottoThe
349ce0441ffSotto.Xr editline 3
350ce0441ffSottolibrary is configured with a
351ce0441ffSotto.Pa .editrc
352ce0441ffSottofile \- refer to
353ce0441ffSotto.Xr editrc 5
354ce0441ffSottofor more information.
355709db6e1Sotto.Sh FILES
356709db6e1Sotto.Bl -tag -width /usr/share/misc/bc.library -compact
357709db6e1Sotto.It Pa /usr/share/misc/bc.library
358709db6e1Sottomath library, read when the
359709db6e1Sotto.Fl l
360709db6e1Sottooption is specified on the command line.
361709db6e1Sotto.El
36266f52024Sotto.Sh SEE ALSO
3635db927c3Sjmc.Xr dc 1
364093fc5b9Sjsg.Rs
365093fc5b9Sjsg.\" 4.4BSD USD:6
366093fc5b9Sjsg.%A L. L. Cherry
367093fc5b9Sjsg.%A R. H. Morris
368*8e776806Sjmc.%T "BC \(em An Arbitrary Precision Desk-Calculator Language"
369093fc5b9Sjsg.Re
3704bfd9043Sotto.Sh STANDARDS
3714bfd9043SottoThe
3724bfd9043Sotto.Nm
3737c5a075bSjmcutility is compliant with the
37499332e38Sjmc.St -p1003.1-2008
3754bfd9043Sottospecification.
3767c5a075bSjmc.Pp
3777c5a075bSjmcThe flags
3786090c863Sjmc.Op Fl ce ,
3796090c863Sjmcas well as the parts noted above,
3807c5a075bSjmcare extensions to that specification.
38166f52024Sotto.Sh HISTORY
38266f52024SottoThe
3835db927c3Sjmc.Nm
384ceac9e61Sjmccommand first appeared in
38566f52024Sotto.At v6 .
386709db6e1SottoA complete rewrite of the
387709db6e1Sotto.Nm
388709db6e1Sottocommand first appeared in
389709db6e1Sotto.Ox 3.5 .
390709db6e1Sotto.Sh AUTHORS
391d281945cSjaredy.An -nosplit
392709db6e1SottoThe original version of the
393709db6e1Sotto.Nm
394709db6e1Sottocommand was written by
395709db6e1Sotto.An Robert Morris
396709db6e1Sottoand
397709db6e1Sotto.An Lorinda Cherry .
398709db6e1SottoThe current version of the
399709db6e1Sotto.Nm
400709db6e1Sottoutility was written by
401709db6e1Sotto.An Otto Moerbeek .
40266f52024Sotto.Sh BUGS
403636cc890SmmccThe
4046e8f072fSmmcc.Ql quit
405636cc890Smmccstatement is interpreted when read, not when executed.
4064bfd9043Sotto.Pp
4074bfd9043SottoSome non-portable extensions, as found in the GNU version of the
4084bfd9043Sotto.Nm
4094bfd9043Sottoutility are not implemented (yet).
410