Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!munnari.oz.au!kaukau.comp.vuw.ac.nz!vuwst1!vuw4341!HARPER From: HARPER@rs1.vuw.ac.nz (HARPER) Newsgroups: comp.lang.rexx Subject: Re: On-line calculator Message-ID: <507@rs1.vuw.ac.nz> Date: 28 Mar 90 03:29:29 GMT References: <506@rs1.vuw.ac.nz> Reply-To: HARPER@rs1.vuw.ac.nz Organization: Victoria University of Wellington, NZ. Lines: 280 Disclaimer: Author bears full responsibility for contents of this article In article <506@rs1.vuw.ac.nz>, HARPER@rs1.vuw.ac.nz (John Harper) writes: >I have written CALC EXEC and CALC MANUAL as described below and will be please >to e-mail them to anybody who asks. (Our computer has no ftp facilities.) > I tried to. Some people who asked have e-addresses our e-mailer can't recognise and I am spending too much of my time e-mailing things. And one person said it was brief and relevant enough to post here in full. So here is a copy of CALC MANUAL (268 lines); stay tuned for CALC EXEC . Note: I have REXX access only to a VM/CMS operating system so I can't implement any changes that might be required for Amiga or any other REXX-capable system. CALC MANUAL by J F Harper Mathematics Dept Victoria University Wellington NZ e-mail or or or uunet!vuwcomp!rs1!harper or HARPER%nz.ac.vuw.rs1@ean-relay.ac.uk or postmaster%harper%nz.ac.vuw.rs1@uk.ac.ean-relay phone +64 4 721 000 or + 64 4 715 341 For CALC EXEC version of 27 Mar 1990 CONTENTS 1. INTRODUCTION 2. WHAT CALC CAN DO 3. ACCURACY 4. METHODS 5. ENVIRONMENT-SETTING COMMANDS 6. FILES 1. INTRODUCTION CALC is an on-line calculator which can do many simple calculations, and some not so simple. It is written in REXX for the IBM VM/CMS operating system. If it seems wrong or incomprehensible, please tell me (address above). Example 1: enter (using either lower or UPPER case) CALC 2+3 and the reply on the screen will be 5 If you enter CALC on a line by itself it will go on doing sums for you until you enter a blank line (just push the RETURN key). Example 2: enter CALC E**10 EXP(10) EXP(5)**2 and you will see three answers that should be the same. 2. WHAT CALC CAN DO It knows about REXX built-in operators e.g. +, -, *, /, % (divide and discard remainder), // (remainder), || (concatenate strings), **integer, REXX built-in functions e.g ABS, SIGN, (lots more in the REXX manual), also E (2.71828...), PI (3.14159...), ANSWER (to your previous calculation) and these mathematical functions: SQRT, FAC (integer factorial), LN (natural log), LOG10 (log to base 10), EXP, POWER, SIN, COS, TAN, ASIN, ACOS, ATAN, ARCSIN, ARCCOS, ARCTAN, SINH, COSH, TANH, SH, CH, TH, ERF, ERFC, GAMMA, LGAMMA, BETA, VSET, VANG, DIST, ANSWER, X, Y, Z, LAT, LON, SIZE. Here ASIN = ARCSIN, ACOS = ARCCOS, ATAN = ARCTAN are inverse trig functions; all trigonometry is in radians until you enter DEG to change to degrees; RAD changes back to radians; POWER(a,b) is EXP(b*LN(a)) where a must be +ve but b need not be integer, (if b is in fact an integer a**b will evaluate faster); SINH = SH, COSH = CH, TANH = TH are hyperbolic functions; ERF,ERFC are error functions (ERFC(0)=1,ERF(x)->1 as x->infinity); GAMMA(n)=FAC(n-1) if n is a positive integer, LGAMMA(x) is LN(GAMMA(x)), and BETA is the beta function: BETA(x,y)=GAMMA(x)*GAMMA(y)/GAMMA(x+y); For GAMMA(x) and BETA(x,y) x and y must be positive. VSET, VANG and DIST are useful for geophysical and geographical work: VSET(lat,lon,size) gives the 3 components (x,y,z) of the vector with length size pointing from the centre of the Earth to latitude lat and longitude lon, VANG(x,y,z) gives lat,lon, and size (i.e. the inverse function of vset), DIST(lat1,lon1,lat2,lon2) is the great-circle angular distance between two points with given latitudes and longitudes. After using VSET the variables X, Y, Z are the vector components found; after using VANG the variables LAT, LON, SIZE are the latitude, longitude, size found. These variables may then be used in further work. VANG and VSET leave ANSWER unchanged. VANG, VSET and DIST all assume a spherical Earth. Example 3: enter CALC 2+3 ANSWER*ANSWER ANSWER+2 and you should get 5 then 25 then 27 If you enter the filename of an EXEC followed by () or by its parameters in parentheses, and the filename is not the name of any CALC function listed above, and the EXEC is written in REXX, then you will find yourself running the EXEC. If the EXEC does not return any value on exit it will run but you will get an error message afterwards. If the EXEC is in EXEC or EXEC 2 dialect instead of REXX it will not even run. To run an EXEC with any name in any dialect see under CMD EXEC below. When using non-numerical input, remember that CALC uses REXX which always puts it in UPPER case unless you enclose the lower case letters in quotes: c2d(a) = C2D(A) = 193 = EBCDIC code of A ; c2d('a') = 129 = EBCDIC code of a. Input parameters which are neither letters nor numbers and are not possible expressions must be in quotes: c2x(/) gives an error while c2x('/') = 61. (However 3/2 is an expression, which = 1.5) For quotes, use ' ' not " " unless you want to explore IBM oddities, e.g. c2x(""/"") = 61 but c2x("/") gives an error if your escape character is the usual " (see TERMINAL in the CP manual). Non-numerical output works: C2X('A') = C1 = EBCDIC hex code of A. As a result, some "wrong" input is interpreted simply as a string of characters, and its output is just the input with lower case letters changed to UPPER case. To make CALC say how long each calculation took (to 0.01 sec total CPU time) when it has finished, enter TIME (NOTIME stops this). CALC works even if called from XEDIT at the ====> line, or from CMS SUBSET. Entering HELP or ? gives help (by BROWSEing this CALC MANUAL file, unless you have just made a REXX error, in which case you get that error's help message.) After errors you are shown the current environment settings (see below). To start writing results of CALC commands to the file CALC RESULTS A enter FILE The result will also appear on the terminal. If CALC RESULTS A already exists, the result will go on a new line at the end. To stop results being filed in this way either exit from CALC by pressing the key or enter NOFILE which leaves you in CALC. Every CALC session starts with the setting NOFILE. If you enter * as the first character on a line CALC will treat it as a comment and do nothing except copy the line into CALC RESULTS A if FILE is active. Comments may also appear after environment-setting commands (e.g. Example 4) except CMD but CALC will ignore them completely. It also ignores ordinary REXX comments between /* and */ Long output lines will be written in CALC RESULTS A as several shorter lines, with the same maximum length that your terminal uses, as given by the REXX linesize() function and set by the CP TERMINAL LINESIZE command. Linesize() can be 0; if so, the CALC RESULTS maximum is 255 characters. Example 4: enter the 4 lines CALC 37 to force working to 37 significant figures FIG 33 to show answers to 33 significant figures FILE EXP(PI*SQRT(163)) and you should get 262537412640768743.999999999999250 on the screen and in the file CALC RESULTS A. NOTE 1: Commands causing errors will get you out of FILE mode and their output will appear only at the terminal. NOTE 2: FILE leaves variables such as ANSWER unchanged. If the first word of an input line is CMD then the rest of the line is treated as a REXX instruction (see Examples 5,6,7 below). There may or may not be any visible output to the terminal. If there is some, it will be filed away by a previous FILE command only if you change SAY to CALL OUT as in Example 6. To run a CP command enter CMD CP before it as in Example 8; to run an EXEC enter CMD EXEC before it, e.g. CMD EXEC DIRMAINT or CMD EXEC DIRMAINT ? and to run a MODULE put CMD before it, e.g. CMD BROWSE CALC EXEC If any CMD command gives a nonzero return code CALC will treat it as an error (see NOTE 1 to FILE above) even if it isn't really, e.g. CMD MAKEBUF Example 5: CMD X = 7*11 gives X that value (77) which can be used in later commands in that CALC session. Warning: it overrides the meaning given to X by VSET (see above); other variables which can be altered in this way are ANSWER E LAT LON PI SIZE X Y and Z , array C. , and all REXX special variables, e.g. RC and SIGL . Note that the input lines CMD X = 7*11 and X = 7*11 are quite different; X = 7*11 would evaluate the Boolean expression (X = 77) and give the answer 1 if the previous value of X was 77, 0 if not. Example 6: CMD TRACE R shows in detail how CALC works (I use it for debugging). Example 7: CMD DO N=1 TO 10; CALL OUT N SQRT(N); END writes a table of numbers and their square roots from 1 to 10 (to achieve this otherwise you would have to issue 10 separate CALC commands). If you change CALL OUT to SAY in Example 7 your terminal output will be the same but nothing will appear in CALC RESULTS A even if you have already entered FILE (OUT is a procedure inside CALC that will SAY an answer, and, if FILE has been requested, put it in CALC RESULTS also.) Example 8: CMD CP Q T shows the time, day of the week, date, and how much CPU time you have used since you logged on: CP Q T is a valid abbreviation of CP QUERY TIME 3. ACCURACY Calculations are done with 15 significant figures and answers displayed to 10 unless you change this by entering CALC or FIG followed by a number, e.g. FIG 5 to display 5 significant figures or CALC 40 to calculate to 40 figures. CALC uses REXX which copes with VERY large or small numbers, + or - 9.999999999E+999999999 to 1E-999999999, or 0. Your answers won't always be good to as many digits as you might think: if f is a rapidly varying function, f(x) can be a long way from f(x*1.00000000000001) and you may get the latter when hoping for the former! This is inevitable in arithmetic with limited accuracy at every step. Hence EXP(x),COSH(x),SINH(x),GAMMA(x) lose a few digits of accuracy if x is large. Similarly, SIN and COS lose accuracy for large x because of the error in subtracting an appropriate multiple of 2*pi or 360 deg. The answer may even have the wrong sign if x is close to a zero of SIN or COS. There can be enormous errors in TAN if x is close to an odd multiple of pi or 90 deg. Also, doing arithmetic with n figures, even with exact data, is not the same as guaranteeing function values or answers correct to n figures. As a result, GAMMA,LGAMMA,PI,EXP,ERF,ERFC and all log, trig and hyperbolic functions give about n-2 figures, and if the answer is very large (N figures before the decimal point), replace n - 2 by n - 2 - log10(N). If you want to be certain of the accuracy of your results, it is often a good idea to increase CALC and see if anything changes. 4. METHODS: SQRT uses the Newton-Raphson method, other elementary functions use Taylor's theorem for small x and analytical properties to get from large to small x. The algorithms should be mostly clear from reading the CALC EXEC source code. LGAMMA uses the recurrence relation to get from LGAMMA(x) to LGAMMA(x+N) where N is a suitably large integer, then the asymptotic expansion. GAMMA(x) is found as EXP(LGAMMA(x)). ERF uses its Taylor series, ERFC(x) the recurrence relation backwards on its repeated integrals, unless x is large enough and the accuracy asked for is low enough to use the asymptotic series instead, in which case it uses that. As ERF(x) + ERFC(x) = 1 and ERF is more accurate and faster for small x, ERFC for large x, ERF has been defined as 1-ERFC for x>3, ERFC as 1-ERF for x<2. If x is between 2 and 3 you may check them against each other. There is also ERFCA(x) which is always the asymptotic series and tells you if it can't give an answer because x is too small. NOTE: If you ask for many significant figures (e.g. 50 or more) CALC will try to oblige, but you may run out of patience, computing funds or virtual machine size, especially if you use gamma or error functions. 5. ENVIRONMENT-SETTING COMMANDS There are several commands that set the environment within which CALC does its calculations. CMD applies only to the current input line, FILE applies until exit from CALC or until a NOFILE command, and all the others stay until you change them: each new CALC session starts where your last session finished. The default setting is what you get if you haven't specified otherwise. CALC n Calculate to n significant figures [default n=15] CMD command Treat command as a REXX instruction, not an expression to be evaluated DEG Trigonometry in degrees FIG n Show answers to n significant figures [default n=10] FILE Write results to the file CALC RESULTS A and to the terminal NOFILE Write results to terminal only [default] RAD Trigonometry in radians [default] TIME Show total CPU time used by each calculation NOTIME Don't show CPU time used by each calculation [default] 6. FILES Files which are needed to use CALC but need not be on your A disk: CALC EXEC which does most of the work CALC MANUAL which you're reading and the VM/CMS operating system including GLOBALV, EXECIO, BROWSE, STATE and YHELP or HELP. If YHELP is available it is used, as it works faster. Files which will appear on your A disk (CALC automatically updates them): LASTING GLOBALV which holds your settings for RAD DEG CALC FIG and TIM CALC RESULTS if you use the FILE command above. You can stack CALC commands in an EXEC and then call CALC to do them. Here are alternative versions in REXX and EXEC 2 dialects which both evaluate the curious number exp(pi*sqrt(163)), which is very close to an integer, and save its value in the file CALC RESULTS A as in Example 4 above: &TRACE | /* A REXX EXEC using CALC * An EXEC2 EXEC using CALC | */ &STACK CALC 37 | queue 'CALC 37' &STACK FIG 33 | queue 'FIG 33' &STACK FILE | queue 'FILE' &STACK EXP(PI*SQRT(163)) | queue 'EXP(PI*SQRT(163))' &STACK | queue /* blank line to exit */ EXEC CALC | 'EXEC CALC'