I'm writing a program on my calculator and I'm trying to figure out
how I can print something like "The answer is 33" I know that I can
use "+" like:
"thirty-three"->answer
Disp "The answer is "+answer
and get out
"The answer is thirty-three,"
but I can't say:
3*11->answer
Disp "The answer is "+answer
and get out
"The answer is 33"
If I could either get the number into a string, or display the text
and the variable, that would be great. Thanks in advance!
-Adam
There is a command that turns a equation into a string called eq>st but I dont
know how to use it well and I dont have my ti85 booklet with me. however your b
ook should tell you how you properly use this command. I think this is how you
would use it but i am mot sure.
(3*11)->string
eq>st(string)->string2 'I'm not sure if this is right. check the book'
disp "the answer is"+string2
A definite way to do this is with the output command.
(3*11)->string
clLCD
output 1,1,"the answer is"
output 1,15,string
I hope this helps you out.
Adam Wright <atwr...@teleport.com> wrote in article
<3282b324....@news.teleport.com>...
> Hi!
>
> I'm writing a program on my calculator and I'm trying to figure out
> how I can print something like "The answer is 33" I know that I can
> use "+" like:
>
> "thirty-three"->answer
> Disp "The answer is "+answer
>
> and get out
> "The answer is thirty-three,"
>
> but I can't say:
> 3*11->answer
> Disp "The answer is "+answer
>
> and get out
> "The answer is 33"
>
> If I could either get the number into a string, or display the text
> and the variable, that would be great. Thanks in advance!
>
> -Adam
I myself try to use the output method previously suggested to you and if
you haven't looked it up yet I think the equatuion to string arguments are
(a,b) where A is the equation name (maybe can put the equation in here) and
b is the ending string name so instead of his ->string2 it'd be
eq>str(3*11,string2) should be the complete command but I am not sure if
you can put the 3*11 in there. Oh well I am probably wrong anyways :)
Hope it is right and it helps.
TIm
It's not too hard to do; if anyone is interested in the algorithm to
convert a number to a string let me know and I'll post it.
-Jonathan
Jonathan, I'm very interested in this. I raised this same question
under a different topic. Please post it! Thanks in advance...
If you have the variable A, which contains the integer 167 (or any
integer), and you want "167" in a string, follow these steps:
make a varialbe called ST for example and store a singe character in
it. You'll see why later
first, use mod(A, 10) to "pluck off" the least significant digit of the
value in A, and store it ina variabe like B. B will contain a value from
0 to 9. the mod function returns the remainder after doing an integer
division. That remainder will become a string in the nest step.
Next use the Sub function to put that digit in your output string. here
id the full command
sub("0123456789",B+1,1)+ST -> ST
your string now has one didgit of your number in it. If ST has nothing
in it when you try to execute this, the program will stop. That's why
you need ST initialized with a random character of your choosing. Ti85
string functions aren't too good.
Next, you need to set up A so that you can use a loop to get the other
digits. What I usually do for readability is use the following expression
(A-B)/10 ->A
this expression shortens 167 to 16. It will strip off the last digit of
any integer.
now just put all this in a while loop (or a repeat loop depending on if
A will be 0 when you call this routine) that terminates when A is 0
,and you have a string. One more thing: you have to remove that
character at the end of the string. this is the best way:
sub(ST,1,length ST-1) -> ST
Make sure ST contains TWO charaters before you shorten ST or you'll
probably get youself in trouble.
That should do it. You can just tinker with the algorithm to make
strings out of negative numbers or numbers with a decimal point. I
suppose I can post actual code (with speed optimizations) if nessecary.
But you learn more by doing it yourself...
-Jonathan
>OK, here's how to make a number into a string....
>That should do it. You can just tinker with the algorithm to make
>strings out of negative numbers or numbers with a decimal point. I
>suppose I can post actual code (with speed optimizations) if nessecary.
>But you learn more by doing it yourself...
Pretty neat, Jonathan. Although -- yikes -- is there no more direct way? Unbelievable. The routine takes about ten seconds to run. "Ti85 string functions aren't too good" is the understatement of the month. Unbelievably lousy. Is it any better on a 92, given its QWERTY keyboard and the "textpectations" that raises? What a cruddy Basic. They don't even have commented lines. Absurd case sensitivity. And, my TI-83 doesn't have a MOD function, at least not one that's documented (or that I can get working), although MOD is easy to simulate. Also the 83 doesn't accept ordinary string variables (like ST); you gotta use canned variables Str0, Str1, Str2, etc. But the following routine works for me. I'd enjoy to see your "speed optimizations" -- anything to cut this down to size or speed it up:
============CUT===============
"NON-EXECUTING COMMENTS IN STR1"=Str1
-167.12=A:"SOURCE VALUE"=Str1
A=B
abs(A)=A:"REMOVE MINUS SIGN IF NEGATIVE NUMBER"=Str1
" "=Str0:"CRASHES IF ZERO LENGTH"=Str1
If int(A)<>A:Then:"IF FRACTION EXISTS..."=Str1
fPart(A)=C
iPart(A)=A
Repeat int(C)=C:"REMOVE DECIMAL POINT FROM FRACTION"=Str1
C*10=C
End
Repeat C<1:"PARSE FRACTION"=Str1
C-int(C/10)*10=D:"SIMULATE MOD FUNCTION"=Str1
sub("0123456789",D+1,1)+Str0=Str0
(C-D)/10=C
End
"."+Str0=Str0:"RESTORE DECIMAL POINT"=Str1
End
Repeat A<1:"PARSE INTEGER"=Str1
A-int(A/10)*10=D
sub("0123456789",D+1,1)+Str0=Str0
(A-D)/10=A
End
sub(Str0,1,length(Str0)-1)=Str0:"REMOVE TRAILING SPACE"=Str1
If abs(B)<>B:Then:"-"+Str0=Str0:End:"RESTORE MINUS SIGN"=Str1
Disp B,Str0
Stop
============CUT===============
Thanks loads for your help.
>OK, here's how to make a number into a string....
>That should do it. You can just tinker with the algorithm to make
>strings out of negative numbers or numbers with a decimal point. I
>suppose I can post actual code (with speed optimizations) if necessary.
First: It makes me happy to see you put comments in your program.
However you probably double the execution time of the algorithm with
your comments. So when you get your program is debugged TAKE OUT THE
COMMENTS.
Explanation: When you store a new value into the number variable A, the
calculator just writes the new value and goes on. When you write a new
value to a string, the calculator must move all the other variables in
RAM after the string to make room for the new string (which is almost
always a new length). It also must update all the pointers. In fact, the
optimazation tip I'll give next involves completely eliminating the
sting variables from the main part of your program.
I don't want to type out a whole routine but I think i can explain it.
There is a variable on all the calculators called Ans. I believe from
observing the calcs that the Ans variable is right next to free ram. It
is therefore the fastest variable to change. So here's a bogus algorythm
that will show you how to exploit the answer variable. DISCLAIMER: Do
not ever, ever, write code like I'll be showing her for school, college
credit, or your job or you will fail and/or get fired.
" "
for(x,3,8
Ans+sub("0123456789",x,1
end
Ans -> Str0
This code will put the string "234567" into Str0 very quickly. Note that
the only variable that changes size is Ans, the fastest variable on the
TI.
( NOte the lack of close parens in the code. This is a great way to make
your programs smaller (they're not nessecary))
Now it gets worse. YOU CANNOT USE THE "STORE" OPERATOR ( -> ) ANYWHERE
IN YOUR ALGORITHM, OR YOU WILL DESTROY YOUR STRING! So if you need to
update the value of a real variable that keeps track of what digit
you're working on, you can't type
mod(C,10) -> C
Here's the workaround:
use a for loop to store stuff in real variables like this
for(C, mod(C,10), -10000000:end
This will change the value of C but not Ans. (Thanks TI) The idea of the
-1000000 is to put a value there that is always less than mod(C,10) so
that the loop always terminates without running. You don't want to loose
speed running an empty FOR loop! (Probably -1 will work fine for this
particular aplication.)
now you're ready to write string programs that don't contain the evil,
slow -> operator. It will be hell to debug them. AND DON'T PUT COMMENTS
IN, OR YOU'LL DESTROY YOUR STRING. Remeber: the ti EVALUATES your
comments; a real compiler on a computer IGNORES them.
********
You know, I feel kinds bad about posting this stuff. I know that at
least one beginner programmer will try to write programs like this and
learn terrible habbits. I programmed in Pascal for years with lots of
comments and READABIBILITY OPTIMAZATIONS, not speed optimazations,
before doing this stuff. I earned the right to write disgusting code
exclusively for myself for the express purpose of increasing speed. If
you're new to programming, please do not try to write programs using
these speed increasing tips. You will end up with programs that you
can't debug. Trust me, I still mess up the ans varible now and then with
a call to a misbehaved sub routine (one that changes Ans). And I NEVER
do these optimizations until I have a working copy of a nicely written
program backed up on my computer. Only AFTER completing a program do I
go back and speed it up.
With that out of the way, there's one more technique for speed
optimazation: write inline code. What I mean by inline code is you need
to remove all the loops from your program by explicitly writing out the
stuff in the loop over and over. This does little good on an 85 (it
tokenizes and links up stuff before running programs), but on my good
old 81, the speed increase you get from removing loops is significant.
Weigh the benifits of the speed increase against the extra memory loss
from writing out all that stuff. The decision is up to you. I sugesst
that you write the program, test it, change it, and see if the change
was worth it. That's how I learned these "secrets" for increasing speed.
Have fun.
-Jonathan
LinR {0,1},{A,A
Eq>St(RegEq,X
Sub(X,1,length X-3
Ans->B
This is faster than my " super-fast" algorythm for everything except
one or two digit integers, plus it's hastle free and memory-friendly.
I've included the e-mail documentation which has an explanation of how
the trick works and the names of the authors.
-Jonathan
*************************
Real-to-String/String-to-Real Modules
-------------------------------------
By Jon Niehof and Jason Wenger
------------------------------
Use these things however you wish.
Note that both routines assume that the input is in Ans and that the output
belongs in Ans. Careful--no error checking! Feel free to examine the code.
String to Real
--------------
Most of you have probably figured this out already. Simply convert the string
to an equation, then store the equation to a variable.
Real to String
--------------
This is a little more complex. It winds up destroying xStat and yStat.
It calculates a linear regression on (0,X) and (1,X) where X is the number
you want converted. Then, since the 85 returns a Regression equation (AHA!)
in the form intercept+slope*x, just convert it to a string, then you can
simply strip off everything from the plus sign on. Remember--we put in a
point (0,X), and the intercept is the point where the x-value is zero!
***NEW STUFF***
Actually, stripping from the "+" onward is more then necessary.
I've modified the program to implement the following suggestion:
<Begin quote>
Date: Sun, 06 Oct 1996 11:48:03 EDT
From: "Jason C. Wenger" <jwe...@juno.com>
To: jnie...@calvin.edu
Subject: Real to String
You are to be commended. You have found a truly graceful and FAST way of
converting real to strings. However, you did overlook one thing. The
regression equation is always the answer we need and "+0x", so we con
modify your program to say:
:{Ans,Ans}->Y
:{0,1}->X
:LinR X,Y
:Eq>St(RegEq,X)
Here's the new part:
:sub(X,1,lngth x-3)
All we have to do is cut off the last three numbers, which will ALWAYS be
"+0x" ! This speeds up the program considerably. Try it.
Please, if you can find any faults in my reasoning, (especially
situations where the regression equation does not end in "+0x") Please
tell me so. I'd like to post this updated version in the CALC-TI
news-list (with proper credit to you, of course) but I don't want to put
it in if it has any bugs.
Thanks for the great program,
Jason "Thursday" Wenger
jwe...@juno.com
Illegitimi no carborundum
<End quote>
I included that whole thing partially for the flattery, I admit :)
Thanks a lot to Jason for the insight! A great little birthday present
(The 6th was my birthday....)
Jason also reminded me of something that I forgot to include in the original $
The real->string routine only works between -7e499 and 7e499. I think we can $
Any questions? E-mail me:
jnie...@calvin.edu
Visit my page! www.calvin.edu/~jnieho38
>Real-to-String/String-to-Real Modules
>-------------------------------------
>By Jon Niehof and Jason Wenger
>------------------------------
The linear regression trick for number-to-$tring conversion needs to be
modified for the TI-83, but the principle is more or less the same (as on
the 85) -- in practice, it needs to be stated thusly:
-167.12=A ; e.g. a real number to convert...
{0,1}=L5:{A,A}=L6
LinReg(ax+b) L5,L6,Y0 ; resulting Y0 equation=0X+(-167.12)
Equ>String(Y0,Str0
sub(Str0,4,length(Str0)-3 ; strip leading "0X+" from Str0
Ans=Str0 ; Str0="-167.12"
You said that one should avoid all "->" operators in the routine, but it
seems labored not to abbreviate the last two lines as:
sub(Str0,4,length(Str0)-3)=Str0
The truth is, we're in $tringland from LinReg onward (derivation of the Y0
equation); it seems pretty clear to me that Y VARS are all stored
internally as strings.
Anyway, the kludge works -- amazing but true. Very clever. Only problem
is, in the middle of a long program the TI-83 interprets this code and then
insists on graphing the damn equation! A neat horizontal line, naturally.
Which isn't exactly what I had in mind. How the devil do I perform the
calc without triggering a graph?