Post Reply 
The new dfc and dfc2f functions in action
07-10-2018, 06:47 AM
Post: #7
RE: The new dfc and dfc2f functions in action
(01-10-2018 02:07 PM)DrD Wrote:  I tried this:

Code:

#cas
edfc(nbr1:=e,nbr2:=sqrt(7.389)):=
begin  
  LOCAL n;
  M0:=dfc(nbr1);
  M1:=dfc(nbr2);
  M2:=MAX(M0,M1)-MIN(M0,M1);

  n:=POS(mat2list(map(M2,(x)->x>0)),1);  // Probably a better way to find a non-zero element in vector?

  L1:=sub(M0,1,n);
  L2:=sub(M1,1,n); 

  IF L1(n)<=L2(n) THEN
    L1(n):=L1(n)+1;
    n:=dfc2f(L1);
  ELSE
    L2(n):=L2(n)+1;
    n:=dfc2f(L2); 
  END;

  return n;
end;
#end

edfc(); // uses defaults: [ e,sqrt(7.389) ) ==> 1071/394 = 2.71827411168

edfc(0.7965, 0.7975); // From first example: ==> 47/59 = 0.796610169492


This is just a quick program push out, not completely tested!!

-Dale-

Some firmware versions don't allow CAS programs to pre-set variables' values, so I suggest replacing the first line with just
Code:
edfc(nbr1,nbr2):=

Suggestion: Replace these two lines:
Code:
M0:=dfc(nbr1);
M1:=dfc(nbr2);
... with these two lines:
Code:
M0:=append(dfc(exact(nbr1)),MAXREAL);
M1:=append(dfc(exact(nbr2)),MAXREAL);
N.B. MAXREAL must be typed in uppercase.

This solves two problems:
(1) It prevents early-terminating continued fractions from erroring out, e.g. edfc(0.4, 0.5).
(2) It gets the intended result for decimal inputs, e.g. edfc(0.4, 0.41), which should return 9/22, but otherwise returns 2/5, because 0.4 in CAS is really slightly less than 0.4 due to CAS' use of binary floats.

Thanks for a great program, Dale!

<0|ΙΈ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: The new dfc and dfc2f functions in action - Joe Horn - 07-10-2018 06:47 AM



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