1	CALC
     HELP on using CALC can be obtained using the following keywords

	COMMANDS	CALC commands(use in @files)
	EXAMPLES	Examples of using CALC with command files
	GUIDE		Basic User's Guide to using CALC
	FEATURES	Advanced CALC features
	FUNCTIONS	Mathematical Functions supported by CALC..
	OCTAL		Using CALC for Octal arithmetic.(also HEX)
	PRECISION	Obtaining ultra high precision results
	VARIABLES	CALC variable types
2	GUIDE
 

     CALC is a calculator designed to evaluate arithmetic expressions.
In its basic form, expression evaluation is similar to that used by ANSI
FORTRAN with calculations performed on INTEGER*4 and REAL*8 constants.
Variables may also be invoked but are limited to single alphabetic
characters. It is assumed that the reader is familiar with FORTRAN data
types, constants, expression syntax, operator precedence, and the syntax
for assigning values to variables. Additional features include octal,
hexadecimal, and multiple precision arithmetic capabilities. Commonly
used commands and expressions can be placed in a file and executed when
convenient.

     To use CALC either define it as a foreign command

			CALC:==$DRA0:[SYSUTL]CALC

or invoke it via the UTL procedure.  One line calculations are done 
by typing

	CALC  expression		If a foreign command
or
	UTL CALC expression		If invoked via UTL

and CALC files are run by typing   CALC @filespec or UTL CALC @filespec.
A complete users guide can be printed by typing PRINT DRA1:[SYSUTL]CALC.MEM.
 
Try typing CALC 123+456 followed by a carriage return. CALC will
evaluate the expression and output the answer 579. Try other expressions
such as
  
    12.0 - 99.                                (answer=-87.00000000000000)
    -(-32767+(6-2)**8-(512/(409-401)))        (answer=-32705)
    3*5/7                                     (answer=2)
    3*(5/7)                                   (answer=0)
 
Mixed mode is legal, for example, 1977/50. is evaluated as 39.54000000000000.
Reals may be expressed using D or E format. For
example, 1.2E10*2.D0**3-1.D-8 is evaluated as 0.95999999999999992D+11.

2	VARIABLES

     Variables may also be used to retain values for later use. CALC
allows variables consisting of a single alphabetic character. As in
FORTRAN, variable A through H and O thru Z default to type real, I thru
N to type integer. To set I to a value use the usual FORTRAN syntax, for
example: I=2**10-1.

     Try typing the single character 'I'. CALC will respond with its
value. We can now use I in various expressions such as J=I-I/3*3.

   % is a special variable that retains the value of the last expression
evaluated. For example, to successively add up the numbers 1, 2, 3, 4, 5,
and 6 we could enter
 
     1
     %+2
     %+3
     %+4
     %+5
     %+6

Note that you can examine the value of the variables by typing the
appropriate single character followed by a carriage return. Such an
examination does not change the value of %. 

 To exit from CALC, type
 
     *E  (or *EXIT)
 or  *S  (or *STOP)
 
 
2	FUNCTIONS
 
CALC recognizes a variety of special functions. For example, to
calculate the square root of 2, we can type SQRT(2.).CALC responds with
the value 1.41421356237310. Each function may have an expression for its
argument. For example, A=2.0*SQRT(ALOG(9.)+3.) sets A to 4.55948443459838.
The following special functions are available:
 
 FUNCTION NAME    ARGUMENT TYPE   FUNCTION VALUE   DESCRIPTION
 ----------------------------------------------------------------
 ABS             REAL            REAL            absolute value
 DABS            REAL            REAL            absolute value
 IABS            INTEGER         INTEGER         absolute value
 IFIX            REAL            INTEGER         REAL to INTEGER 
 AINT            REAL            REAL            REAL truncation
 INT             REAL            INTEGER         REAL to INTEGER 
 IDINT           REAL            INTEGER         REAL to INTEGER 
 EXP             REAL            REAL            e**X
 DEXP            REAL            REAL            e**X
 ALOG            REAL            REAL            natural logarithm
 DLOG            REAL            REAL            natural logarithm
 ALOG10          REAL            REAL            logarithm base 10
 DLOG10          REAL            REAL            logarithm base 10
 SQRT            REAL            REAL            square root
 DSQRT           REAL            REAL            square root
 SIN             REAL            REAL            trigonometric sine
 DSIN            REAL            REAL            trigonometric sine
 COS             REAL            REAL            trigonometric cosine
 DCOS            REAL            REAL            trigonometric cosine
 TANH            REAL            REAL            hyperbolic tangent
 DTANH           REAL            REAL            hyperbolic tangent
 ATAN            REAL            REAL            arc tangent
 DATAN           REAL            REAL            arc tangent
2	OCTAL  
 
     You may change the base used to specify constants by using the
