4 # Regression tests for the Math::Trig package
6 # The tests here are quite modest as the Math::Complex tests exercise
7 # these interfaces quite vigorously.
9 # -- Jarkko Hietaniemi, April 1997
12 if ($ENV{PERL_CORE}) {
24 use vars qw($x $y $z);
28 if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
33 my $e = defined $_[2] ? $_[2] : $eps;
34 print "# near? $_[0] $_[1] $e\n";
35 $_[1] ? (abs($_[0]/$_[1] - 1) < $e) : abs($_[0]) < $e;
41 print 'not ' unless (near(tan($x), sin($x) / cos($x)));
44 print 'not ' unless (near(sinh(2), 3.62686040784702));
47 print 'not ' unless (near(acsch(0.1), 2.99822295029797));
51 print 'not ' unless (ref $x eq 'Math::Complex');
54 # avoid using Math::Complex here
55 $x =~ /^([^-]+)(-[^i]+)i$/;
57 print 'not ' unless (near($y, 1.5707963267949) and
58 near($z, -1.31695789692482));
61 print 'not ' unless (near(deg2rad(90), pi/2));
64 print 'not ' unless (near(rad2deg(pi), 180));
67 use Math::Trig ':radial';
70 my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
72 print 'not ' unless (near($r, sqrt(2))) and
73 (near($t, deg2rad(45))) and
77 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
79 print 'not ' unless (near($x, 1)) and
84 ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
86 print 'not ' unless (near($r, sqrt(2))) and
87 (near($t, deg2rad(45))) and
91 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
93 print 'not ' unless (near($x, 1)) and
100 my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
102 print 'not ' unless (near($r, sqrt(3))) and
103 (near($t, deg2rad(45))) and
104 (near($f, atan2(sqrt(2), 1)));
107 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
109 print 'not ' unless (near($x, 1)) and
114 ($r,$t,$f) = cartesian_to_spherical(1,1,0);
116 print 'not ' unless (near($r, sqrt(2))) and
117 (near($t, deg2rad(45))) and
118 (near($f, deg2rad(90)));
121 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
123 print 'not ' unless (near($x, 1)) and
130 my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
132 print 'not ' unless (near($r, 1)) and
137 ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
139 print 'not ' unless (near($r, 1)) and
146 use Math::Trig 'great_circle_distance';
149 unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2));
153 unless (near(great_circle_distance(0, 0, pi, pi), pi));
157 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
158 my @T = (deg2rad(139.8),deg2rad(90 - 35.7));
160 my $km = great_circle_distance(@L, @T, 6378);
162 print 'not ' unless (near($km, 9605.26637021388));
167 my $R2D = 57.295779513082320876798154814169;
169 sub frac { $_[0] - int($_[0]) }
171 my $lotta_radians = deg2rad(1E+20, 1);
172 print "not " unless near($lotta_radians, 1E+20/$R2D);
175 my $negat_degrees = rad2deg(-1E20, 1);
176 print "not " unless near($negat_degrees, -1E+20*$R2D);
179 my $posit_degrees = rad2deg(-10000, 1);
180 print "not " unless near($posit_degrees, -10000*$R2D);
185 use Math::Trig 'great_circle_direction';
188 unless (near(great_circle_direction(0, 0, 0, pi/2), pi));
191 # Retired test: Relies on atan2(0, 0), which is not portable.
193 # unless (near(great_circle_direction(0, 0, pi, pi), -pi()/2));
196 my @London = (deg2rad( -0.167), deg2rad(90 - 51.3));
197 my @Tokyo = (deg2rad( 139.5), deg2rad(90 - 35.7));
198 my @Berlin = (deg2rad ( 13.417), deg2rad(90 - 52.533));
199 my @Paris = (deg2rad ( 2.333), deg2rad(90 - 48.867));
202 unless (near(rad2deg(great_circle_direction(@London, @Tokyo)),
207 unless (near(rad2deg(great_circle_direction(@Tokyo, @London)),
212 unless (near(rad2deg(great_circle_direction(@Berlin, @Paris)),
218 unless (near(rad2deg(great_circle_direction(@Paris, @Berlin)),
222 use Math::Trig 'great_circle_bearing';
225 unless (near(rad2deg(great_circle_bearing(@Paris, @Berlin)),
229 use Math::Trig 'great_circle_waypoint';
230 use Math::Trig 'great_circle_midpoint';
234 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.0);
236 print 'not ' unless (near($lon, $London[0]));
239 print 'not ' unless (near($lat, $pip2 - $London[1]));
242 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 1.0);
244 print 'not ' unless (near($lon, $Tokyo[0]));
247 print 'not ' unless (near($lat, $pip2 - $Tokyo[1]));
250 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.5);
252 print 'not ' unless (near($lon, 1.55609593577679)); # 89.1577 E
255 print 'not ' unless (near($lat, 1.20296099733328)); # 68.9246 N
258 ($lon, $lat) = great_circle_midpoint(@London, @Tokyo);
260 print 'not ' unless (near($lon, 1.55609593577679)); # 89.1577 E
263 print 'not ' unless (near($lat, 1.20296099733328)); # 68.9246 N
266 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.25);
268 print 'not ' unless (near($lon, 0.516073562850837)); # 29.5688 E
271 print 'not ' unless (near($lat, 1.170565013391510)); # 67.0684 N
273 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.75);
275 print 'not ' unless (near($lon, 2.17494903805952)); # 124.6154 E
278 print 'not ' unless (near($lat, 0.952987032741305)); # 54.6021 N
281 use Math::Trig 'great_circle_destination';
283 my $dir1 = great_circle_direction(@London, @Tokyo);
284 my $dst1 = great_circle_distance(@London, @Tokyo);
286 ($lon, $lat) = great_circle_destination(@London, $dir1, $dst1);
288 print 'not ' unless (near($lon, $Tokyo[0]));
291 print 'not ' unless (near($lat, $pip2 - $Tokyo[1]));
294 my $dir2 = great_circle_direction(@Tokyo, @London);
295 my $dst2 = great_circle_distance(@Tokyo, @London);
297 ($lon, $lat) = great_circle_destination(@Tokyo, $dir2, $dst2);
299 print 'not ' unless (near($lon, $London[0]));
302 print 'not ' unless (near($lat, $pip2 - $London[1]));
305 my $dir3 = (great_circle_destination(@London, $dir1, $dst1))[2];
307 print 'not ' unless (near($dir3, 2.69379263839118)); # about 154.343 deg
310 my $dir4 = (great_circle_destination(@Tokyo, $dir2, $dst2))[2];
312 print 'not ' unless (near($dir4, 3.6993902625701)); # about 211.959 deg
315 print 'not ' unless (near($dst1, $dst2));