home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl560.zip / t / lib / trig.t < prev   
Text File  |  1999-07-20  |  4KB  |  161 lines

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