isMethods.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. if (typeof Test === 'undefined') require('../tester');
  2. Test('`is` methods', function () {
  3. var n;
  4. // isEqualTo eq
  5. // isFinite
  6. // isGreaterThan gt
  7. // isGreaterThanOrEqualTo gte
  8. // isInteger
  9. // isLessThan lt
  10. // isLessThanOrEqualTo lte
  11. // isNaN
  12. // isNegative
  13. // isPositive
  14. // isZero
  15. // valueOf
  16. function t(expected, value) {
  17. Test.areEqual(expected, value);
  18. }
  19. t(BigNumber.prototype.isEqualTo, BigNumber.prototype.eq);
  20. t(BigNumber.prototype.isGreaterThan, BigNumber.prototype.gt);
  21. t(BigNumber.prototype.isGreaterThanOrEqualTo, BigNumber.prototype.gte);
  22. t(BigNumber.prototype.isLessThan,BigNumber.prototype.lt);
  23. t(BigNumber.prototype.isLessThanOrEqualTo, BigNumber.prototype.lte);
  24. BigNumber.config({
  25. DECIMAL_PLACES: 20,
  26. ROUNDING_MODE: 4,
  27. EXPONENTIAL_AT: 1e+9,
  28. RANGE: 1e+9
  29. });
  30. n = new BigNumber(1);
  31. t(true, n.isFinite());
  32. t(true, n.isInteger());
  33. t(false, n.isNaN());
  34. t(false, n.isNegative());
  35. t(true, n.isPositive());
  36. t(false, n.isZero());
  37. t(true, n.isEqualTo(n));
  38. t(true, n.isEqualTo(n, 2));
  39. t(true, n.isEqualTo(1, 3));
  40. t(true, n.isEqualTo(n, 4));
  41. t(true, n.isEqualTo(1, 5));
  42. t(true, n.isEqualTo(n, 6));
  43. t(true, n.isEqualTo(1, 7));
  44. t(true, n.isEqualTo(n, 8));
  45. t(true, n.isEqualTo(1, 9));
  46. t(true, n.isEqualTo(n, 10));
  47. t(true, n.isEqualTo(n, 11));
  48. t(true, n.isEqualTo(1, 12));
  49. t(true, n.isEqualTo(n, 13));
  50. t(true, n.isEqualTo(1, 14));
  51. t(true, n.isEqualTo(n, 15));
  52. t(true, n.isEqualTo(1, 16));
  53. t(true, n.isEqualTo(n, 17));
  54. t(true, n.isEqualTo(1, 18));
  55. t(true, n.isEqualTo(n, 19));
  56. t(true, n.isEqualTo('1.0', 20));
  57. t(true, n.isEqualTo('1.00', 21));
  58. t(true, n.isEqualTo('1.000', 22));
  59. t(true, n.isEqualTo('1.0000', 23));
  60. t(true, n.isEqualTo('1.00000', 24));
  61. t(true, n.isEqualTo('1.000000', 25));
  62. t(true, n.isEqualTo(new BigNumber(1, 10), 26));
  63. t(true, n.isEqualTo(new BigNumber(1), 27));
  64. t(true, n.isEqualTo(1, 28));
  65. t(true, n.isEqualTo(1, 29));
  66. t(true, n.isEqualTo(1, 30));
  67. t(true, n.isEqualTo(1, 31));
  68. t(true, n.isEqualTo(1, 32));
  69. t(true, n.isEqualTo(1, 33));
  70. t(true, n.isEqualTo(1, 34));
  71. t(true, n.isEqualTo(1, 35));
  72. t(true, n.isEqualTo(1, 36));
  73. t(true, n.isGreaterThan(0.99999));
  74. t(false, n.isGreaterThanOrEqualTo(1.1));
  75. t(true, n.isLessThan(1.001));
  76. t(true, n.isLessThanOrEqualTo(2));
  77. t(true, n.toString() === n.valueOf());
  78. n = new BigNumber('-0.1');
  79. t(true, n.isFinite());
  80. t(false, n.isInteger());
  81. t(false, n.isNaN());
  82. t(true, n.isNegative());
  83. t(false, n.isPositive());
  84. t(false, n.isZero());
  85. t(false, n.isEqualTo(0.1));
  86. t(false, n.isGreaterThan(-0.1));
  87. t(true, n.isGreaterThanOrEqualTo(-1));
  88. t(true, n.isLessThan(-0.01));
  89. t(false, n.isLessThanOrEqualTo(-1));
  90. t(true, n.toString() === n.valueOf());
  91. n = new BigNumber(Infinity);
  92. t(false, n.isFinite());
  93. t(false, n.isInteger());
  94. t(false, n.isNaN());
  95. t(false, n.isNegative());
  96. t(true, n.isPositive());
  97. t(false, n.isZero());
  98. t(true, n.eq('Infinity'));
  99. t(true, n.eq(1/0));
  100. t(true, n.gt('9e999'));
  101. t(true, n.gte(Infinity));
  102. t(false, n.lt(Infinity));
  103. t(true, n.lte(Infinity));
  104. t(true, n.toString() === n.valueOf());
  105. n = new BigNumber('-Infinity');
  106. t(false, n.isFinite());
  107. t(false, n.isInteger());
  108. t(false, n.isNaN());
  109. t(true, n.isNegative());
  110. t(false, n.isPositive());
  111. t(false, n.isZero());
  112. t(false, n.isEqualTo(Infinity));
  113. t(true, n.isEqualTo(-1/0));
  114. t(false, n.isGreaterThan(-Infinity));
  115. t(true, n.isGreaterThanOrEqualTo('-Infinity', 8));
  116. t(true, n.isLessThan(0));
  117. t(true, n.isLessThanOrEqualTo(Infinity));
  118. t(true, n.toString() === n.valueOf());
  119. n = new BigNumber('0.0000000');
  120. t(true, n.isFinite());
  121. t(true, n.isInteger());
  122. t(false, n.isNaN());
  123. t(false, n.isNegative());
  124. t(true, n.isPositive());
  125. t(true, n.isZero());
  126. t(true, n.eq(-0));
  127. t(true, n.gt(-0.000001));
  128. t(false, n.gte(0.1));
  129. t(true, n.lt(0.0001));
  130. t(true, n.lte(-0));
  131. t(true, n.toString() === n.valueOf());
  132. n = new BigNumber(-0);
  133. t(true, n.isFinite());
  134. t(true, n.isInteger());
  135. t(false, n.isNaN());
  136. t(true, n.isNegative());
  137. t(false, n.isPositive());
  138. t(true, n.isZero());
  139. t(true, n.isEqualTo('0.000'));
  140. t(true, n.isGreaterThan(-1));
  141. t(false, n.isGreaterThanOrEqualTo(0.1));
  142. t(false, n.isLessThan(0));
  143. t(false, n.isLessThan(0, 36));
  144. t(true, n.isLessThan(0.1));
  145. t(true, n.isLessThanOrEqualTo(0));
  146. t(true, n.valueOf() === '-0');
  147. t(true, n.toJSON() === '-0');
  148. t(true, n.toString() === '0');
  149. n = new BigNumber('NaN');
  150. t(false, n.isFinite());
  151. t(false, n.isInteger());
  152. t(true, n.isNaN());
  153. t(false, n.isNegative());
  154. t(false, n.isPositive());
  155. t(false, n.isZero());
  156. t(false, n.eq(NaN));
  157. t(false, n.eq(Infinity));
  158. t(false, n.gt(0));
  159. t(false, n.gte(0));
  160. t(false, n.lt(1));
  161. t(false, n.lte(-0));
  162. t(false, n.lte(-1));
  163. t(true, n.toString() === n.valueOf());
  164. n = new BigNumber('-1.234e+2');
  165. t(true, n.isFinite());
  166. t(false, n.isInteger());
  167. t(false, n.isNaN());
  168. t(true, n.isNegative());
  169. t(false, n.isPositive());
  170. t(false, n.isZero());
  171. t(true, n.eq(-123.4, 10));
  172. t(true, n.gt('-ff', 16));
  173. t(true, n.gte('-1.234e+3'));
  174. t(true, n.lt(-123.39999));
  175. t(true, n.lte('-123.4e+0'));
  176. t(true, n.toString() === n.valueOf());
  177. n = new BigNumber('5e-200');
  178. t(true, n.isFinite());
  179. t(false, n.isInteger());
  180. t(false, n.isNaN());
  181. t(false, n.isNegative());
  182. t(true, n.isPositive());
  183. t(false, n.isZero());
  184. t(true, n.isEqualTo(5e-200));
  185. t(true, n.isGreaterThan(5e-201));
  186. t(false, n.isGreaterThanOrEqualTo(1));
  187. t(true, n.isLessThan(6e-200));
  188. t(true, n.isLessThanOrEqualTo(5.1e-200));
  189. t(true, n.toString() === n.valueOf());
  190. n = new BigNumber('1');
  191. t(true, n.isEqualTo(n));
  192. t(true, n.isEqualTo(n.toString()));
  193. t(true, n.isEqualTo(n.toString()));
  194. t(true, n.isEqualTo(n.valueOf()));
  195. t(true, n.isEqualTo(n.toFixed()));
  196. t(true, n.isEqualTo(1));
  197. t(true, n.isEqualTo('1e+0'));
  198. t(false, n.isEqualTo(-1));
  199. t(false, n.isEqualTo(0.1));
  200. t(true, new BigNumber(10).isGreaterThan(10, 2));
  201. t(true, new BigNumber(10).isGreaterThan(10, 3));
  202. t(true, new BigNumber(10).isGreaterThan(10, 4));
  203. t(true, new BigNumber(10).isGreaterThan(10, 5));
  204. t(true, new BigNumber(10).isGreaterThan(10, 6));
  205. t(true, new BigNumber(10).isGreaterThan(10, 7));
  206. t(true, new BigNumber(10).isGreaterThan(10, 8));
  207. t(true, new BigNumber(10).isGreaterThan(10, 9));
  208. t(false, new BigNumber(10).isGreaterThan(10, 10));
  209. t(false, new BigNumber(10).isGreaterThan(10, 11));
  210. t(false, new BigNumber(10).isGreaterThan(10, 12));
  211. t(false, new BigNumber(10).isGreaterThan(10, 13));
  212. t(true, new BigNumber(10).isLessThan(10, 11));
  213. t(true, new BigNumber(10).isLessThan(10, 12));
  214. t(true, new BigNumber(10).isLessThan(10, 13));
  215. t(true, new BigNumber(10).isLessThan(10, 14));
  216. t(true, new BigNumber(10).isLessThan(10, 15));
  217. t(true, new BigNumber(10).isLessThan(10, 16));
  218. t(true, new BigNumber(10).isLessThan(10, 17));
  219. t(true, new BigNumber(10).isLessThan(10, 18));
  220. t(true, new BigNumber(10).isLessThan(10, 19));
  221. t(true, new BigNumber(10).isLessThan(10, 20));
  222. t(true, new BigNumber(10).isLessThan(10, 21));
  223. t(true, new BigNumber(10).isLessThan(10, 22));
  224. t(true, new BigNumber(10).isLessThan(10, 34));
  225. t(true, new BigNumber(10).isLessThan(10, 35));
  226. t(true, new BigNumber(10).isLessThan(10, 36));
  227. t(false, new BigNumber(NaN).isLessThan(NaN));
  228. t(false, new BigNumber(Infinity).isLessThan(-Infinity));
  229. t(false, new BigNumber(Infinity).isLessThan(Infinity));
  230. t(true, new BigNumber(Infinity, 10).isLessThanOrEqualTo(Infinity, 2));
  231. t(false, new BigNumber(NaN).isGreaterThanOrEqualTo(NaN));
  232. t(true, new BigNumber(Infinity).isGreaterThanOrEqualTo(Infinity));
  233. t(true, new BigNumber(Infinity).isGreaterThanOrEqualTo(-Infinity));
  234. t(false, new BigNumber(NaN).isGreaterThanOrEqualTo(-Infinity));
  235. t(true, new BigNumber(-Infinity).isGreaterThanOrEqualTo(-Infinity));
  236. t(false, new BigNumber(2, 10).isGreaterThan(10, 2));
  237. t(false, new BigNumber(10, 2).isLessThan(2, 10));
  238. t(true, new BigNumber(255).isLessThanOrEqualTo('ff', 16));
  239. t(true, new BigNumber('a', 16).isGreaterThanOrEqualTo(9, 16));
  240. t(false, new BigNumber(0).isLessThanOrEqualTo('NaN'));
  241. t(false, new BigNumber(0).isGreaterThanOrEqualTo(NaN));
  242. t(false, new BigNumber(NaN, 2).isLessThanOrEqualTo('NaN', 36));
  243. t(false, new BigNumber(NaN, 36).isGreaterThanOrEqualTo(NaN, 2));
  244. t(false, new BigNumber(0).isLessThanOrEqualTo(-Infinity));
  245. t(true, new BigNumber(0).isGreaterThanOrEqualTo(-Infinity));
  246. t(true, new BigNumber(0).isLessThanOrEqualTo('Infinity', 36));
  247. t(false, new BigNumber(0).isGreaterThanOrEqualTo('Infinity', 36));
  248. t(false, new BigNumber(10).isLessThanOrEqualTo(20, 4));
  249. t(true, new BigNumber(10).isLessThanOrEqualTo(20, 5));
  250. t(false, new BigNumber(10).isGreaterThanOrEqualTo(20, 6));
  251. t(false, new BigNumber(1.23001e-2).isLessThan(1.23e-2));
  252. t(true, new BigNumber(1.23e-2).lt(1.23001e-2));
  253. t(false, new BigNumber(1e-2).isLessThan(9.999999e-3));
  254. t(true, new BigNumber(9.999999e-3).lt(1e-2));
  255. t(false, new BigNumber(1.23001e+2).isLessThan(1.23e+2));
  256. t(true, new BigNumber(1.23e+2).lt(1.23001e+2));
  257. t(true, new BigNumber(9.999999e+2).isLessThan(1e+3));
  258. t(false, new BigNumber(1e+3).lt(9.9999999e+2));
  259. t(false, new BigNumber(1.23001e-2).isLessThanOrEqualTo(1.23e-2));
  260. t(true, new BigNumber(1.23e-2).lte(1.23001e-2));
  261. t(false, new BigNumber(1e-2).isLessThanOrEqualTo(9.999999e-3));
  262. t(true, new BigNumber(9.999999e-3).lte(1e-2));
  263. t(false, new BigNumber(1.23001e+2).isLessThanOrEqualTo(1.23e+2));
  264. t(true, new BigNumber(1.23e+2).lte(1.23001e+2));
  265. t(true, new BigNumber(9.999999e+2).isLessThanOrEqualTo(1e+3));
  266. t(false, new BigNumber(1e+3).lte(9.9999999e+2));
  267. t(true, new BigNumber(1.23001e-2).isGreaterThan(1.23e-2));
  268. t(false, new BigNumber(1.23e-2).gt(1.23001e-2));
  269. t(true, new BigNumber(1e-2).isGreaterThan(9.999999e-3));
  270. t(false, new BigNumber(9.999999e-3).gt(1e-2));
  271. t(true, new BigNumber(1.23001e+2).isGreaterThan(1.23e+2));
  272. t(false, new BigNumber(1.23e+2).gt(1.23001e+2));
  273. t(false, new BigNumber(9.999999e+2).isGreaterThan(1e+3));
  274. t(true, new BigNumber(1e+3).gt(9.9999999e+2));
  275. t(true, new BigNumber(1.23001e-2).isGreaterThanOrEqualTo(1.23e-2));
  276. t(false, new BigNumber(1.23e-2).gte(1.23001e-2));
  277. t(true, new BigNumber(1e-2).isGreaterThanOrEqualTo(9.999999e-3));
  278. t(false, new BigNumber(9.999999e-3).gte(1e-2));
  279. t(true, new BigNumber(1.23001e+2).isGreaterThanOrEqualTo(1.23e+2));
  280. t(false, new BigNumber(1.23e+2).gte(1.23001e+2));
  281. t(false, new BigNumber(9.999999e+2).isGreaterThanOrEqualTo(1e+3));
  282. t(true, new BigNumber(1e+3).gte(9.9999999e+2));
  283. Test.isException(function () {new BigNumber(1).lt(true, null)}, "new BigNumber(1).lt(true, null)");
  284. Test.isException(function () {new BigNumber(1).gt('one')}, "new BigNumber(1).gt('one')");
  285. });