minmax.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. if (typeof Test === 'undefined') require('../tester');
  2. Test('minimum and maximum', function () {
  3. var t = function (value){
  4. Test.isTrue(value);
  5. }
  6. BigNumber.config({
  7. DECIMAL_PLACES: 20,
  8. ROUNDING_MODE: 4,
  9. EXPONENTIAL_AT: [-7, 21],
  10. RANGE: 1e9
  11. });
  12. Test.areEqual(BigNumber.maximum, BigNumber.max);
  13. Test.areEqual(BigNumber.minimum, BigNumber.min);
  14. function isMinusZero(x) {
  15. return x.isZero() && x.isNegative();
  16. }
  17. t(isMinusZero(BigNumber.min(-0, 0)));
  18. t(isMinusZero(BigNumber.min(0, -0)));
  19. t(isMinusZero(BigNumber.min('-0', '0')));
  20. t(isMinusZero(BigNumber.min('0', '-0')));
  21. t(isMinusZero(BigNumber.min(-0, 1, 0, 2)));
  22. t(isMinusZero(BigNumber.min(0, 1, 2, -0)));
  23. t(isMinusZero(BigNumber.min(2, 1, -0, 0)));
  24. function isPositiveZero(x) {
  25. return x.isZero() && x.isPositive();
  26. }
  27. t(isPositiveZero(BigNumber.max(-0, 0)));
  28. t(isPositiveZero(BigNumber.max(0, -0)));
  29. t(isPositiveZero(BigNumber.max('-0', '0')));
  30. t(isPositiveZero(BigNumber.max('0', '-0')));
  31. t(isPositiveZero(BigNumber.max(0, -1, -0, -2)));
  32. t(isPositiveZero(BigNumber.max(0, -1, 0, -2, -0)));
  33. t(isPositiveZero(BigNumber.max(-0, -1, -2, -0, 0)));
  34. t(!BigNumber.min(0, 0, 0).isNaN());
  35. t(BigNumber.min(NaN, -2, 0, -1).isNaN());
  36. t(BigNumber.max(NaN, -2, 0, -1).isNaN());
  37. t(BigNumber.min(-2, 0, -1, new BigNumber(NaN)).isNaN());
  38. t(BigNumber.max(-2, 0, -1, new BigNumber(NaN)).isNaN());
  39. t(!BigNumber.min(-2, 0, -1).isNaN());
  40. t(!BigNumber.max(-2, 0, -1).isNaN());
  41. t(!BigNumber.min(-2, 0, -1, Infinity).isNaN());
  42. t(!BigNumber.max(-2, 0, -1, -Infinity).isNaN());
  43. t(!BigNumber.min(-2, 0, -1, Infinity).isNaN());
  44. t(!BigNumber.max(-2, 0, -1, Infinity).isNaN());
  45. t(!BigNumber.min(-2, -Infinity, 0, -1, Infinity).isNaN());
  46. t(new BigNumber(-Infinity).eq(BigNumber.min(-Infinity, -2, 0, -1, Infinity)));
  47. t(new BigNumber(-Infinity).eq(BigNumber.min(Infinity, -2, 0, -1, -Infinity)));
  48. t(new BigNumber(Infinity).eq(BigNumber.max(Infinity, -2, 0, -1, -Infinity)));
  49. t(new BigNumber(Infinity).eq(BigNumber.max(-Infinity, -2, 0, new BigNumber(Infinity), -1)));
  50. t(new BigNumber(-2).eq(BigNumber.min(-2, 0, -1)));
  51. t(new BigNumber(0).eq(BigNumber.max(-2, 0, -1)));
  52. t(new BigNumber(-2).eq(BigNumber.min(-2, -1, 0)));
  53. t(new BigNumber(0).eq(BigNumber.max(-2, -1, 0)));
  54. t(new BigNumber(-2).eq(BigNumber.min(0, -2, -1)));
  55. t(new BigNumber(0).eq(BigNumber.max(0, -2, -1)));
  56. t(new BigNumber(-2).eq(BigNumber.min(0, -1, -2)));
  57. t(new BigNumber(0).eq(BigNumber.max(0, -1, -2)));
  58. t(new BigNumber(-2).eq(BigNumber.min(-1, -2, 0)));
  59. t(new BigNumber(0).eq(BigNumber.max(-1, -2, 0)));
  60. t(new BigNumber(-2).eq(BigNumber.min(-1, 0, -2)));
  61. t(new BigNumber(-1).eq(BigNumber.min(-1, 0, 1)));
  62. t(new BigNumber(1).eq(BigNumber.max(-1, 0, 1)));
  63. t(new BigNumber(-1).eq(BigNumber.min(-1, 1, 0)));
  64. t(new BigNumber(1).eq(BigNumber.max(-1, 1, 0)));
  65. t(new BigNumber(-1).eq(BigNumber.min(0, -1, 1)));
  66. t(new BigNumber(1).eq(BigNumber.max(0, -1, 1)));
  67. t(new BigNumber(-1).eq(BigNumber.min(0, 1, -1)));
  68. t(new BigNumber(1).eq(BigNumber.max(0, 1, -1)));
  69. t(new BigNumber(-1).eq(BigNumber.min(1, -1, 0)));
  70. t(new BigNumber(1).eq(BigNumber.max(1, -1, 0)));
  71. t(new BigNumber(-1).eq(BigNumber.min(1, 0, -1)));
  72. t(new BigNumber(-1).eq(BigNumber.min('-1', 0, new BigNumber(1))));
  73. t(new BigNumber(1).eq(BigNumber.max('-1', 0, new BigNumber(1))));
  74. t(new BigNumber(-1).eq(BigNumber.min('-1', new BigNumber(1), 0)));
  75. t(new BigNumber(1).eq(BigNumber.max('-1', new BigNumber(1), 0)));
  76. t(new BigNumber(-1).eq(BigNumber.min(0, '-1', new BigNumber(1))));
  77. t(new BigNumber(1).eq(BigNumber.max(0, '-1', new BigNumber(1))));
  78. t(new BigNumber(-1).eq(BigNumber.min(0, new BigNumber(1), '-1')));
  79. t(new BigNumber(1).eq(BigNumber.max(0, new BigNumber(1), '-1')));
  80. t(new BigNumber(-1).eq(BigNumber.min(new BigNumber(1), '-1', 0)));
  81. t(new BigNumber(1).eq(BigNumber.max(new BigNumber(1), '-1', 0)));
  82. t(new BigNumber(-1).eq(BigNumber.min(new BigNumber(1), 0, '-1')));
  83. t(new BigNumber(0).eq(BigNumber.min(0, 1, 2)));
  84. t(new BigNumber(2).eq(BigNumber.max(0, 1, 2)));
  85. t(new BigNumber(0).eq(BigNumber.min(0, 2, 1)));
  86. t(new BigNumber(2).eq(BigNumber.max(0, 2, 1)));
  87. t(new BigNumber(0).eq(BigNumber.min(1, 0, 2)));
  88. t(new BigNumber(2).eq(BigNumber.max(1, 0, 2)));
  89. t(new BigNumber(0).eq(BigNumber.min(1, 2, 0)));
  90. t(new BigNumber(2).eq(BigNumber.max(1, 2, 0)));
  91. t(new BigNumber(0).eq(BigNumber.min(2, 1, 0)));
  92. t(new BigNumber(2).eq(BigNumber.max(2, 1, 0)));
  93. t(new BigNumber(0).eq(BigNumber.min(2, 0, 1)));
  94. t(new BigNumber(2).eq(BigNumber.max(2, 0, 1)));
  95. t = function (min, max, arr) {
  96. Test.isTrue(new BigNumber(min).eq(BigNumber.min.apply(null, arr)));
  97. Test.isTrue(new BigNumber(max).eq(BigNumber.max.apply(null, arr)));
  98. }
  99. t(-2, 0, [-2, -1, 0]);
  100. t(-2, 0, [-2, 0, -1]);
  101. t(-2, 0, [-1, -2, 0]);
  102. t(-2, 0, [-1, 0, -2]);
  103. t(-2, 0, [0, -2, -1]);
  104. t(-2, 0, [0, -1, -2]);
  105. t(-1, 1, [-1, 0, 1]);
  106. t(-1, 1, [-1, 1, 0]);
  107. t(-1, 1, [0, -1, 1]);
  108. t(-1, 1, [0, 1, -1]);
  109. t(-1, 1, [1, -1, 0]);
  110. t(-1, 1, [1, 0, -1]);
  111. t(0, 2, [0, 1, 2]);
  112. t(0, 2, [0, 2, 1]);
  113. t(0, 2, [1, 0, 2]);
  114. t(0, 2, [1, 2, 0]);
  115. t(0, 2, [2, 1, 0]);
  116. t(0, 2, [2, 0, 1]);
  117. t(-0.000001, 999.001, [2, -0, '1e-9000000000000000', 324.32423423, -0.000001, '999.001', 10]);
  118. t('-9.99999e+9000000000000000', Infinity, [10, '-9.99999e+9000000000000000', new BigNumber(Infinity), '9.99999e+9000000000000000', 0]);
  119. t('-9.999999e+9000000000000000', '1.01e+9000000000000000', ['-9.99998e+9000000000000000', '-9.999999e+9000000000000000', '9e+8999999999999999', '1.01e+9000000000000000', 1e+300]);
  120. t(1, Infinity, [1, '1e+9000000000000001', 1e200]);
  121. t(-Infinity, 1, [1, '-1e+9000000000000001', -1e200]);
  122. t(0, 1, [1, '1e-9000000000000001', 1e-200]);
  123. t(0, 1, [1, '-1e-9000000000000001', 1e-200]);
  124. t(-3, 3, [1, '2', 3, '-1', -2, '-3']);
  125. });