1#!./perl
2
3#
4# Regression tests for the Math::Trig package
5#
6# The tests here are quite modest as the Math::Complex tests exercise
7# these interfaces quite vigorously.
8# 
9# -- Jarkko Hietaniemi, April 1997
10
11use strict;
12use warnings;
13use Test::More tests => 157;
14
15use Math::Trig 1.18;
16use Math::Trig 1.18 qw(:pi Inf);
17
18our $vax_float = (pack("d",1) =~ /^[\x80\x10]\x40/);
19our $has_inf   = !$vax_float;
20
21my $pip2 = pi / 2;
22
23use strict;
24
25our($x, $y, $z);
26
27my $eps = 1e-11;
28
29if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
30    $eps = 1e-10;
31}
32
33sub near {
34    my $e = defined $_[2] ? $_[2] : $eps;
35    my $d = $_[1] ? abs($_[0]/$_[1] - 1) : abs($_[0]);
36    print "# near? $_[0] $_[1] : $d : $e\n";
37    $_[1] ? ($d < $e) : abs($_[0]) < $e;
38}
39
40print "# Sanity checks\n";
41
42ok(near(sin(1), 0.841470984807897));
43ok(near(cos(1), 0.54030230586814));
44ok(near(tan(1), 1.5574077246549));
45
46ok(near(sec(1), 1.85081571768093));
47ok(near(csc(1), 1.18839510577812));
48ok(near(cot(1), 0.642092615934331));
49
50ok(near(asin(1), 1.5707963267949));
51ok(near(acos(1), 0));
52ok(near(atan(1), 0.785398163397448));
53
54ok(near(asec(1), 0));
55ok(near(acsc(1), 1.5707963267949));
56ok(near(acot(1), 0.785398163397448));
57
58ok(near(sinh(1), 1.1752011936438));
59ok(near(cosh(1), 1.54308063481524));
60ok(near(tanh(1), 0.761594155955765));
61
62ok(near(sech(1), 0.648054273663885));
63ok(near(csch(1), 0.850918128239322));
64ok(near(coth(1), 1.31303528549933));
65
66ok(near(asinh(1), 0.881373587019543));
67ok(near(acosh(1), 0));
68ok(near(atanh(0.9), 1.47221948958322)); # atanh(1.0) would be an error.
69
70ok(near(asech(0.9), 0.467145308103262));
71ok(near(acsch(2), 0.481211825059603));
72ok(near(acoth(2), 0.549306144334055));
73
74print "# Basics\n";
75
76$x = 0.9;
77ok(near(tan($x), sin($x) / cos($x)));
78
79ok(near(sinh(2), 3.62686040784702));
80
81ok(near(acsch(0.1), 2.99822295029797));
82
83$x = asin(2);
84is(ref $x, 'Math::Complex');
85
86# avoid using Math::Complex here
87$x =~ /^([^-]+)(-[^i]+)i$/;
88($y, $z) = ($1, $2);
89ok(near($y,  1.5707963267949));
90ok(near($z, -1.31695789692482));
91
92ok(near(deg2rad(90), pi/2));
93
94ok(near(rad2deg(pi), 180));
95
96use Math::Trig ':radial';
97
98{
99    my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
100
101    ok(near($r, sqrt(2)));
102    ok(near($t, deg2rad(45)));
103    ok(near($z, 1));
104
105    ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
106
107    ok(near($x, 1));
108    ok(near($y, 1));
109    ok(near($z, 1));
110
111    ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
112
113    ok(near($r, sqrt(2)));
114    ok(near($t, deg2rad(45)));
115    ok(near($z, 0));
116
117    ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
118
119    ok(near($x, 1));
120    ok(near($y, 1));
121    ok(near($z, 0));
122}
123
124{
125    my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
126
127    ok(near($r, sqrt(3)));
128    ok(near($t, deg2rad(45)));
129    ok(near($f, atan2(sqrt(2), 1)));
130
131    ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
132
133    ok(near($x, 1));
134    ok(near($y, 1));
135    ok(near($z, 1));
136       
137    ($r,$t,$f) = cartesian_to_spherical(1,1,0);
138
139    ok(near($r, sqrt(2)));
140    ok(near($t, deg2rad(45)));
141    ok(near($f, deg2rad(90)));
142
143    ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
144
145    ok(near($x, 1));
146    ok(near($y, 1));
147    ok(near($z, 0));
148}
149
150{
151    my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
152
153    ok(near($r, 1));
154    ok(near($t, 1));
155    ok(near($z, 1));
156
157    ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
158
159    ok(near($r, 1));
160    ok(near($t, 1));
161    ok(near($z, 1));
162}
163
164{
165    use Math::Trig 'great_circle_distance';
166
167    ok(near(great_circle_distance(0, 0, 0, pi/2), pi/2));
168
169    ok(near(great_circle_distance(0, 0, pi, pi), pi));
170
171    # London to Tokyo.
172    my @L = (deg2rad(-0.5),  deg2rad(90 - 51.3));
173    my @T = (deg2rad(139.8), deg2rad(90 - 35.7));
174
175    my $km = great_circle_distance(@L, @T, 6378);
176
177    ok(near($km, 9605.26637021388));
178}
179
180{
181    my $R2D = 57.295779513082320876798154814169;
182
183    sub frac { $_[0] - int($_[0]) }
184
185    my $lotta_radians = deg2rad(1E+20, 1);
186    ok(near($lotta_radians,  1E+20/$R2D));
187
188    my $negat_degrees = rad2deg(-1E20, 1);
189    ok(near($negat_degrees, -1E+20*$R2D));
190
191    my $posit_degrees = rad2deg(-10000, 1);
192    ok(near($posit_degrees, -10000*$R2D));
193}
194
195{
196    use Math::Trig 'great_circle_direction';
197
198    ok(near(great_circle_direction(0, 0, 0, pi/2), pi));
199
200# Retired test: Relies on atan2(0, 0), which is not portable.
201#	ok(near(great_circle_direction(0, 0, pi, pi), -pi()/2));
202
203    my @London  = (deg2rad(  -0.167), deg2rad(90 - 51.3));
204    my @Tokyo   = (deg2rad( 139.5),   deg2rad(90 - 35.7));
205    my @Berlin  = (deg2rad ( 13.417), deg2rad(90 - 52.533));
206    my @Paris   = (deg2rad (  2.333), deg2rad(90 - 48.867));
207
208    ok(near(rad2deg(great_circle_direction(@London, @Tokyo)),
209	    31.791945393073));
210
211    ok(near(rad2deg(great_circle_direction(@Tokyo, @London)),
212	    336.069766430326));
213
214    ok(near(rad2deg(great_circle_direction(@Berlin, @Paris)),
215	    246.800348034667));
216    
217    ok(near(rad2deg(great_circle_direction(@Paris, @Berlin)),
218	    58.2079877553156));
219
220    use Math::Trig 'great_circle_bearing';
221
222    ok(near(rad2deg(great_circle_bearing(@Paris, @Berlin)),
223	    58.2079877553156));
224
225    use Math::Trig 'great_circle_waypoint';
226    use Math::Trig 'great_circle_midpoint';
227
228    my ($lon, $lat);
229
230    ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.0);
231
232    ok(near($lon, $London[0]));
233
234    ok(near($lat, $London[1]));
235
236    ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 1.0);
237
238    ok(near($lon, $Tokyo[0]));
239
240    ok(near($lat, $Tokyo[1]));
241
242    ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.5);
243
244    ok(near($lon, 1.55609593577679)); # 89.16 E
245
246    ok(near($lat, 0.36783532946162)); # 68.93 N
247
248    ($lon, $lat) = great_circle_midpoint(@London, @Tokyo);
249
250    ok(near($lon, 1.55609593577679)); # 89.16 E
251
252    ok(near($lat, 0.367835329461615)); # 68.93 N
253
254    ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.25);
255
256    ok(near($lon, 0.516073562850837)); # 29.57 E
257
258    ok(near($lat, 0.400231313403387)); # 67.07 N
259
260    ($lon, $lat) = great_circle_waypoint(@London, @Tokyo, 0.75);
261
262    ok(near($lon, 2.17494903805952)); # 124.62 E
263
264    ok(near($lat, 0.617809294053591)); # 54.60 N
265
266    use Math::Trig 'great_circle_destination';
267
268    my $dir1 = great_circle_direction(@London, @Tokyo);
269    my $dst1 = great_circle_distance(@London,  @Tokyo);
270
271    ($lon, $lat) = great_circle_destination(@London, $dir1, $dst1);
272
273    ok(near($lon, $Tokyo[0]));
274
275    ok(near($lat, $pip2 - $Tokyo[1]));
276
277    my $dir2 = great_circle_direction(@Tokyo, @London);
278    my $dst2 = great_circle_distance(@Tokyo,  @London);
279
280    ($lon, $lat) = great_circle_destination(@Tokyo, $dir2, $dst2);
281
282    ok(near($lon, $London[0]));
283
284    ok(near($lat, $pip2 - $London[1]));
285
286    my $dir3 = (great_circle_destination(@London, $dir1, $dst1))[2];
287
288    ok(near($dir3, 2.69379263839118)); # about 154.343 deg
289
290    my $dir4 = (great_circle_destination(@Tokyo,  $dir2, $dst2))[2];
291
292    ok(near($dir4, 3.6993902625701)); # about 211.959 deg
293
294    ok(near($dst1, $dst2));
295}
296
297SKIP: {
298# With netbsd-vax (or any vax) there is neither Inf, nor 1e40.
299skip("different float range", 42) if $vax_float;
300skip("no inf",                42) unless $has_inf;
301
302print "# Infinity\n";
303
304my $BigDouble = eval '1e40';
305
306# E.g. netbsd-alpha core dumps on Inf arith without this.
307local $SIG{FPE} = sub { };
308
309ok(Inf() > $BigDouble);  # This passes in netbsd-alpha.
310ok(Inf() + $BigDouble > $BigDouble); # This coredumps in netbsd-alpha.
311ok(Inf() + $BigDouble == Inf());
312ok(Inf() - $BigDouble > $BigDouble);
313ok(Inf() - $BigDouble == Inf());
314ok(Inf() * $BigDouble > $BigDouble);
315ok(Inf() * $BigDouble == Inf());
316ok(Inf() / $BigDouble > $BigDouble);
317ok(Inf() / $BigDouble == Inf());
318
319ok(-Inf() < -$BigDouble);
320ok(-Inf() + $BigDouble < $BigDouble);
321ok(-Inf() + $BigDouble == -Inf());
322ok(-Inf() - $BigDouble < -$BigDouble);
323ok(-Inf() - $BigDouble == -Inf());
324ok(-Inf() * $BigDouble < -$BigDouble);
325ok(-Inf() * $BigDouble == -Inf());
326ok(-Inf() / $BigDouble < -$BigDouble);
327ok(-Inf() / $BigDouble == -Inf());
328
329print "# sinh/sech/cosh/csch/tanh/coth unto infinity\n";
330
331ok(near(sinh(100), eval '1.3441e+43', 1e-3));
332ok(near(sech(100), eval '7.4402e-44', 1e-3));
333ok(near(cosh(100), eval '1.3441e+43', 1e-3));
334ok(near(csch(100), eval '7.4402e-44', 1e-3));
335ok(near(tanh(100), 1));
336ok(near(coth(100), 1));
337
338ok(near(sinh(-100), eval '-1.3441e+43', 1e-3));
339ok(near(sech(-100), eval ' 7.4402e-44', 1e-3));
340ok(near(cosh(-100), eval ' 1.3441e+43', 1e-3));
341ok(near(csch(-100), eval '-7.4402e-44', 1e-3));
342ok(near(tanh(-100), -1));
343ok(near(coth(-100), -1));
344
345cmp_ok(sinh(1e5), '==', Inf());
346cmp_ok(sech(1e5), '==', 0);
347cmp_ok(cosh(1e5), '==', Inf());
348cmp_ok(csch(1e5), '==', 0);
349cmp_ok(tanh(1e5), '==', 1);
350cmp_ok(coth(1e5), '==', 1);
351
352cmp_ok(sinh(-1e5), '==', -Inf());
353cmp_ok(sech(-1e5), '==', 0);
354cmp_ok(cosh(-1e5), '==', Inf());
355cmp_ok(csch(-1e5), '==', 0);
356cmp_ok(tanh(-1e5), '==', -1);
357cmp_ok(coth(-1e5), '==', -1);
358
359}
360
361print "# great_circle_distance with small angles\n";
362
363for my $e (qw(1e-2 1e-3 1e-4 1e-5)) {
364    # Can't assume == 0 because of floating point fuzz,
365    # but let's hope for at least < $e.
366    cmp_ok(great_circle_distance(0, $e, 0, $e), '<', $e,
367           "great_circle_distance(0, $e, 0, $e) < $e");
368}
369
370for my $e (qw(1e-5 1e-6 1e-7 1e-8)) {
371    # Verify that the distance is positive for points close together. A poor
372    # algorithm is likely to give a distance of zero in some of these cases.
373    cmp_ok(great_circle_distance(2, 2, 2, 2+$e), '>', 0,
374           "great_circle_distance(2, 2, 2, " . (2+$e) . ") > 0");
375}
376
377print "# asin_real, acos_real\n";
378
379is(acos_real(-2.0), pi);
380is(acos_real(-1.0), pi);
381is(acos_real(-0.5), acos(-0.5));
382is(acos_real( 0.0), acos( 0.0));
383is(acos_real( 0.5), acos( 0.5));
384is(acos_real( 1.0), 0);
385is(acos_real( 2.0), 0);
386
387is(asin_real(-2.0), -&pip2);
388is(asin_real(-1.0), -&pip2);
389is(asin_real(-0.5), asin(-0.5));
390is(asin_real( 0.0), asin( 0.0));
391is(asin_real( 0.5), asin( 0.5));
392is(asin_real( 1.0),  pip2);
393is(asin_real( 2.0),  pip2);
394
395# eof
396