This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Unicode ReadMe update for Unicode 3.1.
[perl5.git] / t / lib / trig.t
CommitLineData
5aabfad6
PP
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
11BEGIN {
12 chdir 't' if -d 't';
20822f61 13 @INC = '../lib';
5aabfad6
PP
14}
15
16use Math::Trig;
17
18use strict;
19
20use vars qw($x $y $z);
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
SB
29 my $e = defined $_[2] ? $_[2] : $eps;
30 $_[1] ? (abs($_[0]/$_[1] - 1) < $e) : abs($_[0]) < $e;
5aabfad6
PP
31}
32
fdf27e67 33print "1..23\n";
5aabfad6
PP
34
35$x = 0.9;
36print 'not ' unless (near(tan($x), sin($x) / cos($x)));
37print "ok 1\n";
38
39print 'not ' unless (near(sinh(2), 3.62686040784702));
40print "ok 2\n";
41
42print 'not ' unless (near(acsch(0.1), 2.99822295029797));
43print "ok 3\n";
44
45$x = asin(2);
46print 'not ' unless (ref $x eq 'Math::Complex');
47print "ok 4\n";
48
49# avoid using Math::Complex here
50$x =~ /^([^-]+)(-[^i]+)i$/;
51($y, $z) = ($1, $2);
52print 'not ' unless (near($y, 1.5707963267949) and
53 near($z, -1.31695789692482));
54print "ok 5\n";
55
ace5de91 56print 'not ' unless (near(deg2rad(90), pi/2));
5aabfad6
PP
57print "ok 6\n";
58
ace5de91
GS
59print 'not ' unless (near(rad2deg(pi), 180));
60print "ok 7\n";
61
d54bf66f
JH
62use Math::Trig ':radial';
63
64{
65 my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
66
67 print 'not ' unless (near($r, sqrt(2))) and
68 (near($t, deg2rad(45))) and
69 (near($z, 1));
70 print "ok 8\n";
71
72 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
73
74 print 'not ' unless (near($x, 1)) and
75 (near($y, 1)) and
76 (near($z, 1));
77 print "ok 9\n";
78
79 ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
80
81 print 'not ' unless (near($r, sqrt(2))) and
82 (near($t, deg2rad(45))) and
83 (near($z, 0));
84 print "ok 10\n";
85
86 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
87
88 print 'not ' unless (near($x, 1)) and
89 (near($y, 1)) and
90 (near($z, 0));
91 print "ok 11\n";
92}
93
94{
95 my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
96
97 print 'not ' unless (near($r, sqrt(3))) and
98 (near($t, deg2rad(45))) and
99 (near($f, atan2(sqrt(2), 1)));
100 print "ok 12\n";
101
102 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
103
104 print 'not ' unless (near($x, 1)) and
105 (near($y, 1)) and
106 (near($z, 1));
107 print "ok 13\n";
108
109 ($r,$t,$f) = cartesian_to_spherical(1,1,0);
110
111 print 'not ' unless (near($r, sqrt(2))) and
112 (near($t, deg2rad(45))) and
113 (near($f, deg2rad(90)));
114 print "ok 14\n";
115
116 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
117
118 print 'not ' unless (near($x, 1)) and
119 (near($y, 1)) and
120 (near($z, 0));
121 print "ok 15\n";
122}
123
124{
125 my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
126
127 print 'not ' unless (near($r, 1)) and
128 (near($t, 1)) and
129 (near($z, 1));
130 print "ok 16\n";
131
132 ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
133
134 print 'not ' unless (near($r, 1)) and
135 (near($t, 1)) and
136 (near($z, 1));
137 print "ok 17\n";
138}
139
140{
9db5a202 141 use Math::Trig 'great_circle_distance';
d54bf66f 142
9db5a202
JH
143 print 'not '
144 unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2));
145 print "ok 18\n";
d54bf66f 146
9db5a202
JH
147 print 'not '
148 unless (near(great_circle_distance(0, 0, pi, pi), pi));
149 print "ok 19\n";
d54bf66f 150
9db5a202
JH
151 # London to Tokyo.
152 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
153 my @T = (deg2rad(139.8),deg2rad(90 - 35.7));
d54bf66f 154
9db5a202 155 my $km = great_circle_distance(@L, @T, 6378);
d54bf66f 156
9db5a202
JH
157 print 'not ' unless (near($km, 9605.26637021388));
158 print "ok 20\n";
159}
160
161{
fdf27e67
JH
162 my $R2D = 57.295779513082320876798154814169;
163
164 sub frac { $_[0] - int($_[0]) }
165
9db5a202 166 my $lotta_radians = deg2rad(1E+20, 1);
fdf27e67 167 print "not " unless near($lotta_radians, 1E+20/$R2D);
9db5a202
JH
168 print "ok 21\n";
169
170 my $negat_degrees = rad2deg(-1E20, 1);
fdf27e67 171 print "not " unless near($negat_degrees, -1E+20*$R2D);
9db5a202
JH
172 print "ok 22\n";
173
174 my $posit_degrees = rad2deg(-10000, 1);
fdf27e67 175 print "not " unless near($posit_degrees, -10000*$R2D);
9db5a202 176 print "ok 23\n";
d54bf66f
JH
177}
178
5aabfad6 179# eof