05-trigonometry.t 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!perl -T
  2. use 5.006;
  3. use strict;
  4. use warnings;
  5. use Test::More tests => 111;
  6. use Sidef;
  7. my $o = 'Sidef::Types::Number::Number';
  8. my $pi = $o->pi;
  9. my $d = $o->new(45);
  10. my $r = $pi->div($o->new(4));
  11. sub rad2deg {
  12. $o->new(180)->div($pi)->mul($_[0]);
  13. }
  14. sub deg2rad {
  15. $pi->div($o->new(180))->mul($_[0]);
  16. }
  17. like($r->sin, qr/^0\.7071067811865/);
  18. like($r->cos, qr/^0\.7071067811865/);
  19. like(deg2rad($d)->sin, qr/^0\.7071067811865/);
  20. like(deg2rad($d)->cos, qr/^0\.7071067811865/);
  21. is($r->tan, $o->new(1));
  22. is($r->cot, $o->new(1));
  23. my $asin = $r->sin->asin;
  24. is($asin, $pi->div($o->new(4)));
  25. is(rad2deg($asin), $d);
  26. my $acos = $r->cos->acos;
  27. is($acos, $pi->div($o->new(4)));
  28. is(rad2deg($acos), $d);
  29. my $atan = $r->tan->atan;
  30. is($atan, $pi->div($o->new(4)));
  31. is(rad2deg($atan), $d);
  32. my $acot = $r->cot->acot;
  33. is($acot, $pi->div($o->new(4)));
  34. is(rad2deg($acot), $d);
  35. like($o->new(1)->atan2($o->new(1))->mul($o->new(4)), qr/^3.14159/);
  36. #
  37. ## More tests
  38. #
  39. {
  40. my $x = $o->new(5);
  41. my $y = $x->inv;
  42. my $type = 'Sidef::Types::Number::Number';
  43. my %tests = (
  44. asin => [[$type, qr/^1\.570796326.*?\s*\+\s*2\.2924[^i]*i\z/], [$type, qr/^0\.2013579207903307/],],
  45. sinh => [[$type, qr/^74\.203210577788758/], [$type, qr/^0\.2013360025410939/],],
  46. asinh => [[$type, qr/^2\.312438341272752/], [$type, qr/^0\.198690110349241/],],
  47. acos => [[$type, qr/^-2\.292431669561177[^i]*i\z/], [$type, qr/^1\.36943840600456582/],],
  48. cosh => [[$type, qr/^74\.20994852478784444/], [$type, qr/^1\.020066755619075846/],],
  49. acosh => [[$type, qr/^2\.292431669561177687/], [$type, qr/^1\.369438406004565827[^i]*i\z/],],
  50. tan => [[$type, qr/^-3\.3805150062465856369/], [$type, qr/^0\.202710035508672483321/],],
  51. atan => [[$type, qr/^1\.373400766945015860861/], [$type, qr/^0\.197395559849880758370/],],
  52. tanh => [[$type, qr/^0\.9999092042625951312109/], [$type, qr/^0\.1973753202249040007381/],],
  53. atanh => [[$type, qr/^0\.2027325540540.*?\s*\+\s*1\.5707963267948966[^i]*i\z/], [$type, qr/^0\.20273255405408219/],],
  54. sec => [[$type, qr/^3\.525320085816088406/], [$type, qr/^1\.020338844941192689/],],
  55. asec => [[$type, qr/^1\.36943840600456582777/], [$type, qr/^-2\.29243166956117768[^i]*i\z/],],
  56. sech => [[$type, qr/^0\.01347528222130455730/], [$type, qr/^0\.98032799764472533487/],],
  57. asech => [[$type, qr/^1\.36943840600456582777[^i]*i\z/], [$type, qr/^2\.2924316695611776878007873/],],
  58. csc => [[$type, qr/^-1\.04283521277140581978311985/], [$type, qr/^5\.033489547672344202426096367/],],
  59. acsc => [[$type, qr/^0\.2013579207903307914551255522/], [$type, qr/^1\.570796326794896619231321.*?\s*\+\s*2\.29243166956117768780[^i]*i\z/],],
  60. csch => [[$type, qr/^0\.0134765058305890866553818/], [$type, qr/^4\.9668215688145168965134827/],],
  61. acsch => [[$type, qr/^0\.1986901103492414064746369/], [$type, qr/^2\.3124383412727526202535623/],],
  62. cot => [[$type, qr/^-0\.2958129155327455404277671/], [$type, qr/^4\.93315487558689365736801632/],],
  63. acot => [[$type, qr/^0\.19739555984988075837004976/], [$type, qr/^1\.37340076694501586086127192/],],
  64. coth => [[$type, qr/^1\.00009080398201937553665792/], [$type, qr/^5\.06648956343947271363178778/],],
  65. acoth => [[$type, qr/^0\.202732554054082190989006557/], [$type, qr/^0\.2027325540540821909.*?\s*\+\s*1\.5707963267948966192[^i]*i/],],
  66. );
  67. foreach my $k (keys %tests) {
  68. my $v = $tests{$k};
  69. my $r1 = $x->$k;
  70. is(ref($r1), $v->[0][0], "1) $k");
  71. like("$r1", $v->[0][1], "1) $k");
  72. my $r2 = $y->$k;
  73. is(ref($r2), $v->[1][0], "2) $k");
  74. like("$r2", $v->[1][1], "2) $k");
  75. }
  76. #
  77. ## atan2() tests
  78. #
  79. my $r = $x->atan2($x);
  80. is(ref($r), $type);
  81. like("$r", qr/^0\.785398163397448309/);
  82. $r = $x->atan2($y);
  83. is(ref($r), $type);
  84. like("$r", qr/^1\.530817639671606/);
  85. $r = $y->atan2($x);
  86. is(ref($r), $type);
  87. like("$r", qr/^0\.0399786871232900414/);
  88. $r = $y->atan2($y);
  89. is(ref($r), $type);
  90. like("$r", qr/^0\.785398163397448309/);
  91. }