4 # Regression tests for the Math::Trig package
6 # The tests are quite modest as the Math::Complex tests exercise
7 # these quite vigorously.
9 # -- Jarkko Hietaniemi, April 1997
20 use vars qw($x $y $z);
24 if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t.
29 my $e = defined $_[2] ? $_[2] : $eps;
30 $_[1] ? (abs($_[0]/$_[1] - 1) < $e) : abs($_[0]) < $e;
36 print 'not ' unless (near(tan($x), sin($x) / cos($x)));
39 print 'not ' unless (near(sinh(2), 3.62686040784702));
42 print 'not ' unless (near(acsch(0.1), 2.99822295029797));
46 print 'not ' unless (ref $x eq 'Math::Complex');
49 # avoid using Math::Complex here
50 $x =~ /^([^-]+)(-[^i]+)i$/;
52 print 'not ' unless (near($y, 1.5707963267949) and
53 near($z, -1.31695789692482));
56 print 'not ' unless (near(deg2rad(90), pi/2));
59 print 'not ' unless (near(rad2deg(pi), 180));
62 use Math::Trig ':radial';
65 my ($r,$t,$z) = cartesian_to_cylindrical(1,1,1);
67 print 'not ' unless (near($r, sqrt(2))) and
68 (near($t, deg2rad(45))) and
72 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
74 print 'not ' unless (near($x, 1)) and
79 ($r,$t,$z) = cartesian_to_cylindrical(1,1,0);
81 print 'not ' unless (near($r, sqrt(2))) and
82 (near($t, deg2rad(45))) and
86 ($x,$y,$z) = cylindrical_to_cartesian($r, $t, $z);
88 print 'not ' unless (near($x, 1)) and
95 my ($r,$t,$f) = cartesian_to_spherical(1,1,1);
97 print 'not ' unless (near($r, sqrt(3))) and
98 (near($t, deg2rad(45))) and
99 (near($f, atan2(sqrt(2), 1)));
102 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
104 print 'not ' unless (near($x, 1)) and
109 ($r,$t,$f) = cartesian_to_spherical(1,1,0);
111 print 'not ' unless (near($r, sqrt(2))) and
112 (near($t, deg2rad(45))) and
113 (near($f, deg2rad(90)));
116 ($x,$y,$z) = spherical_to_cartesian($r, $t, $f);
118 print 'not ' unless (near($x, 1)) and
125 my ($r,$t,$z) = cylindrical_to_spherical(spherical_to_cylindrical(1,1,1));
127 print 'not ' unless (near($r, 1)) and
132 ($r,$t,$z) = spherical_to_cylindrical(cylindrical_to_spherical(1,1,1));
134 print 'not ' unless (near($r, 1)) and
141 use Math::Trig 'great_circle_distance';
144 unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2));
148 unless (near(great_circle_distance(0, 0, pi, pi), pi));
152 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
153 my @T = (deg2rad(139.8),deg2rad(90 - 35.7));
155 my $km = great_circle_distance(@L, @T, 6378);
157 print 'not ' unless (near($km, 9605.26637021388));
162 my $R2D = 57.295779513082320876798154814169;
164 sub frac { $_[0] - int($_[0]) }
166 my $lotta_radians = deg2rad(1E+20, 1);
167 print "not " unless near($lotta_radians, 1E+20/$R2D);
170 my $negat_degrees = rad2deg(-1E20, 1);
171 print "not " unless near($negat_degrees, -1E+20*$R2D);
174 my $posit_degrees = rad2deg(-10000, 1);
175 print "not " unless near($posit_degrees, -10000*$R2D);
180 use Math::Trig 'great_circle_direction';
183 unless (near(great_circle_direction(0, 0, 0, pi/2), pi));
187 unless (near(great_circle_direction(0, 0, pi, pi), -pi()/2));
191 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3));
192 my @T = (deg2rad(139.8),deg2rad(90 - 35.7));
194 my $rad = great_circle_direction(@L, @T);
196 print 'not ' unless (near($rad, -0.546644569997376));