version 1, including all changes.
.
Rev |
Author |
# |
Line |
1 |
perry |
1 |
bc |
|
|
2 |
!!!bc |
|
|
3 |
NAME |
|
|
4 |
SYNTAX |
|
|
5 |
VERSION |
|
|
6 |
DESCRIPTION |
|
|
7 |
ENVIRONMENT VARIABLES |
|
|
8 |
DIAGNOSTICS |
|
|
9 |
BUGS |
|
|
10 |
AUTHOR |
|
|
11 |
ACKNOWLEDGEMENTS |
|
|
12 |
---- |
|
|
13 |
!!NAME |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
bc - An arbitrary precision calculator language |
|
|
17 |
!!SYNTAX |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
__bc__ [[ __-hlwsqv__ ] [[long-options] [[ ''file |
|
|
21 |
...'' ] |
|
|
22 |
!!VERSION |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
This man page documents GNU bc version 1.06. |
|
|
26 |
!!DESCRIPTION |
|
|
27 |
|
|
|
28 |
|
|
|
29 |
__bc__ is a language that supports arbitrary precision |
|
|
30 |
numbers with interactive execution of statements. There are |
|
|
31 |
some similarities in the syntax to the C programming |
|
|
32 |
language. A standard math library is available by command |
|
|
33 |
line option. If requested, the math library is defined |
|
|
34 |
before processing any files. __bc__ starts by processing |
|
|
35 |
code from all the files listed on the command line in the |
|
|
36 |
order listed. After all files have been processed, __bc__ |
|
|
37 |
reads from the standard input. All code is executed as it is |
|
|
38 |
read. (If a file contains a command to halt the processor, |
|
|
39 |
__bc__ will never read from the standard |
|
|
40 |
input.) |
|
|
41 |
|
|
|
42 |
|
|
|
43 |
This version of __bc__ contains several extensions beyond |
|
|
44 |
traditional __bc__ implementations and the POSIX draft |
|
|
45 |
standard. Command line options can cause these extensions to |
|
|
46 |
print a warning or to be rejected. This document describes |
|
|
47 |
the language accepted by this processor. Extensions will be |
|
|
48 |
identified as such. |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
__OPTIONS__ |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
-h, --help |
|
|
55 |
|
|
|
56 |
|
|
|
57 |
Print the usage and exit. |
|
|
58 |
|
|
|
59 |
|
|
|
60 |
-i, --interactive |
|
|
61 |
|
|
|
62 |
|
|
|
63 |
Force interactive mode. |
|
|
64 |
|
|
|
65 |
|
|
|
66 |
-l, --mathlib |
|
|
67 |
|
|
|
68 |
|
|
|
69 |
Define the standard math library. |
|
|
70 |
|
|
|
71 |
|
|
|
72 |
-w, --warn |
|
|
73 |
|
|
|
74 |
|
|
|
75 |
Give warnings for extensions to POSIX |
|
|
76 |
__bc__. |
|
|
77 |
|
|
|
78 |
|
|
|
79 |
-s, --standard |
|
|
80 |
|
|
|
81 |
|
|
|
82 |
Process exactly the POSIX __bc__ language. |
|
|
83 |
|
|
|
84 |
|
|
|
85 |
-q, --quiet |
|
|
86 |
|
|
|
87 |
|
|
|
88 |
Do not print the normal GNU bc welcome. |
|
|
89 |
|
|
|
90 |
|
|
|
91 |
-v, --version |
|
|
92 |
|
|
|
93 |
|
|
|
94 |
Print the version number and copyright and |
|
|
95 |
quit. |
|
|
96 |
|
|
|
97 |
|
|
|
98 |
__NUMBERS__ |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
The most basic element in __bc__ is the number. Numbers |
|
|
102 |
are arbitrary precision numbers. This precision is both in |
|
|
103 |
the integer part and the fractional part. All numbers are |
|
|
104 |
represented internally in decimal and all computation is |
|
|
105 |
done in decimal. (This version truncates results from divide |
|
|
106 |
and multiply operations.) There are two attributes of |
|
|
107 |
numbers, the length and the scale. The length is the total |
|
|
108 |
number of significant decimal digits in a number and the |
|
|
109 |
scale is the total number of decimal digits after the |
|
|
110 |
decimal point. For example: |
|
|
111 |
|
|
|
112 |
|
|
|
113 |
.000001 has a length of 6 and scale of 6. |
|
|
114 |
1935.000 has a length of 7 and a scale of 3. |
|
|
115 |
|
|
|
116 |
|
|
|
117 |
__VARIABLES__ |
|
|
118 |
|
|
|
119 |
|
|
|
120 |
Numbers are stored in two types of variables, simple |
|
|
121 |
variables and arrays. Both simple variables and array |
|
|
122 |
variables are named. Names begin with a letter followed by |
|
|
123 |
any number of letters, digits and underscores. All letters |
|
|
124 |
must be lower case. (Full alpha-numeric names are an |
|
|
125 |
extension. In POSIX __bc__ all names are a single lower |
|
|
126 |
case letter.) The type of variable is clear by the context |
|
|
127 |
because all array variable names will be followed by |
|
|
128 |
brackets ([[]). |
|
|
129 |
|
|
|
130 |
|
|
|
131 |
There are four special variables, __scale, ibase, |
|
|
132 |
obase,__ and __last__. __scale__ defines how some |
|
|
133 |
operations use digits after the decimal point. The default |
|
|
134 |
value of __scale__ is 0. __ibase__ and __obase__ |
|
|
135 |
define the conversion base for input and output numbers. The |
|
|
136 |
default for both input and output is base 10. __last__ |
|
|
137 |
(an extension) is a variable that has the value of the last |
|
|
138 |
printed number. These will be discussed in further detail |
|
|
139 |
where appropriate. All of these variables may have values |
|
|
140 |
assigned to them as well as used in |
|
|
141 |
expressions. |
|
|
142 |
|
|
|
143 |
|
|
|
144 |
__COMMENTS__ |
|
|
145 |
|
|
|
146 |
|
|
|
147 |
Comments in __bc__ start with the characters __/*__ |
|
|
148 |
and end with the characters __*/__. Comments may start |
|
|
149 |
anywhere and appear as a single space in the input. (This |
|
|
150 |
causes comments to delimit other input items. For example, a |
|
|
151 |
comment can not be found in the middle of a variable name.) |
|
|
152 |
Comments include any newlines (end of line) between the |
|
|
153 |
start and the end of the comment. |
|
|
154 |
|
|
|
155 |
|
|
|
156 |
To support the use of scripts for __bc__, a single line |
|
|
157 |
comment has been added as an extension. A single line |
|
|
158 |
comment starts at a __#__ character and continues to the |
|
|
159 |
next end of the line. The end of line character is not part |
|
|
160 |
of the comment and is processed normally. |
|
|
161 |
|
|
|
162 |
|
|
|
163 |
__EXPRESSIONS__ |
|
|
164 |
|
|
|
165 |
|
|
|
166 |
The numbers are manipulated by expressions and statements. |
|
|
167 |
Since the language was designed to be interactive, |
|
|
168 |
statements and expressions are executed as soon as possible. |
|
|
169 |
There is no |
|
|
170 |
|
|
|
171 |
|
|
|
172 |
A simple expression is just a constant. __bc__ converts |
|
|
173 |
constants into internal decimal numbers using the current |
|
|
174 |
input base, specified by the variable __ibase__. (There |
|
|
175 |
is an exception in functions.) The legal values of |
|
|
176 |
__ibase__ are 2 through 16. Assigning a value outside |
|
|
177 |
this range to __ibase__ will result in a value of 2 or |
|
|
178 |
16. Input numbers may contain the characters 0-9 and A-F. |
|
|
179 |
(Note: They must be capitals. Lower case letters are |
|
|
180 |
variable names.) Single digit numbers always have the value |
|
|
181 |
of the digit regardless of the value of __ibase__. (i.e. |
|
|
182 |
A = 10.) For multi-digit numbers, __bc__ changes all |
|
|
183 |
input digits greater or equal to ibase to the value of |
|
|
184 |
__ibase__-1. This makes the number __FFF__ always be |
|
|
185 |
the largest 3 digit number of the input base. |
|
|
186 |
|
|
|
187 |
|
|
|
188 |
Full expressions are similar to many other high level |
|
|
189 |
languages. Since there is only one kind of number, there are |
|
|
190 |
no rules for mixing types. Instead, there are rules on the |
|
|
191 |
scale of expressions. Every expression has a scale. This is |
|
|
192 |
derived from the scale of original numbers, the operation |
|
|
193 |
performed and in many cases, the value of the variable |
|
|
194 |
__scale__. Legal values of the variable __scale__ are |
|
|
195 |
0 to the maximum number representable by a C |
|
|
196 |
integer. |
|
|
197 |
|
|
|
198 |
|
|
|
199 |
In the following descriptions of legal expressions, |
|
|
200 |
|
|
|
201 |
|
|
|
202 |
''name'' |
|
|
203 |
|
|
|
204 |
|
|
|
205 |
and an array variable is specified as |
|
|
206 |
|
|
|
207 |
|
|
|
208 |
''name''[[''expr''] |
|
|
209 |
|
|
|
210 |
|
|
|
211 |
Unless specifically mentioned the scale of the result is the |
|
|
212 |
maximum scale of the expressions involved. |
|
|
213 |
|
|
|
214 |
|
|
|
215 |
- expr |
|
|
216 |
|
|
|
217 |
|
|
|
218 |
The result is the negation of the expression. |
|
|
219 |
|
|
|
220 |
|
|
|
221 |
++ var |
|
|
222 |
|
|
|
223 |
|
|
|
224 |
The variable is incremented by one and the new value is the |
|
|
225 |
result of the expression. |
|
|
226 |
|
|
|
227 |
|
|
|
228 |
-- var |
|
|
229 |
|
|
|
230 |
|
|
|
231 |
The variable is decremented by one and the new value is the |
|
|
232 |
result of the expression. |
|
|
233 |
|
|
|
234 |
|
|
|
235 |
var ++ |
|
|
236 |
|
|
|
237 |
|
|
|
238 |
The result of the expression is the value of the variable |
|
|
239 |
and then the variable is incremented by one. |
|
|
240 |
|
|
|
241 |
|
|
|
242 |
var -- |
|
|
243 |
|
|
|
244 |
|
|
|
245 |
The result of the expression is the value of the variable |
|
|
246 |
and then the variable is decremented by one. |
|
|
247 |
|
|
|
248 |
|
|
|
249 |
expr + expr |
|
|
250 |
|
|
|
251 |
|
|
|
252 |
The result of the expression is the sum of the two |
|
|
253 |
expressions. |
|
|
254 |
|
|
|
255 |
|
|
|
256 |
expr - expr |
|
|
257 |
|
|
|
258 |
|
|
|
259 |
The result of the expression is the difference of the two |
|
|
260 |
expressions. |
|
|
261 |
|
|
|
262 |
|
|
|
263 |
expr * expr |
|
|
264 |
|
|
|
265 |
|
|
|
266 |
The result of the expression is the product of the two |
|
|
267 |
expressions. |
|
|
268 |
|
|
|
269 |
|
|
|
270 |
expr / expr |
|
|
271 |
|
|
|
272 |
|
|
|
273 |
The result of the expression is the quotient of the two |
|
|
274 |
expressions. The scale of the result is the value of the |
|
|
275 |
variable __scale__. |
|
|
276 |
|
|
|
277 |
|
|
|
278 |
expr % expr |
|
|
279 |
|
|
|
280 |
|
|
|
281 |
The result of the expression is the |
|
|
282 |
scale__ digits. That result is |
|
|
283 |
used to compute a-(a/b)*b to the scale of the maximum of |
|
|
284 |
__scale__+scale(b) and scale(a). If __scale__ is set |
|
|
285 |
to zero and both expressions are integers this expression is |
|
|
286 |
the integer remainder function. |
|
|
287 |
|
|
|
288 |
|
|
|
289 |
expr ^ expr |
|
|
290 |
|
|
|
291 |
|
|
|
292 |
The result of the expression is the value of the first |
|
|
293 |
raised to the second. The second expression must be an |
|
|
294 |
integer. (If the second expression is not an integer, a |
|
|
295 |
warning is generated and the expression is truncated to get |
|
|
296 |
an integer value.) The scale of the result is __scale__ |
|
|
297 |
if the exponent is negative. If the exponent is positive the |
|
|
298 |
scale of the result is the minimum of the scale of the first |
|
|
299 |
expression times the value of the exponent and the maximum |
|
|
300 |
of __scale__ and the scale of the first expression. (e.g. |
|
|
301 |
scale(a^b) = min(scale(a)*b, max( __scale,__ scale(a))).) |
|
|
302 |
It should be noted that expr^0 will always return the value |
|
|
303 |
of 1. |
|
|
304 |
|
|
|
305 |
|
|
|
306 |
( expr ) |
|
|
307 |
|
|
|
308 |
|
|
|
309 |
This alters the standard precedence to force the evaluation |
|
|
310 |
of the expression. |
|
|
311 |
|
|
|
312 |
|
|
|
313 |
var = expr |
|
|
314 |
|
|
|
315 |
|
|
|
316 |
The variable is assigned the value of the |
|
|
317 |
expression. |
|
|
318 |
|
|
|
319 |
|
|
|
320 |
var |
|
|
321 |
|
|
|
322 |
|
|
|
323 |
This is equivalent to |
|
|
324 |
|
|
|
325 |
|
|
|
326 |
Relational expressions are a special kind of expression that |
|
|
327 |
always evaluate to 0 or 1, 0 if the relation is false and 1 |
|
|
328 |
if the relation is true. These may appear in any legal |
|
|
329 |
expression. (POSIX bc requires that relational expressions |
|
|
330 |
are used only in if, while, and for statements and that only |
|
|
331 |
one relational test may be done in them.) The relational |
|
|
332 |
operators are |
|
|
333 |
|
|
|
334 |
|
|
|
335 |
expr1 |
|
|
336 |
|
|
|
337 |
|
|
|
338 |
The result is 1 if expr1 is strictly less than |
|
|
339 |
expr2. |
|
|
340 |
|
|
|
341 |
|
|
|
342 |
expr1 |
|
|
343 |
|
|
|
344 |
|
|
|
345 |
The result is 1 if expr1 is less than or equal to |
|
|
346 |
expr2. |
|
|
347 |
|
|
|
348 |
|
|
|
349 |
expr1 |
|
|
350 |
|
|
|
351 |
|
|
|
352 |
The result is 1 if expr1 is strictly greater than |
|
|
353 |
expr2. |
|
|
354 |
|
|
|
355 |
|
|
|
356 |
expr1 |
|
|
357 |
|
|
|
358 |
|
|
|
359 |
The result is 1 if expr1 is greater than or equal to |
|
|
360 |
expr2. |
|
|
361 |
|
|
|
362 |
|
|
|
363 |
expr1 == expr2 |
|
|
364 |
|
|
|
365 |
|
|
|
366 |
The result is 1 if expr1 is equal to expr2. |
|
|
367 |
|
|
|
368 |
|
|
|
369 |
expr1 != expr2 |
|
|
370 |
|
|
|
371 |
|
|
|
372 |
The result is 1 if expr1 is not equal to expr2. |
|
|
373 |
|
|
|
374 |
|
|
|
375 |
Boolean operations are also legal. (POSIX __bc__ does NOT |
|
|
376 |
have boolean operations). The result of all boolean |
|
|
377 |
operations are 0 and 1 (for false and true) as in relational |
|
|
378 |
expressions. The boolean operators are: |
|
|
379 |
|
|
|
380 |
|
|
|
381 |
!expr |
|
|
382 |
|
|
|
383 |
|
|
|
384 |
The result is 1 if expr is 0. |
|
|
385 |
|
|
|
386 |
|
|
|
387 |
expr |
|
|
388 |
|
|
|
389 |
|
|
|
390 |
The result is 1 if both expressions are |
|
|
391 |
non-zero. |
|
|
392 |
|
|
|
393 |
|
|
|
394 |
expr || expr |
|
|
395 |
|
|
|
396 |
|
|
|
397 |
The result is 1 if either expression is |
|
|
398 |
non-zero. |
|
|
399 |
|
|
|
400 |
|
|
|
401 |
The expression precedence is as follows: (lowest to |
|
|
402 |
highest) |
|
|
403 |
|
|
|
404 |
|
|
|
405 |
|| operator, left associative |
|
|
406 |
This precedence was chosen so that POSIX compliant __bc__ programs will run correctly. This will cause the use of the relational and logical operators to have some unusual behavior when used with assignment expressions. Consider the expression: |
|
|
407 |
|
|
|
408 |
|
|
|
409 |
a = 3 |
|
|
410 |
|
|
|
411 |
|
|
|
412 |
Most C programmers would assume this would assign the result |
|
|
413 |
of |
|
|
414 |
bc__ is assign the |
|
|
415 |
value 3 to the variable |
|
|
416 |
__ |
|
|
417 |
|
|
|
418 |
|
|
|
419 |
There are a few more special expressions that are provided |
|
|
420 |
in __bc__. These have to do with user defined functions |
|
|
421 |
and standard functions. They all appear as |
|
|
422 |
__name''__(__''parameters''__)__ |
|
|
423 |
__ |
|
|
424 |
|
|
|
425 |
|
|
|
426 |
length ( expression ) |
|
|
427 |
|
|
|
428 |
|
|
|
429 |
The value of the length function is the number of |
|
|
430 |
significant digits in the expression. |
|
|
431 |
|
|
|
432 |
|
|
|
433 |
read ( ) |
|
|
434 |
|
|
|
435 |
|
|
|
436 |
The read function (an extension) will read a number from the |
|
|
437 |
standard input, regardless of where the function occurs. |
|
|
438 |
Beware, this can cause problems with the mixing of data and |
|
|
439 |
program in the standard input. The best use for this |
|
|
440 |
function is in a previously written program that needs input |
|
|
441 |
from the user, but never allows program code to be input |
|
|
442 |
from the user. The value of the read function is the number |
|
|
443 |
read from the standard input using the current value of the |
|
|
444 |
variable __ibase__ for the conversion base. |
|
|
445 |
|
|
|
446 |
|
|
|
447 |
scale ( expression ) |
|
|
448 |
|
|
|
449 |
|
|
|
450 |
The value of the scale function is the number of digits |
|
|
451 |
after the decimal point in the expression. |
|
|
452 |
|
|
|
453 |
|
|
|
454 |
sqrt ( expression ) |
|
|
455 |
|
|
|
456 |
|
|
|
457 |
The value of the sqrt function is the square root of the |
|
|
458 |
expression. If the expression is negative, a run time error |
|
|
459 |
is generated. |
|
|
460 |
|
|
|
461 |
|
|
|
462 |
__STATEMENTS__ |
|
|
463 |
|
|
|
464 |
|
|
|
465 |
Statements (as in most algebraic languages) provide the |
|
|
466 |
sequencing of expression evaluation. In __bc__ statements |
|
|
467 |
are executed |
|
|
468 |
__bc__. In fact, both a |
|
|
469 |
semicolon and a newline are used as statement separators. An |
|
|
470 |
improperly placed newline will cause a syntax error. Because |
|
|
471 |
newlines are statement separators, it is possible to hide a |
|
|
472 |
newline by using the backslash character. The sequence |
|
|
473 |
__bc__ as whitespace instead of a newline. A |
|
|
474 |
statement list is a series of statements separated by |
|
|
475 |
semicolons and newlines. The following is a list of |
|
|
476 |
__bc__ statements and what they do: (Things enclosed in |
|
|
477 |
brackets ([[]) are optional parts of the |
|
|
478 |
statement.) |
|
|
479 |
|
|
|
480 |
|
|
|
481 |
expression |
|
|
482 |
|
|
|
483 |
|
|
|
484 |
This statement does one of two things. If the expression |
|
|
485 |
starts with |
|
|
486 |
obase__. The legal |
|
|
487 |
values for __obase__ are 2 through BC_BASE_MAX. (See the |
|
|
488 |
section LIMITS.) For bases 2 through 16, the usual method of |
|
|
489 |
writing numbers is used. For bases greater than 16, |
|
|
490 |
__bc__ uses a multi-character digit method of printing |
|
|
491 |
the numbers where each higher base digit is printed as a |
|
|
492 |
base 10 number. The multi-character digits are separated by |
|
|
493 |
spaces. Each digit contains the number of characters |
|
|
494 |
required to represent the base ten value of |
|
|
495 |
__bc__, printing a number |
|
|
496 |
causes the side effect of assigning the printed value to the |
|
|
497 |
special variable __last__. This allows the user to |
|
|
498 |
recover the last value printed without having to retype the |
|
|
499 |
expression that printed the number. Assigning to __last__ |
|
|
500 |
is legal and will overwrite the last printed value with the |
|
|
501 |
assigned value. The newly assigned value will remain until |
|
|
502 |
the next number is printed or another value is assigned to |
|
|
503 |
__last__. (Some installations may allow the use of a |
|
|
504 |
single period (.) which is not part of a number as a short |
|
|
505 |
hand notation for for __last__.) |
|
|
506 |
|
|
|
507 |
|
|
|
508 |
string |
|
|
509 |
|
|
|
510 |
|
|
|
511 |
The string is printed to the output. Strings start with a |
|
|
512 |
double quote character and contain all characters until the |
|
|
513 |
next double quote character. All characters are take |
|
|
514 |
literally, including any newline. No newline character is |
|
|
515 |
printed after the string. |
|
|
516 |
|
|
|
517 |
|
|
|
518 |
__print__ list |
|
|
519 |
|
|
|
520 |
|
|
|
521 |
The print statement (an extension) provides another method |
|
|
522 |
of output. The |
|
|
523 |
last__. Strings in |
|
|
524 |
the print statement are printed to the output and may |
|
|
525 |
contain special characters. Special characters start with |
|
|
526 |
the backslash character (). The special characters |
|
|
527 |
recognized by __bc__ are |
|
|
528 |
__ |
|
|
529 |
|
|
|
530 |
|
|
|
531 |
{ statement_list } |
|
|
532 |
|
|
|
533 |
|
|
|
534 |
This is the compound statement. It allows multiple |
|
|
535 |
statements to be grouped together for |
|
|
536 |
execution. |
|
|
537 |
|
|
|
538 |
|
|
|
539 |
__if__ ( expression ) statement1 [[__else__ |
|
|
540 |
statement2] |
|
|
541 |
|
|
|
542 |
|
|
|
543 |
The if statement evaluates the expression and executes |
|
|
544 |
statement1 or statement2 depending on the value of the |
|
|
545 |
expression. If the expression is non-zero, statement1 is |
|
|
546 |
executed. If statement2 is present and the value of the |
|
|
547 |
expression is 0, then statement2 is executed. (The else |
|
|
548 |
clause is an extension.) |
|
|
549 |
|
|
|
550 |
|
|
|
551 |
__while__ ( expression ) statement |
|
|
552 |
|
|
|
553 |
|
|
|
554 |
The while statement will execute the statement while the |
|
|
555 |
expression is non-zero. It evaluates the expression before |
|
|
556 |
each execution of the statement. Termination of the loop is |
|
|
557 |
caused by a zero expression value or the execution of a |
|
|
558 |
break statement. |
|
|
559 |
|
|
|
560 |
|
|
|
561 |
__for__ ( [[expression1] ; [[expression2] ; [[expression3] ) |
|
|
562 |
statement |
|
|
563 |
|
|
|
564 |
|
|
|
565 |
The for statement controls repeated execution of the |
|
|
566 |
statement. Expression1 is evaluated before the loop. |
|
|
567 |
Expression2 is evaluated before each execution of the |
|
|
568 |
statement. If it is non-zero, the statement is evaluated. If |
|
|
569 |
it is zero, the loop is terminated. After each execution of |
|
|
570 |
the statement, expression3 is evaluated before the |
|
|
571 |
reevaluation of expression2. If expression1 or expression3 |
|
|
572 |
are missing, nothing is evaluated at the point they would be |
|
|
573 |
evaluated. If expression2 is missing, it is the same as |
|
|
574 |
substituting the value 1 for expression2. (The optional |
|
|
575 |
expressions are an extension. POSIX __bc__ requires all |
|
|
576 |
three expressions.) The following is equivalent code for the |
|
|
577 |
for statement: |
|
|
578 |
|
|
|
579 |
|
|
|
580 |
expression1; |
|
|
581 |
while (expression2) { |
|
|
582 |
statement; |
|
|
583 |
expression3; |
|
|
584 |
} |
|
|
585 |
|
|
|
586 |
|
|
|
587 |
__break__ |
|
|
588 |
|
|
|
589 |
|
|
|
590 |
This statement causes a forced exit of the most recent |
|
|
591 |
enclosing while statement or for statement. |
|
|
592 |
|
|
|
593 |
|
|
|
594 |
__continue__ |
|
|
595 |
|
|
|
596 |
|
|
|
597 |
The continue statement (an extension) causes the most recent |
|
|
598 |
enclosing for statement to start the next |
|
|
599 |
iteration. |
|
|
600 |
|
|
|
601 |
|
|
|
602 |
__halt__ |
|
|
603 |
|
|
|
604 |
|
|
|
605 |
The halt statement (an extension) is an executed statement |
|
|
606 |
that causes the __bc__ processor to quit only when it is |
|
|
607 |
executed. For example, |
|
|
608 |
__bc__ to terminate because the halt is not |
|
|
609 |
executed. |
|
|
610 |
|
|
|
611 |
|
|
|
612 |
__return__ |
|
|
613 |
|
|
|
614 |
|
|
|
615 |
Return the value 0 from a function. (See the section on |
|
|
616 |
functions.) |
|
|
617 |
|
|
|
618 |
|
|
|
619 |
__return__ ( expression ) |
|
|
620 |
|
|
|
621 |
|
|
|
622 |
Return the value of the expression from a function. (See the |
|
|
623 |
section on functions.) As an extension, the parenthesis are |
|
|
624 |
not required. |
|
|
625 |
|
|
|
626 |
|
|
|
627 |
__PSEUDO STATEMENTS__ |
|
|
628 |
|
|
|
629 |
|
|
|
630 |
These statements are not statements in the traditional |
|
|
631 |
sense. They are not executed statements. Their function is |
|
|
632 |
performed at |
|
|
633 |
|
|
|
634 |
|
|
|
635 |
__limits__ |
|
|
636 |
|
|
|
637 |
|
|
|
638 |
Print the local limits enforced by the local version of |
|
|
639 |
__bc__. This is an extension. |
|
|
640 |
|
|
|
641 |
|
|
|
642 |
__quit__ |
|
|
643 |
|
|
|
644 |
|
|
|
645 |
When the quit statement is read, the __bc__ processor is |
|
|
646 |
terminated, regardless of where the quit statement is found. |
|
|
647 |
For example, |
|
|
648 |
__bc__ to terminate. |
|
|
649 |
|
|
|
650 |
|
|
|
651 |
__warranty__ |
|
|
652 |
|
|
|
653 |
|
|
|
654 |
Print a longer warranty notice. This is an |
|
|
655 |
extension. |
|
|
656 |
|
|
|
657 |
|
|
|
658 |
__FUNCTIONS__ |
|
|
659 |
|
|
|
660 |
|
|
|
661 |
Functions provide a method of defining a computation that |
|
|
662 |
can be executed later. Functions in __bc__ always compute |
|
|
663 |
a value and return it to the caller. Function definitions |
|
|
664 |
are |
|
|
665 |
__ |
|
|
666 |
|
|
|
667 |
|
|
|
668 |
__ define__ ''name'' __(__ ''parameters'' __) {__ ''newline |
|
|
669 |
auto_list statement_list'' __} |
|
|
670 |
__A function call is just an expression of the form name''__(__''parameters''__)____ |
|
|
671 |
|
|
|
672 |
|
|
|
673 |
Parameters are numbers or arrays (an extension). In the |
|
|
674 |
function definition, zero or more parameters are defined by |
|
|
675 |
listing their names separated by commas. Numbers are only |
|
|
676 |
call by value parameters. Arrays are only call by variable. |
|
|
677 |
Arrays are specified in the parameter definition by the |
|
|
678 |
notation name''__[[]__ |
|
|
679 |
__ |
|
|
680 |
|
|
|
681 |
|
|
|
682 |
The ''auto_list'' is an optional list of variables that |
|
|
683 |
are for |
|
|
684 |
''auto__ ''name'', ... ; |
|
|
685 |
''name'' is the name of |
|
|
686 |
an auto variable. Arrays may be specified by using the same |
|
|
687 |
notation as used in parameters. These variables have their |
|
|
688 |
values pushed onto a stack at the start of the function. The |
|
|
689 |
variables are then initialized to zero and used throughout |
|
|
690 |
the execution of the function. At function exit, these |
|
|
691 |
variables are popped so that the original value (at the time |
|
|
692 |
of the function call) of these variables are restored. The |
|
|
693 |
parameters are really auto variables that are initialized to |
|
|
694 |
a value provided in the function call. Auto variables are |
|
|
695 |
different than traditional local variables because if |
|
|
696 |
function A calls function B, B may access function A's auto |
|
|
697 |
variables by just using the same name, unless function B has |
|
|
698 |
called them auto variables. Due to the fact that auto |
|
|
699 |
variables and parameters are pushed onto a stack, __bc__ |
|
|
700 |
supports recursive functions. |
|
|
701 |
|
|
|
702 |
|
|
|
703 |
The function body is a list of __bc__ statements. Again, |
|
|
704 |
statements are separated by semicolons or newlines. Return |
|
|
705 |
statements cause the termination of a function and the |
|
|
706 |
return of a value. There are two versions of the return |
|
|
707 |
statement. The first form, __return__ |
|
|
708 |
__return (__ ''expression'' |
|
|
709 |
__)__ |
|
|
710 |
__return (0)__ |
|
|
711 |
__ |
|
|
712 |
|
|
|
713 |
|
|
|
714 |
Functions also change the usage of the variable |
|
|
715 |
__ibase__. All constants in the function body will be |
|
|
716 |
converted using the value of __ibase__ at the time of the |
|
|
717 |
function call. Changes of __ibase__ will be ignored |
|
|
718 |
during the execution of the function except for the standard |
|
|
719 |
function __read__, which will always use the current |
|
|
720 |
value of __ibase__ for conversion of |
|
|
721 |
numbers. |
|
|
722 |
|
|
|
723 |
|
|
|
724 |
As an extension, the format of the definition has been |
|
|
725 |
slightly relaxed. The standard requires the opening brace be |
|
|
726 |
on the same line as the __define__ keyword and all other |
|
|
727 |
parts must be on following lines. This version of __bc__ |
|
|
728 |
will allow any number of newlines before and after the |
|
|
729 |
opening brace of the function. For example, the following |
|
|
730 |
definitions are legal. |
|
|
731 |
|
|
|
732 |
|
|
|
733 |
define d (n) { return (2*n); } |
|
|
734 |
define d (n) |
|
|
735 |
{ return (2*n); } |
|
|
736 |
|
|
|
737 |
|
|
|
738 |
__MATH LIBRARY__ |
|
|
739 |
|
|
|
740 |
|
|
|
741 |
If __bc__ is invoked with the __-l__ option, a math |
|
|
742 |
library is preloaded and the default scale is set to 20. The |
|
|
743 |
math functions will calculate their results to the scale set |
|
|
744 |
at the time of their call. The math library defines the |
|
|
745 |
following functions: |
|
|
746 |
|
|
|
747 |
|
|
|
748 |
s (''x'') |
|
|
749 |
|
|
|
750 |
|
|
|
751 |
The sine of x, x is in radians. |
|
|
752 |
|
|
|
753 |
|
|
|
754 |
c (''x'') |
|
|
755 |
|
|
|
756 |
|
|
|
757 |
The cosine of x, x is in radians. |
|
|
758 |
|
|
|
759 |
|
|
|
760 |
a (''x'') |
|
|
761 |
|
|
|
762 |
|
|
|
763 |
The arctangent of x, arctangent returns |
|
|
764 |
radians. |
|
|
765 |
|
|
|
766 |
|
|
|
767 |
l (''x'') |
|
|
768 |
|
|
|
769 |
|
|
|
770 |
The natural logarithm of x. |
|
|
771 |
|
|
|
772 |
|
|
|
773 |
e (''x'') |
|
|
774 |
|
|
|
775 |
|
|
|
776 |
The exponential function of raising e to the value |
|
|
777 |
x. |
|
|
778 |
|
|
|
779 |
|
|
|
780 |
j (''n,x'') |
|
|
781 |
|
|
|
782 |
|
|
|
783 |
The bessel function of integer order n of x. |
|
|
784 |
|
|
|
785 |
|
|
|
786 |
__EXAMPLES__ |
|
|
787 |
|
|
|
788 |
|
|
|
789 |
In /bin/sh, the following will assign the value of |
|
|
790 |
pi__. |
|
|
791 |
|
|
|
792 |
|
|
|
793 |
pi=$(echo |
|
|
794 |
|
|
|
795 |
|
|
|
796 |
The following is the definition of the exponential function |
|
|
797 |
used in the math library. This function is written in POSIX |
|
|
798 |
__bc__. |
|
|
799 |
|
|
|
800 |
|
|
|
801 |
scale = 20 |
|
|
802 |
/* Uses the fact that e^x = (e^(x/2))^2 |
|
|
803 |
When x is small enough, we use the series: |
|
|
804 |
e^x = 1 + x + x^2/2! + x^3/3! + ... |
|
|
805 |
*/ |
|
|
806 |
define e(x) { |
|
|
807 |
auto a, d, e, f, i, m, v, z |
|
|
808 |
/* Check the sign of x. */ |
|
|
809 |
if (x |
|
|
810 |
The following is code that uses the extended features of __bc__ to implement a simple program for calculating checkbook balances. This program is best kept in a file so that it can be used many times without having to retype it at every use. |
|
|
811 |
|
|
|
812 |
|
|
|
813 |
scale=2 |
|
|
814 |
print |
|
|
815 |
The following is the definition of the recursive factorial function. |
|
|
816 |
|
|
|
817 |
|
|
|
818 |
define f (x) { |
|
|
819 |
if (x |
|
|
820 |
|
|
|
821 |
|
|
|
822 |
__READLINE AND LIBEDIT OPTIONS__ |
|
|
823 |
|
|
|
824 |
|
|
|
825 |
GNU __bc__ can be compiled (via a configure option) to |
|
|
826 |
use the GNU __readline__ input editor library or the BSD |
|
|
827 |
__libedit__ library. This allows the user to do editing |
|
|
828 |
of lines before sending them to __bc__. It also allows |
|
|
829 |
for a history of previous lines typed. When this option is |
|
|
830 |
selected, __bc__ has one more special variable. This |
|
|
831 |
special variable, __history__ is the number of lines of |
|
|
832 |
history retained. For __readline__, a value of -1 means |
|
|
833 |
that an unlimited number of history lines are retained. |
|
|
834 |
Setting the value of __history__ to a positive number |
|
|
835 |
restricts the number of history lines to the number given. |
|
|
836 |
The value of 0 disables the history feature. The default |
|
|
837 |
value is 100. For more information, read the user manuals |
|
|
838 |
for the GNU __readline__, __history__ and BSD |
|
|
839 |
__libedit__ libraries. One can not enable both |
|
|
840 |
__readline__ and __libedit__ at the same |
|
|
841 |
time. |
|
|
842 |
|
|
|
843 |
|
|
|
844 |
__DIFFERENCES__ |
|
|
845 |
|
|
|
846 |
|
|
|
847 |
This version of __bc__ was implemented from the POSIX |
|
|
848 |
P1003.2/D11 draft and contains several differences and |
|
|
849 |
extensions relative to the draft and traditional |
|
|
850 |
implementations. It is not implemented in the traditional |
|
|
851 |
way using ''dc(1).'' This version is a single process |
|
|
852 |
which parses and runs a byte code translation of the |
|
|
853 |
program. There is an |
|
|
854 |
'' |
|
|
855 |
|
|
|
856 |
|
|
|
857 |
A major source of differences is extensions, where a feature |
|
|
858 |
is extended to add more functionality and additions, where |
|
|
859 |
new features are added. The following is the list of |
|
|
860 |
differences and extensions. |
|
|
861 |
|
|
|
862 |
|
|
|
863 |
LANG |
|
|
864 |
|
|
|
865 |
|
|
|
866 |
This version does not conform to the POSIX standard in the |
|
|
867 |
processing of the LANG environment variable and all |
|
|
868 |
environment variables starting with LC_. |
|
|
869 |
|
|
|
870 |
|
|
|
871 |
names |
|
|
872 |
|
|
|
873 |
|
|
|
874 |
Traditional and POSIX __bc__ have single letter names for |
|
|
875 |
functions, variables and arrays. They have been extended to |
|
|
876 |
be multi-character names that start with a letter and may |
|
|
877 |
contain letters, numbers and the underscore |
|
|
878 |
character. |
|
|
879 |
|
|
|
880 |
|
|
|
881 |
Strings |
|
|
882 |
|
|
|
883 |
|
|
|
884 |
Strings are not allowed to contain NUL characters. POSIX |
|
|
885 |
says all characters must be included in |
|
|
886 |
strings. |
|
|
887 |
|
|
|
888 |
|
|
|
889 |
last |
|
|
890 |
|
|
|
891 |
|
|
|
892 |
POSIX __bc__ does not have a __last__ variable. Some |
|
|
893 |
implementations of __bc__ use the period (.) in a similar |
|
|
894 |
way. |
|
|
895 |
|
|
|
896 |
|
|
|
897 |
comparisons |
|
|
898 |
|
|
|
899 |
|
|
|
900 |
POSIX __bc__ allows comparisons only in the if statement, |
|
|
901 |
the while statement, and the second expression of the for |
|
|
902 |
statement. Also, only one relational operation is allowed in |
|
|
903 |
each of those statements. |
|
|
904 |
|
|
|
905 |
|
|
|
906 |
if statement, else clause |
|
|
907 |
|
|
|
908 |
|
|
|
909 |
POSIX __bc__ does not have an else clause. |
|
|
910 |
|
|
|
911 |
|
|
|
912 |
for statement |
|
|
913 |
|
|
|
914 |
|
|
|
915 |
POSIX __bc__ requires all expressions to be present in |
|
|
916 |
the for statement. |
|
|
917 |
|
|
|
918 |
|
|
|
919 |
|
|
|
920 |
|
|
|
921 |
POSIX __bc__ does not have the logical |
|
|
922 |
operators. |
|
|
923 |
|
|
|
924 |
|
|
|
925 |
read function |
|
|
926 |
|
|
|
927 |
|
|
|
928 |
POSIX __bc__ does not have a read function. |
|
|
929 |
|
|
|
930 |
|
|
|
931 |
print statement |
|
|
932 |
|
|
|
933 |
|
|
|
934 |
POSIX __bc__ does not have a print statement |
|
|
935 |
. |
|
|
936 |
|
|
|
937 |
|
|
|
938 |
continue statement |
|
|
939 |
|
|
|
940 |
|
|
|
941 |
POSIX __bc__ does not have a continue |
|
|
942 |
statement. |
|
|
943 |
|
|
|
944 |
|
|
|
945 |
return statement |
|
|
946 |
|
|
|
947 |
|
|
|
948 |
POSIX __bc__ requires parentheses around the return |
|
|
949 |
expression. |
|
|
950 |
|
|
|
951 |
|
|
|
952 |
array parameters |
|
|
953 |
|
|
|
954 |
|
|
|
955 |
POSIX __bc__ does not (currently) support array |
|
|
956 |
parameters in full. The POSIX grammar allows for arrays in |
|
|
957 |
function definitions, but does not provide a method to |
|
|
958 |
specify an array as an actual parameter. (This is most |
|
|
959 |
likely an oversight in the grammar.) Traditional |
|
|
960 |
implementations of __bc__ have only call by value array |
|
|
961 |
parameters. |
|
|
962 |
|
|
|
963 |
|
|
|
964 |
function format |
|
|
965 |
|
|
|
966 |
|
|
|
967 |
POSIX __bc__ requires the opening brace on the same line |
|
|
968 |
as the __define__ key word and the __auto__ statement |
|
|
969 |
on the next line. |
|
|
970 |
|
|
|
971 |
|
|
|
972 |
=+, =-, =*, =/, =%, =^ |
|
|
973 |
|
|
|
974 |
|
|
|
975 |
POSIX __bc__ does not require these |
|
|
976 |
__a__ by 1 instead of setting __a__ to the value |
|
|
977 |
-1. |
|
|
978 |
|
|
|
979 |
|
|
|
980 |
spaces in numbers |
|
|
981 |
|
|
|
982 |
|
|
|
983 |
Other implementations of __bc__ allow spaces in numbers. |
|
|
984 |
For example, |
|
|
985 |
__bc__. |
|
|
986 |
|
|
|
987 |
|
|
|
988 |
errors and execution |
|
|
989 |
|
|
|
990 |
|
|
|
991 |
This implementation varies from other implementations in |
|
|
992 |
terms of what code will be executed when syntax and other |
|
|
993 |
errors are found in the program. If a syntax error is found |
|
|
994 |
in a function definition, error recovery tries to find the |
|
|
995 |
beginning of a statement and continue to parse the function. |
|
|
996 |
Once a syntax error is found in the function, the function |
|
|
997 |
will not be callable and becomes undefined. Syntax errors in |
|
|
998 |
the interactive execution code will invalidate the current |
|
|
999 |
execution block. The execution block is terminated by an end |
|
|
1000 |
of line that appears after a complete sequence of |
|
|
1001 |
statements. For example, |
|
|
1002 |
|
|
|
1003 |
|
|
|
1004 |
a = 1 |
|
|
1005 |
b = 2 |
|
|
1006 |
has two execution blocks and |
|
|
1007 |
|
|
|
1008 |
|
|
|
1009 |
{ a = 1 |
|
|
1010 |
b = 2 } |
|
|
1011 |
has one execution block. Any runtime error will terminate the execution of the current execution block. A runtime warning will not terminate the current execution block. |
|
|
1012 |
|
|
|
1013 |
|
|
|
1014 |
Interrupts |
|
|
1015 |
|
|
|
1016 |
|
|
|
1017 |
During an interactive session, the SIGINT signal (usually |
|
|
1018 |
generated by the control-C character from the terminal) will |
|
|
1019 |
cause execution of the current execution block to be |
|
|
1020 |
interrupted. It will display a |
|
|
1021 |
bc__ is ready for more input. |
|
|
1022 |
All previously defined functions remain defined and the |
|
|
1023 |
value of all non-auto variables are the value at the point |
|
|
1024 |
of interruption. All auto variables and function parameters |
|
|
1025 |
are removed during the clean up process. During a |
|
|
1026 |
non-interactive session, the SIGINT signal will terminate |
|
|
1027 |
the entire run of __bc__. |
|
|
1028 |
|
|
|
1029 |
|
|
|
1030 |
__LIMITS__ |
|
|
1031 |
|
|
|
1032 |
|
|
|
1033 |
The following are the limits currently in place for this |
|
|
1034 |
__bc__ processor. Some of them may have been changed by |
|
|
1035 |
an installation. Use the limits statement to see the actual |
|
|
1036 |
values. |
|
|
1037 |
|
|
|
1038 |
|
|
|
1039 |
BC_BASE_MAX |
|
|
1040 |
|
|
|
1041 |
|
|
|
1042 |
The maximum output base is currently set at 999. The maximum |
|
|
1043 |
input base is 16. |
|
|
1044 |
|
|
|
1045 |
|
|
|
1046 |
BC_DIM_MAX |
|
|
1047 |
|
|
|
1048 |
|
|
|
1049 |
This is currently an arbitrary limit of 65535 as |
|
|
1050 |
distributed. Your installation may be |
|
|
1051 |
different. |
|
|
1052 |
|
|
|
1053 |
|
|
|
1054 |
BC_SCALE_MAX |
|
|
1055 |
|
|
|
1056 |
|
|
|
1057 |
The number of digits after the decimal point is limited to |
|
|
1058 |
INT_MAX digits. Also, the number of digits before the |
|
|
1059 |
decimal point is limited to INT_MAX digits. |
|
|
1060 |
|
|
|
1061 |
|
|
|
1062 |
BC_STRING_MAX |
|
|
1063 |
|
|
|
1064 |
|
|
|
1065 |
The limit on the number of characters in a string is INT_MAX |
|
|
1066 |
characters. |
|
|
1067 |
|
|
|
1068 |
|
|
|
1069 |
exponent |
|
|
1070 |
|
|
|
1071 |
|
|
|
1072 |
The value of the exponent in the raise operation (^) is |
|
|
1073 |
limited to LONG_MAX. |
|
|
1074 |
|
|
|
1075 |
|
|
|
1076 |
variable names |
|
|
1077 |
|
|
|
1078 |
|
|
|
1079 |
The current limit on the number of unique names is 32767 for |
|
|
1080 |
each of simple variables, arrays and functions. |
|
|
1081 |
!!ENVIRONMENT VARIABLES |
|
|
1082 |
|
|
|
1083 |
|
|
|
1084 |
The following environment variables are processed by |
|
|
1085 |
__bc__: |
|
|
1086 |
|
|
|
1087 |
|
|
|
1088 |
POSIXLY_CORRECT |
|
|
1089 |
|
|
|
1090 |
|
|
|
1091 |
This is the same as the __-s__ option. |
|
|
1092 |
|
|
|
1093 |
|
|
|
1094 |
BC_ENV_ARGS |
|
|
1095 |
|
|
|
1096 |
|
|
|
1097 |
This is another mechanism to get arguments to __bc__. The |
|
|
1098 |
format is the same as the command line arguments. These |
|
|
1099 |
arguments are processed first, so any files listed in the |
|
|
1100 |
environent arguments are processed before any command line |
|
|
1101 |
argument files. This allows the user to set up |
|
|
1102 |
__bc__. The files in the environment |
|
|
1103 |
variables would typically contain function definitions for |
|
|
1104 |
functions the user wants defined every time __bc__ is |
|
|
1105 |
run. |
|
|
1106 |
|
|
|
1107 |
|
|
|
1108 |
BC_LINE_LENGTH |
|
|
1109 |
|
|
|
1110 |
|
|
|
1111 |
This should be an integer specifing the number of characters |
|
|
1112 |
in an output line for numbers. This includes the backslash |
|
|
1113 |
and newline characters for long numbers. |
|
|
1114 |
!!DIAGNOSTICS |
|
|
1115 |
|
|
|
1116 |
|
|
|
1117 |
If any file on the command line can not be opened, __bc__ |
|
|
1118 |
will report that the file is unavailable and terminate. |
|
|
1119 |
Also, there are compile and run time diagnostics that should |
|
|
1120 |
be self-explanatory. |
|
|
1121 |
!!BUGS |
|
|
1122 |
|
|
|
1123 |
|
|
|
1124 |
Error recovery is not very good yet. |
|
|
1125 |
|
|
|
1126 |
|
|
|
1127 |
Email bug reports to __bug-bc@gnu.org__. Be sure to |
|
|
1128 |
include the word ``bc'' somewhere in the ``Subject:'' |
|
|
1129 |
field. |
|
|
1130 |
!!AUTHOR |
|
|
1131 |
|
|
|
1132 |
|
|
|
1133 |
Philip A. Nelson |
|
|
1134 |
philnelson@acm.org |
|
|
1135 |
!!ACKNOWLEDGEMENTS |
|
|
1136 |
|
|
|
1137 |
|
|
|
1138 |
The author would like to thank Steve Sommars |
|
|
1139 |
(Steve.Sommars@att.com) for his extensive help in testing |
|
|
1140 |
the implementation. Many great suggestions were given. This |
|
|
1141 |
is a much better product due to his |
|
|
1142 |
involvement. |
|
|
1143 |
---- |