Base converter with fractions?
02-13-2017, 01:14 PM
Post: #17
 Jan_D Member Posts: 69 Joined: Nov 2016
RE: Base converter with fractions?
Like Han pointed out in one of his good and important articles:

HP Prime CAS programming

it is also possible to write his program with the help of a CAS – helpprogram.

This could be the code:

Code:
 basecalc3(); cashelpprogram(); EXPORT basecalc3() BEGIN   local n:="45.76";     local b1:=8;   local b2:=4;   local maxdigits:=20; // maximum number of digits for frac part   local run:=1;   local t;   local s;           while run do     t:=input(       {          { n, [2], { 15, 80, 0 } },         { b1, [0], { 25, 25, 1 } },         { b2, [0], { 70, 25, 1 } },         { maxdigits, [0], {25,25, 2 } }       },       "Base Converter",       { "n=", "base1=", "base2=", "digits=" },       {         "Enter the integer",         "Enter the base of the integer n",         "Enter the base to convert to",         "Max allowable digits"       }       );     if t then            //when pressed the OK menu key.        if SIZE(n) AND 2<=b1<=62 AND 2<=b2<=62 then                     s:=cashelpprogram(n,b1,b2,maxdigits);                     if s(1)=1 then msgbox("Invalid digid "+s(2)); continue; end;                   n:=s(2);          t:=b1;          b1:=b2;          b2:=t;        else          msgbox("Invalid input";           end;              else       run:=0;            //when pressed Esc leave the loop     end;   end;                   //end loop END; #cas cashelpprogram(n,b1,b2,maxdigits) BEGIN      local j,k,t;     local d;     local ds;      local ipn;   local fpn;     local digits;   local tmp;             ipn:=""; // integer part of n   fpn:=""; // frac part of n   digits:="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";   tmp:={0,0};                k:=size(n);         j:=instring(n,".");         if j then           if (j-1>0) then ipn:=left(n,j-1); end;           if (k-j>0) then fpn:=right(n,k-j); end;           k:=j-2;         else           ipn:=n;           k:=size(n)-1;         end;                      // k = largest exponent                               //conversion of ipn to decimal form and storing it into tmp[1]:         t:=size(ipn);         for j from 1 to t do           ds:=mid(ipn,j,1);           d:=instring(digits,ds)-1;           if ((d >= b1) OR (d < 0)) then RETURN {1,ds}; end;            tmp[1]:=tmp[1]+d*b1^k;           k:=k-1;         end;                                              //k=-1 now!                      //conversion of fpn to decimal form and storing it into tmp[2]:         t:=size(fpn);         for j from 1 to t do           ds:=mid(fpn,j,1);           d:=instring(digits,ds)-1;           if ((d >= b1) OR (d < 0)) then RETURN {1,ds}; end;            tmp[2]:=tmp[2]+d*b1^k;           k:=k-1;         end;                           //tmp[2] contains a decimal number in the form of a fraction now.         n:="";                           //conversion of tmp[1] to base b2 and storing it into n         t:=size(ipn);         if t then           k:=ip(ln(tmp[1])/ln(b2));    //calculation of maximal power of b2                                               //which is less or equal to tmp[1]           for j from k downto 0 do             d:=ip(tmp[1]/(b2^j));             ds:=mid(digits,d+1,1);             n:=n+ds;             tmp[1]:=tmp[1]-d*b2^j;           end;         end;                     //conversion of tmp[2] to base b2 and storing it into n         t:=size(fpn);         if t then           n:=n+".";           for j from 1 to maxdigits do             d:=ip(tmp[2]*b2);             tmp[2]:=tmp[2]*b2-d;             // floating point issue             if tmp[2]<0 then               tmp[2]:=tmp[2]+1;               d:=d-1;             end;             ds:=mid(digits,d+1,1);             n:=n+ds;             if tmp[2]==0 then break; end;           end;         end;                 RETURN {0,n}; END; #end

Disadvantage of this method, though the code is easier, is that cashelpprogram() is not a local program.

Suppose that we had already written another program which also uses a Cas program with the name cashelpprogram(), then this would not affect the working of our program, but when we would execute the earlier program again it would not work properly anymore because it would use the most recent version of cashelpprogram(), and in order to let it work properly again we would have to recompile it.

This can be done easily though, by opening it in the Program Catalog and leaving it again.

So when we forget to do this it can cause unexpected problems.
 « Next Oldest | Next Newest »

 Messages In This Thread Base converter with fractions? - Joe Horn - 01-15-2017, 05:53 AM RE: Base converter with fractions? - Jan_D - 01-22-2017, 03:13 PM RE: Base converter with fractions? - Joe Horn - 01-22-2017, 11:22 PM RE: Base converter with fractions? - Jan_D - 01-26-2017, 12:38 PM RE: Base converter with fractions? - Joe Horn - 01-26-2017, 05:18 PM RE: Base converter with fractions? - Han - 01-27-2017, 05:00 AM RE: Base converter with fractions? - Joe Horn - 01-28-2017, 03:25 AM RE: Base converter with fractions? - Dieter - 01-28-2017, 03:15 PM RE: Base converter with fractions? - Han - 01-26-2017, 06:57 PM RE: Base converter with fractions? - Jan_D - 01-28-2017, 01:42 PM RE: Base converter with fractions? - Han - 01-27-2017, 04:37 AM RE: Base converter with fractions? - Jan_D - 02-02-2017, 05:26 PM RE: Base converter with fractions? - Jan_D - 02-03-2017, 01:51 PM RE: Base converter with fractions? - Jan_D - 02-21-2017, 05:59 PM RE: Base converter with fractions? - Han - 02-21-2017, 07:30 PM RE: Base converter with fractions? - Jan_D - 02-22-2017, 06:03 PM RE: Base converter with fractions? - Gerald H - 01-28-2017, 02:58 PM RE: Base converter with fractions? - Han - 02-03-2017, 02:31 PM RE: Base converter with fractions? - toml_12953 - 02-13-2017, 02:36 PM RE: Base converter with fractions? - Jan_D - 02-03-2017, 05:52 PM RE: Base converter with fractions? - Jan_D - 02-13-2017 01:14 PM RE: Base converter with fractions? - Han - 02-13-2017, 01:27 PM

User(s) browsing this thread: 1 Guest(s)