Calculator Community > TI-BASIC

Convert decimal to fraction as a string (TI-84)

(1/2) > >>

Giacomo Pigani:
Hi, I've wrote some programs in basic which make use of PrettyPrint (and symbolic to use pretty from my basic program)
Now, PrettyPrint takes it's input as a string, and for that I already use BatLib to convert numbers to strings, however I would like that if I have, let's say 0.375, to just be 1/8, which PrettyPrint displays in a nice form.

The TI-84 has a nice little function >Frac which converts an integer into a fraction and displays it in a nice way, hower I can't get the value as a string, nor get the nominator and denominator.

Looking around I found Zfrac, which deals with issue

--- Code: ---call _ZeroOP1
ld hl,OP1+1
ld (hl),'W'
call _RclVarSym
call _ToFrac
call _PushRealO1
call _ZeroOP1
ld hl,'W'
ld (OP1+1),hl
call _StoOther
call _OP1ExOP2
call _PushRealO1
call _ZeroOP1
ld hl,'I'
ld (OP1+1),hl
call _StoOther
ret

--- End code ---

What the code seems to be doing is taking advantage of the _ToFrac system call and then getting the output. This should then save the nominator and the denomitor to W and I, however I haven't been able to compile it or to get the compiled version to work (Yes, I used: Asm(prgmZCALC)


What I'd like, if somebody knows how is to have something like this:
"X^("+frac2str(0.375)+"*2)"-->Str1


Thank you!

E37:
I'm not sure why it won't compile. What is the first line of the the code that you are using Asm() on? @Giacomo Pigani
As for converting it to a fraction, I would suggest doing it manually in basic.
I assume you know the basic trick to convert a number into a string.
I would: Count the numbers past the '.', then make that number (without the decimal) over 10* the numbers past the decimal point. and reduce as much as possible.
The only problem would be repeating decimals, which would have to be dealt with some other way.
If you are looking for an assembler solution, you can try:
:clrhome
:disp A>Frac
:disp ""
:delvar str1
: *assemblyCode*


assemblyCode:
 ld hl,String
 rst 20h
 ld hl,16
 B_CALL(_CreateString)
 inc de
 inc de
 ld hl,textshadow
 ld bc,16
 ldir
 ret
String:
 .db StrngObj,tVarStrng,tStr1,0,0

That should store the first line of the display to str1. If you had your fraction displayed there, it is now a string in str1. Str1 will hold the whole first line, so you may need to trim some spaces. It returns tokens instead of characters. That means that randM( will show up instead of spaces and QuartReg will be instead of a fraction bar.

The hex code for it would be :21aa9de7211000ef27431313210885011000edb0c904aa000000

Xeda112358:
The algorithm described here might be useful as a BASIC solution, too :) It's even a bit more accurate than the built in one, oddly enough.
edit: The code from that link:

--- Code: ---:Ans→X:{1,abs(Ans
:Repeat E‾9>Ans(2
:abs(Ans(2){1,fPart(Ans(1)/Ans(2
:End
:round({X,1}/Ans(1),0
:Ans/gcd(Ans(1),Ans(2
--- End code ---

Giacomo Pigani:
Thanks a lot! Both yours solutions seems to be working well, however apart from the conversion, is there a simple way to create a method which returns the string?

Right now I'm using Xeda112358's answer like this:


--- Code: ---A:prgmFRAC:Ans->Str2
B:prgmFRAC:Ans->Str3
C:prgmFRAC:Ans->Str4

"X^("+Str2+"*2)*"+Str3+"="+Str4-->Str1
--- End code ---

However the code has become messy, is there a way to do something like this?


--- Code: ---"X^("+frac(A)+"*2)*"+frac(B)+"="+frac(C)-->Str1
--- End code ---

Maybe creating some sort of hook

E37:

--- Quote from: Giacomo Pigani on October 24, 2016, 02:41:47 am ---Thanks a lot! Both yours solutions seems to be working well, however apart from the conversion, is there a simple way to create a method which returns the string?

Right now I'm using Xeda112358's answer like this:


--- Code: ---A:prgmFRAC:Ans->Str2
B:prgmFRAC:Ans->Str3
C:prgmFRAC:Ans->Str4

"X^("+Str2+"*2)*"+Str3+"="+Str4-->Str1
--- End code ---

However the code has become messy, is there a way to do something like this?


--- Code: ---"X^("+frac(A)+"*2)*"+frac(B)+"="+frac(C)-->Str1
--- End code ---

Maybe creating some sort of hook

--- End quote ---
You can accomplish that with a parser hook, however that requires that the hook is an app. I would avoid that if at all possible. If your only problem is messy code, then that is not a good reason to fool with a parser hook. (If you do decide to use the hook, then I can't help you because it is beyond my level of skill currently)

Navigation

[0] Message Index

[#] Next page

Go to full version