1 .\"
2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
3 .\" permission to reproduce portions of its copyrighted documentation.
4 .\" Original documentation from The Open Group can be obtained online at
5 .\" http://www.opengroup.org/bookstore/.
6 .\"
7 .\" The Institute of Electrical and Electronics Engineers and The Open
8 .\" Group, have given us permission to reprint portions of their
9 .\" documentation.
10 .\"
11 .\" In the following statement, the phrase ``this text'' refers to portions
12 .\" of the system documentation.
13 .\"
14 .\" Portions of this text are reprinted and reproduced in electronic form
15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
16 .\" Standard for Information Technology -- Portable Operating System
17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6,
18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy
20 .\" between these versions and the original IEEE and The Open Group
21 .\" Standard, the original IEEE and The Open Group Standard is the referee
22 .\" document. The original Standard can be obtained online at
23 .\" http://www.opengroup.org/unix/online.html.
24 .\"
25 .\" This notice shall appear on any product containing this material.
26 .\"
27 .\" The contents of this file are subject to the terms of the
28 .\" Common Development and Distribution License (the "License").
29 .\" You may not use this file except in compliance with the License.
30 .\"
31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
32 .\" or http://www.opensolaris.org/os/licensing.
33 .\" See the License for the specific language governing permissions
34 .\" and limitations under the License.
35 .\"
36 .\" When distributing Covered Code, include this CDDL HEADER in each
37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
38 .\" If applicable, add the following below this CDDL HEADER, with the
39 .\" fields enclosed by brackets "[]" replaced with your own identifying
40 .\" information: Portions Copyright [yyyy] [name of copyright owner]
41 .\"
42 .\"
43 .\" Copyright 1989 AT&T
44 .\" Portions Copyright (c) 1992, X/Open Company Limited. All Rights Reserved.
45 .\" Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved.
46 .\"
47 .TH BC 1 "Aug 29, 2003"
48 .SH NAME
49 bc \- arbitrary precision arithmetic language
50 .SH SYNOPSIS
51 .LP
52 .nf
53 \fB/usr/bin/bc\fR [\fB-c\fR] [\fB-l\fR] [\fIfile\fR]...
54 .fi
55
56 .LP
57 .nf
58 \fB/usr/xpg6/bin/bc\fR [\fB-c\fR] [\fB-l\fR] [\fIfile\fR]...
59 .fi
60
61 .SH DESCRIPTION
62 .sp
63 .LP
64 The \fBbc\fR utility implements an arbitrary precision calculator. It takes
65 input from any files given, then reads from the standard input. If the standard
66 input and standard output to \fBbc\fR are attached to a terminal, the
67 invocation of \fBbc\fR is \fIinteractive\fR, causing behavioral constraints
68 described in the following sections. \fBbc\fR processes a language that
69 resembles C and is a preprocessor for the desk calculator program \fBdc\fR,
70 which it invokes automatically unless the \fB-c\fR option is specified. In this
71 case the \fBdc\fR input is sent to the standard output instead.
72 .SH USAGE
73 .sp
74 .LP
75 The syntax for \fBbc\fR programs is as follows:
76 .sp
77 .ne 2
78 .na
79 \fB\fIL\fR\fR
80 .ad
81 .RS 5n
82 Means a letter \fBa\fR\(mi\fBz\fR,
83 .RE
84
85 .sp
86 .ne 2
87 .na
88 \fB\fIE\fR\fR
89 .ad
90 .RS 5n
91 Means an expression: a (mathematical or logical) value, an operand that takes
92 a value, or a combination of operands and operators that evaluates to a value,
93 .RE
94
95 .sp
96 .ne 2
97 .na
98 \fB\fIS\fR\fR
99 .ad
100 .RS 5n
101 Means a statement.
102 .RE
103
104 .SS "Comments"
105 .sp
106 .LP
107 Enclosed in \fB/*\fR and \fB*/\fR.
108 .SS "Names (Operands)"
109 .br
110 .in +2
111 Simple variables: \fIL\fR.
112 .in -2
113 .br
114 .in +2
115 Array elements: \fIL\fR [ \fIE\fR ] (up to \fBBC_DIM_MAX\fR dimensions).
116 .in -2
117 .br
118 .in +2
119 The words \fBibase\fR, \fBobase\fR (limited to \fBBC_BASE_MAX\fR), and
120 \fBscale\fR (limited to \fBBC_SCALE_MAX\fR).
121 .in -2
122 .SS "Other Operands"
123 .sp
124 .LP
125 Arbitrarily long numbers with optional sign and decimal point. Strings of fewer
126 than \fBBC_STRING_MAX\fR characters, between double quotes ("). \fB(\fR \fIE\fR
127 \fB)\fR
128 .sp
129 .ne 2
130 .na
131 \fB\fBsqrt ( \fR\fIE\fR\fB )\fR\fR
132 .ad
133 .RS 21n
134 Square root
135 .RE
136
137 .sp
138 .ne 2
139 .na
140 \fB\fBlength ( \fR\fIE\fR\fB )\fR\fR
141 .ad
142 .RS 21n
143 Number of significant decimal digits.
144 .RE
145
146 .sp
147 .ne 2
148 .na
149 \fB\fBscale ( \fR\fIE\fR\fB )\fR\fR
150 .ad
151 .RS 21n
152 Number of digits right of decimal point.
153 .RE
154
155 .sp
156 .ne 2
157 .na
158 \fB\fIL \fR\fB( \fR\fIE\fR , ... , \fIE\fR\fB )\fR\fR
159 .ad
160 .RS 21n
161
162 .RE
163
164 .SS "Operators"
165 .sp
166 .ne 2
167 .na
168 \fB\fB+ \(mi * / % ^\fR\fR
169 .ad
170 .sp .6
171 .RS 4n
172 (\fB%\fR is remainder; \fB^\fR is power)
173 .RE
174
175 .sp
176 .ne 2
177 .na
178 \fB\fB++ \(mi\(mi \fR\fR
179 .ad
180 .sp .6
181 .RS 4n
182 (prefix and postfix; apply to names)
183 .RE
184
185 .sp
186 .ne 2
187 .na
188 \fB\fB== <= >= != < >\fR\fR
189 .ad
190 .sp .6
191 .RS 4n
192
193 .RE
194
195 .sp
196 .ne 2
197 .na
198 \fB\fB= =+ =\(mi =* =/ =% =^\fR\fR
199 .ad
200 .sp .6
201 .RS 4n
202
203 .RE
204
205 .SS "Statements"
206 .br
207 .in +2
208 \fIE\fR
209 .in -2
210 .br
211 .in +2
212 \fB{\fR \fIS\fR \fB;\fR.\|.\|. \fB;\fR \fIS\fR \fB}\fR
213 .in -2
214 .br
215 .in +2
216 \fBif (\fR \fIE\fR \fB)\fR \fIS\fR
217 .in -2
218 .br
219 .in +2
220 \fBwhile (\fR \fIE\fR \fB)\fR \fIS\fR
221 .in -2
222 .br
223 .in +2
224 \fBfor (\fR \fIE\fR \fB;\fR \fIE\fR \fB;\fR \fIE\fR \fB)\fR \fIS\fR
225 .in -2
226 .br
227 .in +2
228 null statement
229 .in -2
230 .br
231 .in +2
232 \fBbreak\fR
233 .in -2
234 .br
235 .in +2
236 \fBquit\fR
237 .in -2
238 .sp
239 .LP
240 \&.string
241 .SS "Function Definitions"
242 .br
243 .in +2
244 \fBdefine\fR \fIL\fR \fB(\fR \fIL\fR \fB,\fR.\|.\|.\|\fB,\fR \fIL\fR \fB) {\fR
245 .in -2
246 .br
247 .in +2
248 \fBauto\fR \fIL\fR \fB,\fR.\|.\|.\|\fB,\fR \fIL\fR
249 .in -2
250 .br
251 .in +2
252 \fIS\fR \fB;\fR.\|.\|.\| \fIS\fR
253 .in -2
254 .br
255 .in +2
256 \fBreturn (\fR \fIE\fR \fB)\fR
257 .in -2
258 .br
259 .in +2
260 \fB}\fR
261 .in -2
262 .SS "Functions in \fB-l\fR Math Library"
263 .sp
264 .ne 2
265 .na
266 \fB\fBs(\fR\fIx\fR\fB)\fR\fR
267 .ad
268 .RS 10n
269 sine
270 .RE
271
272 .sp
273 .ne 2
274 .na
275 \fB\fBc(\fR\fIx\fR\fB)\fR\fR
276 .ad
277 .RS 10n
278 cosine
279 .RE
280
281 .sp
282 .ne 2
283 .na
284 \fB\fBe(\fR\fIx\fR\fB)\fR\fR
285 .ad
286 .RS 10n
287 exponential
288 .RE
289
290 .sp
291 .ne 2
292 .na
293 \fB\fBl(\fR\fIx\fR\fB)\fR\fR
294 .ad
295 .RS 10n
296 log
297 .RE
298
299 .sp
300 .ne 2
301 .na
302 \fB\fBa(\fR\fIx\fR\fB)\fR\fR
303 .ad
304 .RS 10n
305 arctangent
306 .RE
307
308 .sp
309 .ne 2
310 .na
311 \fB\fBj(\fR\fIn\fR\fB,\fR\fIx)\fR\fR
312 .ad
313 .RS 10n
314 Bessel function
315 .RE
316
317 .sp
318 .LP
319 All function arguments are passed by value.
320 .sp
321 .LP
322 The value of a statement that is an expression is printed unless the main
323 operator is an assignment. Either semicolons or new-lines may separate
324 statements. Assignment to \fBscale\fR influences the number of digits to be
325 retained on arithmetic operations in the manner of \fBdc\fR. Assignments to
326 \fBibase\fR or \fBobase\fR set the input and output number radix respectively.
327 .sp
328 .LP
329 The same letter may be used as an array, a function, and a simple variable
330 simultaneously. All variables are global to the program. \fBauto\fR variables
331 are stacked during function calls. When using arrays as function arguments or
332 defining them as automatic variables, empty square brackets must follow the
333 array name.
334 .SH OPTIONS
335 .sp
336 .LP
337 The following operands are supported:
338 .sp
339 .ne 2
340 .na
341 \fB\fB-c\fR\fR
342 .ad
343 .RS 6n
344 Compiles only. The output is \fBdc\fR commands that are sent to the standard
345 output.
346 .RE
347
348 .SS "/usr/bin/bc"
349 .sp
350 .ne 2
351 .na
352 \fB\fB-l\fR\fR
353 .ad
354 .RS 6n
355 Defines the math functions and initializes \fBscale\fR to \fB20\fR, instead of
356 the default zero.
357 .RE
358
359 .SS "/usr/xpg6/bin/bc"
360 .sp
361 .ne 2
362 .na
363 \fB\fB-l\fR\fR
364 .ad
365 .RS 6n
366 Defines the math functions and initializes \fBscale\fR to \fB20\fR, instead of
367 the default zero. All math results have the scale of \fB20\fR.
368 .RE
369
370 .SH OPERANDS
371 .sp
372 .LP
373 The following operands are supported:
374 .sp
375 .ne 2
376 .na
377 \fB\fIfile\fR\fR
378 .ad
379 .RS 8n
380 A pathname of a text file containing \fBbc\fR program statements. After all
381 cases of \fIfile\fR have been read, \fBbc\fR reads the standard input.
382 .RE
383
384 .SH EXAMPLES
385 .LP
386 \fBExample 1 \fRSetting the precision of a variable
387 .sp
388 .LP
389 In the shell, the following assigns an approximation of the first ten digits of
390 \fBn\fR to the variable \fIx\fR:
391
392 .sp
393 .in +2
394 .nf
395 \fBx=$(printf "%s\en" 'scale = 10; 104348/33215' | bc)\fR
396 .fi
397 .in -2
398 .sp
399
400 .LP
401 \fBExample 2 \fRDefining a computing function
402 .sp
403 .LP
404 Defines a function to compute an approximate value of the exponential function:
405
406 .sp
407 .in +2
408 .nf
409 \fBscale = 20
410 define e(x){
411 auto a, b, c, i, s
412 a = 1
413 b = 1
414 s = 1
415 for(i=1; 1==1; i++){
416 a = a*x
417 b = b*i
418 c = a/b
419 if(c == 0) return(s)
420 s = s+c
421 }
422 }\fR
423 .fi
424 .in -2
425 .sp
426
427 .LP
428 \fBExample 3 \fRPrinting the approximate values of the function
429 .sp
430 .LP
431 Prints approximate values of the exponential function of the first ten
432 integers:
433
434 .sp
435 .in +2
436 .nf
437 \fBfor(i=1; i<=10; i++) e(i)\fR
438 .fi
439 .in -2
440 .sp
441
442 .sp
443 .LP
444 or
445
446 .sp
447 .in +2
448 .nf
449 \fBfor (i = 1; i <= 10; ++i) { e(i) }\fR
450 .fi
451 .in -2
452 .sp
453
454 .SH ENVIRONMENT VARIABLES
455 .sp
456 .LP
457 See \fBenviron\fR(5) for descriptions of the following environment variables
458 that affect the execution of \fBbc\fR: \fBLANG\fR, \fBLC_ALL\fR,
459 \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
460 .SH EXIT STATUS
461 .sp
462 .LP
463 The following exit values are returned:
464 .sp
465 .ne 2
466 .na
467 \fB\fB0\fR\fR
468 .ad
469 .RS 15n
470 All input files were processed successfully.
471 .RE
472
473 .sp
474 .ne 2
475 .na
476 \fB\fBunspecified\fR\fR
477 .ad
478 .RS 15n
479 An error occurred.
480 .RE
481
482 .SH FILES
483 .sp
484 .ne 2
485 .na
486 \fB\fB/usr/lib/lib.b\fR\fR
487 .ad
488 .RS 25n
489 mathematical library
490 .RE
491
492 .sp
493 .ne 2
494 .na
495 \fB\fB/usr/include/limits.h\fR\fR
496 .ad
497 .RS 25n
498 to define BC_ parameters
499 .RE
500
501 .SH ATTRIBUTES
502 .sp
503 .LP
504 See \fBattributes\fR(5) for descriptions of the following attributes:
505 .sp
506
507 .sp
508 .TS
509 box;
510 c | c
511 l | l .
512 ATTRIBUTE TYPE ATTRIBUTE VALUE
513 _
514 Interface Stability Standard
515 .TE
516
517 .SH SEE ALSO
518 .sp
519 .LP
520 \fBdc\fR(1), \fBawk\fR(1), \fBattributes\fR(5), \fBenviron\fR(5),
521 \fBstandards\fR(5)
522 .SH NOTES
523 .sp
524 .LP
525 The \fBbc\fR command does not recognize the logical operators \fB&&\fR and
526 \fB|\||\fR.
527 .sp
528 .LP
529 The \fBfor\fR statement must have all three expressions (\fIE\fR's).