Commit | Line | Data |
---|---|---|
5aabfad6 | 1 | #!./perl |
2 | ||
3 | # | |
4 | # Regression tests for the Math::Trig package | |
5 | # | |
6 | # The tests are quite modest as the Math::Complex tests exercise | |
7 | # these quite vigorously. | |
8 | # | |
9 | # -- Jarkko Hietaniemi, April 1997 | |
10 | ||
11 | BEGIN { | |
12 | chdir 't' if -d 't'; | |
93430cb4 | 13 | unshift @INC, '../lib'; |
5aabfad6 | 14 | } |
15 | ||
16 | use Math::Trig; | |
17 | ||
18 | use strict; | |
19 | ||
20 | use vars qw($x $y $z); | |
21 | ||
22 | my $eps = 1e-11; | |
23 | ||
2f367121 JH |
24 | if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t. |
25 | $eps = 1e-10; | |
26 | } | |
27 | ||
5aabfad6 | 28 | sub near ($$;$) { |
29 | abs($_[0] - $_[1]) < (defined $_[2] ? $_[2] : $eps); | |
30 | } | |
31 | ||
d54bf66f | 32 | print "1..20\n"; |
5aabfad6 | 33 | |
34 | $x = 0.9; | |
35 | print 'not ' unless (near(tan($x), sin($x) / cos($x))); | |
36 | print "ok 1\n"; | |
37 | ||
38 | print 'not ' unless (near(sinh(2), 3.62686040784702)); | |
39 | print "ok 2\n"; | |
40 | ||
41 | print 'not ' unless (near(acsch(0.1), 2.99822295029797)); | |
42 | print "ok 3\n"; | |
43 | ||
44 | $x = asin(2); | |
45 | print 'not ' unless (ref $x eq 'Math::Complex'); | |
46 | print "ok 4\n"; | |
47 | ||
48 | # avoid using Math::Complex here | |
49 | $x =~ /^([^-]+)(-[^i]+)i$/; | |
50 | ($y, $z) = ($1, $2); | |
51 | print 'not ' unless (near($y, 1.5707963267949) and | |
52 | near($z, -1.31695789692482)); | |
53 | print "ok 5\n"; | |
54 | ||
ace5de91 | 55 | print 'not ' unless (near(deg2rad(90), pi/2)); |
5aabfad6 | 56 | print "ok 6\n"; |
57 | ||
ace5de91 GS |
58 | print 'not ' unless (near(rad2deg(pi), 180)); |
59 | print "ok 7\n"; | |
60 | ||
d54bf66f JH |
61 | use Math::Trig ':radial'; |
62 | ||
63 | { | |
64 | my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1); | |
65 | ||
66 | print 'not ' unless (near($r, sqrt(2))) and | |
67 | (near($t, deg2rad(45))) and | |
68 | (near($z, 1)); | |
69 | print "ok 8\n"; | |
70 | ||
71 | ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z); | |
72 | ||
73 | print 'not ' unless (near($x, 1)) and | |
74 | (near($y, 1)) and | |
75 | (near($z, 1)); | |
76 | print "ok 9\n"; | |
77 | ||
78 | ($r,$t,$z) = cartesian_to_cylindrical(1,1,0); | |
79 | ||
80 | print 'not ' unless (near($r, sqrt(2))) and | |
81 | (near($t, deg2rad(45))) and | |
82 | (near($z, 0)); | |
83 | print "ok 10\n"; | |
84 | ||
85 | ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z); | |
86 | ||
87 | print 'not ' unless (near($x, 1)) and | |
88 | (near($y, 1)) and | |
89 | (near($z, 0)); | |
90 | print "ok 11\n"; | |
91 | } | |
92 | ||
93 | { | |
94 | my ($r,$t,$f) = cartesian_to_spherical(1,1,1); | |
95 | ||
96 | print 'not ' unless (near($r, sqrt(3))) and | |
97 | (near($t, deg2rad(45))) and | |
98 | (near($f, atan2(sqrt(2), 1))); | |
99 | print "ok 12\n"; | |
100 | ||
101 | ($x,$y,$z) = spherical_to_cartesian($r, $t, $f); | |
102 | ||
103 | print 'not ' unless (near($x, 1)) and | |
104 | (near($y, 1)) and | |
105 | (near($z, 1)); | |
106 | print "ok 13\n"; | |
107 | ||
108 | ($r,$t,$f) = cartesian_to_spherical(1,1,0); | |
109 | ||
110 | print 'not ' unless (near($r, sqrt(2))) and | |
111 | (near($t, deg2rad(45))) and | |
112 | (near($f, deg2rad(90))); | |
113 | print "ok 14\n"; | |
114 | ||
115 | ($x,$y,$z) = spherical_to_cartesian($r, $t, $f); | |
116 | ||
117 | print 'not ' unless (near($x, 1)) and | |
118 | (near($y, 1)) and | |
119 | (near($z, 0)); | |
120 | print "ok 15\n"; | |
121 | } | |
122 | ||
123 | { | |
124 | my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1)); | |
125 | ||
126 | print 'not ' unless (near($r, 1)) and | |
127 | (near($t, 1)) and | |
128 | (near($z, 1)); | |
129 | print "ok 16\n"; | |
130 | ||
131 | ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1)); | |
132 | ||
133 | print 'not ' unless (near($r, 1)) and | |
134 | (near($t, 1)) and | |
135 | (near($z, 1)); | |
136 | print "ok 17\n"; | |
137 | } | |
138 | ||
139 | { | |
140 | use Math::Trig 'great_circle_distance'; | |
141 | ||
142 | print 'not ' | |
143 | unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2)); | |
144 | print "ok 18\n"; | |
145 | ||
146 | print 'not ' | |
147 | unless (near(great_circle_distance(0, 0, pi, pi), pi)); | |
148 | print "ok 19\n"; | |
149 | ||
150 | # London to Tokyo. | |
151 | my @L = (deg2rad(-0.5), deg2rad(90 - 51.3)); | |
152 | my @T = (deg2rad(139.8),deg2rad(90 - 35.7)); | |
153 | ||
154 | my $km = great_circle_distance(@L, @T, 6378); | |
155 | ||
156 | print 'not ' unless (near($km, 9605.26637021388)); | |
157 | print "ok 20\n"; | |
158 | } | |
159 | ||
5aabfad6 | 160 | # eof |