home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 210 / 210.d81 / t.basics < prev    next >
Text File  |  2022-08-26  |  6KB  |  270 lines

  1. u
  2.      F A N C Y   F O R M U L A S
  3.  
  4.          by Jeffrey L. Jones
  5.  
  6.  
  7.     BASIC can be wonderful -- then
  8. again, it can be bogged down by
  9. repetitious or even needless code.
  10. When I program or edit programs, it's
  11. my policy to allow BASIC to handle a
  12. job in as few commands as possible. Of
  13. course, if it takes five commands to
  14. perform a job that can be done in one
  15. command, the code isn't very efficient
  16. and wastes space on an issue.
  17.  
  18.     Crunching a program usually means
  19. using either faster or shorter code.
  20. Or both. Using FANCY FORMULAS I can
  21. sometimes trim a block or two from a
  22. program.
  23.  
  24.     Remember page 64 of your
  25. PROGRAMMER'S REFERENCE GUIDE where the
  26. author shows an esoteric usage of the
  27. ON command?  Here it is:
  28.  
  29.  
  30.   ON -(A=7)-2*(A=3)-3*(A<3)-4*(A>7)
  31.   GOTO400,900,1000,100
  32.  
  33.  
  34.     When I first saw it, I couldn't
  35. make heads or tails of it. I thought:
  36. "Why would anyone make a line so
  37. complicated?" Now it's beautiful to
  38. me.
  39.  
  40.     If A=7 then the program will GOTO
  41. 400. IF A=3 then it hits line 900. If
  42. A<3 then line 1000 and if A>7 line
  43. 100. All those computations will
  44. result in either 1, 2, 3 or 4.
  45. Everything in parentheses will result
  46. in either 0 or -1.  True or false.
  47. Computers are great at true/false
  48. tests.
  49.  
  50.     I've probably used pure logic on
  51. only 1% of my programming but from
  52. this issue on, I've officially added a
  53. few standardized LOGICAL FORMULAS to
  54. my arsenal. Here's an example of the
  55. new math:
  56.  
  57.  
  58. 10 N=N-(N<MAX)+(N=MAX)*MAX:gosub1000
  59.  
  60.  
  61.     This increments N from 1 to 5 and
  62. GOSUB's 1000 every time the line is
  63. hit. When MAX=5, this code will cause
  64. N to be incremented until N=5. When
  65. N=5, it is automatically toggled back
  66. to one! Look, Ma! No IF's!
  67.  
  68.     This is the pure math version. But
  69. when I inserted a logical operator,
  70. the code got shorter and only made
  71. references to MAX and the variable
  72. being incremented a minimum of times:
  73.  
  74.  
  75.    10 N=-NOTN*-(N<MAX):GOSUB1000
  76.  
  77.  
  78.     By the way, the following code
  79. will count DOWN to 1 and then toggle
  80. back up to MAX:
  81.  
  82.  
  83.    20 N=NOT-IT+MAX*(IT=1)
  84.  
  85.  
  86.     Here is the most common BASIC
  87. equivalent:
  88.  
  89.  
  90.    10 N=N+1:IFN>MAXTHENN=1
  91.    20 GOSUB1000
  92.  
  93.  
  94.     I couldn't GOSUB 1000 on the same
  95. line because of the IF-THEN command.
  96. If the program were to GOSUB 1000
  97. while N was greater than MAX, a crash
  98. could occur since N>MAX is out of a
  99. specified range. So I couldn't GOSUB
  100. 1000 before the IF-THEN test.
  101.  
  102.     I couldn't GOSUB1000 after the
  103. test since the IF-THEN test will
  104. switch program control to the next
  105. line if the test is false. So I had to
  106. place the GOSUB on a line where it
  107. would ALWAYS be executed, but only if
  108. N was in the specified range. Hence, I
  109. had to split the line.
  110.  
  111.     Believe it or not, for this simple
  112. addition formula, the normal way edges
  113. out the fancy formula by a jiffy or
  114. two but only in big loops. After about
  115. 20,000 consecutive computations, you
  116. might lose a whopping second or two!
  117. So why even use these fancy formulas
  118. -- if not for enhanced line control?
  119.  
  120.     For LOADSTAR 128, I had to rewrite
  121. a program so that it worked on either
  122. a 40- or 80-column monitor. To do this
  123. I had to use fancy formulas or else
  124. write ALL the screen display code
  125. TWICE, thus increasing the size of the
  126. program.
  127.  
  128.     Here's an example of getting three
  129. things done with only one command. If
  130. T=5 then I want to TAB to column 21.
  131. If T=20, I want to TAB to 34. If T is
  132. ANYTHING else, I don't want to tab at
  133. all. I can handle this in one command
  134. instead of three:
  135.  
  136.  
  137.   10 PRINTTAB(-(21*(T=5)+34*(T=20)))
  138.  
  139.  
  140.     Looks complicated as all get out,
  141. right? Here's what's going on:
  142.  
  143.     If you turn on your computer and
  144. type:
  145.  
  146.              PRINT A=5
  147.  
  148.  
  149. you'll get a response of zero since A
  150. is not equal to 5. Zero means FALSE.
  151. You're not asking the computer to
  152. print A or 5. You're testing whether
  153. or not A equals 5.
  154.  
  155.     If you type:
  156.  
  157.  
  158.              PRINT A=0
  159.  
  160.  
  161. you'll get a response of -1.  -1 means
  162. TRUE.  This is because A is truly
  163. equal to zero.  Heck, you just turned
  164. on the computer!
  165.  
  166.               TRUE  = -1
  167.               FALSE =  0
  168.  
  169.     If A=34 and you type:
  170.  
  171.               PRINT A>10
  172.  
  173. you'll get a response of -1 since it's
  174. true.
  175.  
  176.     A really IS greater than 10. It's
  177. not concerned with what the real value
  178. of A is. It only wants to know if it's
  179. greater than 10 or not. Keeping this
  180. in mind, let's take another look at
  181. that last line:
  182.  
  183.  
  184.  10 PRINTTAB(-(21*(T=5)+34*(T=20))
  185.  
  186.  
  187.     In this line I'm tabbing to any of
  188. three columns: 0, 21, or 34.  I'm
  189. multiplying 21 times the test of T=5.
  190. If T really does equal 5 then (T=5)
  191. will be a -1 so:
  192.  
  193.  
  194.     21*(T=5) = 21*(-1)  =-21
  195.  
  196.  
  197.     If T=5 then T can't equal 20 so
  198. the test will be false for 20 and
  199. you'll get a zero. So:
  200.  
  201.  
  202.     34*(T=20) = 34*(0)    =0
  203.  
  204.  
  205.     So if T=5, here's what the
  206. computer sees at one stage of its
  207. computations:
  208.  
  209.  
  210.            TAB(-(-21+0))
  211.  
  212.       =    TAB(-(-21))
  213.  
  214.       =    TAB(21)
  215.  
  216.  
  217.     If T is not equal to 5 or 20 then
  218. the entire computation will result in
  219. zero and there will be no TAB:
  220.  
  221.  
  222.        TAB(-(21*(0)+34*(0)))
  223.  
  224.  
  225.     Of course you can mathematically
  226. offset for a higher default TAB in
  227. computations like these. Here's the
  228. same TAB with a default TAB of 10:
  229.  
  230.  
  231. 10 PRINTTAB(10-(11*(T=5)+24*(T=20)))
  232.  
  233.  
  234.     Normally you'd put SOME type of
  235. formula within TAB(x). This is a
  236. longer formula for sure -- but it sure
  237. beats THREE program lines!
  238.  
  239.     Note that I always multiply the
  240. entire test by -1 since I expect a
  241. negative answer. It'll be 10 plus -21,
  242. or -34. Multiplying by a negative 1
  243. merely makes the number positive since
  244. TAB REQUIRES a positive number. You
  245. can use ABS(x) if you like. I usually
  246. don't. Multiplying by -1 is faster.
  247. Without the offset of 10 the answer
  248. would have either been 0, -11 or -24.
  249. Multiplying by -1 still works since
  250. zero has no sign.
  251.  
  252.     This works fine for me, maybe not
  253. for others. It certainly allowed me to
  254. write a program this month that was
  255. two programs in one -- without any
  256. extra lines or a lot of IF's.
  257.  
  258.     As you can see, your computer has
  259. a high aptitude for true/false tests.
  260. And with this underused logic, you can
  261. make your code compact and powerful.
  262. The next time you're programming, try
  263. giving your computer a true/false test
  264. or two. It should pass with flying
  265. colors. It's those darned essay
  266. questions that stump a microprocessor!
  267.  
  268.  JLJ
  269.  
  270.  
  271.