Author Topic: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines  (Read 15225 times)

0 Members and 1 Guest are viewing this topic.

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #15 on: October 30, 2010, 01:06:42 am »
whoa, why does the Circle one work?

An efficient way to jump without Goto:

Code: [Select]
:Delvar KRepeat K=21
:<stuff>
:Repeat K=21 or K=105
:getkey->K
:End
:End

this is useful for when you want a back and a go on option.  ;)  of course you can replace the keycodes and stuff though.  ;)

This can be optimized even more by doing:

Code: [Select]
DelVar KRepeat K=21
\\code\\
Repeat 42=abs(K-63
getKey→K
End
End
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline calc84maniac

  • eZ80 Guru
  • Coder Of Tomorrow
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2912
  • Rating: +471/-17
    • View Profile
    • TI-Boy CE
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #16 on: October 30, 2010, 01:24:42 am »
You can also remove the Delvar K, since Repeat is a post-test loop. It's also a bad practice to put a loop control statement in the middle of a line, because it can screw with the parser if it has to skip over all this code for some reason (for example, if it is contained in a large If/Then/End block and the If condition is false)
"Most people ask, 'What does a thing do?' Hackers ask, 'What can I make it do?'" - Pablos Holman

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #17 on: October 30, 2010, 02:44:12 pm »
What do you mean by loop control statement in the middle of a line? Could you show a good and bad code example?

Offline JosJuice

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1344
  • Rating: +66/-14
    • View Profile
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #18 on: October 30, 2010, 03:41:04 pm »
What do you mean by loop control statement in the middle of a line? Could you show a good and bad code example?
Middle of a line:
Code: [Select]
:DelVar AIf A
:Return
Not middle of a line:
Code: [Select]
:DelVar A
:If A
:Return
Not middle of a line:
Code: [Select]
:DelVar A:If A:Return
I think the problem mainly occurs if End is placed in the middle of a line.

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #19 on: October 31, 2010, 01:01:35 am »
Oh ok you mean how you can omit the : character. Yeah I know about that trick, I just was used to just saying "omitting the : char" :P

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #20 on: November 08, 2010, 07:28:43 pm »
There a couple of really handy things you can do for optimizations.



Code: [Select]
If K=1 or K=2 or K=3 or K=4 or K=5
\Do something.\

Can be...

Code: [Select]
If max(K={1,2,3,4,5
\Do something.\

(Not mine, I think I saw Nemo do it first but I don't know if it predates him doing it where I saw.)



Code: [Select]
If K=21 or K=105
\Do something.\

Can be...

Code: [Select]
If 42=abs(K-63
\Do something.\

However this one does not always work. You have to test and make sure. The basic algorithm to figure out the numbers is (Keycode #1+Keycode #2)/2 and that will give you the number to subtract by. Then you just subtract one of the keycodes by it and it will give you the number that they will equal. (Example: 105+21=126/2=63 then 105-63=42 and 21-63=-42 which |-42|=42).

(I don't take credit for this optimization. It's Ztrumpet's as far as I know.)



Code: [Select]
X+(K=26)-(K=24
Ans-(Ans=17)+not(Ans→X
Y+(K=34)-(K=25
Ans-(Ans=9)+not(Ans→Y

Can be...

Code: [Select]
min(16,max(1,X+sum(⌂List(K={24,26→X
min(8,max(1,Y+sum(⌂List(K={25,34→Y

(Not sure who I saw this from but it was a while ago but calc84maniac talked about this method a few weeks ago on HCWP.)



Code: [Select]
31(A>1)+31(A>2)+31(A>3)+31(A>4
Can be...

Code: [Select]
31sum(A>{1,2,3,4


Code: [Select]
0
For(A,1,dim(L1
Ans+(C=L1(A
End
If Ans
\Do something.\

(Note: this is untested but it's basically supposed to check a list if it holds the value of variable and if it does it will do something.)

Can be...

Code: [Select]
If max(C=L1
\Do something.\

(I don't remember who this was that showed this but I feel like it was Player...might have been Nemo though. I don't remember though.)



Well that's all for now. If I think of anymore off the top of my head I will post them.
« Last Edit: November 08, 2010, 07:46:53 pm by meishe91 »
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #21 on: November 08, 2010, 08:32:20 pm »
Code: [Select]
If K=1 or K=2 or K=3 or K=4 or K=5
\Do something.\

Can be...

Code: [Select]
If max(K={1,2,3,4,5
\Do something.\

(Not mine, I think I saw Nemo do it first but I don't know if it predates him doing it where I saw.)



Code: [Select]
If K=21 or K=105
\Do something.\

Can be...

Code: [Select]
If 42=abs(K-63
\Do something.\

However this one does not always work. You have to test and make sure. The basic algorithm to figure out the numbers is (Keycode #1+Keycode #2)/2 and that will give you the number to subtract by. Then you just subtract one of the keycodes by it and it will give you the number that they will equal. (Example: 105+21=126/2=63 then 105-63=42 and 21-63=-42 which |-42|=42).

(I don't take credit for this optimization. It's Ztrumpet's as far as I know.)
I'm pretty sure the first one is Weregoose's and the second on is Luby's or HarrierFalcon's from UTI.

Code: [Select]
X+(K=26)-(K=24
Ans-(Ans=17)+not(Ans→X
Y+(K=34)-(K=25
Ans-(Ans=9)+not(Ans→Y

Can be...

Code: [Select]
min(16,max(1,X+sum(⌂List(K={24,26→X
min(8,max(1,Y+sum(⌂List(K={25,34→Y

(Not sure who I saw this from but it was a while ago but calc84maniac talked about this method a few weeks ago on HCWP.)
This stems from a discussion somewhere on TIBD.
« Last Edit: November 08, 2010, 08:32:37 pm by ztrumpet »

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #22 on: November 08, 2010, 08:38:13 pm »
Ah ok, I didn't know about the first one but ya, I remember the TIBD thing now. Thanks :)
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #23 on: November 08, 2010, 10:09:22 pm »
I did a few tests today which broke some of my long held beliefs about speed.

The access time of Ans is the same as the access time for regular variables. So for instance both of these take the same time to execute:
Ans+Ans/Ans = Ans
A+A/A = A

But, this is faster: (N refers to the finance N)
N+N/N = N

This second rule holds true for any system variable so Xmin, ZXmin, TblStart, etc. are all fair game.


However, when it comes to store time:
1 is faster than 1->N which is faster than 1->A
Which means that Ans can be just as fast as the system variables as long as it is the only variable used.

These will execute in nearly the same speed: (The Ans version is like 2% faster)
1:Disp Ans |vs.| 1->N:Disp N


This is only interesting news though if you don't care about program size. N is 2 bytes while Ans is only 1. Not to mention that your code becomes nearly unreadable if you start using the ZXmin and ZYmax variables.
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline jnesselr

  • King Graphmastur
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2270
  • Rating: +81/-20
  • TAO == epic
    • View Profile
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #24 on: November 08, 2010, 10:30:00 pm »
I knew that the finance variables were faster.  How did you do the tests to determine which was the fastest?

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #25 on: November 08, 2010, 10:32:21 pm »
Thank you ThePenguin!  Because of you, Exodus and Elmgon will be able to progress a lot easier; I didn't know I could use the ZXmin and other variables like it.  I needed more speed and more variables. ;D
« Last Edit: November 08, 2010, 10:32:36 pm by ztrumpet »

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #26 on: November 08, 2010, 10:40:45 pm »
Here is the complete testing code:
:startTmr
:Repeat startTmr=Ans+1    ;this is to ensure that the test starts exactly on a second, not halfway through
:End
:startTmr->A
:For(B,1,6000
:<Code goes here>
:End
:Disp checkTmr(A

Here are the real times:
:1       16
:1->N  19
:1->A  22

:Ans  19
:A     19
:N     16

And then the big one with 15,000 iterations:

:1
:Ans   65

:1->N
:N      67
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #27 on: November 08, 2010, 10:45:23 pm »
Here is the complete testing code:
:startTmr
:Repeat startTmr=Ans+1    ;this is to ensure that the test starts exactly on a second, not halfway through
:End
:startTmr->A
:For(B,1,6000
:<Code goes here>
:End
:Disp checkTmr(A
That's awesome.  I really like the repeat loop at the beginning. ;D

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #28 on: November 08, 2010, 11:23:39 pm »
Wow nice tricks guys. One thing about Meishe's tricks, though: weren't those slower?

Offline meishe91

  • Super Ninja
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2946
  • Rating: +115/-11
    • View Profile
    • DeviantArt
Re: TI-83+/84+ (SE) BASIC Tips, Tricks, and Routines
« Reply #29 on: November 08, 2010, 11:30:05 pm »
Wow nice tricks guys. One thing about Meishe's tricks, though: weren't those slower?

I don't know. I haven't noticed to much. If anything they are space savers though.
Spoiler For Spoiler:



For the 51st time, that is not my card! (Magic Joke)