More rounding mechanisms and tests for ContinuedFraction numerics.

This commit is contained in:
Stephan Barth 2023-12-28 11:59:20 +01:00
parent 18981acbe6
commit 0f1c43023e
3 changed files with 74 additions and 20 deletions

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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";