4 # Regression tests for the Math::Trig package
6 # The tests are quite modest as the Math::Complex tests exercise
7 # these quite vigorously.
9 # -- Jarkko Hietaniemi, April 1997
20 use vars qw($x $y $z);
24 if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
29 my $e = defined $_[2] ? $_[2] : $eps;
30 print "# near? $_[0] $_[1] $e\n";
31 $_[1] ? (abs($_[0]/$_[1] - 1) < $e) : abs($_[0]) < $e;
37 print 'not ' unless (near(tan($x), sin($x) / cos($x)));
40 print 'not ' unless (near(sinh(2), 3.62686040784702));
43 print 'not ' unless (near(acsch(0.1), 2.99822295029797));
47 print 'not ' unless (ref $x eq 'Math::Complex');
50 # avoid using Math::Complex here
51 $x =~ /^([^-]+)(-[^i]+)i$/;
53 print 'not ' unless (near($y, 1.5707963267949) and
54 near($z, -1.31695789692482));
57 print 'not ' unless (near(deg2rad(90), pi/2));
60 print 'not ' unless (near(rad2deg(pi), 180));
63 use Math::Trig ':radial';
66 my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
68 print 'not ' unless (near($r, sqrt(2))) and
69 (near($t, deg2rad(45))) and
73 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
75 print 'not ' unless (near($x, 1)) and
80 ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
82 print 'not ' unless (near($r, sqrt(2))) and
83 (near($t, deg2rad(45))) and
87 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
89 print 'not ' unless (near($x, 1)) and
96 my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
98 print 'not ' unless (near($r, sqrt(3))) and
99 (near($t, deg2rad(45))) and
100 (near($f, atan2(sqrt(2), 1)));
103 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
105 print 'not ' unless (near($x, 1)) and
110 ($r,$t,$f) = cartesian_to_spherical(1,1,0);
112 print 'not ' unless (near($r, sqrt(2))) and
113 (near($t, deg2rad(45))) and
114 (near($f, deg2rad(90)));
117 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
119 print 'not ' unless (near($x, 1)) and
126 my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
128 print 'not ' unless (near($r, 1)) and
133 ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
135 print 'not ' unless (near($r, 1)) and
142 use Math::Trig 'great_circle_distance';
145 unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2));
149 unless (near(great_circle_distance(0, 0, pi, pi), pi));
153 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
154 my @T = (deg2rad(139.8),deg2rad(90 - 35.7));
156 my $km = great_circle_distance(@L, @T, 6378);
158 print 'not ' unless (near($km, 9605.26637021388));
163 my $R2D = 57.295779513082320876798154814169;
165 sub frac { $_[0] - int($_[0]) }
167 my $lotta_radians = deg2rad(1E+20, 1);
168 print "not " unless near($lotta_radians, 1E+20/$R2D);
171 my $negat_degrees = rad2deg(-1E20, 1);
172 print "not " unless near($negat_degrees, -1E+20*$R2D);
175 my $posit_degrees = rad2deg(-10000, 1);
176 print "not " unless near($posit_degrees, -10000*$R2D);
181 use Math::Trig 'great_circle_direction';
184 unless (near(great_circle_direction(0, 0, 0, pi/2), pi));
187 # Retired test: Relies on atan(0, 0), which is not portable.
189 # unless (near(great_circle_direction(0, 0, pi, pi), -pi()/2));
192 my @London = (deg2rad( -0.167), deg2rad(90 - 51.3));
193 my @Tokyo = (deg2rad( 139.5), deg2rad(90 - 35.7));
194 my @Berlin = (deg2rad ( 13.417), deg2rad(90 - 52.533));
195 my @Paris = (deg2rad ( 2.333), deg2rad(90 - 48.867));
198 unless (near(rad2deg(great_circle_direction(@London, @Tokyo)),
203 unless (near(rad2deg(great_circle_direction(@Tokyo, @London)),
208 unless (near(rad2deg(great_circle_direction(@Berlin, @Paris)),
213 unless (near(rad2deg(great_circle_direction(@Paris, @Berlin)),