diff --git a/Manticore/Mantis.pm b/Manticore/Mantis.pm index 3bc75a9..dfbb3eb 100644 --- a/Manticore/Mantis.pm +++ b/Manticore/Mantis.pm @@ -141,23 +141,25 @@ sub fontLoader { next unless $fn=~m#(.*)\.mant$#; my $gly = $1; my $fullfile = "$dir/$fn"; - my $fh = undef; - open($fh, '<', $fullfile) or die "Could not read file '$fullfile', because: $!"; - my @blocks = ([]); - while(my $line = <$fh>) { - if($line=~m#^===#) { - push @blocks, [] - } else { - push @{$blocks[-1]}, $line - } - } - my ($head) = @{shift @blocks}; - if($head=~m#^(\S+)#) { - die "Head glyphem identifier does not equal file name in '$fullfile' ('$1' vs. '$gly')" unless $1 eq $gly - } else { - die "Malformed head in file $fullfile" - } - $font{$gly}{data} = [map {rowText2data(join '', @$_)} @blocks] + #my $fh = undef; + #open($fh, '<', $fullfile) or die "Could not read file '$fullfile', because: $!"; + #my @blocks = ([]); + #while(my $line = <$fh>) { + # if($line=~m#^===#) { + # push @blocks, [] + # } else { + # push @{$blocks[-1]}, $line + # } + #} + #my ($head) = @{shift @blocks}; + #if($head=~m#^(\S+)#) { + # die "Head glyphem identifier does not equal file name in '$fullfile' ('$1' vs. '$gly')" unless $1 eq $gly + #} else { + # die "Malformed head in file $fullfile" + #} + #$font{$gly}{data} = [map {rowText2data(join '', @$_)} @blocks] + # TODO above is old code, below is new code, but not working yet + $font{$gly}{data} = loadFile($fullfile); } } } diff --git a/Manticore/Num/ContinuedFraction.pm b/Manticore/Num/ContinuedFraction.pm index f6dde0b..cd55c15 100644 --- a/Manticore/Num/ContinuedFraction.pm +++ b/Manticore/Num/ContinuedFraction.pm @@ -7,6 +7,7 @@ use warnings; use Math::BigRat; +### Methods and constructor # Approximation object: # { # a => BigRat, this integer @@ -54,8 +55,39 @@ sub firstk { } } +# take the common start part, but at most k parts +sub commonk { + my ($self, $other, $k) = @_; + if($k <= 0) { + return [] + } else { + return [] if $self->{a} != $other->{a}; + my $c = $self->child(); + my $d = $other->child(); + return [$self->{a}] unless defined $c; + my $l = $c->commonk($d, $k-1); + unshift @$l, $self->{a}; + return $l + } +} +### Functions +# list of integers -> bigrat +sub reconstruct { + my $l = shift; + my $ret = Math::BigRat->new($l->[-1]); + for(reverse (0..$#$l-1)) { + $ret = $l->[$_] + 1/$ret + } + return $ret; +} - +# round to bigRat with ContinuedFraction of $steps parts +sub roundSteps { + my ($steps, $x) = @_; + my $cf = Manticore::Num::ContinuedFraction->new($x); + my $l = $cf->firstk($steps); + return reconstruct($l); +} 1; diff --git a/tests/testCf.pl b/tests/testCf.pl index cd5e7c6..5a2b01f 100755 --- a/tests/testCf.pl +++ b/tests/testCf.pl @@ -4,21 +4,41 @@ use strict; use warnings; use Math::Trig; +use Math::BigRat lib => 'GMP'; use Data::Dumper; BEGIN { push @INC, '..' } +use Manticore::Num::Trig; use Manticore::Num::ContinuedFraction; -my $pi = Manticore::Num::ContinuedFraction->new(7.25); +my $pi = Math::BigRat->new(3); + +my $poly = 30; + +for(1..5) { +$pi = $pi + Manticore::Num::Trig::proxSin($poly,$pi); +print "===\ns: $pi\n"; +$pi = Manticore::Num::ContinuedFraction::roundSteps($poly,$pi); +print "r: $pi\n"; +} + +my $pi1 = Manticore::Num::ContinuedFraction->new($pi); + +$pi = $pi + Manticore::Num::Trig::proxSin($poly+2,$pi); +print "===\ns: $pi\n"; +$pi = Manticore::Num::ContinuedFraction::roundSteps($poly,$pi); +print "r: $pi\n"; + +my $pi2 = Manticore::Num::ContinuedFraction->new($pi); #print $pi->child(); # #print Data::Dumper::Dumper($pi); -my $c = $pi->firstk(25); +my $c = $pi1->commonk($pi2, 25); print "[[ @$c ]]\n";