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'; 13 @INC = '../lib'; 14} 15 16use Math::Trig; 17 18use strict; 19 20use vars qw($x $y $z); 21 22my $eps = 1e-11; 23 24if ($^O eq 'unicos') { # See lib/Math/Complex.pm and t/lib/complex.t. 25 $eps = 1e-10; 26} 27 28sub near ($$;$) { 29 my $e = defined $_[2] ? $_[2] : $eps; 30 print "# near? $_[0] $_[1] $e\n"; 31 $_[1] ? (abs($_[0]/$_[1] - 1) < $e) : abs($_[0]) < $e; 32} 33 34print "1..29\n"; 35 36$x = 0.9; 37print 'not ' unless (near(tan($x), sin($x) / cos($x))); 38print "ok 1\n"; 39 40print 'not ' unless (near(sinh(2), 3.62686040784702)); 41print "ok 2\n"; 42 43print 'not ' unless (near(acsch(0.1), 2.99822295029797)); 44print "ok 3\n"; 45 46$x = asin(2); 47print 'not ' unless (ref $x eq 'Math::Complex'); 48print "ok 4\n"; 49 50# avoid using Math::Complex here 51$x =~ /^([^-]+)(-[^i]+)i$/; 52($y, $z) = ($1, $2); 53print 'not ' unless (near($y, 1.5707963267949) and 54 near($z, -1.31695789692482)); 55print "ok 5\n"; 56 57print 'not ' unless (near(deg2rad(90), pi/2)); 58print "ok 6\n"; 59 60print 'not ' unless (near(rad2deg(pi), 180)); 61print "ok 7\n"; 62 63use 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{ 142 use Math::Trig 'great_circle_distance'; 143 144 print 'not ' 145 unless (near(great_circle_distance(0, 0, 0, pi/2), pi/2)); 146 print "ok 18\n"; 147 148 print 'not ' 149 unless (near(great_circle_distance(0, 0, pi, pi), pi)); 150 print "ok 19\n"; 151 152 # London to Tokyo. 153 my @L = (deg2rad(-0.5), deg2rad(90 - 51.3)); 154 my @T = (deg2rad(139.8),deg2rad(90 - 35.7)); 155 156 my $km = great_circle_distance(@L, @T, 6378); 157 158 print 'not ' unless (near($km, 9605.26637021388)); 159 print "ok 20\n"; 160} 161 162{ 163 my $R2D = 57.295779513082320876798154814169; 164 165 sub frac { $_[0] - int($_[0]) } 166 167 my $lotta_radians = deg2rad(1E+20, 1); 168 print "not " unless near($lotta_radians, 1E+20/$R2D); 169 print "ok 21\n"; 170 171 my $negat_degrees = rad2deg(-1E20, 1); 172 print "not " unless near($negat_degrees, -1E+20*$R2D); 173 print "ok 22\n"; 174 175 my $posit_degrees = rad2deg(-10000, 1); 176 print "not " unless near($posit_degrees, -10000*$R2D); 177 print "ok 23\n"; 178} 179 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 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)); 190 print "ok 25\n"; 191 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)); 196 197 print 'not ' 198 unless (near(rad2deg(great_circle_direction(@London, @Tokyo)), 199 31.791945393073)); 200 201 print "ok 26\n"; 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"; 216} 217 218# eof 219