62 lines
1.1 KiB
Perl
62 lines
1.1 KiB
Perl
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)};
|
|
if(0 == $r) {
|
|
return undef
|
|
}
|
|
my $c = Manticore::Num::ContinuedFraction->new(1/$r);
|
|
$self->{c} = $c;
|
|
}
|
|
return $self->{c}
|
|
}
|
|
|
|
sub firstk {
|
|
my ($self, $k) = @_;
|
|
if($k <= 0) {
|
|
return []
|
|
} else {
|
|
my $c = $self->child();
|
|
return [$self->{a}] unless defined $c;
|
|
my $l = $c->firstk($k-1);
|
|
unshift @$l, $self->{a};
|
|
return $l
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
1;
|