http://www.perlmonks.org?node_id=1199065

This is an update to an old node by esteemed monk tilly.

Say you have a decimal number like 0.421875 and you want to print it as a fraction. Now, "obviously", that's equal to 27/64, but how do you write a program to find that out? The best way is with the method of continued fractions, and the surprise is that it's dead simple. This program produces a sequence of fractions that are increasingly good approximations of the input number.

```use Math::BigInt;
use Math::BigRat;
die 'number required' unless @ARGV == 1;
my \$x = my \$y = Math::BigRat->new(\$ARGV[0])->babs();
my \$h = my \$k1 = Math::BigInt->new(1);
my \$k = my \$h1 = Math::BigInt->new(0);
while (1) {
my \$t = \$y->as_int();
(\$h, \$h1) = (\$t * \$h + \$h1, \$h);
(\$k, \$k1) = (\$t * \$k + \$k1, \$k);
my \$val = Math::BigRat->new(\$h, \$k);
my \$err = \$val - \$x;
printf "%s: %s / %s = %.16g (%.1g)\n", \$t, \$h, \$k, \$val, \$err;
\$y -= \$t or last;
\$y = 1 / \$y;
}
__END__
0: 0 / 1 = 0 (-0.4)
2: 1 / 2 = 0.5 (0.08)
2: 2 / 5 = 0.4 (-0.02)
1: 3 / 7 = 0.4285714285714285 (0.007)
2: 8 / 19 = 0.4210526315789473 (-0.0008)
3: 27 / 64 = 0.421875 (0)