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";
32 use Math::Trig 1.07 qw(Inf);
38 use vars qw($x $y $z);
42 if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
47 my $e = defined $_[2] ? $_[2] : $eps;
48 my $d = $_[1] ? abs($_[0]/$_[1] - 1) : abs($_[0]);
49 print "# near? $_[0] $_[1] : $d : $e\n";
50 $_[1] ? ($d < $e) : abs($_[0]) < $e;
53 print "# Sanity checks\n";
55 ok(near(sin(1), 0.841470984807897));
56 ok(near(cos(1), 0.54030230586814));
57 ok(near(tan(1), 1.5574077246549));
59 ok(near(sec(1), 1.85081571768093));
60 ok(near(csc(1), 1.18839510577812));
61 ok(near(cot(1), 0.642092615934331));
63 ok(near(asin(1), 1.5707963267949));
65 ok(near(atan(1), 0.785398163397448));
68 ok(near(acsc(1), 1.5707963267949));
69 ok(near(acot(1), 0.785398163397448));
71 ok(near(sinh(1), 1.1752011936438));
72 ok(near(cosh(1), 1.54308063481524));
73 ok(near(tanh(1), 0.761594155955765));
75 ok(near(sech(1), 0.648054273663885));
76 ok(near(csch(1), 0.850918128239322));
77 ok(near(coth(1), 1.31303528549933));
79 ok(near(asinh(1), 0.881373587019543));
80 ok(near(acosh(1), 0));
81 ok(near(atanh(0.9), 1.47221948958322)); # atanh(1.0) would be an error.
83 ok(near(asech(0.9), 0.467145308103262));
84 ok(near(acsch(2), 0.481211825059603));
85 ok(near(acoth(2), 0.549306144334055));
90 ok(near(tan($x), sin($x) / cos($x)));
92 ok(near(sinh(2), 3.62686040784702));
94 ok(near(acsch(0.1), 2.99822295029797));
97 is(ref $x, 'Math::Complex');
99 # avoid using Math::Complex here
100 $x =~ /^([^-]+)(-[^i]+)i$/;
102 ok(near($y, 1.5707963267949));
103 ok(near($z, -1.31695789692482));
105 ok(near(deg2rad(90), pi/2));
107 ok(near(rad2deg(pi), 180));
109 use Math::Trig ':radial';
112 my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
114 ok(near($r, sqrt(2)));
115 ok(near($t, deg2rad(45)));
118 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
124 ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
126 ok(near($r, sqrt(2)));
127 ok(near($t, deg2rad(45)));
130 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
138 my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
140 ok(near($r, sqrt(3)));
141 ok(near($t, deg2rad(45)));
142 ok(near($f, atan2(sqrt(2), 1)));
144 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
150 ($r,$t,$f) = cartesian_to_spherical(1,1,0);
152 ok(near($r, sqrt(2)));
153 ok(near($t, deg2rad(45)));
154 ok(near($f, deg2rad(90)));
156 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
164 my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
170 ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
178 use Math::Trig 'great_circle_distance';
180 ok(near(great_circle_distance(0, 0, 0, pi/2), pi/2));
182 ok(near(great_circle_distance(0, 0, pi, pi), pi));
185 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
186 my @T = (deg2rad(139.8), deg2rad(90 - 35.7));
188 my $km = great_circle_distance(@L, @T, 6378);
190 ok(near($km, 9605.26637021388));
194 my $R2D = 57.295779513082320876798154814169;
196 sub frac { $_[0] - int($_[0]) }
198 my $lotta_radians = deg2rad(1E+20, 1);
199 ok(near($lotta_radians, 1E+20/$R2D));
201 my $negat_degrees = rad2deg(-1E20, 1);
202 ok(near($negat_degrees, -1E+20*$R2D));
204 my $posit_degrees = rad2deg(-10000, 1);
205 ok(near($posit_degrees, -10000*$R2D));
209 use Math::Trig 'great_circle_direction';
211 ok(near(great_circle_direction(0, 0, 0, pi/2), pi));
213 # Retired test: Relies on atan2(0, 0), which is not portable.
214 # ok(near(great_circle_direction(0, 0, pi, pi), -pi()/2));
216 my @London = (deg2rad( -0.167), deg2rad(90 - 51.3));
217 my @Tokyo = (deg2rad( 139.5), deg2rad(90 - 35.7));
218 my @Berlin = (deg2rad ( 13.417), deg2rad(90 - 52.533));
219 my @Paris = (deg2rad ( 2.333), deg2rad(90 - 48.867));
221 ok(near(rad2deg(great_circle_direction(@London, @Tokyo)),
224 ok(near(rad2deg(great_circle_direction(@Tokyo, @London)),
227 ok(near(rad2deg(great_circle_direction(@Berlin, @Paris)),
230 ok(near(rad2deg(great_circle_direction(@Paris, @Berlin)),
233 use Math::Trig 'great_circle_bearing';
235 ok(near(rad2deg(great_circle_bearing(@Paris, @Berlin)),
238 use Math::Trig 'great_circle_waypoint';
239 use Math::Trig 'great_circle_midpoint';
243 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.0);
245 ok(near($lon, $London[0]));
247 ok(near($lat, $London[1]));
249 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 1.0);
251 ok(near($lon, $Tokyo[0]));
253 ok(near($lat, $Tokyo[1]));
255 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.5);
257 ok(near($lon, 1.55609593577679)); # 89.16 E
259 ok(near($lat, 0.36783532946162)); # 68.93 N
261 ($lon, $lat) = great_circle_midpoint(@London, @Tokyo);
263 ok(near($lon, 1.55609593577679)); # 89.16 E
265 ok(near($lat, 0.367835329461615)); # 68.93 N
267 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.25);
269 ok(near($lon, 0.516073562850837)); # 29.57 E
271 ok(near($lat, 0.400231313403387)); # 67.07 N
273 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.75);
275 ok(near($lon, 2.17494903805952)); # 124.62 E
277 ok(near($lat, 0.617809294053591)); # 54.60 N
279 use Math::Trig 'great_circle_destination';
281 my $dir1 = great_circle_direction(@London, @Tokyo);
282 my $dst1 = great_circle_distance(@London, @Tokyo);
284 ($lon, $lat) = great_circle_destination(@London, $dir1, $dst1);
286 ok(near($lon, $Tokyo[0]));
288 ok(near($lat, $pip2 - $Tokyo[1]));
290 my $dir2 = great_circle_direction(@Tokyo, @London);
291 my $dst2 = great_circle_distance(@Tokyo, @London);
293 ($lon, $lat) = great_circle_destination(@Tokyo, $dir2, $dst2);
295 ok(near($lon, $London[0]));
297 ok(near($lat, $pip2 - $London[1]));
299 my $dir3 = (great_circle_destination(@London, $dir1, $dst1))[2];
301 ok(near($dir3, 2.69379263839118)); # about 154.343 deg
303 my $dir4 = (great_circle_destination(@Tokyo, $dir2, $dst2))[2];
305 ok(near($dir4, 3.6993902625701)); # about 211.959 deg
307 ok(near($dst1, $dst2));
310 print "# Infinity\n";
312 my $BigDouble = 1e40;
314 ok(Inf() > $BigDouble);
315 ok(Inf() + $BigDouble > $BigDouble);
316 ok(Inf() + $BigDouble == Inf());
317 ok(Inf() - $BigDouble > $BigDouble);
318 ok(Inf() - $BigDouble == Inf());
319 ok(Inf() * $BigDouble > $BigDouble);
320 ok(Inf() * $BigDouble == Inf());
321 ok(Inf() / $BigDouble > $BigDouble);
322 ok(Inf() / $BigDouble == Inf());
324 ok(-Inf() < -$BigDouble);
325 ok(-Inf() + $BigDouble < $BigDouble);
326 ok(-Inf() + $BigDouble == -Inf());
327 ok(-Inf() - $BigDouble < -$BigDouble);
328 ok(-Inf() - $BigDouble == -Inf());
329 ok(-Inf() * $BigDouble < -$BigDouble);
330 ok(-Inf() * $BigDouble == -Inf());
331 ok(-Inf() / $BigDouble < -$BigDouble);
332 ok(-Inf() / $BigDouble == -Inf());
334 print "# sinh/sech/cosh/csch/tanh/coth unto infinity\n";
336 ok(near(sinh(100), 1.3441e+43, 1e-3));
337 ok(near(sech(100), 7.4402e-44, 1e-3));
338 ok(near(cosh(100), 1.3441e+43, 1e-3));
339 ok(near(csch(100), 7.4402e-44, 1e-3));
340 ok(near(tanh(100), 1));
341 ok(near(coth(100), 1));
343 ok(near(sinh(-100), -1.3441e+43, 1e-3));
344 ok(near(sech(-100), 7.4402e-44, 1e-3));
345 ok(near(cosh(-100), 1.3441e+43, 1e-3));
346 ok(near(csch(-100), -7.4402e-44, 1e-3));
347 ok(near(tanh(-100), -1));
348 ok(near(coth(-100), -1));
350 ok(sinh(1e4) == Inf());
352 ok(cosh(1e4) == Inf());
357 ok(sinh(-1e4) == -Inf());
359 ok(cosh(-1e4) == Inf());
361 ok(tanh(-1e4) == -1);
362 ok(coth(-1e4) == -1);