*B command. Legal forms are
 
           command                   action
           -------                   ------
             *B                  displays current default base
             *B 8                changes default base to octal
             *B 10               changes default base to 10
             *B 16               changes default base to 16
 
   Suppose we have changed the default base to octal. Then adding
(7 + 1)   we obtain the result    00000000010  (BASE  8).  If the
default base is hexadecimal, we can enter   9 + 1    which is
evaluated as   0000000A (BASE 16).   Suppose we have assigned  A=1
then    1+A  gives  2.000000000000000   even when the default base 16.
If we wish to add the hexadecimal digit 'A' to 1, enter 1+0A.
We now obtain the desired  0000000B (BASE 16).  This leading 0
is only necessary when the first hexadecimal digit is greater than 9.

   If  constants  are entered with digits that are not legal for the base
being used, the entire number is converted using a more appropriate base.
For example, if we have set the default base to octal and type  1+9.
The 9 is not an octal number so it is converted to base 10. If a base 16
number is involved, the result will be in base 16.

   You may temporarily change the base for a single integer constant by
preceeding it with
 
     ^8          for octal
     ^10         for base 10
     ^16         for base 16
 
For example, if the default base is 10,  100+^840  gives   132,
a base 10 integer.  I=100+^1610   gives  116 also a base 10 integer.
 
   Note that the '^' can only be used to specify the base of constants and
that expressions such as ^16I  are illegal.
 
   To declare variables to be integers of a specific base, we can use the
commands
 
     *INTEGER        (base 10)
     *OCTAL          (base 8)
     *HEX            (base 16)
 
for example,
 
     *INTEGER A     declares variable A to be a base 10 integer.
 
     *HEX B,Z,F     declares variables B, Z, and F to be base 16 integers.
 
     *DECIMAL       lists all the variables that have been declared to be
                    of type DECIMAL.

 
   To summarize, there are three distinct ways of making base declarations
when using CALC. The first is to use the *B command to designate the
base default value. This is used to determine the base for constants
when they occur in expressions. It does not in any way influence the
type of any variables found in an expression. The only way to change the
type of a variable is with a specific CALC command such as
     
         *INTEGER A,B
 
 Suppose for example that the default base is 10 and we enter
 
        *OCTAL A
        A=100
 
 then CALC responds with    00000000144  (BASE 8).
 
Finally, the last way to change a base is to use the explicit base
specifiers for a constant, for example
 
     ^10 123
     ^8 777
     ^16 AB
 
2	PRECISION
                              -MULTIPLE PRECISION-

     Normally integer arithmetic (base 8, 10, and 16) is done internally
with INTEGER*4 variables. To allow for larger numbers, CALC has multiple
precision capabilities that allow numbers up to 99 digits to be
manipulated. Constants are converted to a multiple precision data type
when the number of digits specified exceeds a certain value. This value
depends upon the specified base. Leading zeroes are included in this
count and can be used to force constants to be of type multiple
precision.
 
         base           maximum number of digits before conversion
         ----           ------------------------------------------
          8               10
         10                9
         16                7
 
 
     Suppose we type (with the default base of 10) the number   1234567890,
then CALC echoes with    1,234,567,890  (BASE 10).
 
     The commas indicate that % now has type multiple precision base 10.
Similarly, typing 1234ABCD results in 1234,ABCD (BASE 16). Notice that
base 16 multiple precision numbers are separated by commas every 4
digits, octal and base 10 numbers every 3 digits.

    You may perform the usual operations of addition, subtraction,
multiplication, division, and exponentiation. As of version 1.0,
exponentiation of a multiple precision number may only be to a
non-negative integral power. To declare variables of type multiple
precision, use
 
        *M8            (multiple precision base 8)
        *M10           (multiple precision base 10)
        *M16           (multiple precision base 16)
 
 for example,
 
        *M8 A,B        declares A and B to be multiple precision
                       octal variables.


Then typing    A=32768  results in CALC responding with   100,000  (BASE  8).
 
