Commit | Line | Data |
---|---|---|
f87489ac WJ |
1 | .\" |
2 | .\" bc.1 - the *roff document processor source for the bc manual | |
3 | .\" | |
4 | .\" This file is part of bc written for MINIX. | |
5 | .\" Copyright (C) 1991 Free Software Foundation, Inc. | |
6 | .\" | |
7 | .\" This program is free software; you can redistribute it and/or modify | |
8 | .\" it under the terms of the GNU General Public License as published by | |
9 | .\" the Free Software Foundation; either version 2 of the License , or | |
10 | .\" (at your option) any later version. | |
11 | .\" | |
12 | .\" This program is distributed in the hope that it will be useful, | |
13 | .\" but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | .\" GNU General Public License for more details. | |
16 | .\" | |
17 | .\" You should have received a copy of the GNU General Public License | |
18 | .\" along with this program; see the file COPYING. If not, write to | |
19 | .\" the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
20 | .\" | |
21 | .\" You may contact the author by: | |
22 | .\" e-mail: phil@cs.wwu.edu | |
23 | .\" us-mail: Philip A. Nelson | |
24 | .\" Computer Science Department, 9062 | |
25 | .\" Western Washington University | |
26 | .\" Bellingham, WA 98226-9062 | |
27 | .\" | |
28 | .\" | |
29 | .TH bc 1 .\" "Command Manual" v1.01 "Nov 22, 1991" | |
30 | .SH NAME | |
31 | bc - An arbitrary precision calculator language | |
32 | .SH SYNTAX | |
33 | \fBbc\fR [ \fB-lws\fR ] [ \fI file ...\fR ] | |
34 | .SH DESCRIPTION | |
35 | \fBbc\fR is a language that supports arbitrary precision numbers | |
36 | with interactive execution of statements. There are some similarities | |
37 | in the syntax to the C programming language. | |
38 | A standard math library is available by command line option. | |
39 | If requested, the math library is defined before processing any files. | |
40 | \fBbc\fR starts by processing code from all the files listed | |
41 | on the command line in the order listed. After all files have been | |
42 | processed, \fBbc\fR reads from the standard input. All code is | |
43 | executed as it is read. (If a file contains a command to halt the | |
44 | processor, \fBbc\fR will never read from the standard input.) | |
45 | .PP | |
46 | This version of \fBbc\fR contains several extensions beyond | |
47 | traditional \fBbc\fR implementations and the POSIX draft standard. | |
48 | Command line options can cause these extensions to print a warning | |
49 | or to be rejected. This | |
50 | document describes the language accepted by this processor. | |
51 | Extensions will be identified as such. | |
52 | .SS OPTIONS | |
53 | .IP -l | |
54 | Define the standard math library. | |
55 | .IP -w | |
56 | Give warnings for extensions to POSIX \fBbc\fR. | |
57 | .IP -s | |
58 | Process exactly the POSIX \fBbc\fR language. | |
59 | .SS NUMBERS | |
60 | The most basic element in \fBbc\fR is the number. Numbers are | |
61 | arbitrary precision numbers. This precision is both in the integer | |
62 | part and the fractional part. All numbers are represented internally | |
63 | in decimal and all computation is done in decimal. (This version | |
64 | truncates results from divide and multiply operations.) There are two | |
65 | attributes of numbers, the length and the scale. The length is the | |
66 | total number of significant decimal digits in a number and the scale | |
67 | is the total number of decimal digits after the decimal point. For | |
68 | example: | |
69 | .nf | |
70 | .RS | |
71 | .000001 has a length of 6 and scale of 6. | |
72 | 1935.000 has a length of 7 and a scale of 3. | |
73 | .RE | |
74 | .fi | |
75 | .SS VARIABLES | |
76 | Numbers are stored in two types of variables, simple variables and | |
77 | arrays. Both simple variables and array variables are named. Names | |
78 | begin with a letter followed by any number of letters, digits and | |
79 | underscores. All letters must be lower case. (Full alpha-numeric | |
80 | names are an extension. In POSIX \fBbc\fR all names are a single | |
81 | lower case letter.) The type of variable is clear by the context | |
82 | because all array variable names will be followed by brackets ([]). | |
83 | .PP | |
84 | There are four special variables, \fBscale, ibase, obase,\fR and | |
85 | \fBlast\fR. \fBscale\fR defines how some operations use digits after the | |
86 | decimal point. The default value of \fBscale\fR is 0. \fBibase\fR | |
87 | and \fBobase\fR define the conversion base for input and output | |
88 | numbers. The default for both input and output is base 10. | |
89 | \fBlast\fR (an extension) is a variable that has the value of the last | |
90 | printed number. These will be discussed in further detail where | |
91 | appropriate. All of these variables may have values assigned to them | |
92 | as well as used in expressions. | |
93 | .SS COMMENTS | |
94 | Comments in \fBbc\fR start with the characters \fB/*\fR and end with | |
95 | the characters \fB*/\fR. Comments may start anywhere and appear as a | |
96 | single space in the input. (This causes comments to delimit other | |
97 | input items. For example, a comment can not be found in the middle of | |
98 | a variable name.) Comments include any newlines (end of line) between | |
99 | the start and the end of the comment. | |
100 | .SS EXPRESSIONS | |
101 | The numbers are manipulated by expressions and statements. Since | |
102 | the language was designed to be interactive, statements and expressions | |
103 | are executed as soon as possible. There is no "main" program. Instead, | |
104 | code is executed as it is encountered. (Functions, discussed in | |
105 | detail later, are defined when encountered.) | |
106 | .PP | |
107 | A simple expression is just a constant. \fBbc\fR converts constants | |
108 | into internal decimal numbers using the current input base, specified | |
109 | by the variable \fBibase\fR. (There is an exception in functions.) | |
110 | The legal values of \fBibase\fR are 2 through 16 (F). Assigning a | |
111 | value outside this range to \fBibase\fR will result in a value of 2 | |
112 | or 16. Input numbers may contain the characters 0-9 and A-F. (Note: | |
113 | They must be capitals. Lower case letters are variable names.) | |
114 | Single digit numbers always have the value of the digit regardless of | |
115 | the value of \fBibase\fR. (i.e. A = 10.) For multi-digit numbers, | |
116 | \fBbc\fR changes all input digits greater or equal to ibase to the | |
117 | value of \fBibase\fR-1. This makes the number \fBFFF\fR always be | |
118 | the largest 3 digit number of the input base. | |
119 | .PP | |
120 | Full expressions are similar to many other high level languages. | |
121 | Since there is only one kind of number, there are no rules for mixing | |
122 | types. Instead, there are rules on the scale of expressions. Every | |
123 | expression has a scale. This is derived from the scale of original | |
124 | numbers, the operation performed and in many cases, the value of the | |
125 | variable \fBscale\fR. Legal values of the variable \fBscale\fR are | |
126 | 0 to the maximum number representable by a C integer. | |
127 | .PP | |
128 | In the following descriptions of legal expressions, "expr" refers to a | |
129 | complete expression and "var" refers to a simple or an array variable. | |
130 | A simple variable is just a | |
131 | .RS | |
132 | \fIname\fR | |
133 | .RE | |
134 | and an array variable is specified as | |
135 | .RS | |
136 | \fIname\fR[\fIexpr\fR] | |
137 | .RE | |
138 | Unless specifically | |
139 | mentioned the scale of the result is the maximum scale of the | |
140 | expressions involved. | |
141 | .IP "- expr" | |
142 | The result is the negation of the expression. | |
143 | .IP "++ var" | |
144 | The variable is incremented by one and the new value is the result of | |
145 | the expression. | |
146 | .IP "-- var" | |
147 | The variable | |
148 | is decremented by one and the new value is the result of the | |
149 | expression. | |
150 | .IP "var ++" | |
151 | The result of the expression is the value of | |
152 | the variable and then the variable is incremented by one. | |
153 | .IP "var --" | |
154 | The result of the expression is the value of the variable and then | |
155 | the variable is decremented by one. | |
156 | .IP "expr + expr" | |
157 | The result of the expression is the sum of the two expressions. | |
158 | .IP "expr - expr" | |
159 | The result of the expression is the difference of the two expressions. | |
160 | .IP "expr * expr" | |
161 | The result of the expression is the product of the two expressions. | |
162 | .IP "expr / expr" | |
163 | The result of the expression is the quotient of the two expressions. | |
164 | The scale of the result is the value of the variable \fBscale\fR. | |
165 | .IP "expr % expr" | |
166 | The result of the expression is the "remainder" and it is computed in the | |
167 | following way. To compute a%b, first a/b is computed to \fBscale\fR | |
168 | digits. That result is used to compute a-(a/b)*b to the scale of the | |
169 | maximum of \fBscale\fR+scale(b) and scale(a). If \fBscale\fR is set | |
170 | to zero and both expressions are integers this expression is the | |
171 | integer remainder function. | |
172 | .IP "expr ^ expr" | |
173 | The result of the expression is the value of the first raised to the | |
174 | second. The second expression must be an integer. (If the second | |
175 | expression is not an integer, a warning is generated and the | |
176 | expression is truncated to get an integer value.) The scale of the | |
177 | result is \fBscale\fR if the exponent is negative. If the exponent | |
178 | is positive the scale of the result is the minimum of the scale of the | |
179 | first expression times the value of the exponent and the maximum of | |
180 | \fBscale\fR and the scale of the first expression. (e.g. scale(a^b) | |
181 | = min(scale(a)*b, max( \fBscale,\fR scale(a))).) It should be noted | |
182 | that expr^0 will always return the value of 1. | |
183 | .IP "( expr )" | |
184 | This alters the standard precedence to force the evaluation of the | |
185 | expression. | |
186 | .IP "var = expr" | |
187 | The variable is assigned the value of the expression. | |
188 | .IP "var <op>= expr" | |
189 | This is equivalent to "var = var <op> expr" with the exception that | |
190 | the "var" part is evaluated only once. This can make a difference if | |
191 | "var" is an array. | |
192 | .PP | |
193 | Relational expressions are a special kind of expression | |
194 | that always evaluate to 0 or 1, 0 if the relation is false and 1 if | |
195 | the relation is true. These may appear in any legal expression. | |
196 | (POSIX bc requires that relational expressions are used only in if, | |
197 | while, and for statements and that only one relational test may be | |
198 | done in them.) The relational operators are | |
199 | .IP "expr1 < expr2" | |
200 | The result is 1 if expr1 is strictly less than expr2. | |
201 | .IP "expr1 <= expr2" | |
202 | The result is 1 if expr1 is less than or equal to expr2. | |
203 | .IP "expr1 > expr2" | |
204 | The result is 1 if expr1 is strictly greater than expr2. | |
205 | .IP "expr1 >= expr2" | |
206 | The result is 1 if expr1 is greater than or equal to expr2. | |
207 | .IP "expr1 == expr2" | |
208 | The result is 1 if expr1 is equal to expr2. | |
209 | .IP "expr1 != expr2" | |
210 | The result is 1 if expr1 is not equal to expr2. | |
211 | .PP | |
212 | Boolean operations are also legal. (POSIX \fBbc\fR does NOT have | |
213 | boolean operations). The result of all boolean operations are 0 and 1 | |
214 | (for false and true) as in relational expressions. The boolean | |
215 | operators are: | |
216 | .IP "!expr" | |
217 | The result is 1 if expr is 0. | |
218 | .IP "expr && expr" | |
219 | The result is 1 if both expressions are non-zero. | |
220 | .IP "expr || expr" | |
221 | The result is 1 if either expression is non-zero. | |
222 | .PP | |
223 | The expression precedence is as follows: (lowest to highest) | |
224 | .nf | |
225 | .RS | |
226 | || operator, left associative | |
227 | && operator, left associative | |
228 | ! operator, nonassociative | |
229 | Relational operators, left associative | |
230 | Assignment operator, right associative | |
231 | + and - operators, left associative | |
232 | *, / and % operators, left associative | |
233 | ^ operator, right associative | |
234 | unary - operator, nonassociative | |
235 | ++ and -- operators, nonassociative | |
236 | .RE | |
237 | .fi | |
238 | .PP | |
239 | This precedence was chosen so that POSIX compliant \fBbc\fR programs | |
240 | will run correctly. This will cause the use of the relational and | |
241 | logical operators to have some unusual behavior when used with | |
242 | assignment expressions. Consider the expression: | |
243 | .RS | |
244 | a = 3 < 5 | |
245 | .RE | |
246 | .PP | |
247 | Most C programmers would assume this would assign the result of "3 < | |
248 | 5" (the value 1) to the variable "a". What this does in \fBbc\fR is | |
249 | assign the value 3 to the variable "a" and then compare 3 to 5. It is | |
250 | best to use parenthesis when using relational and logical operators | |
251 | with the assignment operators. | |
252 | .PP | |
253 | There are a few more special expressions that are provided in \fBbc\fR. | |
254 | These have to do with user defined functions and standard | |
255 | functions. They all appear as "\fIname\fB(\fIparameters\fB)\fR". | |
256 | See the section on functions for user defined functions. The standard | |
257 | functions are: | |
258 | .IP "length ( expression )" | |
259 | The value of the length function is the number of significant digits in the | |
260 | expression. | |
261 | .IP "read ( )" | |
262 | The read function (an extension) will read a number from the standard | |
263 | input, regardless of where the function occurs. Beware, this can | |
264 | cause problems with the mixing of data and program in the standard input. | |
265 | The best use for this function is in a previously written program that | |
266 | needs input from the user, but never allows program code to be input | |
267 | from the user. The value of the read function is the number read from | |
268 | the standard input using the current value of the variable | |
269 | \fBibase\fR for the conversion base. | |
270 | .IP "scale ( expression )" | |
271 | The value of the scale function is the number of digits after the decimal | |
272 | point in the expression. | |
273 | .IP "sqrt ( expression )" | |
274 | The value of the sqrt function is the square root of the expression. If | |
275 | the expression is negative, a run time error is generated. | |
276 | .SS STATEMENTS | |
277 | Statements (as in most algebraic languages) provide the sequencing of | |
278 | expression evaluation. In \fBbc\fR statements are executed "as soon | |
279 | as possible." Execution happens when a newline in encountered and | |
280 | there is one or more complete statements. Due to this immediate | |
281 | execution, newlines are very important in \fBbc\fR. In fact, both a | |
282 | semicolon and a newline are used as statement separators. An | |
283 | improperly placed newline will cause a syntax error. Because newlines | |
284 | are statement separators, it is possible to hide a newline by using | |
285 | the backslash character. The sequence "\e<nl>", where <nl> is the | |
286 | newline appears to \fBbc\fR as whitespace instead of a newline. A | |
287 | statement list is a series of statements separated by semicolons and | |
288 | newlines. The following is a list of \fBbc\fR statements and what | |
289 | they do: (Things enclosed in brackets ([]) are optional parts of the | |
290 | statement.) | |
291 | .IP "expression" | |
292 | This statement does one of two things. If the expression starts with | |
293 | "<variable> <assignment> ...", it is considered to be an assignment | |
294 | statement. If the expression is not an assignment statement, the | |
295 | expression is evaluated and printed to the output. After the number | |
296 | is printed, a newline is printed. For example, "a=1" is an assignment | |
297 | statement and "(a=1)" is an expression that has an embedded | |
298 | assignment. All numbers that are printed are printed in the base | |
299 | specified by the variable \fBobase\fR. The legal values for \fB | |
300 | obase\fR are 2 through BC_BASE_MAX. (See the section LIMITS.) For | |
301 | bases 2 through 16, the usual method of writing numbers is used. For | |
302 | bases greater than 16, \fBbc\fR uses a multi-character digit method | |
303 | of printing the numbers where each higher base digit is printed as a | |
304 | base 10 number. The multi-character digits are separated by spaces. | |
305 | Each digit contains the number of characters required to represent the | |
306 | base ten value of "obase-1". Since numbers are of arbitrary | |
307 | precision, some numbers may not be printable on a single output line. | |
308 | These long numbers will be split across lines using the "\e" as the | |
309 | last character on a line. The maximum number of characters printed | |
310 | per line is 70. Due to the interactive nature of \fBbc\fR printing | |
311 | a number cause the side effect of assigning the printed value the the | |
312 | special variable \fBlast\fR. This allows the user to recover the | |
313 | last value printed without having to retype the expression that | |
314 | printed the number. Assigning to \fBlast\fR is legal and will | |
315 | overwrite the last printed value with the assigned value. The newly | |
316 | assigned value will remain until the next number is printed or another | |
317 | value is assigned to \fBlast\fR. | |
318 | .IP "string" | |
319 | The string is printed to the output. Strings start with a double quote | |
320 | character and contain all characters until the next double quote character. | |
321 | All characters are take literally, including any newline. No newline | |
322 | character is printed after the string. | |
323 | .IP "\fBprint\fR list" | |
324 | The print statement (an extension) provides another method of output. | |
325 | The "list" is a list of strings and expressions separated by commas. | |
326 | Each string or expression is printed in the order of the list. No | |
327 | terminating newline is printed. Expressions are evaluated and their | |
328 | value is printed and assigned the the variable \fBlast\fR. Strings | |
329 | in the print statement are printed to the output and may contain | |
330 | special characters. Special characters start with the backslash | |
331 | character (\e). The special characters recognized by \fBbc\fR are | |
332 | "b" (bell), "f" (form feed), "n" (newline), "r" (carriage return), "t" | |
333 | (tab), and "\e" (backslash). Any other character following the | |
334 | backslash will be ignored. This still does not allow the double quote | |
335 | character to be part of any string. | |
336 | .IP "{ statement_list }" | |
337 | This is the compound statement. It allows multiple statements to be | |
338 | grouped together for execution. | |
339 | .IP "\fBif\fR ( expression ) \fBthen\fR statement1 [\fBelse\fR statement2]" | |
340 | The if statement evaluates the expression and executes statement1 or | |
341 | statement2 depending on the value of the expression. If the expression | |
342 | is non-zero, statement1 is executed. If statement2 is present and | |
343 | the value of the expression is 0, then statement2 is executed. (The | |
344 | else clause is an extension.) | |
345 | .IP "\fBwhile\fR ( expression ) statement" | |
346 | The while statement will execute the statement while the expression | |
347 | is non-zero. It evaluates the expression before each execution of | |
348 | the statement. Termination of the loop is caused by a zero | |
349 | expression value or the execution of a break statement. | |
350 | .IP "\fBfor\fR ( [expression1] ; [expression2] ; [expression3] ) statement" | |
351 | The for statement controls repeated execution of the statement. | |
352 | Expression1 is evaluated before the loop. Expression2 is evaluated | |
353 | before each execution of the statement. If it is non-zero, the statement | |
354 | is evaluated. If it is zero, the loop is terminated. After each | |
355 | execution of the statement, expression3 is evaluated before the reevaluation | |
356 | of expression2. If expression1 or expression3 are missing, nothing is | |
357 | evaluated at the point they would be evaluated. | |
358 | If expression2 is missing, it is the same as substituting | |
359 | the value 1 for expression2. (The optional expressions are an | |
360 | extension. POSIX \fBbc\fR requires all three expressions.) | |
361 | The following is equivalent code for the for statement: | |
362 | .nf | |
363 | .RS | |
364 | expression1; | |
365 | while (expression2) { | |
366 | statement; | |
367 | expression3; | |
368 | } | |
369 | .RE | |
370 | .fi | |
371 | .IP "\fBbreak\fR" | |
372 | This statement causes a forced exit of the most recent enclosing while | |
373 | statement or for statement. | |
374 | .IP "\fBcontinue\fR" | |
375 | The continue statement (an extension) causes the most recent enclosing | |
376 | for statement to start the next iteration. | |
377 | .IP "\fBhalt\fR" | |
378 | The halt statement (an extension) is an executed statement that causes | |
379 | the \fBbc\fR processor to quit only when it is executed. For example, | |
380 | "if (0 == 1) halt" will not cause \fBbc\fR to terminate because the halt is | |
381 | not executed. | |
382 | .IP "\fBreturn\fR" | |
383 | Return the value 0 from a function. (See the section on functions.) | |
384 | .IP "\fBreturn\fR ( expression )" | |
385 | Return the value of the expression from a function. (See the section on | |
386 | functions.) | |
387 | .SS PSEUDO STATEMENTS | |
388 | These statements are not statements in the traditional sense. They are | |
389 | not executed statements. Their function is performed at "compile" time. | |
390 | .IP "\fBlimits\fR" | |
391 | Print the local limits enforced by the local version of \fBbc\fR. This | |
392 | is an extension. | |
393 | .IP "\fBquit\fR" | |
394 | When the quit statement is read, the \fBbc\fR processor | |
395 | is terminated, regardless of where the quit statement is found. For | |
396 | example, "if (0 == 1) quit" will cause \fBbc\fR to terminate. | |
397 | .IP "\fBwarranty\fR" | |
398 | Print a longer warranty notice. This is an extension. | |
399 | .SS FUNCTIONS | |
400 | Functions provide a method of defining a computation that can be executed | |
401 | later. Functions in | |
402 | .B bc | |
403 | always compute a value and return it to the caller. Function definitions | |
404 | are "dynamic" in the sense that a function is undefined until a definition | |
405 | is encountered in the input. That definition is then used until another | |
406 | definition function for the same name is encountered. The new definition | |
407 | then replaces the older definition. A function is defined as follows: | |
408 | .nf | |
409 | .RS | |
410 | \fBdefine \fIname \fB( \fIparameters \fB) { \fInewline | |
411 | \fI auto_list statement_list \fB}\fR | |
412 | .RE | |
413 | .fi | |
414 | A function call is just an expression of the form | |
415 | "\fIname\fB(\fIparameters\fB)\fR". | |
416 | .PP | |
417 | Parameters are numbers or arrays (an extension). In the function definition, | |
418 | zero or more parameters are defined by listing their names separated by | |
419 | commas. Numbers are only call by value parameters. Arrays are only | |
420 | call by variable. Arrays are specified in the parameter definition by | |
421 | the notation "\fIname\fB[]\fR". In the function call, actual parameters | |
422 | are full expressions for number parameters. The same notation is used | |
423 | for passing arrays as for defining array parameters. The named array is | |
424 | passed by variable to the function. Since function definitions are dynamic, | |
425 | parameter numbers and types are checked when a function is called. Any | |
426 | mismatch in number or types of parameters will cause a runtime error. | |
427 | A runtime error will also occur for the call to an undefined function. | |
428 | .PP | |
429 | The \fIauto_list\f is an optional list of variables that are for | |
430 | "local" use. The syntax of the auto list (if present) is "\fBauto | |
431 | \fIname\fR, ... ;". (The semicolon is optional.) Each \fIname\fR is | |
432 | the name of an auto variable. Arrays may be specified by using the | |
433 | same notation as used in parameters. These variables have their | |
434 | values pushed onto a stack at the start of the function. The | |
435 | variables are then initialized to zero and used throughout the | |
436 | execution of the function. At function exit, these variables are | |
437 | popped so that the original value (at the time of the function call) | |
438 | of these variables are restored. The parameters are really auto | |
439 | variables that are initialized to a value provided in the function | |
440 | call. Auto variables are different than traditional local variables | |
441 | in the fact that if function A calls function B, B may access function | |
442 | A's auto variables by just using the same name, unless function B has | |
443 | called them auto variables. Due to the fact that auto variables and | |
444 | parameters are pushed onto a stack, \fBbc\fR supports recursive functions. | |
445 | .PP | |
446 | The function body is a list of \fBbc\fR statements. Again, statements | |
447 | are separated by semicolons or newlines. Return statements cause the | |
448 | termination of a function and the return of a value. There are two | |
449 | versions of the return statement. The first form, "\fBreturn\fR", returns | |
450 | the value 0 to the calling expression. The second form, | |
451 | "\fBreturn ( \fIexpression \fB)\fR", computes the value of the expression | |
452 | and returns that value to the calling expression. There is an implied | |
453 | "\fBreturn (0)\fR" at the end of every function. This allows a function | |
454 | to terminate and return 0 without an explicit return statement. | |
455 | .PP | |
456 | Functions also change the usage of the variable \fBibase\fR. All | |
457 | constants in the function body will be converted using the value of | |
458 | \fBibase\fR at the time of the function call. Changes of \fBibase\fR | |
459 | will be ignored during the execution of the function except for the | |
460 | standard function \fBread\fR, which will always use the current value | |
461 | of \fBibase\fR for conversion of numbers. | |
462 | .SS MATH LIBRARY | |
463 | If \fBbc\fR is invoked with the \fB-l\fR option, a math library is preloaded | |
464 | and the default scale is set to 20. The math functions will calculate their | |
465 | results to the scale set at the time of their call. | |
466 | The math library defines the following functions: | |
467 | .IP "s (\fIx\fR)" | |
468 | The sine of x in radians. | |
469 | .IP "c (\fIx\fR)" | |
470 | The cosine of x in radians. | |
471 | .IP "a (\fIx\fR)" | |
472 | The arctangent of x. | |
473 | .IP "l (\fIx\fR)" | |
474 | The natural logarithm of x. | |
475 | .IP "e (\fIx\fR)" | |
476 | The exponential function of raising e to the value x. | |
477 | .IP "j (\fIn,x\fR)" | |
478 | The bessel function of integer order n of x. | |
479 | .SS EXAMPLES | |
480 | In /bin/sh, the following will assign the value of "pi" to the shell | |
481 | variable \fBpi\fR. | |
482 | .RS | |
483 | \f(CW | |
484 | pi=$(echo "scale=10; 4*a(1)" | bc -l) | |
485 | \fR | |
486 | .RE | |
487 | .PP | |
488 | The following is the definition of the exponential function used in the | |
489 | math library. This function is written in POSIX \fBbc\fR. | |
490 | .nf | |
491 | .RS | |
492 | \f(CW | |
493 | scale = 20 | |
494 | ||
495 | /* Uses the fact that e^x = (e^(x/2))^2 | |
496 | When x is small enough, we use the series: | |
497 | e^x = 1 + x + x^2/2! + x^3/3! + ... | |
498 | */ | |
499 | ||
500 | define e(x) { | |
501 | auto a, d, e, f, i, m, v, z | |
502 | ||
503 | /* Check the sign of x. */ | |
504 | if (x<0) { | |
505 | m = 1 | |
506 | x = -x | |
507 | } | |
508 | ||
509 | /* Precondition x. */ | |
510 | z = scale; | |
511 | scale = 4 + z + .44*x; | |
512 | while (x > 1) { | |
513 | f += 1; | |
514 | x /= 2; | |
515 | } | |
516 | ||
517 | /* Initialize the variables. */ | |
518 | v = 1+x | |
519 | a = x | |
520 | d = 1 | |
521 | ||
522 | for (i=2; 1; i++) { | |
523 | e = (a *= x) / (d *= i) | |
524 | if (e == 0) { | |
525 | if (f>0) while (f--) v = v*v; | |
526 | scale = z | |
527 | if (m) return (1/v); | |
528 | return (v/1); | |
529 | } | |
530 | v += e | |
531 | } | |
532 | } | |
533 | \fR | |
534 | .RE | |
535 | .fi | |
536 | .PP | |
537 | The following is code that uses the extended features of \fBbc\fR to | |
538 | implement a simple program for calculating checkbook balances. This | |
539 | program is best kept in a file so that it can be used many times | |
540 | without having to retype it at every use. | |
541 | .nf | |
542 | .RS | |
543 | \f(CW | |
544 | scale=2 | |
545 | print "\enCheck book program!\en" | |
546 | print " Remember, deposits are negative transactions.\en" | |
547 | print " Exit by a 0 transaction.\en\en" | |
548 | ||
549 | print "Initial balance? "; bal = read() | |
550 | bal /= 1 | |
551 | print "\en" | |
552 | while (1) { | |
553 | "current balance = "; bal | |
554 | "transaction? "; trans = read() | |
555 | if (trans == 0) break; | |
556 | bal -= trans | |
557 | bal /= 1 | |
558 | } | |
559 | quit | |
560 | \fR | |
561 | .RE | |
562 | .fi | |
563 | .PP | |
564 | The following is the definition of the recursive factorial function. | |
565 | .nf | |
566 | .RS | |
567 | \f(CW | |
568 | define f (x) { | |
569 | if (x <= 1) return (1); | |
570 | return (f(x-1) * x); | |
571 | } | |
572 | \fR | |
573 | .RE | |
574 | .fi | |
575 | .SS DIFFERENCES | |
576 | This version of | |
577 | .B bc | |
578 | was implemented from the POSIX P1003.2/D11 draft and contains | |
579 | several differences and extensions relative to the draft and | |
580 | traditional implementations. | |
581 | It is not implemented in the traditional way using | |
582 | .I dc(1). | |
583 | This version is a single process which parses and runs a byte code | |
584 | translation of the program. There is an "undocumented" option (-c) | |
585 | that causes the program to output the byte code to | |
586 | the standard output instead of running it. It was mainly used for | |
587 | debugging the parser and preparing the math library. | |
588 | .PP | |
589 | A major source of differences is | |
590 | extensions, where a feature is extended to add more functionality and | |
591 | additions, where new features are added. | |
592 | The following is the list of differences and extensions. | |
593 | .IP LANG environment | |
594 | This version does not conform to the POSIX standard in the processing | |
595 | of the LANG environment variable and all environment variables starting | |
596 | with LC_. | |
597 | .IP names | |
598 | Traditional and POSIX | |
599 | .B bc | |
600 | have single letter names for functions, variables and arrays. They have | |
601 | been extended to be multi-character names that start with a letter and | |
602 | may contain letters, numbers and the underscore character. | |
603 | .IP last | |
604 | POSIX \fBbc\fR does not have a \fBlast\fR variable. Some implementations | |
605 | of \fBbc\fR use the period (.) in a similar way. | |
606 | .IP comparisons | |
607 | POSIX \fBbc\fR allows comparisons only in the if statement, the while | |
608 | statement, and the second expression of the for statement. Also, only | |
609 | one relational operation is allowed in each of those statements. | |
610 | .IP "if statement, else clause" | |
611 | POSIX \fBbc\fR does not have an else clause. | |
612 | .IP "for statement" | |
613 | POSIX \fBbc\fR requires all expressions to be present in the for statement. | |
614 | .IP "&&, ||, !" | |
615 | POSIX \fBbc\fR does not have the logical operators. | |
616 | .IP "read function" | |
617 | POSIX \fBbc\fR does not have a read function. | |
618 | .IP "print statement" | |
619 | POSIX \fBbc\fR does not have a print statement . | |
620 | .IP "continue statement" | |
621 | POSIX \fBbc\fR does not have a continue statement. | |
622 | .IP "array parameters" | |
623 | POSIX \fBbc\fR does not have array parameters. Other implementations | |
624 | of \fBbc\fR may have call by value array parameters. | |
625 | .IP "=+, =-, =*, =/, =%, =^" | |
626 | POSIX \fBbc\fR does not require these "old style" assignment operators to | |
627 | be defined. This version may allow these "old style" assignments. Use | |
628 | the limits statement to see if the installed version supports them. If | |
629 | it does support the "old style" assignment operators, the statement | |
630 | "a =- 1" will decrement \fBa\fR by 1 instead of setting \fBa\fR to the | |
631 | value -1. | |
632 | .IP "spaces in numbers" | |
633 | Other implementations of \fBbc\fR allow spaces in numbers. For example, | |
634 | "x=1 3" would assign the value 13 to the variable x. The same statement | |
635 | would cause a syntax error in this version of \fBbc\fR. | |
636 | .IP "errors and execution" | |
637 | This implementation varies from other implementations in terms of what | |
638 | code will be executed when syntax and other errors are found in the | |
639 | program. If a syntax error is found in a function definition, error | |
640 | recovery tries to find the beginning of a statement and continue to | |
641 | parse the function. Once a syntax error is found in the function, the | |
642 | function will not be callable and becomes undefined. | |
643 | Syntax errors in the interactive execution code will invalidate the | |
644 | current execution block. The execution block is terminated by an | |
645 | end of line that appears after a complete sequence of statements. | |
646 | For example, | |
647 | .nf | |
648 | .RS | |
649 | a = 1 | |
650 | b = 2 | |
651 | .RE | |
652 | .fi | |
653 | has two execution blocks and | |
654 | .nf | |
655 | .RS | |
656 | { a = 1 | |
657 | b = 2 } | |
658 | .RE | |
659 | .fi | |
660 | has one execution block. Any runtime error will terminate the execution | |
661 | of the current execution block. A runtime warning will not terminate the | |
662 | current execution block. | |
663 | .SS LIMITS | |
664 | The following are the limits currently in place for this | |
665 | .B bc | |
666 | processor. Some of them may have been changed by an installation. | |
667 | Use the limits statement to see the actual values. | |
668 | .IP BC_BASE_MAX | |
669 | The maximum output base is currently set at 999. The maximum input base | |
670 | is 16. | |
671 | .IP BC_DIM_MAX | |
672 | This is currently an arbitrary limit of 65535 as distributed. Your | |
673 | installation may be different. | |
674 | .IP BC_SCALE_MAX | |
675 | The number of digits after the decimal point is limited to INT_MAX digits. | |
676 | Also, the number of digits before the decimal point is limited to INT_MAX | |
677 | digits. | |
678 | .IP BC_STRING_MAX | |
679 | The limit on the number of characters in a string is INT_MAX characters. | |
680 | .IP exponent | |
681 | The value of the exponent in the raise operation (^) is limited to LONG_MAX. | |
682 | .IP multiply | |
683 | The multiply routine may yield incorrect results if a number | |
684 | has more than LONG_MAX / 90 total digits. For 32 bit longs, this number is | |
685 | 23,860,929 digits. | |
686 | .IP "code size" | |
687 | Each function and the "main" program are limited to 10240 bytes of | |
688 | compiled byte code each. This limit (BC_MAX_SEGS) can be easily changed | |
689 | to have more than 10 segments of 1024 bytes. | |
690 | .IP "variable names" | |
691 | The current limit on the number of unique names is 32767 for each of | |
692 | simple variables, arrays and functions. | |
693 | .SH FILES | |
694 | In most installations, \fBbc\fR is completely self-contained. | |
695 | Where executable size is of importance or the C compiler does | |
696 | not deal with very long strings, \fBbc\fR will read | |
697 | the standard math library from the file /usr/local/lib/libmath.b. | |
698 | (The actual location may vary. It may be /lib/libmath.b.) | |
699 | .SH DIAGNOSTICS | |
700 | If any file on the command line can not be opened, \fBbc\fR will report | |
701 | that the file is unavailable and terminate. Also, there are compile | |
702 | and run time diagnostics that should be self-explanatory. | |
703 | .SH BUGS | |
704 | Error recovery is not very good yet. | |
705 | .SH AUTHOR | |
706 | .nf | |
707 | Philip A. Nelson | |
708 | phil@cs.wwu.edu | |
709 | .fi | |
710 | .SH ACKNOWLEDGEMENTS | |
711 | The author would like to thank Steve Sommars (sesv@iwtsf.att.com) for | |
712 | his extensive help in testing the implementation. Many great suggestions | |
713 | were given. This is a much better product due to his involvement. |