Author Topic: Convert decimal to fraction as a string (TI-84)  (Read 7999 times)

0 Members and 1 Guest are viewing this topic.

Offline Giacomo Pigani

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 12
  • Rating: +0/-0
    • View Profile
Convert decimal to fraction as a string (TI-84)
« on: October 22, 2016, 05:47:18 pm »
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: [Select]
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

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!

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #1 on: October 23, 2016, 01:12:17 pm »
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
« Last Edit: October 23, 2016, 01:42:23 pm by E37 »
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #2 on: October 23, 2016, 02:36:52 pm »
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: [Select]
: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

Offline Giacomo Pigani

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 12
  • Rating: +0/-0
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #3 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: [Select]
A:prgmFRAC:Ans->Str2
B:prgmFRAC:Ans->Str3
C:prgmFRAC:Ans->Str4

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

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

Code: [Select]
"X^("+frac(A)+"*2)*"+frac(B)+"="+frac(C)-->Str1
Maybe creating some sort of hook

Offline E37

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 358
  • Rating: +23/-0
  • Trial and error is the best teacher
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #4 on: October 24, 2016, 02:29:26 pm »
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: [Select]
A:prgmFRAC:Ans->Str2
B:prgmFRAC:Ans->Str3
C:prgmFRAC:Ans->Str4

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

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

Code: [Select]
"X^("+frac(A)+"*2)*"+frac(B)+"="+frac(C)-->Str1
Maybe creating some sort of hook
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)
I'm still around... kind of.

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #5 on: October 24, 2016, 05:05:23 pm »
As @E37 said, it would require a parser hook, which is probably overkill. One question I have, though: For what purpose do you need this? Depending on the context, there could be elegant solutions available.

Offline Giacomo Pigani

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 12
  • Rating: +0/-0
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #6 on: October 24, 2016, 07:12:53 pm »
Thanks again, it was just for some statistics programs I've been making.
Basically I output some steps using PrettyPrint, which requires a string, so I convert everything to string.
Since I spent a lot of time making it, I wanted it to be perfect, so I decided I would turn decimal fractions like 0,4375 into something more readable like 7/16.

Now I've done that, thanks to you. Who cares if the code is messier ;-)

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #7 on: October 24, 2016, 08:02:41 pm »
Well, I mean if your code is only working with rational numbers (a/b, with a,b integers and b>0), you can manually keep track of the numerators and denominators. For example, take two numbers, 3/7 and 15/11, and represent them as {3,7->L1:{15,11->L2.

Addition:
{L1(1)L2(2)+L1(2)L2(1),L1(2)L2(2):Ans/gcd(abs(Ans(1)),abs(Ans(2
Subtraction:
{L1(1)L2(2)-L1(2)L2(1),L1(2)L2(2):Ans/gcd(abs(Ans(1)),abs(Ans(2
Multiplication:
{L1(1)L2(1),L1(2)L2(2):Ans/gcd(abs(Ans(1)),abs(Ans(2
Division:
{L1(1)L2(2),L1(2)L2(1):Ans/gcd(abs(Ans(1)),abs(Ans(2


Offline Giacomo Pigani

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 12
  • Rating: +0/-0
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #8 on: October 25, 2016, 06:42:10 pm »
Thanks for the idea but that's just not possible, however I just remembered that omnicalc features a simplify function.
I already tested on simple values, where it would just return the input, however i believe that if i give it in input the function that calculates the decimal fraction it should simplify it in a regular fraction

By the way, thank you for making batlib, it was a great resource for my project!

Offline Xeda112358

  • they/them
  • Moderator
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 4704
  • Rating: +719/-6
  • Calc-u-lator, do doo doo do do do.
    • View Profile
Re: Convert decimal to fraction as a string (TI-84)
« Reply #9 on: October 26, 2016, 11:19:58 am »
I'm glad to hear it! Every so often, I take time to look at my code and try to come up with better ways of doing things. who knows, maybe in a few years there will be a Batlib 2 :P