summaryrefslogtreecommitdiff
path: root/usr.bin/bc/bc.1
blob: ee20078f1f3579f10eab2027c759bb33cdb3718d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
.\"	$OpenBSD: bc.1,v 1.8 2003/10/20 09:27:48 jmc Exp $
.\"
.\" Copyright (C) Caldera International Inc.  2001-2002.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code and documentation must retain the above
.\"    copyright notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\"    must display the following acknowledgement:
.\"	This product includes software developed or owned by Caldera
.\"	International, Inc.
.\" 4. Neither the name of Caldera International, Inc. nor the names of other
.\"    contributors may be used to endorse or promote products derived from
.\"    this software without specific prior written permission.
.\"
.\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
.\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
.\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.\"	@(#)bc.1	6.8 (Berkeley) 8/8/91
.\"
.Dd August 8, 1991
.Dt BC 1
.Sh NAME
.Nm bc
.Nd arbitrary-precision arithmetic language and calculator
.Sh SYNOPSIS
.Nm bc
.Op Fl cl
.Op Ar file ...
.Sh DESCRIPTION
.Nm
is an interactive processor for a language which resembles
C but provides unlimited precision arithmetic.
It takes input from any files given, then reads
the standard input.
.Pp
Options available:
.Bl -tag -width Ds
.It Fl c
.Nm
is actually a preprocessor for
.Xr dc 1 ,
which it invokes automatically, unless the
.Fl c
.Pq compile only
option is present.
In this case the generated
.Xr dc 1
instructions are sent to the standard output,
instead of being interpreted by a running
.Xr dc 1
process.
.It Fl l
Allow specification
of an arbitrary precision math library.
.El
.Pp
The syntax for
.Nm
programs is as follows:
.Sq L
means letter a-z;
.Sq E
means expression;
.Sq S
means statement.
.Pp
Comments
.Bd -unfilled -offset indent -compact
are enclosed in /* and */
.Ed
.Pp
Names
.Bd -unfilled -offset indent -compact
simple variables: L
array elements: L [ E ]
The words `ibase', `obase', and `scale'
.Ed
.Pp
Other operands
.Bd -unfilled -offset indent -compact
arbitrarily long numbers with optional sign and decimal point
( E )
sqrt ( E )
length ( E )	number of significant decimal digits
scale ( E )	number of digits right of decimal point
L ( E , ... , E )
.Ed
.Pp
Operators
.Bd -unfilled -offset indent -compact
+  \-  *  /  %  ^ (`%' is remainder; `^' is power)
++   \-\-         (prefix and postfix; apply to names)
==  <=  >=  !=  <  >
=  +=  \-=  *=  /=  %=  ^=
.Ed
.Pp
Statements
.Bd -unfilled -offset indent -compact
E
{ S ; ... ; S }
if ( E ) S
while ( E ) S
for ( E ; E ; E ) S
null statement
break
continue
quit
a string of characters, enclosed in double quotes
.Ed
.Pp
All three E's in a for statement may be empty.
This is a non-portable extension.
The continue statement is also a non-portable extension.
.Pp
Function definitions
.Bd -unfilled -offset indent -compact
define L ( L ,..., L ) {
	auto L, ... , L
	S; ... S
	return ( E )
}
.Ed
.Pp
The sequence
.Sq \e\<newline><whitespace>
is ignored within numbers.
A string may contain any character, except double quote.
.Pp
Functions in
.Fl lm
math library
.Bl -tag -width j(n,x) -offset indent -compact
.It s(x)
sine
.It c(x)
cosine
.It e(x)
exponential
.It l(x)
log
.It a(x)
arctangent
.It j(n,x)
Bessel function
.El
.Pp
All function arguments are passed by value.
.Pp
The value of a statement that is an expression is printed
unless the main operator is an assignment.
Either semicolons or newlines may separate statements.
Assignment to
.Ar scale
influences the number of digits to be retained on arithmetic
operations in the manner of
.Xr dc 1 .
Assignments to
.Ar ibase
or
.Ar obase
set the input and output number radix respectively.
.Pp
The same letter may be used as an array, a function,
and a simple variable simultaneously.
All variables are global to the program.
`Auto' variables are pushed down during function calls.
When using arrays as function arguments
or defining them as automatic variables,
empty square brackets must follow the array name.
.Pp
For example
.Bd -literal -offset indent
scale = 20
define e(x){
	auto a, b, c, i, s
	a = 1
	b = 1
	s = 1
	for(i=1; 1==1; i++){
		a = a*x
		b = b*i
		c = a/b
		if(c == 0) return(s)
		s = s+c
	}
}
.Ed
.Pp
defines a function to compute an approximate value of
the exponential function and
.Pp
.Dl for(i=1; i<=10; i++) e(i)
.Pp
prints approximate values of the exponential function of
the first ten integers.
.Sh SEE ALSO
.Xr dc 1
.Rs
.%A L. L. Cherry
.%A R. Morris
.%T "BC \- An arbitrary precision desk-calculator language"
.Re
.Sh STANDARDS
The
.Nm
utility is expected to conform to the
.St -p1003.2
specification.
.Sh HISTORY
The
.Nm
command appeared in
.At v6 .
.Sh BUGS
No
.Sq && ,
.Sq \(or\(or ,
or
.Sq \&!
operators.
.Pp
.Ql Quit
is interpreted when read, not when executed.
.Pp
Some non-portable extensions, as found in the GNU version of the
.Nm
utility are not implemented (yet).