.chapter The VAX Desk Calculator .IFNOT NBI .RM72 .LM5 .ENDIF NBI .STHL ,0 .ap .HL 1 Introduction This chapter documents the use of CALC, the VAX Desk Calculator. CALC may either be accessed from the VAX Professional Workstation menu or as a foreign command (defined by SYS$SYSVPWFILES:SETVPWSYM.COM during SYSLOGIN.COM) by typing CALC. .NHY .J .F 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. .hl 1 Getting Started When invoked, CALC prompts for input with .lit CALC> Try typing 123+456 .end literal followed by a carriage return. CALC will evaluate the expression and output the answer .lit 579 .end literal It then prompts for further input. Try other expressions such as .lit 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 .end literal 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: .lit I=2**10-1 .end literal Try typing the single character 'I'. CALC will respond with its value. We can now use I in various expressions such as .lit J=I-I/3*3 .end literal % is a special variable that retains the value of the last expression evaluated. For example, to add up the numbers 1, 2, 3, 4, 5, and 6 successively, we could enter .lit 1 %+2 %+3 %+4 %+5 %+6 .end literal 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 .lit *E (or *EXIT) or *S (or *STOP) or Control-Y .end literal .HL 1 Special Functions CALC recognizes a variety of special functions. For example, to calculate the square root of 2, we can type .lit SQRT(2.) .end literal CALC responds with the value 1.41421356237310 Each function may have an expression for its argument. For example, .lit A=2.0*SQRT(ALOG(9.)+3.) sets A to 4.55948443459838 .end literal The following special functions are available: .lit 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 convert REAL to INTEGER AINT REAL REAL REAL truncation INT REAL INTEGER convert REAL to INTEGER IDINT REAL INTEGER convert 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 .end literal .hl 1 Working In Octal and Hexadecimal You may change the base used to specify constants by using the *B command. Legal forms are .lit 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 is 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. .end literal 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 .lit *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. .end literal 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 .lit *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 .end literal .hl 1 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. .lit 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) .end literal 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 .lit *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) .end literal .hl 1 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. .lit 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 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. This is 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. .end lit .page .lit *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 values of such variables are output, FORTRAN's D format is used. *DECIMAL Declares specified variables to be REAL*8. When the values 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. .end literal .hl 1 Additional Features CALC has a HELP command. When CALC is prompting (CALC>) you may type either HELP or ? followed optionally by a keyword. If no keyword is present, a list of available keywords for obtaining help will be displayed. HELP works just like getting normal HELP from the VAX system. If a given keyword has help under several sub-topics, you can request HELP on specific sub-topics by typing HELP etc. 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: .lit 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 ** .end literal 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. .hl 1 Command File Examples .lit 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 .end lit .PG .lit 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. .end lit .PG .lit 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. .end lit .PG .lit 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> .end lit .PG .lit 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) Then we invoke the procedure by typing *@ADD 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) CALC> .end literal .hl 1 Usage Notes Version 1.0 .lit 1. When you iterate on a file by a call such as *@REPEAT X then note that A) X must have been set to a positive value when the command is executed or else the file will not be executed. B) If the file of commands does not change the value of the variable X you will enter an infinite loop. You can explicitly set X to a non-positive value, use the *Z command to zero it (if it is not %), or include a *R command to give you a chance to reset the variable and get out of the loop. C) *E and *S will allow you to exit from the command file REPEAT but will not of themselves prevent repetitions. D) Entering constants echo on the terminal (assuming *V is properly set) and can change the value and type of the variable %. This is important to remember when using % to control the iteration of a file. 2. When you first enter CALC, the variable % has type INTEGER and holds the version number of the program. 3. In practice, multiple precision arithmetic may be limited to less than 99 digits because of your terminal's inability to print that many characters. 4. No implicit conversion is made to multiple precision when operations with reals or integers cause an overflow. This was done in version 1.0 in case the multiple precision routines have to be removed when creating a small task image for some operating system. 5. In FORTRAN, -A**2 is the same as -(A**2) with CALC, -A**2 is the same as (-A)**2 (just like SNOBOL!) 6. If R and A are positive reals and I is a positive integer, some compilers like RSX-11M's F4P won't allow (at run time) evaluation of (-I)**R (-A)**R 7. Under RSX-11M F4P you will find 2**.5 to have value 1 while 2.**.5 has value 1.41421356237310 8. 10E10 is a hexadecimal constant (integer) while 10.E10 is a real. .end lit .pg .hl 1 Appendix A .lit ASCII CALC ASCII CALC ASCII CALC ASCII CALC CODE PRINTS CODES PRINTS CODE PRINTS CODE PRINTS ---- ------ ----- ------ ---- ------ ---- ------ 0 ^ 32 64 @ 96 ` 1 ^! 33 ! 65 A 97 a 2 ^" 34 " 66 B 98 b 3 ^# 35 # 67 C 99 c 4 ^$ 36 $ 68 D 100 d 5 ^% 37 % 69 E 101 e 6 ^& 38 & 70 F 102 f 7 ^' 39 ' 71 G 103 g 8 ^( 40 ( 72 H 104 h 9 ^) 41 ) 73 I 105 i 10 ^* 42 * 74 J 106 j 11 ^+ 43 + 75 K 107 k 12 ^, 44 , 76 L 108 l 13 ^_ 45 - 77 M 109 m 14 ^. 46 . 78 N 110 n 15 ^/ 47 / 79 O 111 o 16 ^0 48 0 80 P 112 p 17 ^1 49 1 81 Q 113 q 18 ^2 50 2 82 R 114 r 19 ^3 51 3 83 S 115 s 20 ^4 52 4 84 T 116 t 21 ^5 53 5 85 U 117 u 22 ^6 54 6 86 V 118 v 23 ^7 55 7 87 W 119 w 24 ^8 56 8 88 X 120 x 25 ^9 57 9 89 Y 121 y 26 ^: 58 : 90 Z 122 Z 27 ^; 59 ; 91 [ 123 { 28 ^< 60 < 92 \ 124 | 29 ^= 61 = 93 ] 125 } 30 ^> 62 > 94 ^ 126 ~ 31 ^? 63 ? 95 _ 127 .end literal