2 .\" Copyright 1989 AT&T Copyright (c) 2003, Sun Microsystems, Inc. All Rights Reserved Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
3 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
4 .\" http://www.opengroup.org/bookstore/.
5 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
6 .\" This notice shall appear on any product containing this material.
7 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
8 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
9 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
10 .TH BC 1 "Oct 25, 2017"
12 bc \- arbitrary precision arithmetic language
16 \fB/usr/bin/bc\fR [\fB-c\fR] [\fB-l\fR] [\fIfile\fR]...
22 The \fBbc\fR utility implements an arbitrary precision calculator. It takes
23 input from any files given, then reads from the standard input. If the standard
24 input and standard output to \fBbc\fR are attached to a terminal, the
25 invocation of \fBbc\fR is \fIinteractive\fR, causing behavioral constraints
26 described in the following sections. \fBbc\fR processes a language that
27 resembles C and is a preprocessor for the desk calculator program \fBdc\fR,
28 which it invokes automatically unless the \fB-c\fR option is specified. In this
29 case the \fBdc\fR input is sent to the standard output instead.
33 The syntax for \fBbc\fR programs is as follows:
40 Means a letter \fBa\fR\(mi\fBz\fR,
49 Means an expression: a (mathematical or logical) value, an operand that takes
50 a value, or a combination of operands and operators that evaluates to a value,
65 Enclosed in \fB/*\fR and \fB*/\fR.
66 .SS "Names (Operands)"
69 Simple variables: \fIL\fR.
73 Array elements: \fIL\fR [ \fIE\fR ] (up to \fBBC_DIM_MAX\fR dimensions).
77 The words \fBibase\fR, \fBobase\fR (limited to \fBBC_BASE_MAX\fR), and
78 \fBscale\fR (limited to \fBBC_SCALE_MAX\fR).
83 Arbitrarily long numbers with optional sign and decimal point. Strings of fewer
84 than \fBBC_STRING_MAX\fR characters, between double quotes ("). \fB(\fR \fIE\fR
89 \fB\fBsqrt ( \fR\fIE\fR\fB )\fR\fR
98 \fB\fBlength ( \fR\fIE\fR\fB )\fR\fR
101 Number of significant decimal digits.
107 \fB\fBscale ( \fR\fIE\fR\fB )\fR\fR
110 Number of digits right of decimal point.
116 \fB\fIL \fR\fB( \fR\fIE\fR , ... , \fIE\fR\fB )\fR\fR
126 \fB\fB+ \(mi * / % ^\fR\fR
130 (\fB%\fR is remainder; \fB^\fR is power)
136 \fB\fB++ \(mi\(mi \fR\fR
140 (prefix and postfix; apply to names)
146 \fB\fB== <= >= != < >\fR\fR
156 \fB\fB= =+ =\(mi =* =/ =% =^\fR\fR
170 \fB{\fR \fIS\fR \fB;\fR.\|.\|. \fB;\fR \fIS\fR \fB}\fR
174 \fBif (\fR \fIE\fR \fB)\fR \fIS\fR
178 \fBwhile (\fR \fIE\fR \fB)\fR \fIS\fR
182 \fBfor (\fR \fIE\fR \fB;\fR \fIE\fR \fB;\fR \fIE\fR \fB)\fR \fIS\fR
199 .SS "Function Definitions"
202 \fBdefine\fR \fIL\fR \fB(\fR \fIL\fR \fB,\fR.\|.\|.\|\fB,\fR \fIL\fR \fB) {\fR
206 \fBauto\fR \fIL\fR \fB,\fR.\|.\|.\|\fB,\fR \fIL\fR
210 \fIS\fR \fB;\fR.\|.\|.\| \fIS\fR
214 \fBreturn (\fR \fIE\fR \fB)\fR
220 .SS "Functions in \fB-l\fR Math Library"
224 \fB\fBs(\fR\fIx\fR\fB)\fR\fR
233 \fB\fBc(\fR\fIx\fR\fB)\fR\fR
242 \fB\fBe(\fR\fIx\fR\fB)\fR\fR
251 \fB\fBl(\fR\fIx\fR\fB)\fR\fR
260 \fB\fBa(\fR\fIx\fR\fB)\fR\fR
269 \fB\fBj(\fR\fIn\fR\fB,\fR\fIx)\fR\fR
277 All function arguments are passed by value.
280 The value of a statement that is an expression is printed unless the main
281 operator is an assignment. Either semicolons or new-lines may separate
282 statements. Assignment to \fBscale\fR influences the number of digits to be
283 retained on arithmetic operations in the manner of \fBdc\fR. Assignments to
284 \fBibase\fR or \fBobase\fR set the input and output number radix respectively.
287 The same letter may be used as an array, a function, and a simple variable
288 simultaneously. All variables are global to the program. \fBauto\fR variables
289 are stacked during function calls. When using arrays as function arguments or
290 defining them as automatic variables, empty square brackets must follow the
295 The following operands are supported:
302 Compiles only. The output is \fBdc\fR commands that are sent to the standard
312 Defines the math functions and initializes \fBscale\fR to \fB20\fR, instead of
313 the default zero. All math results have the scale of \fB20\fR.
319 The following operands are supported:
326 A pathname of a text file containing \fBbc\fR program statements. After all
327 cases of \fIfile\fR have been read, \fBbc\fR reads the standard input.
332 \fBExample 1 \fRSetting the precision of a variable
335 In the shell, the following assigns an approximation of the first ten digits of
336 \fBn\fR to the variable \fIx\fR:
341 \fBx=$(printf "%s\en" 'scale = 10; 104348/33215' | bc)\fR
347 \fBExample 2 \fRDefining a computing function
350 Defines a function to compute an approximate value of the exponential function:
374 \fBExample 3 \fRPrinting the approximate values of the function
377 Prints approximate values of the exponential function of the first ten
383 \fBfor(i=1; i<=10; i++) e(i)\fR
395 \fBfor (i = 1; i <= 10; ++i) { e(i) }\fR
400 .SH ENVIRONMENT VARIABLES
403 See \fBenviron\fR(5) for descriptions of the following environment variables
404 that affect the execution of \fBbc\fR: \fBLANG\fR, \fBLC_ALL\fR,
405 \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and \fBNLSPATH\fR.
409 The following exit values are returned:
416 All input files were processed successfully.
422 \fB\fBunspecified\fR\fR
432 \fB\fB/usr/lib/lib.b\fR\fR
441 \fB\fB/usr/include/limits.h\fR\fR
444 to define BC_ parameters
450 See \fBattributes\fR(5) for descriptions of the following attributes:
458 ATTRIBUTE TYPE ATTRIBUTE VALUE
460 Interface Stability Standard
466 \fBdc\fR(1), \fBawk\fR(1), \fBattributes\fR(5), \fBenviron\fR(5),
471 The \fBbc\fR command does not recognize the logical operators \fB&&\fR and
475 The \fBfor\fR statement must have all three expressions (\fIE\fR's).