Annotation of 43BSDReno/share/doc/usd/06.bc/bc, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.