Here https://github.com/wlmb/Photonic/blob/5e94c0a940dff619492ac0a629eba26a4bff3259/lib/Photonic/Utils.pm#L294-L327 is a PDL-using implementation of the Lentz method of continuing fractions, algorithm from Numerical Recipes:
sub lentzCF { # continued fraction using lentz method Numerical Recipes # p. 171. Arguments are as, bs, maximum number of iterations and # smallness parameter # a0+b1/a1+b2+... my $as=shift; my $bs=shift; my $max=shift; my $small=shift; my $tiny=r2C(1.e-30); my $converged=0; my $fn=$as->slice(0); $fn=$tiny if all($fn==0); my $n=1; my ($fnm1, $Cnm1, $Dnm1)=($fn, $fn, r2C(0)); #previous coeffs. my ($Cn, $Dn); #current coeffs. my $Deltan; while($n<$max){ $Dn=$as->slice($n)+$bs->slice($n)*$Dnm1; $Dn=$tiny if all($Dn==0); $Cn=$as->slice($n)+$bs->slice($n)/$Cnm1; $Cn=$tiny if all($Cn==0); $Dn=1/$Dn; $Deltan=$Cn*$Dn; $fn=$fnm1*$Deltan; last if $converged=$Deltan->approx(1, $small)->all; $fnm1=$fn; $Dnm1=$Dn; $Cnm1=$Cn; $n++; } $fn = $fn->slice("(0)"); return wantarray? ($fn, $n): $fn; }
Having made the odd tweak to it myself in passing, it continues to make me want to rewrite it in PP, which would probably go much quicker and would also allow pthreading for big batches of numbers. Probably it would live most naturally in either PDL::Math (the core), or maybe a PDL::NumericalAnalysis?

In reply to Re: decimal to fraction by etj
in thread decimal to fraction by no_slogan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.