2	COMMANDS
                             -ADDITIONAL COMMANDS-

     All commands to CALC (as distinguished from expressions to be
evaluated) begin with an asterisk. To obtain a list of all possible
commands, type a question mark followed by a carriage return. Most of
the commands have already been described. The following section gives an
explanation of the remaining commands.

        COMMAND               DESCRIPTION
        -------               -----------
 
 *@filename       Where filename is the name of a file of CALC commands.
                  CALC reads the file and executes the commands. Up to 
                  5 nested calls can be made. Recursive calls are not
                  allowed. CALC prompts with CALC<n> before each command
                  line is executed, where n is the calling level.
 
                      You may optionally follow the file name with a blank
                  followed by a single variable name (a single alphabetic
                  character or %). CALC will then execute the file until
                  the value of that variable is zero or negative. The test
                  of this variable is made before the file is executed and
                  not during execution of commands within the file. If the
                  variable's value is not positive when the command is
                  initially encountered, the file will not be opened for
                  execution. See the section on command file examples for
                  ways to use this option.
 
 *ASCII           Declares a list of variables to be of type ASCII. Useful
                  when decoding ASCII characters. For example, if we set
                  A to be of type ASCII, then typing
                               A=77
                  results in the character 'M' being output. The inverse
                  operation is the single quote. It allows us to specify
                  a single ASCII constant. For example, if we type
                          'M
                  then the character 'M' is echoed and indicates that
                  % holds that character and has data type ASCII. Suppose
                  that the variable I has data type INTEGER. Then we can
                  output the base 10 code for the ASCII character 'M' by
                  entering
                               I='M
                  which results in 77 being output. Notice that you may
                  not be able to enter certain control characters that are
                  intercepted by your operating system. Characters whose
                  value is less than 32. are output by printing the
                  character '^' followed by the equivalent ASCII character
                  of that number plus 32. For example,
 
                               A=10
                  results in
  
                               ^*
 
                  being output since 42 is the ASCII code for the character
                  '*'. See apendix A for a table of the characters output
                  by CALC to represent such non-printable characters.
 
*C                COMMENT line. The characters that follow are ignored by
                  CALC. This is useful when documenting files containing
                  CALC  commands.
 
 
 
*N                NOVIEW. Prevents CALC from outputting the value of the
                  expressions evaluated. This is especially useful when
                  executing files containing CALC commands that initialize
                  variables to special values. Equivalent to *V 1
 
 
*V                VIEW. Controls CALC's printing options:
 
                  command       output class
               -------------   --------------
                  *V 0          error messages
 
                  *V 1          error messages
                                command lines read from files
 
                  *V 2          error messages
                                value of expressions evaluated
 
                  *V 3          error messages
                                command lines read from a file
                                value of expressions evaluated

                  *V            same as *V 3
 
 
                  The default setting is *V 3. Notice that other legal
                  forms are *VIEW 1 and *V2
 
 
*R                READ. Allows a single line to be read from the terminal.
                  Useful in files of CALC commands to allow additional
                  commands to be entered (like *S to exit from that file)
                  or simply as a way to halt terminal output until the
                  carriage return key is pressed.
 
 
*REAL             declares specified variables to be REAL*8. When the value
                  of such variables are output, FORTRAN's D format is used.
 
 
*DECIMAL          Declares specified variables to be REAL*8. When the value
                  of such variables are output, FORTRAN's F format is used.
                  Variables A-H and O-Z default to type DECIMAL.
 
*S                STOP. Same as *E
 
*E                EXIT. Terminates CALC session unless it is used within
                  a file of CALC commands. In this case, CALC closes the
                  file and continues with the next command.
 
*Z                ZERO. Zeroes all variables except %. Data types are not
                  changed.
2	FEATURES
                             -ADDITIONAL FEATURES-

     CALC is similar to FORTRAN with respect to operator precedence.
Blanks may occur anywhere on a command line without effect except after
a single quote mark used to specify a single ASCII character constant.
CALC extends the ANSI FORTRAN syntax by allowing the following:

     1. multiple assignments on one line, for example
 
        I=J=K=812
 
     2. Unary + and unary - are allowed, for example
 
        2*-3
        +2+-7
        -2**4
        are all legal. The last expression evaluates to 16 because the
        unary - has a higher precedence than exponentiation.
 
     3. exponentiation may be indicated by using ! as well as **
 
     If any of the declarations are entered (such as *INTEGER or *M8)
