]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - share/doc/usd/06.bc/bc
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / share / doc / usd / 06.bc / bc
1 .\"     $FreeBSD$
2 .\"     $OpenBSD: bc,v 1.9 2004/07/09 10:23:05 jmc Exp $
3 .\"
4 .\" Copyright (C) Caldera International Inc.  2001-2002.
5 .\" All rights reserved.
6 .\"
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
9 .\" are met:
10 .\" 1. Redistributions of source code and documentation must retain the above
11 .\"    copyright notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\"    notice, this list of conditions and the following disclaimer in the
14 .\"    documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\"    must display the following acknowledgement:
17 .\"     This product includes software developed or owned by Caldera
18 .\"     International, Inc.
19 .\" 4. Neither the name of Caldera International, Inc. nor the names of other
20 .\"    contributors may be used to endorse or promote products derived from
21 .\"    this software without specific prior written permission.
22 .\"
23 .\" USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
24 .\" INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
25 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 .\" IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE FOR ANY DIRECT,
28 .\" INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 .\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 .\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33 .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 .\" POSSIBILITY OF SUCH DAMAGE.
35 .\"
36 .\"     @(#)bc  6.2 (Berkeley) 4/17/91
37 .\"
38 .if n \{\
39 .po 5n
40 .ll 70n
41 .\}
42 .EH 'USD:6-%''BC \- An Arbitrary Precision Desk-Calculator Language'
43 .OH 'BC \- An Arbitrary Precision Desk-Calculator Language''USD:6-%'
44 .\".RP
45 .TL
46 BC \- An Arbitrary Precision Desk-Calculator Language
47 .AU
48 Lorinda Cherry
49 .AU
50 Robert Morris
51 .AI
52 .\" .MH
53 .AB
54 BC is a language and a compiler for doing arbitrary precision arithmetic
55 on the PDP-11 under the
56 .UX
57 time-sharing
58 system.  The output of the compiler is interpreted and executed by
59 a collection of routines which can input, output, and do
60 arithmetic on indefinitely large integers and on scaled fixed-point
61 numbers.
62 .PP
63 These routines are themselves based on a dynamic storage allocator.
64 Overflow does not occur until all available core storage
65 is exhausted.
66 .PP
67 The language has a complete control structure as well as immediate-mode
68 operation.  Functions can be defined and saved for later execution.
69 .PP
70 Two five hundred-digit numbers can be multiplied to give a
71 thousand digit result in about ten seconds.
72 .PP
73 A small collection of library functions is also available,
74 including sin, cos, arctan, log, exponential, and Bessel functions of
75 integer order.
76 .PP
77 Some of the uses of this compiler are
78 .IP \-
79 to do computation with large integers,
80 .IP \-
81 to do computation accurate to many decimal places,
82 .IP \-
83 conversion of numbers from one base to another base.
84 .AE
85 .PP
86 .SH
87 Introduction
88 .PP
89 BC is a language and a compiler for doing arbitrary precision
90 arithmetic on the
91 .UX
92 time-sharing system [1].
93 The compiler was written to make conveniently available a
94 collection of routines (called DC [5]) which are capable of doing
95 arithmetic on integers of arbitrary size.  The compiler
96 is by no means intended to provide a complete programming
97 language.
98 It is a minimal language facility.
99 .PP
100 There is a scaling provision that permits the
101 use of decimal point notation.
102 Provision is made for input and output in bases other than
103 decimal.  Numbers can be converted from decimal to octal by
104 simply setting the output base to equal 8.
105 .PP
106 The actual limit on the number of digits that can
107 be handled depends on the amount of storage available on the machine.
108 Manipulation of numbers with many hundreds of digits
109 is possible even on the smallest versions of
110 .UX .
111 .PP
112 The syntax of BC has been deliberately selected to agree
113 substantially with the C language [2].  Those who
114 are familiar with C will find few surprises in this language.
115 .SH
116 Simple Computations with Integers
117 .PP
118 The simplest kind of statement is an arithmetic expression
119 on a line by itself.
120 For instance, if you type in the line:
121 .DS
122 .ft B
123 142857 + 285714
124 .ft P
125 .DE
126 the program responds immediately with the line
127 .DS
128 .ft B
129 428571
130 .ft P
131 .DE
132 The operators \-, *, /, %, and ^ can also be used; they
133 indicate subtraction, multiplication, division, remaindering, and
134 exponentiation, respectively.  Division of integers produces an
135 integer result truncated toward zero.
136 Division by zero produces an error
137 comment.
138 .PP
139 Any term in an expression may be prefixed by a minus sign to
140 indicate that it is to be negated (the `unary' minus sign).
141 The expression
142 .DS
143 .ft B
144 7+\-3
145 .ft P
146 .DE
147 is interpreted to mean that \-3 is to be added to 7.
148 .PP
149 More complex expressions with several operators and with
150 parentheses are interpreted just as in
151 Fortran, with ^ having the greatest binding
152 power, then * and % and /, and finally + and \-.
153 Contents of parentheses are evaluated before material
154 outside the parentheses.
155 Exponentiations are
156 performed from right to left and the other operators
157 from left to right.
158 The two expressions
159 .DS
160 .ft B
161 a^b^c  and  a^(b^c)
162 .ft P
163 .DE
164 are equivalent, as are the two expressions
165 .DS
166 .ft B
167 a*b*c  and  (a*b)*c
168 .ft P
169 .DE
170 BC shares with Fortran and C the undesirable convention that
171 .DS
172 \fBa/b*c\fP  is equivalent to  \fB(a/b)*c\fP
173 .ft P
174 .DE
175 .PP
176 Internal storage registers to hold numbers have single lower-case
177 letter names.  The value of an expression can be assigned to
178 a register in the usual way.  The statement
179 .DS
180 .ft B
181 x = x + 3
182 .ft P
183 .DE
184 has the effect of increasing by three the value of the contents of the
185 register named x.
186 When, as in this case, the outermost operator is an =, the
187 assignment is performed but the result is not printed.
188 Only 26 of these named storage registers are available.
189 .PP
190 There is a built-in square root function whose
191 result is truncated to an integer (but see scaling below).
192 The lines
193 .DS
194 .ft B
195 x = sqrt(191)
196 x
197 .ft P
198 .DE
199 produce the printed result
200 .DS
201 .ft B
202 13
203 .ft P
204 .DE
205 .SH
206 Bases
207 .PP
208 There are special internal quantities, called `ibase' and `obase'.
209 The contents of `ibase', initially set to 10,
210 determines the base used for interpreting numbers read in.
211 For example, the lines
212 .DS
213 .ft B
214 ibase = 8
215 11
216 .ft P
217 .DE
218 will produce the output line
219 .DS
220 .ft B
221 9
222 .ft P
223 .DE
224 and you are all set up to do octal to decimal conversions.
225 Beware, however of trying to change the input base back
226 to decimal by typing
227 .DS
228 .ft B
229 ibase = 10
230 .ft P
231 .DE
232 Because the number 10 is interpreted as octal, this statement will
233 have no effect.
234 For those who deal in hexadecimal notation,
235 the characters A\-F are permitted in numbers
236 (no matter what base is in effect)
237 and are
238 interpreted as digits having values 10\-15 respectively.
239 The statement
240 .DS
241 .ft B
242 ibase = A
243 .ft P
244 .DE
245 will change you back to decimal input base no matter what the
246 current input base is.
247 Negative and large positive input bases are
248 permitted but useless.
249 No mechanism has been provided for the input of arbitrary
250 numbers in bases less than 1 and greater than 16.
251 .PP
252 The contents of `obase', initially set to 10, are used as the base for output
253 numbers.  The lines
254 .DS
255 .ft B
256 obase = 16
257 1000
258 .ft P
259 .DE
260 will produce the output line
261 .DS
262 .ft B
263 3E8
264 .ft P
265 .DE
266 which is to be interpreted as a 3-digit hexadecimal number.
267 Very large output bases are permitted, and they are sometimes useful.
268 For example, large numbers can be output in groups of five digits
269 by setting `obase' to 100000.
270 Strange (i.e. 1, 0, or negative) output bases are
271 handled appropriately.
272 .PP
273 Very large numbers are split across lines with 70 characters per line.
274 Lines which are continued end with \\.
275 Decimal output conversion is practically instantaneous, but output
276 of very large numbers (i.e., more than 100 digits) with other bases
277 is rather slow.
278 Non-decimal output conversion of
279 a one hundred digit number takes about
280 three seconds.
281 .PP
282 It is best to remember that `ibase' and `obase' have no effect
283 whatever on the course of internal computation or
284 on the evaluation of expressions, but only affect input and
285 output conversion, respectively.
286 .SH
287 Scaling
288 .PP
289 A third special internal quantity called `scale' is
290 used to determine the scale of calculated
291 quantities.
292 Numbers may have
293 up to a specific number of decimal digits after the decimal point.
294 This fractional part is retained in further computations.
295 We refer to the number of digits after the decimal point of
296 a number as its scale.
297 The current implementation allows scales to be as large as can be
298 represented by a 32-bit unsigned number minus one.
299 This is a non-portable extension.
300 The original implementation allowed for a maximum scale of 99.
301 .PP
302 When two scaled numbers are combined by
303 means of one of the arithmetic operations, the result
304 has a scale determined by the following rules.  For
305 addition and subtraction, the scale of the result is the larger
306 of the scales of the two operands.  In this case,
307 there is never any truncation of the result.
308 For multiplications, the scale of the result is never
309 less than the maximum of the two scales of the operands,
310 never more than the sum of the scales of the operands
311 and, subject to those two restrictions,
312 the scale of the result is set equal to the contents of the internal
313 quantity `scale'.
314 The scale of a quotient is the contents of the internal
315 quantity `scale'.  The scale of a remainder is
316 the sum of the scales of the quotient and the divisor.
317 The result of an exponentiation is scaled as if
318 the implied multiplications were performed.
319 An exponent must be an integer.
320 The scale of a square root is set to the maximum of the scale
321 of the argument and the contents of `scale'.
322 .PP
323 All of the internal operations are actually carried out in terms
324 of integers, with digits being discarded when necessary.
325 In every case where digits are discarded, truncation and
326 not rounding is performed.
327 .PP
328 The contents of
329 `scale' must be no greater than
330 4294967294 and no less than 0.  It is initially set to 0.
331 .PP
332 The internal quantities `scale', `ibase', and `obase' can be
333 used in expressions just like other variables.
334 The line
335 .DS
336 .ft B
337 scale = scale + 1
338 .ft P
339 .DE
340 increases the value of `scale' by one, and the line
341 .DS
342 .ft B
343 scale
344 .ft P
345 .DE
346 causes the current value of `scale' to be printed.
347 .PP
348 The value of `scale' retains its meaning as a
349 number of decimal digits to be retained in internal
350 computation even when `ibase' or `obase' are not equal to 10.
351 The internal computations (which are still conducted in decimal,
352 regardless of the bases) are performed to the specified number
353 of decimal digits, never hexadecimal or octal or any
354 other kind of digits.
355 .SH
356 Functions
357 .PP
358 The name of a function is a single lower-case letter.
359 Function names are permitted to collide with simple
360 variable names.
361 Twenty-six different defined functions are permitted
362 in addition to the twenty-six variable names.
363 The line
364 .DS
365 .ft B
366         define a(x){
367 .ft P
368 .DE
369 begins the definition of a function with one argument.
370 This line must be followed by one or more statements,
371 which make up the body of the function, ending
372 with a right brace }.
373 Return of control from a function occurs when a return
374 statement is executed or when the end of the function is reached.
375 The return statement can take either
376 of the two forms
377 .DS
378 .ft B
379 return
380 return(x)
381 .ft P
382 .DE
383 In the first case, the value of the function is 0, and in
384 the second, the value of the expression in parentheses.
385 .PP
386 Variables used in the function can be declared as automatic
387 by a statement of the form
388 .DS
389 .ft B
390 auto x,y,z
391 .ft P
392 .DE
393 There can be only one `auto' statement in a function and it must
394 be the first statement in the definition.
395 These automatic variables are allocated space and initialized
396 to zero on entry to the function and thrown away on return.  The
397 values of any variables with the same names outside the function
398 are not disturbed.
399 Functions may be called recursively and the automatic variables
400 at each level of call are protected.
401 The parameters named in a function definition are treated in
402 the same way as the automatic variables of that function
403 with the single exception that they are given a value
404 on entry to the function.
405 An example of a function definition is
406 .DS
407 .ft B
408         define a(x,y){
409                 auto z
410                 z = x*y
411                 return(z)
412         }
413 .ft P
414 .DE
415 The value of this function, when called, will be the
416 product of its
417 two arguments.
418 .PP
419 A function is called by the appearance of its name
420 followed by a string of arguments enclosed in
421 parentheses and separated by commas.
422 The result
423 is unpredictable if the wrong number of arguments is used.
424 .PP
425 Functions with no arguments are defined and called using
426 parentheses with nothing between them: b().
427 .PP
428 If the function
429 .ft I
430 a
431 .ft
432 above has been defined, then the line
433 .DS
434 .ft B
435 a(7,3.14)
436 .ft P
437 .DE
438 would cause the result 21.98 to be printed and the line
439 .DS
440 .ft B
441 x = a(a(3,4),5)
442 .ft P
443 .DE
444 would cause the value of x to become 60.
445 .SH
446 Subscripted Variables
447 .PP
448 A single lower-case letter variable name
449 followed by an expression in brackets is called a subscripted
450 variable (an array element).
451 The variable name is called the array name and the expression
452 in brackets is called the subscript.
453 Only one-dimensional arrays are
454 permitted.  The names of arrays are permitted to
455 collide with the names of simple variables and function names.
456 Any fractional
457 part of a subscript is discarded before use.
458 Subscripts must be greater than or equal to zero and 
459 less than or equal to 2047.
460 .PP
461 Subscripted variables may be freely used in expressions, in
462 function calls, and in return statements.
463 .PP
464 An array name may be used as an argument to a function,
465 or may be declared as automatic in
466 a function definition by the use of empty brackets:
467 .DS
468 .ft B
469 f(a[\|])
470 define f(a[\|])
471 auto a[\|]
472 .ft P
473 .DE
474 When an array name is so used, the whole contents of the array
475 are copied for the use of the function, and thrown away on exit
476 from the function.
477 Array names which refer to whole arrays cannot be used
478 in any other contexts.
479 .SH
480 Control Statements
481 .PP
482 The `if', the `while', and the `for' statements
483 may be used to alter the flow within programs or to cause iteration.
484 The range of each of them is a statement or
485 a compound statement consisting of a collection of
486 statements enclosed in braces.
487 They are written in the following way
488 .DS
489 .ft B
490 if(relation) statement
491 if(relation) statement else statement
492 while(relation) statement
493 for(expression1; relation; expression2) statement
494 .ft P
495 .DE
496 or
497 .DS
498 .ft B
499 if(relation) {statements}
500 if(relation) {statements} else {statements}
501 while(relation) {statements}
502 for(expression1; relation; expression2) {statements}
503 .ft P
504 .DE
505 .PP
506 A relation in one of the control statements is an expression of the form
507 .DS
508 .ft B
509 x>y
510 .ft P
511 .DE
512 where  two expressions are related by one of the six relational
513 operators `<', `>', `<=', `>=', `==', or `!='.
514 The relation `=='
515 stands for `equal to' and `!=' stands for `not equal to'.
516 The meaning of the remaining relational operators is
517 clear.
518 .PP
519 BEWARE of using `=' instead of `==' in a relational.  Unfortunately,
520 both of them are legal, so you will not get a diagnostic
521 message, but `=' really will not do a comparison.
522 .PP
523 The `if' statement causes execution of its range
524 if and only if the relation is true.
525 Then control passes to the next statement in sequence.
526 If an `else' branch is present, the statements in this branch are
527 executed if the relation is false.
528 The `else' keyword is a non-portable extension.
529 .PP
530 The `while' statement causes execution of its range
531 repeatedly as long as the relation
532 is true.  The relation is tested before each execution
533 of its range and if the relation
534 is false, control passes to the next statement beyond the range
535 of the while.
536 .PP
537 The `for' statement begins
538 by executing `expression1'.  Then the relation is tested
539 and, if true, the statements in the range of the `for' are executed.
540 Then `expression2' is executed.  The relation is tested, and so on.
541 The typical use of the `for' statement is for a controlled iteration,
542 as in the statement
543 .DS
544 .ft B
545 for(i=1; i<=10; i=i+1) i
546 .ft P
547 .DE
548 which will print the integers from 1 to 10.
549 Here are some examples of the use of the control statements.
550 .DS
551 .ft B
552 define f(n){
553 auto i, x
554 x=1
555 for(i=1; i<=n; i=i+1) x=x*i
556 return(x)
557 }
558 .ft P
559 .DE
560 The line
561 .DS
562 .ft B
563         f(a)
564 .ft P
565 .DE
566 will print
567 .ft I
568 a
569 .ft
570 factorial if
571 .ft I
572 a
573 .ft
574 is a positive integer.
575 Here is the definition of a function which will
576 compute values of the binomial coefficient
577 (m and n are assumed to be positive integers).
578 .DS
579 .ft B
580 define b(n,m){
581 auto x, j
582 x=1
583 for(j=1; j<=m; j=j+1) x=x*(n\-j+1)/j
584 return(x)
585 }
586 .ft P
587 .DE
588 The following function computes values of the exponential function
589 by summing the appropriate series
590 without regard for possible truncation errors:
591 .DS
592 .ft B
593 scale = 20
594 define e(x){
595         auto a, b, c, d, n
596         a = 1
597         b = 1
598         c = 1
599         d = 0
600         n = 1
601         while(1==1){
602                 a = a*x
603                 b = b*n
604                 c = c + a/b
605                 n = n + 1
606                 if(c==d) return(c)
607                 d = c
608         }
609 }
610 .ft P
611 .DE
612 .SH
613 Some Details
614 .PP
615 There are some language features that every user should know
616 about even if he will not use them.
617 .PP
618 Normally statements are typed one to a line.  It is also permissible
619 to type several statements on a line separated by semicolons.
620 .PP
621 If an assignment statement is parenthesized, it then has
622 a value and it can be used anywhere that an expression can.
623 For example, the line
624 .DS
625 .ft B
626 (x=y+17)
627 .ft P
628 .DE
629 not only makes the indicated assignment, but also prints the
630 resulting value.
631 .PP
632 Here is an example of a use of the value of an
633 assignment statement even when it is not parenthesized.
634 .DS
635 .ft B
636 x = a[i=i+1]
637 .ft P
638 .DE
639 causes a value to be assigned to x and also increments i
640 before it is used as a subscript.
641 .PP
642 The following constructs work in BC in exactly the same manner
643 as they do in the C language.  Consult the appendix or the
644 C manuals [2] for their exact workings.
645 .DS
646 .ft B
647 .ta 2i
648 x=y=z  is the same as   x=(y=z)
649 x += y  x = x+y
650 x \-= y x = x\-y
651 x *= y  x = x*y
652 x /= y  x = x/y
653 x %= y  x = x%y
654 x ^= y  x = x^y
655 x++     (x=x+1)\-1
656 x\-\-   (x=x\-1)+1
657 ++x     x = x+1
658 \-\-x   x = x\-1
659 .ft P
660 .DE
661 Even if you don't intend to use the constructs,
662 if you type one inadvertently, something correct but unexpected
663 may happen.
664 .SH
665 Three Important Things
666 .PP
667 1.  To exit a BC program, type `quit'.
668 .PP
669 2. There is a comment convention identical to that of C and
670 of PL/I.  Comments begin with `/*' and end with `*/'.
671 As a non-portable extension, comments may also start with a `#' and end with
672 a newline.
673 The newline is not part of the comment.
674 .PP
675 3. There is a library of math functions which may be obtained by
676 typing at command level
677 .DS
678 .ft B
679 bc \-l
680 .ft P
681 .DE
682 This command will load a set of library functions
683 which, at the time of writing, consists of sine (named `s'),
684 cosine (`c'), arctangent (`a'), natural logarithm (`l'),
685 exponential (`e') and Bessel functions of integer order (`j(n,x)').  Doubtless more functions will be added
686 in time.
687 The library sets the scale to 20.  You can reset it to something
688 else if you like.
689 The design of these mathematical library routines
690 is discussed elsewhere [3].
691 .PP
692 If you type
693 .DS
694 .ft B
695 bc file ...
696 .ft P
697 .DE
698 BC will read and execute the named file or files before accepting
699 commands from the keyboard.  In this way, you may load your
700 favorite programs and function definitions.
701 .SH
702 Acknowledgement
703 .PP
704 The compiler is written in YACC [4]; its original
705 version  was written by S. C. Johnson.
706 .SH
707 References
708 .IP [1]
709 K. Thompson and D. M. Ritchie,
710 .ft I
711 UNIX Programmer's Manual,
712 .ft
713 Bell Laboratories,
714 1978.
715 .IP [2]
716 B. W. Kernighan and
717 D. M. Ritchie,
718 .ft I
719 The C Programming Language,
720 .ft
721 Prentice-Hall, 1978.
722 .IP [3]
723 R. Morris,
724 .ft I
725 A Library of Reference Standard Mathematical Subroutines,
726 .ft
727 Bell Laboratories internal memorandum, 1975.
728 .IP [4]
729 S. C. Johnson,
730 .ft I
731 YACC \(em Yet Another Compiler-Compiler.
732 .ft
733 Bell Laboratories Computing Science Technical Report #32, 1978.
734 .IP [5]
735 R. Morris and L. L. Cherry,
736 .ft I
737 DC \- An Interactive Desk Calculator.
738 .ft
739 .LP
740 .bp
741 .ft B
742 .DS C
743 Appendix
744 .DE
745 .ft
746 .NH
747 Notation
748 .PP
749 In the following pages syntactic categories are in \fIitalics\fP;
750 literals are in \fBbold\fP; material in brackets [\|] is optional.
751 .NH
752 Tokens
753 .PP
754 Tokens consist of keywords, identifiers, constants, operators,
755 and separators.
756 Token separators may be blanks, tabs or comments.
757 Newline characters or semicolons separate statements.
758 .NH 2
759 Comments
760 .PP
761 Comments are introduced by the characters /* and terminated by
762 */.
763 As a non-portable extension, comments may also start with a # and
764 end with a newline.
765 The newline is not part of the comment.
766 .NH 2
767 Identifiers
768 .PP
769 There are three kinds of identifiers \- ordinary identifiers, array identifiers
770 and function identifiers.
771 All three types consist of single lower-case letters.
772 Array identifiers are followed by square brackets, possibly
773 enclosing an expression describing a subscript.
774 Arrays are singly dimensioned and may contain up to 2048
775 elements.
776 Indexing begins at zero so an array may be indexed from 0 to 2047.
777 Subscripts are truncated to integers.
778 Function identifiers are followed by parentheses, possibly enclosing arguments.
779 The three types of identifiers do not conflict;
780 a program can have a variable named \fBx\fP,
781 an array named \fBx\fP and a function named \fBx\fP, all of which are separate and
782 distinct.
783 .NH 2
784 Keywords
785 .PP
786 The following are reserved keywords:
787 .ft B
788 .ta .5i 1.0i
789 .nf
790         ibase   if
791         obase   break
792         scale   define
793         sqrt    auto
794         length  return
795         while   quit
796         for     continue
797         else    last
798         print
799 .fi
800 .ft
801 .NH 2
802 Constants
803 .PP
804 Constants consist of arbitrarily long numbers
805 with an optional decimal point.
806 The hexadecimal digits \fBA\fP\-\fBF\fP are also recognized as digits with
807 values 10\-15, respectively.
808 .NH 1
809 Expressions
810 .PP
811 The value of an expression is printed unless the main
812 operator is an assignment.
813 The value printed is assigned to the special variable \fBlast\fP.
814 A single dot may be used as a synonym for \fBlast\fP.
815 This is a non-portable extension.
816 Precedence is the same as the order
817 of presentation here, with highest appearing first.
818 Left or right associativity, where applicable, is
819 discussed with each operator.
820 .bp
821 .NH 2
822 Primitive expressions
823 .NH 3
824 Named expressions
825 .PP
826 Named expressions are
827 places where values are stored.
828 Simply stated,
829 named expressions are legal on the left
830 side of an assignment.
831 The value of a named expression is the value stored in the place named.
832 .NH 4
833 \fIidentifiers\fR
834 .PP
835 Simple identifiers are named expressions.
836 They have an initial value of zero.
837 .NH 4
838 \fIarray-name\fP\|[\|\fIexpression\fP\|]
839 .PP
840 Array elements are named expressions.
841 They have an initial value of zero.
842 .NH 4
843 \fBscale\fR, \fBibase\fR and \fBobase\fR
844 .PP
845 The internal registers
846 \fBscale\fP, \fBibase\fP and \fBobase\fP are all named expressions.
847 \fBscale\fP is the number of digits after the decimal point to be
848 retained in arithmetic operations.
849 \fBscale\fR has an initial value of zero.
850 \fBibase\fP and \fBobase\fP are the input and output number
851 radix respectively.
852 Both \fBibase\fR and \fBobase\fR have initial values of 10.
853 .NH 3
854 Function calls
855 .NH 4
856 \fIfunction-name\fB\|(\fR[\fIexpression\fR\|[\fB,\|\fIexpression\|\fR.\|.\|.\|]\|]\fB)
857 .PP
858 A function call consists of a function name followed by parentheses
859 containing a comma-separated list of
860 expressions, which are the function arguments.
861 A whole array passed as an argument is specified by the
862 array name followed by empty square brackets.
863 All function arguments are passed by
864 value.
865 As a result, changes made to the formal parameters have
866 no effect on the actual arguments.
867 If the function terminates by executing a return
868 statement, the value of the function is
869 the value of the expression in the parentheses of the return
870 statement or is zero if no expression is provided
871 or if there is no return statement.
872 .NH 4
873 sqrt\|(\|\fIexpression\fP\|)
874 .PP
875 The result is the square root of the expression.
876 The result is truncated in the least significant decimal place.
877 The scale of the result is
878 the scale of the expression or the
879 value of
880 .ft B
881 scale,
882 .ft
883 whichever is larger.
884 .NH 4
885 length\|(\|\fIexpression\fP\|)
886 .PP
887 The result is the total number of significant decimal digits in the expression.
888 The scale of the result is zero.
889 .NH 4
890 scale\|(\|\fIexpression\fP\|)
891 .PP
892 The result is the scale of the expression.
893 The scale of the result is zero.
894 .NH 3
895 Constants
896 .PP
897 Constants are primitive expressions.
898 .NH 3
899 Parentheses
900 .PP
901 An expression surrounded by parentheses is
902 a primitive expression.
903 The parentheses are used to alter the
904 normal precedence.
905 .NH 2
906 Unary operators
907 .PP
908 The unary operators
909 bind right to left.
910 .NH 3
911 \-\|\fIexpression\fP
912 .PP
913 The result is the negative of the expression.
914 .NH 3
915 ++\|\fInamed-expression\fP
916 .PP
917 The named expression is
918 incremented by one.
919 The result is the value of the named expression after
920 incrementing.
921 .NH 3
922 \-\-\|\fInamed-expression\fP
923 .PP
924 The named expression is
925 decremented by one.
926 The result is the value of the named expression after
927 decrementing.
928 .NH 3
929 \fInamed-expression\fP\|++
930 .PP
931 The named expression is
932 incremented by one.
933 The result is the value of the named expression before
934 incrementing.
935 .NH 3
936 \fInamed-expression\fP\|\-\-
937 .PP
938 The named expression is
939 decremented by one.
940 The result is the value of the named expression before
941 decrementing.
942 .NH 2
943 Exponentiation operator
944 .PP
945 The exponentiation operator binds right to left.
946 .NH 3
947 \fIexpression\fP ^ \fIexpression\fP
948 .PP
949 The result is the first
950 expression raised to the power of the
951 second expression.
952 The second expression must be an integer.
953 If \fIa\fP
954 is the scale of the left expression
955 and \fIb\fP is the absolute value
956 of the right expression,
957 then the scale of the result is:
958 .PP
959 min\|(\|\fIa\(mub\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP\|)\|)
960 .NH 2
961 Multiplicative operators
962 .PP
963 The operators *, /, % bind left to right.
964 .NH 3
965 \fIexpression\fP * \fIexpression\fP
966 .PP
967 The result is the product
968 of the two expressions.
969 If \fIa\fP and \fIb\fP are the
970 scales of the two expressions,
971 then the scale of the result is:
972 .PP
973 min\|(\|\fIa+b\fP,\|max\|(\|\fBscale\fP,\|\fIa\fP,\|\fIb\fP\|)\|)
974 .NH 3
975 \fIexpression\fP / \fIexpression\fP
976 .PP
977 The result is the quotient of the two expressions.
978 The scale of the result is the value of \fBscale\fR.
979 .NH 3
980 \fIexpression\fP % \fIexpression\fP
981 .PP
982 The % operator produces the remainder of the division
983 of the two expressions.
984 More precisely,
985 \fIa\fP%\fIb\fP is \fIa\fP\-\fIa\fP/\fIb\fP*\fIb\fP.
986 .PP
987 The scale of the result is the sum of the scale of
988 the divisor and the value of
989 .ft B
990 scale
991 .ft
992 .NH 2
993 Additive operators
994 .PP
995 The additive operators bind left to right.
996 .NH 3
997 \fIexpression\fP + \fIexpression\fP
998 .PP
999 The result is the sum of the two expressions.
1000 The scale of the result is
1001 the maximum of the scales of the expressions.
1002 .NH 3
1003 \fIexpression\fP \- \fIexpression\fP
1004 .PP
1005 The result is the difference of the two expressions.
1006 The scale of the result is the
1007 maximum of the scales of the expressions.
1008 .NH 2
1009 assignment operators
1010 .PP
1011 The assignment operators bind right to left.
1012 .NH 3
1013 \fInamed-expression\fP = \fIexpression\fP
1014 .PP
1015 This expression results in assigning the value of the expression
1016 on the right
1017 to the named expression on the left.
1018 .NH 3
1019 \fInamed-expression\fP += \fIexpression\fP
1020 .NH 3
1021 \fInamed-expression\fP \-= \fIexpression\fP
1022 .NH 3
1023 \fInamed-expression\fP *= \fIexpression\fP
1024 .NH 3
1025 \fInamed-expression\fP /= \fIexpression\fP
1026 .NH 3
1027 \fInamed-expression\fP %= \fIexpression\fP
1028 .NH 3
1029 \fInamed-expression\fP ^= \fIexpression\fP
1030 .PP
1031 The result of the above expressions is equivalent
1032 to ``named expression = named expression OP expression'',
1033 where OP is the operator after the = sign.
1034 .NH 1
1035 Relations
1036 .PP
1037 Unlike all other operators, the relational operators
1038 are only valid as the object of an \fBif\fP, \fBwhile\fP,
1039 or inside a \fBfor\fP statement.
1040 .NH 2
1041 \fIexpression\fP < \fIexpression\fP
1042 .NH 2
1043 \fIexpression\fP > \fIexpression\fP
1044 .NH 2
1045 \fIexpression\fP <= \fIexpression\fP
1046 .NH 2
1047 \fIexpression\fP >= \fIexpression\fP
1048 .NH 2
1049 \fIexpression\fP == \fIexpression\fP
1050 .NH 2
1051 \fIexpression\fP != \fIexpression\fP
1052 .NH 1
1053 Storage classes
1054 .PP
1055 There are only two storage classes in BC, global and automatic
1056 (local).
1057 Only identifiers that are to be local to a function need be 
1058 declared with the \fBauto\fP command.
1059 The arguments to a function
1060 are local to the function.
1061 All other identifiers are assumed to be global
1062 and available to all functions.
1063 All identifiers, global and local, have initial values
1064 of zero.
1065 Identifiers declared as \fBauto\fP are allocated on entry to the function 
1066 and released on returning from the function.
1067 They therefore do not retain values between function calls.
1068 \fBauto\fP arrays are specified by the array name followed by empty square brackets.
1069 .PP
1070 Automatic variables in BC do not work in exactly the same way
1071 as in either C or PL/I.  On entry to a function, the old values of
1072 the names that appear as parameters and as automatic
1073 variables are pushed onto a stack.  
1074 Until return is made from the function, reference to these
1075 names refers only to the new values.
1076 .NH 1
1077 Statements
1078 .PP
1079 Statements must be separated by semicolon or newline.
1080 Except where altered by control statements, execution
1081 is sequential.
1082 .NH 2
1083 Expression statements
1084 .PP
1085 When a statement is an expression, unless
1086 the main operator is an assignment, the value
1087 of the expression is printed, followed by a newline character.
1088 .NH 2
1089 Compound statements
1090 .PP
1091 Statements may be grouped together and used when one statement is expected
1092 by surrounding them with { }.
1093 .NH 2
1094 Quoted string statements
1095 .PP
1096 "any string"
1097 .sp .5
1098 This statement prints the string inside the quotes.
1099 .NH 2
1100 If statements
1101 .sp .5
1102 \fBif\|(\|\fIrelation\fB\|)\|\fIstatement\fR
1103 .PP
1104 The substatement is executed if the relation is true.
1105 .NH 2
1106 If-else statements
1107 .sp .5
1108 \fBif\|(\|\fIrelation\fB\|)\|\fIstatement\fB\|else\|\fIstatement\fR
1109 .PP
1110 The first substatement is executed if the relation is true, the second
1111 substatement if the relation is false.
1112 The \fBif-else\fR statement is a non-portable extension.
1113 .NH 2
1114 While statements
1115 .sp .5
1116 \fBwhile\|(\|\fIrelation\fB\|)\|\fIstatement\fR
1117 .PP
1118 The statement is executed while the relation
1119 is true.
1120 The test occurs before each execution of the statement.
1121 .NH 2
1122 For statements
1123 .sp .5
1124 \fBfor\|(\|\fIexpression\fB; \fIrelation\fB; \fIexpression\fB\|)\|\fIstatement\fR
1125 .PP
1126 The \fBfor\fR statement is the same as
1127 .nf
1128 .ft I
1129         first-expression
1130         \fBwhile\|(\fPrelation\|\fB) {\fP
1131                 statement
1132                 last-expression
1133         }
1134 .ft R
1135 .fi
1136 .PP
1137 All three expressions may be left out.
1138 This is a non-portable extension.
1139 .NH 2
1140 Break statements
1141 .sp .5
1142 \fBbreak\fP
1143 .PP
1144 \fBbreak\fP causes termination of a \fBfor\fP or \fBwhile\fP statement.
1145 .NH 2
1146 Continue statements
1147 .sp .5
1148 \fBcontinue\fP
1149 .PP
1150 \fBcontinue\fP causes the next iteration of a \fBfor\fP or \fBwhile\fP
1151 statement to start, skipping the remainder of the loop.
1152 For a \fBwhile\fP statement, execution continues with the evaluation
1153 of the condition.
1154 For a \fBfor\fP statement, execution continues with evaluation of
1155 the last-expression.
1156 The \fBcontinue\fP statement is a non-portable extension.
1157 .NH 2
1158 Auto statements
1159 .sp .5
1160 \fBauto \fIidentifier\fR\|[\|\fB,\fIidentifier\fR\|]
1161 .PP
1162 The \fBauto\fR statement causes the values of the identifiers to be pushed down.
1163 The identifiers can be ordinary identifiers or array identifiers.
1164 Array identifiers are specified by following the array name by empty square
1165 brackets.
1166 The auto statement must be the first statement
1167 in a function definition.
1168 .NH 2
1169 Define statements
1170 .sp .5
1171 .nf
1172 \fBdefine(\|\fR[\fIparameter\|\fR[\fB\|,\|\fIparameter\|.\|.\|.\|\fR]\|]\|\fB)\|{\fI
1173         statements\|\fB}\fR
1174 .fi
1175 .PP
1176 The \fBdefine\fR statement defines a function.
1177 The parameters may
1178 be ordinary identifiers or array names.
1179 Array names must be followed by empty square brackets.
1180 As a non-portable extension, the opening brace may also appear on the
1181 next line.
1182 .NH 2
1183 Return statements
1184 .sp .5
1185 \fBreturn\fP
1186 .sp .5
1187 \fBreturn(\fI\|expression\|\fB)\fR
1188 .PP
1189 The \fBreturn\fR statement causes termination of a function,
1190 popping of its auto variables, and
1191 specifies the result of the function.
1192 The first form is equivalent to \fBreturn(0)\fR.
1193 The result of the function is the result of the expression
1194 in parentheses.
1195 Leaving out the expression between parentheses is equivalent to
1196 \fBreturn(0)\fR.
1197 As a non-portable extension, the parentheses may be left out.
1198 .NH 2
1199 Print
1200 .PP
1201 The \fBprint\fR statement takes a list of comma-separated expressions.
1202 Each expression in the list is evaluated and the computed
1203 value is printed and assigned to the variable `last'.
1204 No trailing newline is printed.
1205 The expression may also be a string enclosed in double quotes.
1206 Within these strings the following escape sequences may be used:
1207 \ea 
1208 for bell (alert),
1209 `\eb'
1210 for backspace,
1211 `\ef'
1212 for formfeed,
1213 `\en'
1214 for newline,   
1215 `\er'
1216 for carriage return,
1217 `\et'
1218 `for tab,
1219 `\eq'
1220 for double quote and
1221 `\e\e'
1222 for backslash.
1223 Any other character following a backslash will be ignored.
1224 Strings will not be assigned to `last'.
1225 The \fBprint\fR statement is a non-portable extension.
1226 .NH 2
1227 Quit
1228 .PP
1229 The \fBquit\fR statement stops execution of a BC program and returns
1230 control to UNIX when it is first encountered.
1231 Because it is not treated as an executable statement,
1232 it cannot be used
1233 in a function definition or in an 
1234 .ft B
1235 if, for,
1236 .ft
1237 or
1238 .ft B
1239 while
1240 .ft
1241 statement.