More rational numerics.
This commit is contained in:
parent
f312ad57db
commit
18981acbe6
@ -32,10 +32,30 @@ 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;
|
||||
|
||||
34
Manticore/Num/Trig.pm
Normal file
34
Manticore/Num/Trig.pm
Normal file
@ -0,0 +1,34 @@
|
||||
package Manticore::Num::Trig;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
# 1/0! - x^2/2! + x^4/4! - x^6/6!
|
||||
sub proxCos {
|
||||
my ($steps, $x) = @_;
|
||||
my $mxsq = -$x*$x;
|
||||
my $sum = 0;
|
||||
my $fa = 1;
|
||||
for(1..$steps) {
|
||||
my $step2 = $_ * 2;
|
||||
$sum += $fa;
|
||||
$fa *= $mxsq/($step2*($step2-1))
|
||||
}
|
||||
return $sum
|
||||
}
|
||||
|
||||
# x/1! - x^3/3! + x^5/5! - x^7/7!
|
||||
sub proxSin {
|
||||
my ($steps, $x) = @_;
|
||||
my $mxsq = -$x*$x;
|
||||
my $sum = 0;
|
||||
my $fa = $x;
|
||||
for(1..$steps) {
|
||||
my $step2 = $_ * 2;
|
||||
$sum += $fa;
|
||||
$fa *= $mxsq/($step2*($step2+1))
|
||||
}
|
||||
return $sum
|
||||
}
|
||||
|
||||
1;
|
||||
@ -12,9 +12,13 @@ BEGIN {
|
||||
|
||||
use Manticore::Num::ContinuedFraction;
|
||||
|
||||
my $pi = Manticore::Num::ContinuedFraction->new(pi);
|
||||
my $pi = Manticore::Num::ContinuedFraction->new(7.25);
|
||||
|
||||
print $pi->child();
|
||||
#print $pi->child();
|
||||
#
|
||||
#print Data::Dumper::Dumper($pi);
|
||||
|
||||
print Data::Dumper::Dumper($pi);
|
||||
my $c = $pi->firstk(25);
|
||||
|
||||
print "[[ @$c ]]\n";
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user