[PD] number to fractions external?

Mathieu Bouchard matju at artengine.ca
Sat Dec 17 21:29:00 CET 2011


Le 2011-12-17 à 03:44:00, i go bananas a écrit :

> how do you get from a continued fraction in the form like this:
> [0;1,5,2,2] 
> to a fraction in the form like this:
> 27/32
> this patch gets as far as that [0; 1,5,2,2] form.  but i'm still not sure how to get further

keep track of the last two fractions and feed that to something like expr.

e.g.
$f1 = element of the list
$f2 = last numerator
$f3 = last denominator
$f4 = next-to-last numerator
$f5 = next-to-last denominator

the number before the semicolon should go with the values 1 0 0 1 to start 
the algorithm (yes, you have to pretend that a denominator is zero, but 
don't worry)

then it would be something like :

[expr $f1*$f2+$f4;
       $f1*$f3+$f5;
       $f2;
       $f3]

and then you take the four outputs and feed them back into expr together 
with the next number in the continued fraction...

the advantage of this, is that you can go left-to-right.

With the more obvious method, you have to go right-to-left, because the 
innermost terms (inside many parentheses) are at the right.

The left-to-right formula is something I learned in Number Theory course 
in fév.2003, but naturally, I had to look it up in Wikipédia in order to 
remember, as I don't use this very often...

Let's try the formula with [0;1,5,2,2] :
0 1 0 0 1 gives 0 1 1 0
1 0 1 1 0 gives 1 1 0 1
5 1 1 0 1 gives 5 6 1 1
2 5 6 1 1 gives 11 13 5 6
2 11 13 5 6 gives 27 32 11 13

looks like it works ! 27 32 appear in the 2nd and 1st outlets.

  ______________________________________________________________________
| Mathieu BOUCHARD ----- téléphone : +1.514.383.3801 ----- Montréal, QC


More information about the Pd-list mailing list