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
11 use Test::More tests => 153;
14 use Math::Trig 1.18 qw(:pi Inf);
24 if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
29 my $e = defined $_[2] ? $_[2] : $eps;
30 my $d = $_[1] ? abs($_[0]/$_[1] - 1) : abs($_[0]);
31 print "# near? $_[0] $_[1] : $d : $e\n";
32 $_[1] ? ($d < $e) : abs($_[0]) < $e;
35 print "# Sanity checks\n";
37 ok(near(sin(1), 0.841470984807897));
38 ok(near(cos(1), 0.54030230586814));
39 ok(near(tan(1), 1.5574077246549));
41 ok(near(sec(1), 1.85081571768093));
42 ok(near(csc(1), 1.18839510577812));
43 ok(near(cot(1), 0.642092615934331));
45 ok(near(asin(1), 1.5707963267949));
47 ok(near(atan(1), 0.785398163397448));
50 ok(near(acsc(1), 1.5707963267949));
51 ok(near(acot(1), 0.785398163397448));
53 ok(near(sinh(1), 1.1752011936438));
54 ok(near(cosh(1), 1.54308063481524));
55 ok(near(tanh(1), 0.761594155955765));
57 ok(near(sech(1), 0.648054273663885));
58 ok(near(csch(1), 0.850918128239322));
59 ok(near(coth(1), 1.31303528549933));
61 ok(near(asinh(1), 0.881373587019543));
62 ok(near(acosh(1), 0));
63 ok(near(atanh(0.9), 1.47221948958322)); # atanh(1.0) would be an error.
65 ok(near(asech(0.9), 0.467145308103262));
66 ok(near(acsch(2), 0.481211825059603));
67 ok(near(acoth(2), 0.549306144334055));
72 ok(near(tan($x), sin($x) / cos($x)));
74 ok(near(sinh(2), 3.62686040784702));
76 ok(near(acsch(0.1), 2.99822295029797));
79 is(ref $x, 'Math::Complex');
81 # avoid using Math::Complex here
82 $x =~ /^([^-]+)(-[^i]+)i$/;
84 ok(near($y, 1.5707963267949));
85 ok(near($z, -1.31695789692482));
87 ok(near(deg2rad(90), pi/2));
89 ok(near(rad2deg(pi), 180));
91 use Math::Trig ':radial';
94 my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
96 ok(near($r, sqrt(2)));
97 ok(near($t, deg2rad(45)));
100 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
106 ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
108 ok(near($r, sqrt(2)));
109 ok(near($t, deg2rad(45)));
112 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
120 my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
122 ok(near($r, sqrt(3)));
123 ok(near($t, deg2rad(45)));
124 ok(near($f, atan2(sqrt(2), 1)));
126 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
132 ($r,$t,$f) = cartesian_to_spherical(1,1,0);
134 ok(near($r, sqrt(2)));
135 ok(near($t, deg2rad(45)));
136 ok(near($f, deg2rad(90)));
138 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
146 my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
152 ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
160 use Math::Trig 'great_circle_distance';
162 ok(near(great_circle_distance(0, 0, 0, pi/2), pi/2));
164 ok(near(great_circle_distance(0, 0, pi, pi), pi));
167 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
168 my @T = (deg2rad(139.8), deg2rad(90 - 35.7));
170 my $km = great_circle_distance(@L, @T, 6378);
172 ok(near($km, 9605.26637021388));
176 my $R2D = 57.295779513082320876798154814169;
178 sub frac { $_[0] - int($_[0]) }
180 my $lotta_radians = deg2rad(1E+20, 1);
181 ok(near($lotta_radians, 1E+20/$R2D));
183 my $negat_degrees = rad2deg(-1E20, 1);
184 ok(near($negat_degrees, -1E+20*$R2D));
186 my $posit_degrees = rad2deg(-10000, 1);
187 ok(near($posit_degrees, -10000*$R2D));
191 use Math::Trig 'great_circle_direction';
193 ok(near(great_circle_direction(0, 0, 0, pi/2), pi));
195 # Retired test: Relies on atan2(0, 0), which is not portable.
196 # ok(near(great_circle_direction(0, 0, pi, pi), -pi()/2));
198 my @London = (deg2rad( -0.167), deg2rad(90 - 51.3));
199 my @Tokyo = (deg2rad( 139.5), deg2rad(90 - 35.7));
200 my @Berlin = (deg2rad ( 13.417), deg2rad(90 - 52.533));
201 my @Paris = (deg2rad ( 2.333), deg2rad(90 - 48.867));
203 ok(near(rad2deg(great_circle_direction(@London, @Tokyo)),
206 ok(near(rad2deg(great_circle_direction(@Tokyo, @London)),
209 ok(near(rad2deg(great_circle_direction(@Berlin, @Paris)),
212 ok(near(rad2deg(great_circle_direction(@Paris, @Berlin)),
215 use Math::Trig 'great_circle_bearing';
217 ok(near(rad2deg(great_circle_bearing(@Paris, @Berlin)),
220 use Math::Trig 'great_circle_waypoint';
221 use Math::Trig 'great_circle_midpoint';
225 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.0);
227 ok(near($lon, $London[0]));
229 ok(near($lat, $London[1]));
231 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 1.0);
233 ok(near($lon, $Tokyo[0]));
235 ok(near($lat, $Tokyo[1]));
237 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.5);
239 ok(near($lon, 1.55609593577679)); # 89.16 E
241 ok(near($lat, 0.36783532946162)); # 68.93 N
243 ($lon, $lat) = great_circle_midpoint(@London, @Tokyo);
245 ok(near($lon, 1.55609593577679)); # 89.16 E
247 ok(near($lat, 0.367835329461615)); # 68.93 N
249 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.25);
251 ok(near($lon, 0.516073562850837)); # 29.57 E
253 ok(near($lat, 0.400231313403387)); # 67.07 N
255 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.75);
257 ok(near($lon, 2.17494903805952)); # 124.62 E
259 ok(near($lat, 0.617809294053591)); # 54.60 N
261 use Math::Trig 'great_circle_destination';
263 my $dir1 = great_circle_direction(@London, @Tokyo);
264 my $dst1 = great_circle_distance(@London, @Tokyo);
266 ($lon, $lat) = great_circle_destination(@London, $dir1, $dst1);
268 ok(near($lon, $Tokyo[0]));
270 ok(near($lat, $pip2 - $Tokyo[1]));
272 my $dir2 = great_circle_direction(@Tokyo, @London);
273 my $dst2 = great_circle_distance(@Tokyo, @London);
275 ($lon, $lat) = great_circle_destination(@Tokyo, $dir2, $dst2);
277 ok(near($lon, $London[0]));
279 ok(near($lat, $pip2 - $London[1]));
281 my $dir3 = (great_circle_destination(@London, $dir1, $dst1))[2];
283 ok(near($dir3, 2.69379263839118)); # about 154.343 deg
285 my $dir4 = (great_circle_destination(@Tokyo, $dir2, $dst2))[2];
287 ok(near($dir4, 3.6993902625701)); # about 211.959 deg
289 ok(near($dst1, $dst2));
292 print "# Infinity\n";
294 my $BigDouble = 1e40;
296 # E.g. netbsd-alpha core dumps on Inf arith without this.
297 local $SIG{FPE} = sub { };
299 ok(Inf() > $BigDouble); # This passes in netbsd-alpha.
300 ok(Inf() + $BigDouble > $BigDouble); # This coredumps in netbsd-alpha.
301 ok(Inf() + $BigDouble == Inf());
302 ok(Inf() - $BigDouble > $BigDouble);
303 ok(Inf() - $BigDouble == Inf());
304 ok(Inf() * $BigDouble > $BigDouble);
305 ok(Inf() * $BigDouble == Inf());
306 ok(Inf() / $BigDouble > $BigDouble);
307 ok(Inf() / $BigDouble == Inf());
309 ok(-Inf() < -$BigDouble);
310 ok(-Inf() + $BigDouble < $BigDouble);
311 ok(-Inf() + $BigDouble == -Inf());
312 ok(-Inf() - $BigDouble < -$BigDouble);
313 ok(-Inf() - $BigDouble == -Inf());
314 ok(-Inf() * $BigDouble < -$BigDouble);
315 ok(-Inf() * $BigDouble == -Inf());
316 ok(-Inf() / $BigDouble < -$BigDouble);
317 ok(-Inf() / $BigDouble == -Inf());
319 print "# sinh/sech/cosh/csch/tanh/coth unto infinity\n";
321 ok(near(sinh(100), 1.3441e+43, 1e-3));
322 ok(near(sech(100), 7.4402e-44, 1e-3));
323 ok(near(cosh(100), 1.3441e+43, 1e-3));
324 ok(near(csch(100), 7.4402e-44, 1e-3));
325 ok(near(tanh(100), 1));
326 ok(near(coth(100), 1));
328 ok(near(sinh(-100), -1.3441e+43, 1e-3));
329 ok(near(sech(-100), 7.4402e-44, 1e-3));
330 ok(near(cosh(-100), 1.3441e+43, 1e-3));
331 ok(near(csch(-100), -7.4402e-44, 1e-3));
332 ok(near(tanh(-100), -1));
333 ok(near(coth(-100), -1));
335 cmp_ok(sinh(1e5), '==', Inf());
336 cmp_ok(sech(1e5), '==', 0);
337 cmp_ok(cosh(1e5), '==', Inf());
338 cmp_ok(csch(1e5), '==', 0);
339 cmp_ok(tanh(1e5), '==', 1);
340 cmp_ok(coth(1e5), '==', 1);
342 cmp_ok(sinh(-1e5), '==', -Inf());
343 cmp_ok(sech(-1e5), '==', 0);
344 cmp_ok(cosh(-1e5), '==', Inf());
345 cmp_ok(csch(-1e5), '==', 0);
346 cmp_ok(tanh(-1e5), '==', -1);
347 cmp_ok(coth(-1e5), '==', -1);
349 print "# great_circle_distance with small angles\n";
351 for my $e (qw(1e-2 1e-3 1e-4 1e-5)) {
352 # Can't assume == 0 because of floating point fuzz,
353 # but let's hope for at least < $e.
354 cmp_ok(great_circle_distance(0, $e, 0, $e), '<', $e);
357 print "# asin_real, acos_real\n";
359 is(acos_real(-2.0), pi);
360 is(acos_real(-1.0), pi);
361 is(acos_real(-0.5), acos(-0.5));
362 is(acos_real( 0.0), acos( 0.0));
363 is(acos_real( 0.5), acos( 0.5));
364 is(acos_real( 1.0), 0);
365 is(acos_real( 2.0), 0);
367 is(asin_real(-2.0), -&pip2);
368 is(asin_real(-1.0), -&pip2);
369 is(asin_real(-0.5), asin(-0.5));
370 is(asin_real( 0.0), asin( 0.0));
371 is(asin_real( 0.5), asin( 0.5));
372 is(asin_real( 1.0), pip2);
373 is(asin_real( 2.0), pip2);