For example, if I have the expression 2/3, I want to get 2 and 3 as seperate numbers. Can I do that in 68k TI-BASIC?
I don't know where Battlesquid is at, but he'll be around to help you shortly.
Convert the number variable containing your fraction to a string. Use inString to return position of "/" in that string then isolate numerator and denominator using Left( and Right( based on the returned position. You may want to handle circumstance where input isn't a fraction. Shout if you need code example
Assuming that the fraction is in a variable named frac:
setMode("Exact/Approx","EXACT")
string(frac)&"/1"→str
inString(str,"/")→pos
dim(str)-pos→pos2
expr(mid(str,1,pos-1))→num
expr(right(str,pos2))→denom
Close ;)
I usually make overly complicated programs. I think I know how to incorporate right() into the code, but making this code was way more fun :P
Definitely fun.. (I might have a go at a code example for this after some sleep :o) Typo on last line tho? Should that be pos2 not pos?
Fixed that typo, thanks for pointing that out :)
NP, should read pos2+1 though? :p.
Suggest..
string(frac)&"/1"→str
..to avoid Domain Error when handling fractions that resolve to whole numbers e.g 3/1, 16/16.. code will return Denom of 1.
Last three lines could be boiled down to 2 and no need for variables, but will leave that for now as I see OP has found in-built functions to the job a whole lot better:)
Woah, never saw that solution, great job Step7! I decided to put everything in variables because it would be easier to read.
Also, the code works with the current expr(right(str,pos2))→denom, adding one would increase the value of the denominator, which would be inaccurate, and putting the one inside the right() command yields a syntax error.
And I never realized that there were commands that separate the numerator and denominator. Like I said, I always make overly complicated programs :)
I was suggesting adding 1 to Pos2 (not resulting Denom), to skip one place right of the "/", think as you have it will get "/3" returned and error on evaluating that expression?
expr(right(str,pos2+1))→denom
Yep, I still get a syntax error. The only way I could position the 1 was to add it outside the parenthesis, and that just added 1 to the value of the denominator. I tried out using 2/3, and it worked perfectly…
Think I've muddled the code I was thinking of with what you've listed.
Try..
setMode("Exact/Approx","EXACT")
string(frac)&"/1"→str
inString(str,"/")→pos
expr(left(str,pos-1))→num
expr(right(str,pos+1))→denom
Yep, that works! Great job on the optimization :)
2/3→frac
getNum(frac) © 2
getDenom(frac) © 3
Wiki pages please? I honestly don't know how to fill out the command template.
Nice work :) works with unity denominators too, which squid's code won't (domain errors for 3/1, 16/16).
+1 on TO DO:
tibasicdev•wikidot•com/68k:getdenom
tibasicdev•wikidot•com/68k:getnum
I have a similar requirement to extract powers from expressions like 22.55. Squid's code could be modified to yield those, but is there built in functions to do this also? Will start a new thread if no answers here..