undo change#1379 (order of tests *is* significant)
[p5sagit/p5-mst-13.2.git] / t / lib / trig.t
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';
13     @INC = '../lib';
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
24 sub near ($$;$) {
25     abs($_[0] - $_[1]) < (defined $_[2] ? $_[2] : $eps);
26 }
27
28 print "1..20\n";
29
30 $x = 0.9;
31 print 'not ' unless (near(tan($x), sin($x) / cos($x)));
32 print "ok 1\n";
33
34 print 'not ' unless (near(sinh(2), 3.62686040784702));
35 print "ok 2\n";
36
37 print 'not ' unless (near(acsch(0.1), 2.99822295029797));
38 print "ok 3\n";
39
40 $x = asin(2);
41 print 'not ' unless (ref $x eq 'Math::Complex');
42 print "ok 4\n";
43
44 # avoid using Math::Complex here
45 $x =~ /^([^-]+)(-[^i]+)i$/;
46 ($y, $z) = ($1, $2);
47 print 'not ' unless (near($y,  1.5707963267949) and
48                      near($z, -1.31695789692482));
49 print "ok 5\n";
50
51 print 'not ' unless (near(deg2rad(90), pi/2));
52 print "ok 6\n";
53
54 print 'not ' unless (near(rad2deg(pi), 180));
55 print "ok 7\n";
56
57 use Math::Trig ':radial';
58
59 {
60     my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
61
62     print 'not ' unless (near($r, sqrt(2)))     and
63                         (near($t, deg2rad(45))) and
64                         (near($z, 1));
65     print "ok 8\n";
66
67     ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
68
69     print 'not ' unless (near($x, 1)) and
70                         (near($y, 1)) and
71                         (near($z, 1));
72     print "ok 9\n";
73
74     ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
75
76     print 'not ' unless (near($r, sqrt(2)))     and
77                         (near($t, deg2rad(45))) and
78                         (near($z, 0));
79     print "ok 10\n";
80
81     ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
82
83     print 'not ' unless (near($x, 1)) and
84                         (near($y, 1)) and
85                         (near($z, 0));
86     print "ok 11\n";
87 }
88
89 {
90     my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
91
92     print 'not ' unless (near($r, sqrt(3)))     and
93                         (near($t, deg2rad(45))) and
94                         (near($f, atan2(sqrt(2), 1)));
95     print "ok 12\n";
96
97     ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
98
99     print 'not ' unless (near($x, 1)) and
100                         (near($y, 1)) and
101                         (near($z, 1));
102     print "ok 13\n";
103
104     ($r,$t,$f) = cartesian_to_spherical(1,1,0);
105
106     print 'not ' unless (near($r, sqrt(2)))     and
107                         (near($t, deg2rad(45))) and
108                         (near($f, deg2rad(90)));
109     print "ok 14\n";
110
111     ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
112
113     print 'not ' unless (near($x, 1)) and
114                         (near($y, 1)) and
115                         (near($z, 0));
116     print "ok 15\n";
117 }
118
119 {
120     my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
121
122     print 'not ' unless (near($r, 1)) and
123                         (near($t, 1)) and
124                         (near($z, 1));
125     print "ok 16\n";
126
127     ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
128
129     print 'not ' unless (near($r, 1)) and
130                         (near($t, 1)) and
131                         (near($z, 1));
132     print "ok 17\n";
133 }
134
135 {
136         use Math::Trig 'great_circle_distance';
137
138         print 'not '
139             unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2));
140         print "ok 18\n";
141
142         print 'not '
143             unless (near(great_circle_distance(0, 0, pi, pi), pi));
144         print "ok 19\n";
145
146         # London to Tokyo.
147         my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
148         my @T = (deg2rad(139.8),deg2rad(90 - 35.7));
149
150         my $km = great_circle_distance(@L, @T, 6378);
151
152         print 'not ' unless (near($km, 9605.26637021388));
153         print "ok 20\n";
154 }
155
156 # eof