Blob
1 .TH BC 12 .SH NAME3 bc \- arbitrary-precision arithmetic language4 .SH SYNOPSIS5 .B bc6 [7 .B -c8 ]9 [10 .B -l11 ]12 [13 .B -s14 ]15 [16 .I file ...17 ]18 .SH DESCRIPTION19 .I Bc20 is an interactive processor for a language that resembles21 C but provides arithmetic on numbers of arbitrary length with up22 to 100 digits right of the decimal point.23 It takes input from any files given, then reads24 the standard input.25 The26 .B -l27 argument stands for the name28 of an arbitrary precision math library.29 The30 .B -s31 argument suppresses the automatic display32 of calculation results; all output is via the33 .B print34 command.35 .PP36 The following syntax for37 .I bc38 programs is like that of C;39 .I L40 means letter41 .BR a - z ,42 .I E43 means expression,44 .I S45 means statement.46 .TF length(E)47 .TP48 Lexical49 .RS50 .HP51 comments are enclosed in52 .B /* */53 .HP54 newlines end statements55 .RE56 .TP57 Names58 .IP59 simple variables:60 .I L61 .br62 array elements:63 .IB L [ E ]64 .br65 The words66 .BR ibase ,67 .BR obase ,68 and69 .B scale70 .TP71 Other operands72 .IP73 arbitrarily long numbers with optional sign and decimal point.74 .RS75 .TP76 .BI ( E )77 .TP78 .BI sqrt( E )79 .TP80 .BI length( E )81 number of significant decimal digits82 .TP83 .BI scale( E )84 number of digits right of decimal point85 .TP86 .IB L ( E , ... ,\fIE\fP)87 function call88 .RE89 .TP90 Operators91 .RS92 .HP93 .B "+ - * / % ^\ "94 .RB ( %95 is remainder;96 .B ^97 is power)98 .HP99 .B "++ --\ "100 .TP101 .B "== <= >= != < >"102 .TP103 .B "= += -= *= /= %= ^="104 .RE105 .TP106 Statements107 .RS108 .br109 .I E110 .br111 .B {112 .I S113 .B ;114 \&...115 .B ;116 .I S117 .B }118 .br119 .B "print"120 .I E121 .br122 .B "if ("123 .I E124 .B )125 .I S126 .br127 .B "while ("128 .I E129 .B )130 .I S131 .br132 .B "for ("133 .I E134 .B ;135 .I E136 .B ;137 .I E138 .B ")"139 .I S140 .br141 null statement142 .br143 .B break144 .br145 .B quit146 .br147 \fL"\fRtext\fL"\fR148 .RE149 .TP150 Function definitions151 .RS152 .br153 .B define154 .I L155 .B (156 .I L157 .B ,158 \&...159 .B ,160 .I L161 .B ){162 .PD0163 .br164 .B auto165 .I L166 .B ,167 \&...168 .B ,169 .I L170 .br171 .I S172 .B ;173 \&...174 .B ;175 .I S176 .br177 .B return178 .I E179 .LP180 .B }181 .RE182 .TP183 Functions in184 .B -l185 math library186 .RS187 .TP188 .BI s( x )189 sine190 .TP191 .BI c( x )192 cosine193 .TP194 .BI e( x )195 exponential196 .TP197 .BI l( x )198 log199 .TP200 .BI a( x )201 arctangent202 .TP203 .BI j( "n, x" )204 Bessel function205 .RE206 .PP207 .DT208 All function arguments are passed by value.209 .PD210 .PP211 The value of an expression at the top level is printed212 unless the main operator is an assignment or the213 .B -s214 command line argument is given.215 Text in quotes, which may include newlines, is always printed.216 Either semicolons or newlines may separate statements.217 Assignment to218 .B scale219 influences the number of digits to be retained on arithmetic220 operations in the manner of221 .IR dc (1).222 Assignments to223 .B ibase224 or225 .B obase226 set the input and output number radix respectively.227 .PP228 The same letter may be used as an array, a function,229 and a simple variable simultaneously.230 All variables are global to the program.231 Automatic variables are pushed down during function calls.232 In a declaration of an array as a function argument233 or automatic variable234 empty square brackets must follow the array name.235 .PP236 .I Bc237 is actually a preprocessor for238 .IR dc (1),239 which it invokes automatically, unless the240 .B -c241 (compile only)242 option is present.243 In this case the244 .I dc245 input is sent to the standard output instead.246 .SH EXAMPLE247 Define a function to compute an approximate value of248 the exponential.249 Use it to print 10 values.250 (The exponential function in the library gives better answers.)251 .PP252 .EX253 scale = 20254 define e(x) {255 auto a, b, c, i, s256 a = 1257 b = 1258 s = 1259 for(i=1; 1; i++) {260 a *= x261 b *= i262 c = a/b263 if(c == 0) return s264 s += c265 }266 }267 for(i=1; i<=10; i++) print e(i)268 .EE269 .SH FILES270 .B \*9/lib/bclib271 mathematical library272 .SH SOURCE273 .B \*9/src/cmd/bc.y274 .SH "SEE ALSO"275 .IR dc (1),276 .IR hoc (1)277 .SH BUGS278 No279 .LR && ,280 .LR || ,281 or282 .L !283 operators.284 .PP285 A286 .L for287 statement must have all three288 .LR E s.289 .PP290 A291 .L quit292 is interpreted when read, not when executed.