aboutsummaryrefslogtreecommitdiff
path: root/man/man1/bc.1
blob: 497481301b8872d1fce02dcf0e35329800070a31 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
.TH BC 1 
.SH NAME
bc \- arbitrary-precision arithmetic language
.SH SYNOPSIS
.B bc
[
.B -c
]
[
.B -l
]
[
.B -s
]
[
.I file ...
]
.SH DESCRIPTION
.I Bc
is an interactive processor for a language that resembles
C but provides arithmetic on numbers of arbitrary length with up
to 100 digits right of the decimal point.
It takes input from any files given, then reads
the standard input.
The
.B -l
argument stands for the name
of an arbitrary precision math library.
The
.B -s
argument suppresses the automatic display
of calculation results; all output is via the
.B print
command.
.PP
The following syntax for 
.I bc
programs is like that of C;
.I L
means letter
.BR a - z ,
.I E
means expression,
.I S
means statement.
.TF length(E)
.TP
Lexical
.RS
.HP
comments are enclosed in
.B /* */
.HP
newlines end statements
.RE
.TP
Names
.IP
simple variables:
.I L
.br
array elements:
.IB L [ E ]
.br
The words
.BR ibase ,
.BR obase ,
and
.B scale
.TP
Other operands
.IP
arbitrarily long numbers with optional sign and decimal point.
.RS
.TP
.BI ( E )
.TP
.BI sqrt( E )
.TP
.BI length( E )
number of significant decimal digits
.TP
.BI scale( E )
number of digits right of decimal point
.TP
.IB L ( E , ... ,\fIE\fP)
function call
.RE
.TP
Operators
.RS
.HP
.B "+  -  *  /  %  ^\ "
.RB ( %
is remainder;
.B ^
is power)
.HP
.B "++  --\ "
.TP
.B "==  <=  >=  !=  <  >"
.TP
.B "=  +=  -=  *=  /=  %=  ^="
.RE
.TP
Statements
.RS
.br
.I E
.br
.B {
.I S
.B ;
\&...
.B ;
.I S
.B }
.br
.B "print"
.I E
.br
.B "if ("
.I E
.B )
.I S
.br
.B "while ("
.I E
.B )
.I S
.br
.B "for ("
.I E
.B ;
.I E
.B ;
.I E
.B ")"
.I  S
.br
null statement
.br
.B break
.br
.B quit
.br
\fL"\fRtext\fL"\fR
.RE
.TP
Function definitions
.RS
.br
.B define
.I L
.B (
.I L
.B ,
\&...
.B ,
.I L
.B ){
.PD0
.br
.B auto
.I L
.B ,
\&...
.B ,
.I L
.br
.I S
.B ;
\&...
.B ;
.I  S
.br
.B return
.I E
.LP
.B }
.RE
.TP
Functions in 
.B -l
math library
.RS
.TP
.BI s( x )
sine
.TP
.BI c( x )
cosine
.TP
.BI e( x )
exponential
.TP
.BI l( x )
log
.TP
.BI a( x )
arctangent
.TP
.BI j( "n, x" )
Bessel function
.RE
.PP
.DT
All function arguments are passed by value.
.PD
.PP
The value of an expression at the top level is printed
unless the main operator is an assignment or the
.B -s
command line argument is given.
Text in quotes, which may include newlines, is always printed.
Either semicolons or newlines may separate statements.
Assignment to
.B scale
influences the number of digits to be retained on arithmetic
operations in the manner of
.MR dc (1) .
Assignments to
.B ibase
or
.B 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.
Automatic variables are pushed down during function calls.
In a declaration of an array as a function argument
or automatic variable
empty square brackets must follow the array name.
.PP
.I Bc
is actually a preprocessor for
.MR dc (1) ,
which it invokes automatically, unless the
.B -c
(compile only)
option is present.
In this case the
.I dc
input is sent to the standard output instead.
.SH EXAMPLE
Define a function to compute an approximate value of
the exponential.
Use it to print 10 values.
(The exponential function in the library gives better answers.)
.PP
.EX
scale = 20
define e(x) {
	auto a, b, c, i, s
	a = 1
	b = 1
	s = 1
	for(i=1; 1; i++) {
		a *= x
		b *= i
		c = a/b
		if(c == 0) return s
		s += c
	}
}
for(i=1; i<=10; i++) print e(i)
.EE
.SH FILES
.B \*9/lib/bclib
mathematical library
.SH SOURCE
.B \*9/src/cmd/bc.y
.SH "SEE ALSO"
.IR dc (1), 
.MR hoc (1)
.SH BUGS
No
.LR && ,
.LR || ,
or
.L !
operators.
.PP
A
.L for
statement must have all three
.LR E s.
.PP
A
.L quit
is interpreted when read, not when executed.