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