1

For a ti-84 program I used the quadratic equation and stored the root with the plus sign as N and the root with the negative sign as R. I then want to only use the positive root as the value for the rest of the program (if both are positive it doesn't matter I just want to check one is positive), so I did as follows:

If N>0 --> U

ElseR-->U

But it didn't work. Is it not possible to use store as a command in an if-else statement? Or is there another way to only choose the positive variable and store that as U?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gabriel
  • 129
  • 1

2 Answers2

3

Okay, listen. Blazo's answer is wrong. Every If statement that has an Else also needs a Then and an End. This is the correct way to write it without piecewise expressions (piecewise expressions are usually the best way to go, though, so I suggest you check out dohaqatar's answer as well):

If N>0
Then
N->U
Else
R->U
End

With two separate lone If's, you can reduce the size of the code by one byte to 17 bytes:

If N>0
N->U
If N<=0
R->U

You can compress it even more, saving 5 more bytes to make 12 bytes:

R->U
If N>0
N->U

You can also use a piecewise expression such as dohaqatar's below, also 12 bytes:

N<0:RAns+Nnot(Ans->U

The best part is, an algorithm change reduces code size to 6 bytes:

max(R,N->U
Timtech
  • 1,224
  • 2
  • 19
  • 30
1

This can be quite easily accomplished through your standard If, Else statements; however, A quicker way of doing this is through piece-wise functions. In TI-Basic, every Boolean expression evaluates to either 1 or 0, representing true and false respectively. This fact can be exploited to make certain conditional expressions much shorter.

Your code using standard If Else syntax:

If N>0
Then
N→U
Else
R→U
End

This option is 18 bytes long.

Using piece-wise expressions, your code can be compressed to this:

N<0:RAns+Nnot(Ans→U

Resulting in a line of code only 12 bytes long.

ankh-morpork
  • 1,732
  • 1
  • 19
  • 28
  • An algorithm change to `max(R,N->U` might be better :D but I love your piecewise expression. – Timtech Mar 08 '15 at 13:30
  • @Timtech I was about to comment on your answer about that. Clever solution to use `max(` like that. I noticed it has the added benefit of not throwing an error on complex numbers. – ankh-morpork Mar 08 '15 at 13:52
  • Yeah, after optimizing for a while, I never thought to come share it on SO before. I'm glad I did! – Timtech Mar 08 '15 at 15:51