home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Loadstar 210
/
210.d81
/
t.basics
< prev
next >
Wrap
Text File
|
2022-08-26
|
6KB
|
270 lines
u
F A N C Y F O R M U L A S
by Jeffrey L. Jones
BASIC can be wonderful -- then
again, it can be bogged down by
repetitious or even needless code.
When I program or edit programs, it's
my policy to allow BASIC to handle a
job in as few commands as possible. Of
course, if it takes five commands to
perform a job that can be done in one
command, the code isn't very efficient
and wastes space on an issue.
Crunching a program usually means
using either faster or shorter code.
Or both. Using FANCY FORMULAS I can
sometimes trim a block or two from a
program.
Remember page 64 of your
PROGRAMMER'S REFERENCE GUIDE where the
author shows an esoteric usage of the
ON command? Here it is:
ON -(A=7)-2*(A=3)-3*(A<3)-4*(A>7)
GOTO400,900,1000,100
When I first saw it, I couldn't
make heads or tails of it. I thought:
"Why would anyone make a line so
complicated?" Now it's beautiful to
me.
If A=7 then the program will GOTO
400. IF A=3 then it hits line 900. If
A<3 then line 1000 and if A>7 line
100. All those computations will
result in either 1, 2, 3 or 4.
Everything in parentheses will result
in either 0 or -1. True or false.
Computers are great at true/false
tests.
I've probably used pure logic on
only 1% of my programming but from
this issue on, I've officially added a
few standardized LOGICAL FORMULAS to
my arsenal. Here's an example of the
new math:
10 N=N-(N<MAX)+(N=MAX)*MAX:gosub1000
This increments N from 1 to 5 and
GOSUB's 1000 every time the line is
hit. When MAX=5, this code will cause
N to be incremented until N=5. When
N=5, it is automatically toggled back
to one! Look, Ma! No IF's!
This is the pure math version. But
when I inserted a logical operator,
the code got shorter and only made
references to MAX and the variable
being incremented a minimum of times:
10 N=-NOTN*-(N<MAX):GOSUB1000
By the way, the following code
will count DOWN to 1 and then toggle
back up to MAX:
20 N=NOT-IT+MAX*(IT=1)
Here is the most common BASIC
equivalent:
10 N=N+1:IFN>MAXTHENN=1
20 GOSUB1000
I couldn't GOSUB 1000 on the same
line because of the IF-THEN command.
If the program were to GOSUB 1000
while N was greater than MAX, a crash
could occur since N>MAX is out of a
specified range. So I couldn't GOSUB
1000 before the IF-THEN test.
I couldn't GOSUB1000 after the
test since the IF-THEN test will
switch program control to the next
line if the test is false. So I had to
place the GOSUB on a line where it
would ALWAYS be executed, but only if
N was in the specified range. Hence, I
had to split the line.
Believe it or not, for this simple
addition formula, the normal way edges
out the fancy formula by a jiffy or
two but only in big loops. After about
20,000 consecutive computations, you
might lose a whopping second or two!
So why even use these fancy formulas
-- if not for enhanced line control?
For LOADSTAR 128, I had to rewrite
a program so that it worked on either
a 40- or 80-column monitor. To do this
I had to use fancy formulas or else
write ALL the screen display code
TWICE, thus increasing the size of the
program.
Here's an example of getting three
things done with only one command. If
T=5 then I want to TAB to column 21.
If T=20, I want to TAB to 34. If T is
ANYTHING else, I don't want to tab at
all. I can handle this in one command
instead of three:
10 PRINTTAB(-(21*(T=5)+34*(T=20)))
Looks complicated as all get out,
right? Here's what's going on:
If you turn on your computer and
type:
PRINT A=5
you'll get a response of zero since A
is not equal to 5. Zero means FALSE.
You're not asking the computer to
print A or 5. You're testing whether
or not A equals 5.
If you type:
PRINT A=0
you'll get a response of -1. -1 means
TRUE. This is because A is truly
equal to zero. Heck, you just turned
on the computer!
TRUE = -1
FALSE = 0
If A=34 and you type:
PRINT A>10
you'll get a response of -1 since it's
true.
A really IS greater than 10. It's
not concerned with what the real value
of A is. It only wants to know if it's
greater than 10 or not. Keeping this
in mind, let's take another look at
that last line:
10 PRINTTAB(-(21*(T=5)+34*(T=20))
In this line I'm tabbing to any of
three columns: 0, 21, or 34. I'm
multiplying 21 times the test of T=5.
If T really does equal 5 then (T=5)
will be a -1 so:
21*(T=5) = 21*(-1) =-21
If T=5 then T can't equal 20 so
the test will be false for 20 and
you'll get a zero. So:
34*(T=20) = 34*(0) =0
So if T=5, here's what the
computer sees at one stage of its
computations:
TAB(-(-21+0))
= TAB(-(-21))
= TAB(21)
If T is not equal to 5 or 20 then
the entire computation will result in
zero and there will be no TAB:
TAB(-(21*(0)+34*(0)))
Of course you can mathematically
offset for a higher default TAB in
computations like these. Here's the
same TAB with a default TAB of 10:
10 PRINTTAB(10-(11*(T=5)+24*(T=20)))
Normally you'd put SOME type of
formula within TAB(x). This is a
longer formula for sure -- but it sure
beats THREE program lines!
Note that I always multiply the
entire test by -1 since I expect a
negative answer. It'll be 10 plus -21,
or -34. Multiplying by a negative 1
merely makes the number positive since
TAB REQUIRES a positive number. You
can use ABS(x) if you like. I usually
don't. Multiplying by -1 is faster.
Without the offset of 10 the answer
would have either been 0, -11 or -24.
Multiplying by -1 still works since
zero has no sign.
This works fine for me, maybe not
for others. It certainly allowed me to
write a program this month that was
two programs in one -- without any
extra lines or a lot of IF's.
As you can see, your computer has
a high aptitude for true/false tests.
And with this underused logic, you can
make your code compact and powerful.
The next time you're programming, try
giving your computer a true/false test
or two. It should pass with flying
colors. It's those darned essay
questions that stump a microprocessor!
JLJ