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
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
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).
|
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 .nf
52 \fB/usr/bin/bc\fR [\fB-c\fR] [\fB-l\fR] [\fIfile\fR]...
53 .fi
54
55 .LP
56 .nf
57 \fB/usr/xpg6/bin/bc\fR [\fB-c\fR] [\fB-l\fR] [\fIfile\fR]...
58 .fi
59
60 .SH DESCRIPTION
61 The \fBbc\fR utility implements an arbitrary precision calculator. It takes
62 input from any files given, then reads from the standard input. If the standard
63 input and standard output to \fBbc\fR are attached to a terminal, the
64 invocation of \fBbc\fR is \fIinteractive\fR, causing behavioral constraints
65 described in the following sections. \fBbc\fR processes a language that
66 resembles C and is a preprocessor for the desk calculator program \fBdc\fR,
67 which it invokes automatically unless the \fB-c\fR option is specified. In this
68 case the \fBdc\fR input is sent to the standard output instead.
69 .SH USAGE
70 The syntax for \fBbc\fR programs is as follows:
71 .sp
72 .ne 2
73 .na
74 \fB\fIL\fR\fR
75 .ad
76 .RS 5n
77 Means a letter \fBa\fR\(mi\fBz\fR,
78 .RE
79
80 .sp
81 .ne 2
82 .na
83 \fB\fIE\fR\fR
84 .ad
85 .RS 5n
86 Means an expression: a (mathematical or logical) value, an operand that takes
87 a value, or a combination of operands and operators that evaluates to a value,
88 .RE
89
90 .sp
91 .ne 2
92 .na
93 \fB\fIS\fR\fR
94 .ad
95 .RS 5n
96 Means a statement.
97 .RE
98
99 .SS "Comments"
100 Enclosed in \fB/*\fR and \fB*/\fR.
101 .SS "Names (Operands)"
102 .in +2
103 Simple variables: \fIL\fR.
104 .in -2
105 .br
106 .in +2
107 Array elements: \fIL\fR [ \fIE\fR ] (up to \fBBC_DIM_MAX\fR dimensions).
108 .in -2
109 .br
110 .in +2
111 The words \fBibase\fR, \fBobase\fR (limited to \fBBC_BASE_MAX\fR), and
112 \fBscale\fR (limited to \fBBC_SCALE_MAX\fR).
113 .in -2
114 .SS "Other Operands"
115 Arbitrarily long numbers with optional sign and decimal point. Strings of fewer
116 than \fBBC_STRING_MAX\fR characters, between double quotes ("). \fB(\fR \fIE\fR
117 \fB)\fR
118 .sp
119 .ne 2
120 .na
121 \fB\fBsqrt ( \fR\fIE\fR\fB )\fR\fR
122 .ad
123 .RS 21n
124 Square root
125 .RE
126
127 .sp
128 .ne 2
129 .na
130 \fB\fBlength ( \fR\fIE\fR\fB )\fR\fR
131 .ad
132 .RS 21n
133 Number of significant decimal digits.
134 .RE
135
136 .sp
137 .ne 2
138 .na
139 \fB\fBscale ( \fR\fIE\fR\fB )\fR\fR
140 .ad
141 .RS 21n
142 Number of digits right of decimal point.
143 .RE
144
145 .sp
146 .ne 2
147 .na
148 \fB\fIL \fR\fB( \fR\fIE\fR , ... , \fIE\fR\fB )\fR\fR
149 .ad
150 .RS 21n
151
152 .RE
153
154 .SS "Operators"
155 .ne 2
156 .na
157 \fB\fB+ \(mi * / % ^\fR\fR
158 .ad
159 .sp .6
160 .RS 4n
161 (\fB%\fR is remainder; \fB^\fR is power)
162 .RE
163
164 .sp
165 .ne 2
166 .na
167 \fB\fB++ \(mi\(mi \fR\fR
168 .ad
169 .sp .6
170 .RS 4n
171 (prefix and postfix; apply to names)
172 .RE
173
174 .sp
175 .ne 2
176 .na
177 \fB\fB== <= >= != < >\fR\fR
178 .ad
179 .sp .6
180 .RS 4n
181
182 .RE
183
184 .sp
185 .ne 2
186 .na
187 \fB\fB= =+ =\(mi =* =/ =% =^\fR\fR
188 .ad
189 .sp .6
190 .RS 4n
191
192 .RE
193
194 .SS "Statements"
195 .in +2
196 \fIE\fR
197 .in -2
198 .br
199 .in +2
200 \fB{\fR \fIS\fR \fB;\fR.\|.\|. \fB;\fR \fIS\fR \fB}\fR
201 .in -2
202 .br
203 .in +2
204 \fBif (\fR \fIE\fR \fB)\fR \fIS\fR
205 .in -2
206 .br
207 .in +2
208 \fBwhile (\fR \fIE\fR \fB)\fR \fIS\fR
209 .in -2
210 .br
211 .in +2
212 \fBfor (\fR \fIE\fR \fB;\fR \fIE\fR \fB;\fR \fIE\fR \fB)\fR \fIS\fR
213 .in -2
214 .br
215 .in +2
216 null statement
217 .in -2
218 .br
219 .in +2
220 \fBbreak\fR
221 .in -2
222 .br
223 .in +2
224 \fBquit\fR
225 .in -2
226 .sp
227 .LP
228 \&.string
229 .SS "Function Definitions"
230 .in +2
231 \fBdefine\fR \fIL\fR \fB(\fR \fIL\fR \fB,\fR.\|.\|.\|\fB,\fR \fIL\fR \fB) {\fR
232 .in -2
233 .br
234 .in +2
235 \fBauto\fR \fIL\fR \fB,\fR.\|.\|.\|\fB,\fR \fIL\fR
236 .in -2
237 .br
238 .in +2
239 \fIS\fR \fB;\fR.\|.\|.\| \fIS\fR
240 .in -2
241 .br
242 .in +2
243 \fBreturn (\fR \fIE\fR \fB)\fR
244 .in -2
245 .br
246 .in +2
247 \fB}\fR
248 .in -2
249 .SS "Functions in \fB-l\fR Math Library"
250 .ne 2
251 .na
252 \fB\fBs(\fR\fIx\fR\fB)\fR\fR
253 .ad
254 .RS 10n
255 sine
256 .RE
257
258 .sp
259 .ne 2
260 .na
261 \fB\fBc(\fR\fIx\fR\fB)\fR\fR
262 .ad
263 .RS 10n
264 cosine
265 .RE
266
267 .sp
268 .ne 2
269 .na
301 .RE
302
303 .sp
304 .LP
305 All function arguments are passed by value.
306 .sp
307 .LP
308 The value of a statement that is an expression is printed unless the main
309 operator is an assignment. Either semicolons or new-lines may separate
310 statements. Assignment to \fBscale\fR influences the number of digits to be
311 retained on arithmetic operations in the manner of \fBdc\fR. Assignments to
312 \fBibase\fR or \fBobase\fR set the input and output number radix respectively.
313 .sp
314 .LP
315 The same letter may be used as an array, a function, and a simple variable
316 simultaneously. All variables are global to the program. \fBauto\fR variables
317 are stacked during function calls. When using arrays as function arguments or
318 defining them as automatic variables, empty square brackets must follow the
319 array name.
320 .SH OPTIONS
321 The following operands are supported:
322 .sp
323 .ne 2
324 .na
325 \fB\fB-c\fR\fR
326 .ad
327 .RS 6n
328 Compiles only. The output is \fBdc\fR commands that are sent to the standard
329 output.
330 .RE
331
332 .SS "/usr/bin/bc"
333 .ne 2
334 .na
335 \fB\fB-l\fR\fR
336 .ad
337 .RS 6n
338 Defines the math functions and initializes \fBscale\fR to \fB20\fR, instead of
339 the default zero.
340 .RE
341
342 .SS "/usr/xpg6/bin/bc"
343 .ne 2
344 .na
345 \fB\fB-l\fR\fR
346 .ad
347 .RS 6n
348 Defines the math functions and initializes \fBscale\fR to \fB20\fR, instead of
349 the default zero. All math results have the scale of \fB20\fR.
350 .RE
351
352 .SH OPERANDS
353 The following operands are supported:
354 .sp
355 .ne 2
356 .na
357 \fB\fIfile\fR\fR
358 .ad
359 .RS 8n
360 A pathname of a text file containing \fBbc\fR program statements. After all
361 cases of \fIfile\fR have been read, \fBbc\fR reads the standard input.
362 .RE
363
364 .SH EXAMPLES
365 \fBExample 1 \fRSetting the precision of a variable
366 .sp
367 .LP
368 In the shell, the following assigns an approximation of the first ten digits of
369 \fBn\fR to the variable \fIx\fR:
370
371 .sp
372 .in +2
373 .nf
374 \fBx=$(printf "%s\en" 'scale = 10; 104348/33215' | bc)\fR
375 .fi
376 .in -2
377 .sp
378
379 .LP
380 \fBExample 2 \fRDefining a computing function
381 .sp
382 .LP
383 Defines a function to compute an approximate value of the exponential function:
384
414 .in +2
415 .nf
416 \fBfor(i=1; i<=10; i++) e(i)\fR
417 .fi
418 .in -2
419 .sp
420
421 .sp
422 .LP
423 or
424
425 .sp
426 .in +2
427 .nf
428 \fBfor (i = 1; i <= 10; ++i) { e(i) }\fR
429 .fi
430 .in -2
431 .sp
432
433 .SH ENVIRONMENT VARIABLES
434 See \fBenviron\fR(5) for descriptions of the following environment variables
435 that affect the execution of \fBbc\fR: \fBLANG\fR, \fBLC_ALL\fR,
436 \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
437 .SH EXIT STATUS
438 The following exit values are returned:
439 .sp
440 .ne 2
441 .na
442 \fB\fB0\fR\fR
443 .ad
444 .RS 15n
445 All input files were processed successfully.
446 .RE
447
448 .sp
449 .ne 2
450 .na
451 \fB\fBunspecified\fR\fR
452 .ad
453 .RS 15n
454 An error occurred.
455 .RE
456
457 .SH FILES
458 .ne 2
459 .na
460 \fB\fB/usr/lib/lib.b\fR\fR
461 .ad
462 .RS 25n
463 mathematical library
464 .RE
465
466 .sp
467 .ne 2
468 .na
469 \fB\fB/usr/include/limits.h\fR\fR
470 .ad
471 .RS 25n
472 to define BC_ parameters
473 .RE
474
475 .SH ATTRIBUTES
476 See \fBattributes\fR(5) for descriptions of the following attributes:
477 .sp
478
479 .sp
480 .TS
481 box;
482 c | c
483 l | l .
484 ATTRIBUTE TYPE ATTRIBUTE VALUE
485 _
486 Interface Stability Standard
487 .TE
488
489 .SH SEE ALSO
490 \fBdc\fR(1), \fBawk\fR(1), \fBattributes\fR(5), \fBenviron\fR(5),
491 \fBstandards\fR(5)
492 .SH NOTES
493 The \fBbc\fR command does not recognize the logical operators \fB&&\fR and
494 \fB|\||\fR.
495 .sp
496 .LP
497 The \fBfor\fR statement must have all three expressions (\fIE\fR's).
|