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