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