and no argument to this command is given, then CALC will print out the
variables that have been assigned that data type. Note that a variable
can be assigned to different data types using such commands and still
not be assigned a value. If you attempt to output the value of such a
variable, an error message will result.
2	EXAMPLES
                             -COMMAND FILE EXAMPLES
 
 
 EXAMPLE 1:
          PROBLEM: be able to enter the coefficients of a second
          degree polynomial and have the roots output.
 
          Solution: create the following file and call it ROOT:
 
 
 *CALCULATES THE ROOTS OF A 2ND DEGREE EQUATION
 *C YOU WILL BE ASKED TO ENTER THE VALUES OF
 *C A,B AND C WHERE
 *C
 *C    2
 *C A X  +B X   +   C  =   0
 *C
 *C
 *C ENTER THE VALUE OF A
 *R
 A=%
 *C ENTER THE VALUE OF B
 *R
 B=%
 *C ENTER THE VALUE OF C
 *R
 C=%
 *C THE ROOTS ARE:
 X=(-B+SQRT(B*B-4.*A*C))/2.*A
 Y=(-B-SQRT(B*B-4.*A*C))/2.*A
 *C
 *C AS YOU CAN SEE BECAUSE
 A*X*X+B*X+C
 A*Y*Y+B*Y+C
  
 
          Then run the procedure by entering CALC and typing
 
          *@ROOT
 
 
 
 EXAMPLE 2:
 
         PROBLEM: Suppose we are working on a problem that requires
                us to convert many decimal 16 bit word addresses to
                octal  byte  address.  We  would like to be able to
                simply enter the decimal word address and have CALC
                respond with the octal byte address.
 
 
         SOLUTION: Create a file of the following commands and call
                 it BYT:
 
 
 *C
 *C
 *C
 *C
 *C ENTER DECIMAL WORDS
 *R
 A=%+%
 
          Then we enter CALC and type the following:
 
 *OCTAL A        (to make A an octal variable)
 1               (to set % to a non-zero number)
 *@BYT %         (to execute the file of conversion commands)
 
 
          We will then repeatedly execute the file BYT until we
          enter a zero as the number to be converted.
 
 EXAMPLE 3:
         Problem: We wish to set up a command file called SIGN that
                allows us to enter a number, and then execute the
                files MINUS, ZERO, and PLUS according to the value
                entered.
 
         Solution: Create the file SIGN consisting of:
 
 
 *C ENTER THE NUMBER WHOSE SIGN IS TO BE DETERMINED
 *R
 I=J=%
 I=-I
 K=1
 *C K STAYS AT 1 IF NUMBER IS NON-ZERO
 *@MINUS I
 *@PLUS J
 *@ZERO K
 
         The file MINUS:
 *C THE NUMBER IS NEGATIVE
 I=K=0
          The file PLUS:
 
 *C THE NUMBER IS POSITIVE
 J=K=0
          and the file ZERO:
 
 
 *C THE NUMBER IS ZERO
 K=0
 
       Notice that K is used to control the execution of the file ZERO
by initializing it to 1 and resetting it to zero if either of the files
PLUS or MINUS are executed.

 EXAMPLE 4:
         PROBLEM: Enter the number N and calculate the numbers
                1!, 2!, 3!, 4!, ... N!
                where 
                N! = N*(N-1)*(N-2)*...*3*2*1
 
 
           SOLUTION: Since the numbers may grow very large, we
                   will use a multiple precision variable. Create
                   the following files:
 
 
 MFACT:
 
 *C INPUT THE LIMIT OF THE FACTORIAL LIST
 *V 0
 *R
 N=%
 *M10 A
 I=0
 *@MF1 N
 *C SET VIEW AND TYPE FOR VARIABLE A AT DEFAULT VALUES
 *DECIMAL A
 *V
  
  
 MF1:
  
 A=1
 N=N-1
 I=I+1
 J=I
 *@MF2 J
 *V2
 A
 *V0
 
 MF2:
 
 A=A*J
 J=J-1
 
    We then enter CALC and type
 
*@MFACT
   A typical run of this procedure would look like:

	 CAL
	 CALC>*@MFACT
	 CALC<2>*C INPUT THE LIMIT OF THE FACTORIAL LIST
	 CALC<2>*V 0
	 CALC<2>4
	 1
	 (BASE 10)
	 2
	 (BASE 10)
	 6
	 (BASE 10)
	 24
	 (BASE 10)
	 CALC>

 EXAMPLE 5:
         PROBLEM: we wish to specify a default base and successively add
                 numbers into a sum.
 
 
         SOLUTION: create the following files of CALC commands:
 
 
  ADD:
 
	 *V
	 *C ENTER DEFAULT BASE
	 *R
	 *C CHANGE DATA TYPE OF SUM
	 *C BY SPECIFYING THE DATA TYPE FOR I AND J
	 *R
	 *C TO EXIT, TYPE 0   TO ZERO OUT, ADD -I
	 *V0
	 I=0
	 1
	 *@ADD1 %
	 *V
 
 
  ADD1:
 
	 *V
	 *R
	 *V0
	 J=%
	 I=I+%
	 *V2
	 I
	 *V0
	 %=IABS(J)
  A typical run follows:
	 CALC>*@ADD
	 CALC<2>*V
	 CALC<2>*C ENTER DEFAULT BASE
	 CALC<2>*R
	 CALC<2>*B 8
	 DEFAULT BASE IS  8
	 CALC<2>*C CHANGE DATA TYPE OF SUM
	 CALC<2>*C BY SPECIFYING THE DATA TYPE FOR I AND J
	 CALC<2>*R

	 CALC<2>*OCTAL I,J
	 CALC<2>*C TO EXIT, TYPE 0   TO ZERO OUT, ADD -I
	 CALC<2>*V0
	 CALC<3>177
	 00000000177  (BASE 8)
	 CALC<3>1
	 00000000200  (BASE 8)
	 CALC<3>0
	 00000000200  (BASE 8)
