83Plus:Basic:Tricks

From WikiTI
Revision as of 11:11, 14 July 2006 by Saibot84 (Talk | contribs)

Jump to: navigation, search


There is a whole list of tricks that one can implement, once one is comfortable programming in TI-Basic which allow for a whole onslaught of capabilities, such as using less memory or making the program run faster. There used to be a site called Kevtiva Inc. which had the largest list of TI-BASIC hacks I (Saibot84) have ever seen, including how to force a RAM reset (without the use of asm programs) but I've forgotten most of the meanest hacks. Here are some of the many tricks: (Please use common sense in deciding whether a "-" is a minus or a negative sign. When the use is un-common-sense-ical or possibly confusing, it will be specified.)

Memory

Memory Management

Number values
In order to be able to save memory on the calculator, it helps to have a bit of an understanding as to how the calc manages the memory, especially in regards to numbers. Because TIOS is designed for school, and so is oriented for precision, it saves all real number values as 14-digits and an exponent of 10. In other words, it saves the number π as 3.1415926535898 × 100, but in memory, it is something more similar (not exact) to 0 31415926535898 where the 0 tells TIOS what the exponent of 10 is that need to be applied to the 31415926535898. However, this also applies to smaller values, such that a 13 is saved as 1 13000000000000. For small, rational, terminating values, it can be seen that a good deal of memory is being wasted. I've created the following formula which, when saved to Y0 (or any other Y function, works with numbers in the same way that the sub( function works with strings:
int(fPart(X/10^(int(2-A+log(X))))*10^(F))
translated as a sub( statement, it would be the equivalent of sub(X,A,F) regardless of the position of the decimal point in X. If you don't understand what the formula is doing, it's dividing X by 10value so that the digit we're looking for (identified in A) is "moved" to be the first digit to the right of the decimal point, then ignores anything to the left or the decimal piont, then multiplies the value left over by 10F and ignores anything to the right of the decimal point, thus providing the sub(X,A,F).
use more than 27 vars
If you need to use more than the 27 vars TIOS gives you with A-Z, other than using lists, matrices, strings, or Ans, you can go look in the [VARS] menu under Window. Of these, being that the calculator is usually in function mode, you can use any of the window vars under T/theta (which are for parametric mode) and/or under U/V/W (which are for polar). It is recommended that you prefer those under U/V/W because this mode is usually never used, whereas parametric has been used on more than one occasion by programs, such as for drawing X in terms of Y. You can also go into the Finance menu under [APPS] and go to Vars, but IIRC, some are read-only vars, so you will need to experiment to see if a var is accessible or not, but it is certain, that at least the N can be used problemlessly in your programs. You can also use the n, which is found by going to the catalog and pressing "N" as a variable, as well as the sequential functions u,v, and w.
Debugging
One of the easiest ways to help you debug your program is through the use of the Pause command. You can use the Pause command to display the value of a var, or just to track the progress of your program. During any of the Pause commands, you can press [ON] to break the program and choose Goto so that the calc will automatically bring you to the point in your program where it was Pause-ing when you interrupted it with the [ON] key. Some common bugs include using the Y var or the X var while drawing to the screen (TIOS tends to change the value of Y and/or X when doing certain things on the graph screen, so avoid using Y or X when working on the graph screen.

Memory Saving

Never write out closing ) parentheses at the end of a line
You save 1 byte. The reason for this is that TIOS closes all still-open parentheses when it encounters $3F, which is the new-line character that is input when you press enter in the Program Editor, or when it encounters the store arrow.
Never write out closing " quotes at the end of a line
You save 1 byte. The reason for this is that TIOS closes any still-open quotes when it encounters $3F, which is the new-line character that is input when you press enter in the Program Editor, or when it encounters the store arrow.
Never use a Pause right after a Disp, unless the Pause has text
This is because both Disp and Pause can display text to the homescreen. Disp writes the text to the screen and then continues forward, but Pause waits for the [ENTER] key to be pressed. When Pause is used with text, such as in
:Pause "TEXT"
then it writes the text to the homescreen first, and then waits for the user to press [ENTER].
CAPITALIZE
For the most part, yes, it is nice to see that not EVERY character is in UPPERCASE on the calculator, but if you're looking to save space, don't use lower case letters. Each lowercase letter takes up twice as much space as the uppercase ones.
Read-Only Lists and Matrices
If you have a list or matrix that will be read-only, it behooves you to save it to a string or Y function (Y1-Y0). Instead of doing
:{1,2,3,4,5,6,7,8,9→L1
:L1(5 (to retrieve the 5th element of the list)
try doing
:"{1,2,3,4,5,6,7,8,9→Str1
:expr(Str1
:Ans(5
or
:"{1,2,3,4,5,6,7,8,9→Y1
:Y1:Ans(5
The benefit is that you are now no longer wasting the space, as explained in the Number Values. When choosing between storing your list to a string and storing it to a function, keep in mind that string values are somewhat editable, while functions are completely read-only. If you needed to edit something in the function, you'd have to convert it to a string first before editing, and then overwrite the original function. It is therefore recommended that you save your lists to strings.
No NewLine Required
The following commands do not require that you neither begin a new line, nor use a colon before the next command: Archive, Unarchive, DelVar. Example:
Archive XArchive YArchive ZUnarchive ZUnarchive TDelVar ADelVar BFor(X,0,1
this doesn't work with Lists, or labels, and presumably neither with the prgm token.

Speed Boosters

Use For( loops
In light of research I once read online, as well as through personal research and experimentation, it is fairly simple to see that For( loops are the fastest of the looping options. To prove this, try running the following program on your calc, and see which one is the fastest:
Program:LOOPTEST
:Disp "FOR
:For(X,1,10000
:End
:Pause "FOR IS DONE
:1→X
:Disp "WHILE
:While X<10001
:X+1→X
:End
:Pause "WHILE IS DONE
:0→X
:Disp "GOTO
:Lbl XX
:X+1→X
:If X<10001
:Goto XX
Pause "GOTO DONE
You will notice, especially if using a stopwatch, that the For( loop, IIRC, should even run a FEW SECONDS faster than the While loop, and/or the Goto loop. Therefore, see if you can't change your loops to use For( instead. In the case of the While, change
:While X<Y
to
:X-1→X
:For(X,X,Y
:X-1→X
You most likely will be losing a few more bytes in memory, but it's very much worth it for the speed... and you've already saved much speed by doing the tricks above ;-)
Get Returns on your IfThens, Fors, etc.
Each time you do an If-Then statement or a For( statement (among others), when the calc is "waiting" for an End statement, these conditions are taking up memory, which, after a while, slow down your BASIC programs. However, the benefit is that this is only the case, as long as your program is running... as soon as the calculator returns to the homescreen, these used memory is cleared again and you are ready to go at it again. The benefit of this is that when the calculator encounters a Return statement in a BASIC program, it "cancels" any conditions for its waiting for an End.
Avoid storing to vars in a loop
Brandon Green writes in his BASIC Guru Online that his experiments have led to the conclusion that storing variables is a prime cause of slowdowns in TI-BASIC programs. Therefore, try to restructure your code so as to avoid using the → as much as possible.

Graphical Touches

Use Text(-1,X,Y,value or text
I learned this from Kevtiva Inc. Normally, you do something like this:
:Text(15,10,"MY TEXT
following this, the text "MY TEXT" will be written on the 15th row, in the 10th column, in the small font... however, try adding a "-1" (without the quotation marks) as the first argument...
:Text(-1,15,10,"MY TEXT
This time, the text "MY TEXT" will be written on the 15th row in the 10th column, in the LARGE font
Write blank spaces to the graph screen when needing to erase stuff
as long as what you are trying to erase is at least 6 pixels high, it is srongly recommeded that you use the Text( command to write blank spaces to the screen instead of creating a Line(A,B,C,D,0 loop. This is because to write a line, white or black, the calculator needs to do a lot of math to convert the X and Y coordinates into pixel coordinates, while writing text to the screen is almost at the same speed as it would take to draw a sprite to the screen in any asm program (since for the calc, text chars are just sprites) and so writing (blank) text to clear the screen is much faster.
Try drawing graphics to the screen using text instead of lines
With access to the lowercase letters, as well as the Catalog menu, it is possible to draw graphics to the screen using text. For example, to draw a heart for a Zelda game would be much faster if you do something like the following:
:"vQ6Qv  "
:For(X,1,7
:Text(10,9+X,sub(Ans,X,1
:End
Another typical example is that of "drawing" a status bar to the screen using something such as:
(P is the percent to be shaded in)
(L is the length of the bar in pixels)
(H is the horizontal position)
(V is the vertical position, using the top-left corner of the screen as point 0,0)
:Text(V,H,"(
:For(X,0,PL/100
:Text(V,H+X+1,"8
:End
:For(X,1,(100-P)L/100
:Text(V,H+PL/100+X,")
:End
Using this technique, small graphics such as these can be drawn much faster than if we tried to draw them using lines and/or pixels and/or points.
Simplify your Life
Save a GDB at the beginning of your program. "Graph Databases (GDBs) are useful storage centers of graph informaiton. This means that it saves not only the Zoom settings, but also Axes on/off, coord on/off, and the Y= equations. Using these can reduce the size of your program." (quoted from http://web.archive.org/web/20021020121625/www.kevtiva.com/calc/gdb.htm) Turn off the axes with AxesOff, turn off functions with FuncOff, and set the Window to Xmin=0, Xmax=94, Xscl=8, Ymin=-62, Ymax=0, Yscl=8. When your program is exiting, have it reset the previous GDB so that you don't mess up the gamer's graph settings. While debugging your graphics, moving your cursor on the graph screen will now tell you both the pixel coordinates, as well as the point coordinates since you have synchronized them. http://www.meebo.com//skin/default/img/emoticons/big_smile.gif
Convert Pts into Pxls
If you have not used the previous tip, it is important to know that the coordinates you give to any Pt command will be different from those you give to any Pxl command (if you want to use, say Pt-On/Pt-Off, with Pxl-Test). The reason for this is that Pt commands take your window settings into account, while Pxl commands don't.Here is one set of formulas you can use to convert your Pt coordinates into Pxl coordinates:
round(94X/(Xmax-Xmin)+47,0)->A
round(-62Y/(Ymax-Ymin)+31,0)->B
 -RobbieMc
With this formula, the Pxl command is Pxl-XX(B,A), where XX is On, Off, Test, or Change. Another formula can be found in the Ans your answers section of Cool Hacks. http://meebo.com/skin/default/img/emoticons/smile.gif
Get rid of the "Done"
use the following right before your program exits to avoid having the calc display the "Done" text on the homescreen
:ClrHome
:Output(1,1,"
Get rid of the RunIndic
On the homescreen, do Output(1,16,"_ in the loop that is displaying the run indicator. On the graph screen, do Text(-1,0,90,"_ in the loop that is displaying the run indicator. (_ in this context refers to the space character.)

Cool Hacks

Have " and the store arrow in a string
(IIRC, brought to you by Kevtiva Inc.) Type a " and the store arrow at the homescreen and press enter. An error message will appear; choose to Quit. Press [Y=] and go to Y1. Press [2nd] [ENTER] to paste the previous homescreen entry into Y1. Press [2nd] [MODE] to {ESC}. At the homescreen, type Equ>Str(Y1,Str1 (or whatever Str you want to store it to and press [ENTER]. You now have the " quote and the store arrow in your string. http://www.meebo.com//skin/default/img/emoticons/wink.gif
If condition:End
This will need a bigger explanation, but in short, you can have a conditional End statement that helps your loops run faster by only executing the End statement if needed. The reason this works is because you didn't use a Then statement: an If followed by a single non-Then statement is interpreted as an If with no Else clause and the single statement as the equivalent of the Then clause, even if the statement is End. You can therefore also insert comments into your code (which is not recommended for BASIC unless you're still debugging it) by doing If 0:Whatever. For instance:
(untested...)(lines are numbered)
01:For(X,0,1
02:getKey→K
03:K→X
04:If not(Ans
05:End
06:0→X
07:"-
08:If K=25
09:"LEFT
10:If K=26
11:"UP
12:If K=27
13:"RIGHT
14:If K=34
15:"DOWN
16:If K=45
17:"CLEAR
18:If Ans="-
19:End
20:Disp Ans
21:If K=45
22:1→X
23:End
Explanation: Lines 1-2 are normal. Line 3 resets X if there was no key, which will loop back to the For( statement, or allow the routine to continue if there WAS a key. The answer from the getKey is in Ans, so if no key was pressed, Line 5 will be executed, which loops back to Line 1 without wasting any more time. http://www.meebo.com//skin/default/img/emoticons/smile.gif ... If a key was pressed, X is reset, which will cause the routine to loop back to the For( statement the next time it encounters an End statement. If the condition in Line 4 is not met, it automatically skips to Line 6. The string "-" is stored in Ans. We then go into a series of checks looking for any of the arrow keys, and changing Ans to reflect that. Line 8 is executed, and if the condition is not met, it skips to Line 10; if that condition is not met, it skips to Line 12; if that condition is not met, it skips to Line 14; if that condition is not met, it skips to Line 16; if that condition is not met, it skips to Line 18. Then, it checks the Ans var. If the Ans var is still unchanged, then none of the keys we are looking for have been found, so we End, which we've already said will loop back to the For( statement, essentially beginning the getKey loop once more. If Ans is different, then one of our keys was found, so we Disp the value in Ans (which is a String). We then check to see if it was the [CLEAR] key., and if it was, we set X so that the next End statement that is encountered will essentially close the loop and the routine will continue beyond this part of the code. We then End, which checks the value of X. If X is 0, it loops back to the For( statement, recommencing the whole loop, but if X is 1, it "closes" the loop, forgets about it completely, and moves on with its life. Thus can you build in looping conditions into your programs without having to use Lbl or Goto statements. http://www.meebo.com//skin/default/img/emoticons/cool.gif
Ans your answers
the Ans var is, IMHO, the most useful yet most unsafe var on the whole calculator because it can be so many different variable types (real, complex, list, matrix, string)... One way to reduce the number of varables you're using in your program is to carefully structure your program so that it stores as much information into the Ans var as possible. One way to do this is to setup Ans to be a list, say {3,1,4,2,5}. Doing Ans(4) is not going to multiply each value in the list by four, but rather going to give you the 4th value in the list:2. This happens because the OS will treat the Ans variable exactly the same as if it were L1 or any other list variable. I've already demonstrated that the Ans var can be used to keep track of whether one of the keys we were looking for was found or not, but there are other things you can do with the Ans var (as long as you're careful not to change the value in Ans unintentionally). For instance, doing {Ans(2),Ans(4),Ans(1),Ans(3),Ans(5) will change Ans to be the list {1,2,3,4,5}... Note that the Ans var is changed AFTER the whole command has been executed. There will probably be more examples of using Ans in a program at a later date. Therefore, you can do something like this:
(50% tested)
(to convert from Pt coordinates to Pxl coordinates)
A is the X-coord
B is the Y-coord

{63/(Ymax-Ymin),95/(Xmax-Xmin

;{pixels per Y, pixels per X

{abs(Ymax-B),abs(Xmin+A),int(Ans(2))+(0=fPart(Ans(2))),int(Ans(1))+(0=fPart(Ans(1

;{Y,X,int ppY,int ppX
;Ans(1)Ans(3) is now the Pxl-Y
;Ans(2)Ans(4) is now the Pxl-X
Autodetect Degree or Radian Mode
Robert Maresh states that this is one thing he learned from James Matthew's Asmguru.hlp
:If 0<cos(9)
:Then
:Disp "Degree Mode
:Else
:Disp "Radian Mode
:End
Autodetect if the Calculator is an 82 or 83/+
Robert Maresh states that
abs -1+1 would return 2 on a TI-82
abs(-1+1 would return 0 on a TI-83 (this is because the abs is followed by a parenthese)

and that one can therefore use the following to display whether the calc is an 82 or 83

Text(0,0,"YOU HAVE A TI-",83-.5(abs -1+1

Miscellaneous

Have conditions built into your formulas
This will probably need a better explanation, but I have found it useful to build certain conditions into my formulas, instead of having to write all those If statements... for instance, for piece-wise graphing, you can do Y=(X^2)(X>0)+(2X)(X<1), which is the same as If X>0:Y=X^2:If X<1:Y=2X ...right now, I can't really say what you'll be saving by doing this (memory/speed/etc) because I don't remember, but I can assure you this come quite in handy because you can then have one formula solve a variety of different problems, without the hassle of dealing with a lot of If-Then statements. For example, if you were to save that formula as Y1, then you'd only need to do Y1(number) to have it do the whole sequence of conditionals. I consider this one of the more complicated tricks to implement (because one can easily get confused as to how to build it as well as to what conditions one is looking for) but I have found it to be extremely useful. For example, let us say you write a text editor program in BASIC that allows the user to edit Str1, as displayed on the homescreen using Output(1,1,Str1. However, you run into a problem when Str1 is longer than the 96 chars that fit on the homescreen, so you can, using the sub( and length( functions, you could have one line of code something like
:length(Str1
:Output(1,1,sub(Str1,1(Ans<96)+16frac(Ans/16)(Ans>95)+(16int(Ans/16)-80)(Ans>95),Ans+80-16int(Ans/16
should make the Output scroll up one line whenever the screen has been filled (untested, please verify). What this line is saying is: give me from Str1, starting at the first byte if Str1 is smaller than 96 charachters, otherwise, calculate the beginning to be one "row" less than the total, and give me all the rest of the chars till the end. If you could not follow that, don't worry, as that is what I meant when I said this is one of the harder tricks. If you did follow with what I was trying to do, Kudos to you! Keep in mind that the conditions within the () can hold any of the condition-elements of If statements, i.e. and, not, or, =, ≠, >, <, ≥, ≤. BTW, this BASIC trick does NOT work on the 89. I tried, but there I could not get the 89 to convert a binary operation into a numerical value, the way the z80s do. (I could be wrong about this working on all z80s... I've tested it on an 82 and an 83+)
See program in MirageOS

If you want to see your program in a shell, like MirageOS, you must type the following code in the first line of the program:

::"Description

That will show the program name with the description you typed.

Further Reading

The Complete TI-83 BASIC Optimization Guide, Version 2 http://www.ticalc.org/archives/files/fileinfo/145/14542.html

Special Thanks

Special thanks to:

Kevtiva Inc. http://web.archive.org/web/20021010114152/www.kevtiva.com/calc/index.html © 2000 Kevtiva Interactive

BASIC Guru Online http://bgo.netfirms.com/ Copyright © 2000-2006 BASIC Guru Online. All Rights Reserved.

Detached Solutions http://www.detachedsolutions.com/forum/ © 2000-2006 Detached Solutions