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'; |
20822f61 |
13 | @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 |
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 ($$;$) { |
e64f0054 |
29 | my $e = defined $_[2] ? $_[2] : $eps; |
d139edd6 |
30 | print "# near? $_[0] $_[1] $e\n"; |
e64f0054 |
31 | $_[1] ? (abs($_[0]/$_[1] - 1) < $e) : abs($_[0]) < $e; |
5aabfad6 |
32 | } |
33 | |
d139edd6 |
34 | print "1..29\n"; |
5aabfad6 |
35 | |
36 | $x = 0.9; |
37 | print 'not ' unless (near(tan($x), sin($x) / cos($x))); |
38 | print "ok 1\n"; |
39 | |
40 | print 'not ' unless (near(sinh(2), 3.62686040784702)); |
41 | print "ok 2\n"; |
42 | |
43 | print 'not ' unless (near(acsch(0.1), 2.99822295029797)); |
44 | print "ok 3\n"; |
45 | |
46 | $x = asin(2); |
47 | print 'not ' unless (ref $x eq 'Math::Complex'); |
48 | print "ok 4\n"; |
49 | |
50 | # avoid using Math::Complex here |
51 | $x =~ /^([^-]+)(-[^i]+)i$/; |
52 | ($y, $z) = ($1, $2); |
53 | print 'not ' unless (near($y, 1.5707963267949) and |
54 | near($z, -1.31695789692482)); |
55 | print "ok 5\n"; |
56 | |
ace5de91 |
57 | print 'not ' unless (near(deg2rad(90), pi/2)); |
5aabfad6 |
58 | print "ok 6\n"; |
59 | |
ace5de91 |
60 | print 'not ' unless (near(rad2deg(pi), 180)); |
61 | print "ok 7\n"; |
62 | |
d54bf66f |
63 | use Math::Trig ':radial'; |
64 | |
65 | { |
66 | my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1); |
67 | |
68 | print 'not ' unless (near($r, sqrt(2))) and |
69 | (near($t, deg2rad(45))) and |
70 | (near($z, 1)); |
71 | print "ok 8\n"; |
72 | |
73 | ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z); |
74 | |
75 | print 'not ' unless (near($x, 1)) and |
76 | (near($y, 1)) and |
77 | (near($z, 1)); |
78 | print "ok 9\n"; |
79 | |
80 | ($r,$t,$z) = cartesian_to_cylindrical(1,1,0); |
81 | |
82 | print 'not ' unless (near($r, sqrt(2))) and |
83 | (near($t, deg2rad(45))) and |
84 | (near($z, 0)); |
85 | print "ok 10\n"; |
86 | |
87 | ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z); |
88 | |
89 | print 'not ' unless (near($x, 1)) and |
90 | (near($y, 1)) and |
91 | (near($z, 0)); |
92 | print "ok 11\n"; |
93 | } |
94 | |
95 | { |
96 | my ($r,$t,$f) = cartesian_to_spherical(1,1,1); |
97 | |
98 | print 'not ' unless (near($r, sqrt(3))) and |
99 | (near($t, deg2rad(45))) and |
100 | (near($f, atan2(sqrt(2), 1))); |
101 | print "ok 12\n"; |
102 | |
103 | ($x,$y,$z) = spherical_to_cartesian($r, $t, $f); |
104 | |
105 | print 'not ' unless (near($x, 1)) and |
106 | (near($y, 1)) and |
107 | (near($z, 1)); |
108 | print "ok 13\n"; |
109 | |
110 | ($r,$t,$f) = cartesian_to_spherical(1,1,0); |
111 | |
112 | print 'not ' unless (near($r, sqrt(2))) and |
113 | (near($t, deg2rad(45))) and |
114 | (near($f, deg2rad(90))); |
115 | print "ok 14\n"; |
116 | |
117 | ($x,$y,$z) = spherical_to_cartesian($r, $t, $f); |
118 | |
119 | print 'not ' unless (near($x, 1)) and |
120 | (near($y, 1)) and |
121 | (near($z, 0)); |
122 | print "ok 15\n"; |
123 | } |
124 | |
125 | { |
126 | my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1)); |
127 | |
128 | print 'not ' unless (near($r, 1)) and |
129 | (near($t, 1)) and |
130 | (near($z, 1)); |
131 | print "ok 16\n"; |
132 | |
133 | ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1)); |
134 | |
135 | print 'not ' unless (near($r, 1)) and |
136 | (near($t, 1)) and |
137 | (near($z, 1)); |
138 | print "ok 17\n"; |
139 | } |
140 | |
141 | { |
9db5a202 |
142 | use Math::Trig 'great_circle_distance'; |
d54bf66f |
143 | |
9db5a202 |
144 | print 'not ' |
145 | unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2)); |
146 | print "ok 18\n"; |
d54bf66f |
147 | |
9db5a202 |
148 | print 'not ' |
149 | unless (near(great_circle_distance(0, 0, pi, pi), pi)); |
150 | print "ok 19\n"; |
d54bf66f |
151 | |
9db5a202 |
152 | # London to Tokyo. |
153 | my @L = (deg2rad(-0.5), deg2rad(90 - 51.3)); |
154 | my @T = (deg2rad(139.8),deg2rad(90 - 35.7)); |
d54bf66f |
155 | |
9db5a202 |
156 | my $km = great_circle_distance(@L, @T, 6378); |
d54bf66f |
157 | |
9db5a202 |
158 | print 'not ' unless (near($km, 9605.26637021388)); |
159 | print "ok 20\n"; |
160 | } |
161 | |
162 | { |
fdf27e67 |
163 | my $R2D = 57.295779513082320876798154814169; |
164 | |
165 | sub frac { $_[0] - int($_[0]) } |
166 | |
9db5a202 |
167 | my $lotta_radians = deg2rad(1E+20, 1); |
fdf27e67 |
168 | print "not " unless near($lotta_radians, 1E+20/$R2D); |
9db5a202 |
169 | print "ok 21\n"; |
170 | |
171 | my $negat_degrees = rad2deg(-1E20, 1); |
fdf27e67 |
172 | print "not " unless near($negat_degrees, -1E+20*$R2D); |
9db5a202 |
173 | print "ok 22\n"; |
174 | |
175 | my $posit_degrees = rad2deg(-10000, 1); |
fdf27e67 |
176 | print "not " unless near($posit_degrees, -10000*$R2D); |
9db5a202 |
177 | print "ok 23\n"; |
d54bf66f |
178 | } |
179 | |
7e5f197a |
180 | { |
181 | use Math::Trig 'great_circle_direction'; |
182 | |
183 | print 'not ' |
184 | unless (near(great_circle_direction(0, 0, 0, pi/2), pi)); |
185 | print "ok 24\n"; |
186 | |
3e5d91f1 |
187 | # Retired test: Relies on atan(0, 0), which is not portable. |
188 | # print 'not ' |
189 | # unless (near(great_circle_direction(0, 0, pi, pi), -pi()/2)); |
7e5f197a |
190 | print "ok 25\n"; |
191 | |
d139edd6 |
192 | my @London = (deg2rad( -0.167), deg2rad(90 - 51.3)); |
193 | my @Tokyo = (deg2rad( 139.5), deg2rad(90 - 35.7)); |
194 | my @Berlin = (deg2rad ( 13.417), deg2rad(90 - 52.533)); |
195 | my @Paris = (deg2rad ( 2.333), deg2rad(90 - 48.867)); |
7e5f197a |
196 | |
d139edd6 |
197 | print 'not ' |
198 | unless (near(rad2deg(great_circle_direction(@London, @Tokyo)), |
199 | 31.791945393073)); |
200 | |
7e5f197a |
201 | print "ok 26\n"; |
d139edd6 |
202 | print 'not ' |
203 | unless (near(rad2deg(great_circle_direction(@Tokyo, @London)), |
204 | 336.069766430326)); |
205 | print "ok 27\n"; |
206 | |
207 | print 'not ' |
208 | unless (near(rad2deg(great_circle_direction(@Berlin, @Paris)), |
209 | 246.800348034667)); |
210 | |
211 | print "ok 28\n"; |
212 | print 'not ' |
213 | unless (near(rad2deg(great_circle_direction(@Paris, @Berlin)), |
214 | 58.2079877553156)); |
215 | print "ok 29\n"; |
7e5f197a |
216 | } |
217 | |
5aabfad6 |
218 | # eof |