general.t 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!perl -T
  2. use 5.006;
  3. use strict;
  4. use warnings;
  5. use Test::More;
  6. plan tests => 99;
  7. {
  8. use Math::AnyNum qw(:overload);
  9. is(ref(42), 'Math::AnyNum');
  10. like(sqrt(42), qr/^6\.480740698407860230965967436087996\d*\z/);
  11. like(sqrt(-1), qr/^i\z/);
  12. like(sqrt(-3.5), qr/^1\.87082869338697069279187436\d*i\z/);
  13. # False values
  14. ok(!0);
  15. ok(!(0.0));
  16. ok(!(0 / 3));
  17. ok(!NaN);
  18. ok(!(NaN + 4 * i));
  19. ok(!(4 * i + NaN));
  20. ok(!(0 * i));
  21. ok(!(0 + 0 * i));
  22. # True values
  23. ok(!!(1));
  24. ok(!!(3.41));
  25. ok(!!(3 + 4 * i));
  26. ok(!!(4 * i));
  27. ok(!!(3 + 0 * i));
  28. ok(!!(3 / 4));
  29. ok(!!(-3 / 4));
  30. ok((Math::AnyNum->new('0.1') + Math::AnyNum->new('0.2')) == Math::AnyNum->new_f('0.3'));
  31. ok((Math::AnyNum->new('0.01') + Math::AnyNum->new('0.02')) == Math::AnyNum->new_f('0.03'));
  32. ok((Math::AnyNum->new('0.001') + Math::AnyNum->new('0.002')) == Math::AnyNum->new_f('0.003'));
  33. my $x = -2;
  34. like($x->sqrt, qr/^1\.4142135623730950488016\d*i\z/);
  35. ok($x == -2);
  36. my $z = 42;
  37. is(-$z, "-42");
  38. like(-sqrt(sqrt($z)), qr/^-2\.5457298950218305182697889605762886\d*\z/);
  39. is(-(3 + 4 * sqrt(-1)), -3 - 4 * sqrt(-1));
  40. is(3.0000, 3);
  41. is(1e3, 1000);
  42. # Addition
  43. is(5 + 18, 23);
  44. like(sqrt(2) + sqrt(3), qr/^3\.1462643699419723423291350657155704455\d*\z/);
  45. like(sqrt(-5) + sqrt(-2), qr/^3\.65028153987288474521086239294097431\d*i\z/);
  46. like(3 + sqrt(2), qr/^4\.414213562373095048801\d*\z/);
  47. like(sqrt(2) + 3, qr/^4\.414213562373095048801\d*\z/);
  48. # Overloaded constants
  49. is(9475632169135683520444551629250785939312732016903426588732995012037887035065279608177193340166382911,
  50. '9475632169135683520444551629250785939312732016903426588732995012037887035065279608177193340166382911');
  51. is(1238123_8126379123123_213, '12381238126379123123213');
  52. is(1238_12213, '123812213');
  53. is(12_001, '12001');
  54. is(0x2A, '42');
  55. is(0x2_a, '42');
  56. is(05_2, '42');
  57. is(0b1, '1');
  58. is(01234, '668');
  59. is(01_234, '668');
  60. is(0b11_11_1, '31');
  61. #<<<
  62. #~ {
  63. #~ no warnings;
  64. #~ is(0b11111111111111111111111111111111111_1111, '549755813887');
  65. #~ is(0b111111111_1111___11111100111__10_0011111_11110111111_11111111111111111_1111, '36893434408654077951');
  66. #~ is(0b__, '0');
  67. #~ is(0x__, '0');
  68. #~ is(0__, '0');
  69. #~ }
  70. #>>>
  71. # Complex numbers
  72. is(Math::AnyNum->new('3+4i'), '3+4i');
  73. is(Math::AnyNum->new('-i'), '-i');
  74. is(Math::AnyNum->new('i'), 'i');
  75. is(Math::AnyNum->new('-1i'), '-i');
  76. is(Math::AnyNum->new('-0i'), '0');
  77. is(Math::AnyNum->new('0i'), '0');
  78. is(Math::AnyNum->new('+i'), 'i');
  79. is(Math::AnyNum->new('+1i'), 'i');
  80. is(Math::AnyNum->new('2i'), '2i');
  81. is(Math::AnyNum->new('-2i'), '-2i');
  82. is(Math::AnyNum->new('0-2i'), '-2i');
  83. is(Math::AnyNum->new('-0-2i'), '-2i');
  84. is(Math::AnyNum->new('3.5-2.5i'), '3.5-2.5i');
  85. is(Math::AnyNum->new('3.5-2i'), '3.5-2i');
  86. is(Math::AnyNum->new('-3.5+2i'), '-3.5+2i');
  87. is(Math::AnyNum->new('-2-3.5i'), '-2-3.5i');
  88. #is(Math::AnyNum->new('1/2+3i'), '0.5+3i'); # not supported yet
  89. #is(Math::AnyNum->new('1/2-5/8i'), '0.5-0.625i'); # =//=
  90. #is(Math::AnyNum->new('-1/2-13.2i'), '-0.5-13.2i'); # =//=
  91. is(Math::AnyNum->new('1e3-1e2i'), '1000-100i');
  92. is(Math::AnyNum->new('1.42e-3-1.42e-3i'), '0.00142-0.00142i');
  93. is(Math::AnyNum->new('-1.42e-3-1.42e-3i'), '-0.00142-0.00142i');
  94. is(Math::AnyNum->new('1.42e+3-1.42e+3i'), '1420-1420i');
  95. is(Math::AnyNum->new('1.42e+3+1.42e+3i'), '1420+1420i');
  96. is(Math::AnyNum->new('-1.42e-3i'), '-0.00142i');
  97. is(Math::AnyNum->new('1.42e-3i'), '0.00142i');
  98. is(Math::AnyNum->new('+1.42e-3i'), '0.00142i');
  99. is(Math::AnyNum->new('-3-i'), '-3-i');
  100. is(Math::AnyNum->new('-3+i'), '-3+i');
  101. is(Math::AnyNum->new('-3+0i'), '-3');
  102. is(Math::AnyNum->new('-3-0i'), '-3');
  103. is(Math::AnyNum->new('3+i'), '3+i');
  104. is(Math::AnyNum->new('3-i'), '3-i');
  105. is(Math::AnyNum->new('3-1i'), '3-i');
  106. is(Math::AnyNum->new('3+0i'), '3');
  107. # With extra-spaces
  108. is(Math::AnyNum->new('3 + 4i'), '3+4i');
  109. is(Math::AnyNum->new('-3.5 - 4i'), '-3.5-4i');
  110. # Special values
  111. is(Math::AnyNum->new('-1.42e-3'), '-0.00142');
  112. is(Math::AnyNum->new('42/12'), '7/2');
  113. is(Math::AnyNum->new('12.34'), '12.34');
  114. is(Math::AnyNum->new('0/0'), 'NaN');
  115. is(Math::AnyNum->new('0/0', 36), 'NaN');
  116. is(Math::AnyNum->new('000/000', 16), 'NaN');
  117. is(Math::AnyNum->new('dfp/abc', 12), 'NaN');
  118. is(Math::AnyNum->new('hi'), 'NaN');
  119. is(Math::AnyNum->new('-0/0'), 'NaN');
  120. is(Math::AnyNum->new('1234'), '1234');
  121. is(Math::AnyNum->new('-1234'), '-1234');
  122. is(Math::AnyNum->new('ff', 16), '255');
  123. is(Math::AnyNum->new('ff/ae', 16), '85/58');
  124. # Tests for a leading plus sign
  125. is(Math::AnyNum->new('+3/4'), '3/4');
  126. is(Math::AnyNum->new('+3/4', 36), '3/4');
  127. is(Math::AnyNum->new('+ff/aa', 36), '3/2');
  128. is(Math::AnyNum->new('+ff', 36), '555');
  129. is(Math::AnyNum->new('+42'), '42');
  130. is(Math::AnyNum->new('+42', 16), '66');
  131. # Stringification of very small values
  132. like(1 / exp(Math::AnyNum->new('459')), qr/^4\.5586138580111498673250123473364\d*e-200\z/);
  133. }