package Manticore::Num::ContinuedFraction; # lazy and potential infinite continued fraction use strict; use warnings; use Math::BigRat; # Approximation object: # { # a => BigRat, this integer # r => remainder, input number; undef if fraction has ended # c => child, better approximation (or undef) # e => end of list? # } sub new { my ($pkg, $x) = @_; die "ContinuedFraction can only be constructed of positive numbers!" if $x<0; my $prop = int $x; if(0 == $prop) { return undef; # TODO } my $rem = $x - $prop; return bless { a => $prop, r => $rem, } } sub child { my $self = shift; if(not $self->{c}) { my ($a, $r) = @{$self}{qw(a r)}; my $c = Manticore::Num::ContinuedFraction->new(1/$r); $self->{c} = $c; } return $self->{c} } 1;