VAL for the QL
The QL has great string manipulation functions that offer a great deal of flexibility. The method of coercion should eliminate the need for the VAL function, however, there are times when you MUST have it.
For example, I was working on a program that would plot any mathematical function on the screen. The function had to be a two variable function (y=SIN(x), y=x^2, etc.). The plotting part of the program was easy to write, but the function had to be embedded in the SuperBASIC lines because it could not be put in a string and then evaluated. A friend of mine had the same sort of problem, so we set out trying to find a solution.
On the 2068, the following would be solution:
100 INPUT "y = ";a$
110 INPUT "x goes from? ";x1
120 INPUT "x goes to? ";x2
130 FOR x=x1 TO x2
140 LET y=VAL a$
160 REM you now have x and y co-ords
170 REM do scaling and plotting
180 NEXT x
190 STOP
After hours of thought, we both came to the same conclusion. We have to make full use of the ability to work with channels on the QL. Since we both have the MIRACLE SYSTEMS TRUMP CARD, references to ‘ram1_’ are used, but these can be changed to any device name. The following program skeleton can achieve the same results as above (in a round-about way).
100 DEFine FuNction y(x)
110 z=COS(x)
120 RETurn z
130 END DEFine
140 INPUT "y = "!a$
150 INPUT "x goes from? "!x1
160 INPUT "x goes to? "!x2
170 OPEN_OVER #3, ram1_temp
180 PRINT #3, "110 z=";a$
190 CLOSE #3
200 MERGE ram1_temp
210 FOR x=x1 TO x2
220 y=y(x)
230 REMark you now have x and y co-ords
240 REMark do scaling & plotting
250 END FOR x
Here is a brief explaination. First of all, TK2_EXT calls up the Toolkit II extensions. They really aren’t necessary, but make things fast and simple. If you don’t have the toolkit or added RAM, then change OPEN_OVER in line 130 to OPEN_NEW. OPEN_OVER just OVERwrites the file if it already exists. Also, change ram1_ to any valid device which you happen to have.
Line 110 just sets up a dummy equation to avoid a null assignment. Since the QL does not have the VAL function, a problem arises when you say
a$= "x^2+4*SIN(x)-LN(x)"
x = 2
y = ??
How are you going to evaluate ‘y’ with the value of ‘x’? You want to sub the value of x into the equation, then assign that result to y. The way in which the program does it is simple when you look closely. It just changes the line that has the equation in it by merging a one line program. This one line program contains the equation that was input by the user. With RAM disks it behaves like a built in function, but with microdrives it is sure to be a pain.
If you are going to use this routine, be sure to make the appropriate changes to the line that stores the equation in the new program (line 180 in the listing). Also, don’t be confused with the DEF FN. It uses x as a parameter, but it is not the same as the x later on in the program.
Any questions or suggestions? Call or write to me.
SENEN RACKI
[address omitted]
[phone number omitted]
SINC-LINK WANTS YOUR ARTICLES, RIGHT? WRITE!
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.