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