1N/A#!./perl
1N/A
1N/A#
1N/A# Regression tests for the Math::Trig package
1N/A#
1N/A# The tests are quite modest as the Math::Complex tests exercise
1N/A# these quite vigorously.
1N/A#
1N/A# -- Jarkko Hietaniemi, April 1997
1N/A
1N/ABEGIN {
1N/A chdir 't' if -d 't';
1N/A @INC = '../lib';
1N/A}
1N/A
1N/Ause Math::Trig;
1N/A
1N/Ause strict;
1N/A
1N/Ause vars qw($x $y $z);
1N/A
1N/Amy $eps = 1e-11;
1N/A
1N/Aif ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
1N/A $eps = 1e-10;
1N/A}
1N/A
1N/Asub near ($$;$) {
1N/A my $e = defined $_[2] ? $_[2] : $eps;
1N/A print "# near? $_[0] $_[1] $e\n";
1N/A $_[1] ? (abs($_[0]/$_[1] - 1) < $e) : abs($_[0]) < $e;
1N/A}
1N/A
1N/Aprint "1..29\n";
1N/A
1N/A$x = 0.9;
1N/Aprint 'not ' unless (near(tan($x), sin($x) / cos($x)));
1N/Aprint "ok 1\n";
1N/A
1N/Aprint 'not ' unless (near(sinh(2), 3.62686040784702));
1N/Aprint "ok 2\n";
1N/A
1N/Aprint 'not ' unless (near(acsch(0.1), 2.99822295029797));
1N/Aprint "ok 3\n";
1N/A
1N/A$x = asin(2);
1N/Aprint 'not ' unless (ref $x eq 'Math::Complex');
1N/Aprint "ok 4\n";
1N/A
1N/A# avoid using Math::Complex here
1N/A$x =~ /^([^-]+)(-[^i]+)i$/;
1N/A($y, $z) = ($1, $2);
1N/Aprint 'not ' unless (near($y, 1.5707963267949) and
1N/A near($z, -1.31695789692482));
1N/Aprint "ok 5\n";
1N/A
1N/Aprint 'not ' unless (near(deg2rad(90), pi/2));
1N/Aprint "ok 6\n";
1N/A
1N/Aprint 'not ' unless (near(rad2deg(pi), 180));
1N/Aprint "ok 7\n";
1N/A
1N/Ause Math::Trig ':radial';
1N/A
1N/A{
1N/A my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
1N/A
1N/A print 'not ' unless (near($r, sqrt(2))) and
1N/A (near($t, deg2rad(45))) and
1N/A (near($z, 1));
1N/A print "ok 8\n";
1N/A
1N/A ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
1N/A
1N/A print 'not ' unless (near($x, 1)) and
1N/A (near($y, 1)) and
1N/A (near($z, 1));
1N/A print "ok 9\n";
1N/A
1N/A ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
1N/A
1N/A print 'not ' unless (near($r, sqrt(2))) and
1N/A (near($t, deg2rad(45))) and
1N/A (near($z, 0));
1N/A print "ok 10\n";
1N/A
1N/A ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
1N/A
1N/A print 'not ' unless (near($x, 1)) and
1N/A (near($y, 1)) and
1N/A (near($z, 0));
1N/A print "ok 11\n";
1N/A}
1N/A
1N/A{
1N/A my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
1N/A
1N/A print 'not ' unless (near($r, sqrt(3))) and
1N/A (near($t, deg2rad(45))) and
1N/A (near($f, atan2(sqrt(2), 1)));
1N/A print "ok 12\n";
1N/A
1N/A ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
1N/A
1N/A print 'not ' unless (near($x, 1)) and
1N/A (near($y, 1)) and
1N/A (near($z, 1));
1N/A print "ok 13\n";
1N/A
1N/A ($r,$t,$f) = cartesian_to_spherical(1,1,0);
1N/A
1N/A print 'not ' unless (near($r, sqrt(2))) and
1N/A (near($t, deg2rad(45))) and
1N/A (near($f, deg2rad(90)));
1N/A print "ok 14\n";
1N/A
1N/A ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
1N/A
1N/A print 'not ' unless (near($x, 1)) and
1N/A (near($y, 1)) and
1N/A (near($z, 0));
1N/A print "ok 15\n";
1N/A}
1N/A
1N/A{
1N/A my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
1N/A
1N/A print 'not ' unless (near($r, 1)) and
1N/A (near($t, 1)) and
1N/A (near($z, 1));
1N/A print "ok 16\n";
1N/A
1N/A ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
1N/A
1N/A print 'not ' unless (near($r, 1)) and
1N/A (near($t, 1)) and
1N/A (near($z, 1));
1N/A print "ok 17\n";
1N/A}
1N/A
1N/A{
1N/A use Math::Trig 'great_circle_distance';
1N/A
1N/A print 'not '
1N/A unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2));
1N/A print "ok 18\n";
1N/A
1N/A print 'not '
1N/A unless (near(great_circle_distance(0, 0, pi, pi), pi));
1N/A print "ok 19\n";
1N/A
1N/A # London to Tokyo.
1N/A my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
1N/A my @T = (deg2rad(139.8),deg2rad(90 - 35.7));
1N/A
1N/A my $km = great_circle_distance(@L, @T, 6378);
1N/A
1N/A print 'not ' unless (near($km, 9605.26637021388));
1N/A print "ok 20\n";
1N/A}
1N/A
1N/A{
1N/A my $R2D = 57.295779513082320876798154814169;
1N/A
1N/A sub frac { $_[0] - int($_[0]) }
1N/A
1N/A my $lotta_radians = deg2rad(1E+20, 1);
1N/A print "not " unless near($lotta_radians, 1E+20/$R2D);
1N/A print "ok 21\n";
1N/A
1N/A my $negat_degrees = rad2deg(-1E20, 1);
1N/A print "not " unless near($negat_degrees, -1E+20*$R2D);
1N/A print "ok 22\n";
1N/A
1N/A my $posit_degrees = rad2deg(-10000, 1);
1N/A print "not " unless near($posit_degrees, -10000*$R2D);
1N/A print "ok 23\n";
1N/A}
1N/A
1N/A{
1N/A use Math::Trig 'great_circle_direction';
1N/A
1N/A print 'not '
1N/A unless (near(great_circle_direction(0, 0, 0, pi/2), pi));
1N/A print "ok 24\n";
1N/A
1N/A# Retired test: Relies on atan(0, 0), which is not portable.
1N/A# print 'not '
1N/A# unless (near(great_circle_direction(0, 0, pi, pi), -pi()/2));
1N/A print "ok 25\n";
1N/A
1N/A my @London = (deg2rad( -0.167), deg2rad(90 - 51.3));
1N/A my @Tokyo = (deg2rad( 139.5), deg2rad(90 - 35.7));
1N/A my @Berlin = (deg2rad ( 13.417), deg2rad(90 - 52.533));
1N/A my @Paris = (deg2rad ( 2.333), deg2rad(90 - 48.867));
1N/A
1N/A print 'not '
1N/A unless (near(rad2deg(great_circle_direction(@London, @Tokyo)),
1N/A 31.791945393073));
1N/A
1N/A print "ok 26\n";
1N/A print 'not '
1N/A unless (near(rad2deg(great_circle_direction(@Tokyo, @London)),
1N/A 336.069766430326));
1N/A print "ok 27\n";
1N/A
1N/A print 'not '
1N/A unless (near(rad2deg(great_circle_direction(@Berlin, @Paris)),
1N/A 246.800348034667));
1N/A
1N/A print "ok 28\n";
1N/A print 'not '
1N/A unless (near(rad2deg(great_circle_direction(@Paris, @Berlin)),
1N/A 58.2079877553156));
1N/A print "ok 29\n";
1N/A}
1N/A
1N/A# eof