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}) {
19 eval { require Test::More };
21 # We are willing to lose testing in e.g. 5.00504.
22 print "1..0 # No Test::More, skipping\n";
37 use vars qw($x $y $z);
41 if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
46 my $e = defined $_[2] ? $_[2] : $eps;
47 my $d = $_[1] ? abs($_[0]/$_[1] - 1) : abs($_[0]);
48 print "# near? $_[0] $_[1] : $d : $e\n";
49 $_[1] ? ($d < $e) : abs($_[0]) < $e;
53 ok(near(tan($x), sin($x) / cos($x)));
55 ok(near(sinh(2), 3.62686040784702));
57 ok(near(acsch(0.1), 2.99822295029797));
60 is(ref $x, 'Math::Complex');
62 # avoid using Math::Complex here
63 $x =~ /^([^-]+)(-[^i]+)i$/;
65 ok(near($y, 1.5707963267949));
66 ok(near($z, -1.31695789692482));
68 ok(near(deg2rad(90), pi/2));
70 ok(near(rad2deg(pi), 180));
72 use Math::Trig ':radial';
75 my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
77 ok(near($r, sqrt(2)));
78 ok(near($t, deg2rad(45)));
81 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
87 ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
89 ok(near($r, sqrt(2)));
90 ok(near($t, deg2rad(45)));
93 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
101 my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
103 ok(near($r, sqrt(3)));
104 ok(near($t, deg2rad(45)));
105 ok(near($f, atan2(sqrt(2), 1)));
107 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
113 ($r,$t,$f) = cartesian_to_spherical(1,1,0);
115 ok(near($r, sqrt(2)));
116 ok(near($t, deg2rad(45)));
117 ok(near($f, deg2rad(90)));
119 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
127 my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
133 ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
141 use Math::Trig 'great_circle_distance';
143 ok(near(great_circle_distance(0, 0, 0, pi/2), pi/2));
145 ok(near(great_circle_distance(0, 0, pi, pi), pi));
148 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
149 my @T = (deg2rad(139.8),deg2rad(90 - 35.7));
151 my $km = great_circle_distance(@L, @T, 6378);
153 ok(near($km, 9605.26637021388));
157 my $R2D = 57.295779513082320876798154814169;
159 sub frac { $_[0] - int($_[0]) }
161 my $lotta_radians = deg2rad(1E+20, 1);
162 ok(near($lotta_radians, 1E+20/$R2D));
164 my $negat_degrees = rad2deg(-1E20, 1);
165 ok(near($negat_degrees, -1E+20*$R2D));
167 my $posit_degrees = rad2deg(-10000, 1);
168 ok(near($posit_degrees, -10000*$R2D));
172 use Math::Trig 'great_circle_direction';
174 ok(near(great_circle_direction(0, 0, 0, pi/2), pi));
176 # Retired test: Relies on atan2(0, 0), which is not portable.
177 # ok(near(great_circle_direction(0, 0, pi, pi), -pi()/2));
179 my @London = (deg2rad( -0.167), deg2rad(90 - 51.3));
180 my @Tokyo = (deg2rad( 139.5), deg2rad(90 - 35.7));
181 my @Berlin = (deg2rad ( 13.417), deg2rad(90 - 52.533));
182 my @Paris = (deg2rad ( 2.333), deg2rad(90 - 48.867));
184 ok(near(rad2deg(great_circle_direction(@London, @Tokyo)),
187 ok(near(rad2deg(great_circle_direction(@Tokyo, @London)),
190 ok(near(rad2deg(great_circle_direction(@Berlin, @Paris)),
193 ok(near(rad2deg(great_circle_direction(@Paris, @Berlin)),
196 use Math::Trig 'great_circle_bearing';
198 ok(near(rad2deg(great_circle_bearing(@Paris, @Berlin)),
201 use Math::Trig 'great_circle_waypoint';
202 use Math::Trig 'great_circle_midpoint';
206 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.0);
208 ok(near($lon, $London[0]));
210 ok(near($lat, $London[1]));
212 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 1.0);
214 ok(near($lon, $Tokyo[0]));
216 ok(near($lat, $Tokyo[1]));
218 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.5);
220 ok(near($lon, 1.55609593577679)); # 89.16 E
222 ok(near($lat, 0.36783532946162)); # 68.93 N
224 ($lon, $lat) = great_circle_midpoint(@London, @Tokyo);
226 ok(near($lon, 1.55609593577679)); # 89.16 E
228 ok(near($lat, 0.367835329461615)); # 68.93 N
230 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.25);
232 ok(near($lon, 0.516073562850837)); # 29.57 E
234 ok(near($lat, 0.400231313403387)); # 67.07 N
236 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.75);
238 ok(near($lon, 2.17494903805952)); # 124.62 E
240 ok(near($lat, 0.617809294053591)); # 54.60 N
242 use Math::Trig 'great_circle_destination';
244 my $dir1 = great_circle_direction(@London, @Tokyo);
245 my $dst1 = great_circle_distance(@London, @Tokyo);
247 ($lon, $lat) = great_circle_destination(@London, $dir1, $dst1);
249 ok(near($lon, $Tokyo[0]));
251 ok(near($lat, $pip2 - $Tokyo[1]));
253 my $dir2 = great_circle_direction(@Tokyo, @London);
254 my $dst2 = great_circle_distance(@Tokyo, @London);
256 ($lon, $lat) = great_circle_destination(@Tokyo, $dir2, $dst2);
258 ok(near($lon, $London[0]));
260 ok(near($lat, $pip2 - $London[1]));
262 my $dir3 = (great_circle_destination(@London, $dir1, $dst1))[2];
264 ok(near($dir3, 2.69379263839118)); # about 154.343 deg
266 my $dir4 = (great_circle_destination(@Tokyo, $dir2, $dst2))[2];
268 ok(near($dir4, 3.6993902625701)); # about 211.959 deg
270 ok(near($dst1, $dst2));