100 lines
2.1 KiB
Perl
Executable File
100 lines
2.1 KiB
Perl
Executable File
#!/usr/bin/env perl
|
|
|
|
# Preexperiments to rational geometry
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use Math::Trig;
|
|
|
|
# generate rational approximations to nth parts of an arc represented by sin and cos
|
|
# approach leads to values in principal usable as first iteration, but has vastly to big numbers
|
|
sub nthArc {
|
|
my $n = shift;
|
|
# basis triangle with half the angle
|
|
my $prex = $n;
|
|
my $prey = 1;
|
|
dumpangles("pre", $prex, $prey, 45/$n);
|
|
# complex multiplication, double the angle
|
|
my $x = $prex*$prex - $prey*$prey;
|
|
my $y = 2*$prex*$prey;
|
|
dumpangles("square", $x, $y, 90/$n);
|
|
my $npowx = 1;
|
|
my $npowy = 0;
|
|
for(1..$n) {
|
|
my $nnpowx = $x*$npowx - $y*$npowy;
|
|
my $nnpowy = $x*$npowy + $y*$npowx;
|
|
$npowx = $nnpowx;
|
|
$npowy = $nnpowy;
|
|
}
|
|
dumpangles("n-powered", $npowx, $npowy, 90);
|
|
}
|
|
|
|
sub dumpangles {
|
|
my ($name, $x, $y, $compare) = @_;
|
|
my $goal = "";
|
|
$goal = " (goal: $compare)" if $compare;;
|
|
my $phi = atan2($y,$x)/pi*180;
|
|
print "[$name] $x $y -- $phi$goal\n";
|
|
}
|
|
|
|
# exp is
|
|
# sum_n=0..inf x^n/n!
|
|
|
|
# 1/0! - x^2/2! + x^4/4! - x^6/6!
|
|
sub preCos {
|
|
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 preSin {
|
|
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
|
|
}
|
|
|
|
|
|
while(<>) {
|
|
# if(m#^([1-9][0-9]*)$#) {
|
|
# nthArc($1)
|
|
# } else {
|
|
# print "Bad parameter\n";
|
|
# }
|
|
if(m#^([1-9][0-9]*)\s+([0-9\.]+)$#) {
|
|
my ($st, $x) = ($1, $2);
|
|
my $c = cos($x);
|
|
my $cappr = preCos($st, $x);
|
|
print "$x -- c:$c cappr:$cappr\n";
|
|
my $s = sin($x);
|
|
my $sappr = preSin($st, $x);
|
|
print "$x -- s:$s sappr:$sappr\n";
|
|
} elsif(m#pi\s+([0-9]+)\s+([0-9]+)#) {
|
|
my ($bigit, $smallit) = ($1, $2);
|
|
my $pi = 22/7;
|
|
for(1..$bigit) {
|
|
$pi = $pi + preSin($smallit, $pi);
|
|
}
|
|
print "piappr: $pi -- truepu: ", pi, "\n"
|
|
} else {
|
|
print "Bad parameters\n";
|
|
}
|
|
}
|
|
|
|
|