1bc(1) User Commands bc(1)
2
3
4
6 bc - arbitrary precision arithmetic language
7
9 /usr/bin/bc [-c] [-l] [file]...
10
11
12 /usr/xpg6/bin/bc [-c] [-l] [file]...
13
14
16 The bc utility implements an arbitrary precision calculator. It takes
17 input from any files given, then reads from the standard input. If the
18 standard input and standard output to bc are attached to a terminal,
19 the invocation of bc is interactive, causing behavioral constraints
20 described in the following sections. bc processes a language that
21 resembles C and is a preprocessor for the desk calculator program dc,
22 which it invokes automatically unless the -c option is specified. In
23 this case the dc input is sent to the standard output instead.
24
26 The syntax for bc programs is as follows:
27
28 L Means a letter a−z,
29
30
31 E Means an expression: a (mathematical or logical) value, an operand
32 that takes a value, or a combination of operands and operators
33 that evaluates to a value,
34
35
36 S Means a statement.
37
38
39 Comments
40 Enclosed in /* and */.
41
42 Names (Operands)
43 Simple variables: L.
44 Array elements: L [ E ] (up to BC_DIM_MAX dimensions).
45 The words ibase, obase (limited to BC_BASE_MAX), and scale (limited
46 to BC_SCALE_MAX).
47
48 Other Operands
49 Arbitrarily long numbers with optional sign and decimal point. Strings
50 of fewer than BC_STRING_MAX characters, between double quotes ("). ( E
51 )
52
53 sqrt ( E ) Square root
54
55
56 length ( E ) Number of significant decimal digits.
57
58
59 scale ( E ) Number of digits right of decimal point.
60
61
62 L ( E , ... , E )
63
64
65 Operators
66 + − * / % ^
67
68 (% is remainder; ^ is power)
69
70
71 ++ −−
72
73 (prefix and postfix; apply to names)
74
75
76 == <= >= != < >
77
78
79
80
81 = =+ =− =* =/ =% =^
82
83
84
85
86 Statements
87 E
88 { S ;... ; S }
89 if ( E ) S
90 while ( E ) S
91 for ( E ; E ; E ) S
92 null statement
93 break
94 quit
95
96
97 .string
98
99 Function Definitions
100 define L ( L ,..., L ) {
101 auto L ,..., L
102 S ;... S
103 return ( E )
104 }
105
106 Functions in -l Math Library
107 s(x) sine
108
109
110 c(x) cosine
111
112
113 e(x) exponential
114
115
116 l(x) log
117
118
119 a(x) arctangent
120
121
122 j(n,x) Bessel function
123
124
125
126 All function arguments are passed by value.
127
128
129 The value of a statement that is an expression is printed unless the
130 main operator is an assignment. Either semicolons or new-lines may sep‐
131 arate statements. Assignment to scale influences the number of digits
132 to be retained on arithmetic operations in the manner of dc. Assign‐
133 ments to ibase or obase set the input and output number radix respec‐
134 tively.
135
136
137 The same letter may be used as an array, a function, and a simple vari‐
138 able simultaneously. All variables are global to the program. auto
139 variables are stacked during function calls. When using arrays as func‐
140 tion arguments or defining them as automatic variables, empty square
141 brackets must follow the array name.
142
144 The following operands are supported:
145
146 -c Compiles only. The output is dc commands that are sent to the
147 standard output.
148
149
150 /usr/bin/bc
151 -l Defines the math functions and initializes scale to 20, instead
152 of the default zero.
153
154
155 /usr/xpg6/bin/bc
156 -l Defines the math functions and initializes scale to 20, instead
157 of the default zero. All math results have the scale of 20.
158
159
161 The following operands are supported:
162
163 file A pathname of a text file containing bc program statements.
164 After all cases of file have been read, bc reads the standard
165 input.
166
167
169 Example 1 Setting the precision of a variable
170
171
172 In the shell, the following assigns an approximation of the first ten
173 digits of n to the variable x:
174
175
176 x=$(printf "%s\n" 'scale = 10; 104348/33215' | bc)
177
178
179
180 Example 2 Defining a computing function
181
182
183 Defines a function to compute an approximate value of the exponential
184 function:
185
186
187 scale = 20
188 define e(x){
189 auto a, b, c, i, s
190 a = 1
191 b = 1
192 s = 1
193 for(i=1; 1==1; i++){
194 a = a*x
195 b = b*i
196 c = a/b
197 if(c == 0) return(s)
198 s = s+c
199 }
200 }
201
202
203
204 Example 3 Printing the approximate values of the function
205
206
207 Prints approximate values of the exponential function of the first ten
208 integers:
209
210
211 for(i=1; i<=10; i++) e(i)
212
213
214
215
216 or
217
218
219 for (i = 1; i <= 10; ++i) { e(i) }
220
221
222
224 See environ(5) for descriptions of the following environment variables
225 that affect the execution of bc: LANG, LC_ALL, LC_CTYPE, LC_MESSAGES,
226 and NLSPATH.
227
229 The following exit values are returned:
230
231 0 All input files were processed successfully.
232
233
234 unspecified An error occurred.
235
236
238 /usr/lib/lib.b mathematical library
239
240
241 /usr/include/limits.h to define BC_ parameters
242
243
245 See attributes(5) for descriptions of the following attributes:
246
247
248
249
250 ┌─────────────────────────────┬─────────────────────────────┐
251 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
252 ├─────────────────────────────┼─────────────────────────────┤
253 │Availability │SUNWesu │
254 ├─────────────────────────────┼─────────────────────────────┤
255 │Interface Stability │Standard │
256 └─────────────────────────────┴─────────────────────────────┘
257
259 dc(1), awk(1), attributes(5), environ(5), standards(5)
260
262 The bc command does not recognize the logical operators && and ||.
263
264
265 The for statement must have all three expressions (E's).
266
267
268
269SunOS 5.11 29 Aug 2003 bc(1)