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);
16 our $vax_float = (pack("d",1) =~ /^[\x80\x10]\x40/);
17 our $has_inf = !$vax_float;
27 if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
32 my $e = defined $_[2] ? $_[2] : $eps;
33 my $d = $_[1] ? abs($_[0]/$_[1] - 1) : abs($_[0]);
34 print "# near? $_[0] $_[1] : $d : $e\n";
35 $_[1] ? ($d < $e) : abs($_[0]) < $e;
38 print "# Sanity checks\n";
40 ok(near(sin(1), 0.841470984807897));
41 ok(near(cos(1), 0.54030230586814));
42 ok(near(tan(1), 1.5574077246549));
44 ok(near(sec(1), 1.85081571768093));
45 ok(near(csc(1), 1.18839510577812));
46 ok(near(cot(1), 0.642092615934331));
48 ok(near(asin(1), 1.5707963267949));
50 ok(near(atan(1), 0.785398163397448));
53 ok(near(acsc(1), 1.5707963267949));
54 ok(near(acot(1), 0.785398163397448));
56 ok(near(sinh(1), 1.1752011936438));
57 ok(near(cosh(1), 1.54308063481524));
58 ok(near(tanh(1), 0.761594155955765));
60 ok(near(sech(1), 0.648054273663885));
61 ok(near(csch(1), 0.850918128239322));
62 ok(near(coth(1), 1.31303528549933));
64 ok(near(asinh(1), 0.881373587019543));
65 ok(near(acosh(1), 0));
66 ok(near(atanh(0.9), 1.47221948958322)); # atanh(1.0) would be an error.
68 ok(near(asech(0.9), 0.467145308103262));
69 ok(near(acsch(2), 0.481211825059603));
70 ok(near(acoth(2), 0.549306144334055));
75 ok(near(tan($x), sin($x) / cos($x)));
77 ok(near(sinh(2), 3.62686040784702));
79 ok(near(acsch(0.1), 2.99822295029797));
82 is(ref $x, 'Math::Complex');
84 # avoid using Math::Complex here
85 $x =~ /^([^-]+)(-[^i]+)i$/;
87 ok(near($y, 1.5707963267949));
88 ok(near($z, -1.31695789692482));
90 ok(near(deg2rad(90), pi/2));
92 ok(near(rad2deg(pi), 180));
94 use Math::Trig ':radial';
97 my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
99 ok(near($r, sqrt(2)));
100 ok(near($t, deg2rad(45)));
103 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
109 ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
111 ok(near($r, sqrt(2)));
112 ok(near($t, deg2rad(45)));
115 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
123 my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
125 ok(near($r, sqrt(3)));
126 ok(near($t, deg2rad(45)));
127 ok(near($f, atan2(sqrt(2), 1)));
129 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
135 ($r,$t,$f) = cartesian_to_spherical(1,1,0);
137 ok(near($r, sqrt(2)));
138 ok(near($t, deg2rad(45)));
139 ok(near($f, deg2rad(90)));
141 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
149 my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
155 ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
163 use Math::Trig 'great_circle_distance';
165 ok(near(great_circle_distance(0, 0, 0, pi/2), pi/2));
167 ok(near(great_circle_distance(0, 0, pi, pi), pi));
170 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
171 my @T = (deg2rad(139.8), deg2rad(90 - 35.7));
173 my $km = great_circle_distance(@L, @T, 6378);
175 ok(near($km, 9605.26637021388));
179 my $R2D = 57.295779513082320876798154814169;
181 sub frac { $_[0] - int($_[0]) }
183 my $lotta_radians = deg2rad(1E+20, 1);
184 ok(near($lotta_radians, 1E+20/$R2D));
186 my $negat_degrees = rad2deg(-1E20, 1);
187 ok(near($negat_degrees, -1E+20*$R2D));
189 my $posit_degrees = rad2deg(-10000, 1);
190 ok(near($posit_degrees, -10000*$R2D));
194 use Math::Trig 'great_circle_direction';
196 ok(near(great_circle_direction(0, 0, 0, pi/2), pi));
198 # Retired test: Relies on atan2(0, 0), which is not portable.
199 # ok(near(great_circle_direction(0, 0, pi, pi), -pi()/2));
201 my @London = (deg2rad( -0.167), deg2rad(90 - 51.3));
202 my @Tokyo = (deg2rad( 139.5), deg2rad(90 - 35.7));
203 my @Berlin = (deg2rad ( 13.417), deg2rad(90 - 52.533));
204 my @Paris = (deg2rad ( 2.333), deg2rad(90 - 48.867));
206 ok(near(rad2deg(great_circle_direction(@London, @Tokyo)),
209 ok(near(rad2deg(great_circle_direction(@Tokyo, @London)),
212 ok(near(rad2deg(great_circle_direction(@Berlin, @Paris)),
215 ok(near(rad2deg(great_circle_direction(@Paris, @Berlin)),
218 use Math::Trig 'great_circle_bearing';
220 ok(near(rad2deg(great_circle_bearing(@Paris, @Berlin)),
223 use Math::Trig 'great_circle_waypoint';
224 use Math::Trig 'great_circle_midpoint';
228 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.0);
230 ok(near($lon, $London[0]));
232 ok(near($lat, $London[1]));
234 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 1.0);
236 ok(near($lon, $Tokyo[0]));
238 ok(near($lat, $Tokyo[1]));
240 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.5);
242 ok(near($lon, 1.55609593577679)); # 89.16 E
244 ok(near($lat, 0.36783532946162)); # 68.93 N
246 ($lon, $lat) = great_circle_midpoint(@London, @Tokyo);
248 ok(near($lon, 1.55609593577679)); # 89.16 E
250 ok(near($lat, 0.367835329461615)); # 68.93 N
252 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.25);
254 ok(near($lon, 0.516073562850837)); # 29.57 E
256 ok(near($lat, 0.400231313403387)); # 67.07 N
258 ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.75);
260 ok(near($lon, 2.17494903805952)); # 124.62 E
262 ok(near($lat, 0.617809294053591)); # 54.60 N
264 use Math::Trig 'great_circle_destination';
266 my $dir1 = great_circle_direction(@London, @Tokyo);
267 my $dst1 = great_circle_distance(@London, @Tokyo);
269 ($lon, $lat) = great_circle_destination(@London, $dir1, $dst1);
271 ok(near($lon, $Tokyo[0]));
273 ok(near($lat, $pip2 - $Tokyo[1]));
275 my $dir2 = great_circle_direction(@Tokyo, @London);
276 my $dst2 = great_circle_distance(@Tokyo, @London);
278 ($lon, $lat) = great_circle_destination(@Tokyo, $dir2, $dst2);
280 ok(near($lon, $London[0]));
282 ok(near($lat, $pip2 - $London[1]));
284 my $dir3 = (great_circle_destination(@London, $dir1, $dst1))[2];
286 ok(near($dir3, 2.69379263839118)); # about 154.343 deg
288 my $dir4 = (great_circle_destination(@Tokyo, $dir2, $dst2))[2];
290 ok(near($dir4, 3.6993902625701)); # about 211.959 deg
292 ok(near($dst1, $dst2));
296 # With netbsd-vax (or any vax) there is neither Inf, nor 1e40.
297 skip("different float range", 42) if $vax_float;
298 skip("no inf", 42) unless $has_inf;
300 print "# Infinity\n";
302 my $BigDouble = eval '1e40';
304 # E.g. netbsd-alpha core dumps on Inf arith without this.
305 local $SIG{FPE} = sub { };
307 ok(Inf() > $BigDouble); # This passes in netbsd-alpha.
308 ok(Inf() + $BigDouble > $BigDouble); # This coredumps in netbsd-alpha.
309 ok(Inf() + $BigDouble == Inf());
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());
317 ok(-Inf() < -$BigDouble);
318 ok(-Inf() + $BigDouble < $BigDouble);
319 ok(-Inf() + $BigDouble == -Inf());
320 ok(-Inf() - $BigDouble < -$BigDouble);
321 ok(-Inf() - $BigDouble == -Inf());
322 ok(-Inf() * $BigDouble < -$BigDouble);
323 ok(-Inf() * $BigDouble == -Inf());
324 ok(-Inf() / $BigDouble < -$BigDouble);
325 ok(-Inf() / $BigDouble == -Inf());
327 print "# sinh/sech/cosh/csch/tanh/coth unto infinity\n";
329 ok(near(sinh(100), eval '1.3441e+43', 1e-3));
330 ok(near(sech(100), eval '7.4402e-44', 1e-3));
331 ok(near(cosh(100), eval '1.3441e+43', 1e-3));
332 ok(near(csch(100), eval '7.4402e-44', 1e-3));
333 ok(near(tanh(100), 1));
334 ok(near(coth(100), 1));
336 ok(near(sinh(-100), eval '-1.3441e+43', 1e-3));
337 ok(near(sech(-100), eval ' 7.4402e-44', 1e-3));
338 ok(near(cosh(-100), eval ' 1.3441e+43', 1e-3));
339 ok(near(csch(-100), eval '-7.4402e-44', 1e-3));
340 ok(near(tanh(-100), -1));
341 ok(near(coth(-100), -1));
343 cmp_ok(sinh(1e5), '==', Inf());
344 cmp_ok(sech(1e5), '==', 0);
345 cmp_ok(cosh(1e5), '==', Inf());
346 cmp_ok(csch(1e5), '==', 0);
347 cmp_ok(tanh(1e5), '==', 1);
348 cmp_ok(coth(1e5), '==', 1);
350 cmp_ok(sinh(-1e5), '==', -Inf());
351 cmp_ok(sech(-1e5), '==', 0);
352 cmp_ok(cosh(-1e5), '==', Inf());
353 cmp_ok(csch(-1e5), '==', 0);
354 cmp_ok(tanh(-1e5), '==', -1);
355 cmp_ok(coth(-1e5), '==', -1);
359 print "# great_circle_distance with small angles\n";
361 for my $e (qw(1e-2 1e-3 1e-4 1e-5)) {
362 # Can't assume == 0 because of floating point fuzz,
363 # but let's hope for at least < $e.
364 cmp_ok(great_circle_distance(0, $e, 0, $e), '<', $e);
367 print "# asin_real, acos_real\n";
369 is(acos_real(-2.0), pi);
370 is(acos_real(-1.0), pi);
371 is(acos_real(-0.5), acos(-0.5));
372 is(acos_real( 0.0), acos( 0.0));
373 is(acos_real( 0.5), acos( 0.5));
374 is(acos_real( 1.0), 0);
375 is(acos_real( 2.0), 0);
377 is(asin_real(-2.0), -&pip2);
378 is(asin_real(-1.0), -&pip2);
379 is(asin_real(-0.5), asin(-0.5));
380 is(asin_real( 0.0), asin( 0.0));
381 is(asin_real( 0.5), asin( 0.5));
382 is(asin_real( 1.0), pip2);
383 is(asin_real( 2.0), pip2);