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