negated.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. if (typeof Test === 'undefined') require('../tester');
  2. Test('negated', function () {
  3. function t(expected, value){
  4. Test.areEqual(String(expected), new BigNumber(value).negated().toString());
  5. }
  6. BigNumber.config({
  7. DECIMAL_PLACES: 20,
  8. ROUNDING_MODE: 4,
  9. RANGE: 1E9,
  10. EXPONENTIAL_AT: [-7, 21]
  11. });
  12. t(-4, 4);
  13. t(-2147483648, 2147483648);
  14. t(-0.25, 0.25);
  15. t(-0.0625, 0.0625);
  16. t(-1, 1);
  17. t(1, -1);
  18. t(0, 0);
  19. t(NaN, NaN);
  20. t(-Infinity, Infinity);
  21. t(-Infinity, +Infinity);
  22. t(Infinity, -Infinity);
  23. t(+Infinity, -Infinity);
  24. t('0', '0');
  25. t('-238', '238');
  26. t('1.3e-11', '-0.000000000013');
  27. t('-33.1', '33.1');
  28. t('2.61', '-2.61');
  29. t('-4', '4.0');
  30. t('-5.8', '5.8');
  31. t('-3.52e-7', '0.000000352');
  32. t('190', '-190');
  33. t('4.47', '-4.47');
  34. t('6.9525e-12', '-0.0000000000069525');
  35. t('1.3', '-1.3');
  36. t('-6.21', '6.21');
  37. t('2', '-2');
  38. t('-1', '1');
  39. t('147.857', '-147.857');
  40. t('-26.517', '26.517');
  41. t('-3', '3');
  42. t('5', '-5');
  43. t('204', '-204');
  44. t('2.1e-8', '-0.000000021');
  45. t('3.7015e-7', '-0.00000037015');
  46. t('-50.1839', '50.1839');
  47. t('44768.1', '-44768.1');
  48. t('3.8e-15', '-0.0000000000000038');
  49. t('-7.4379', '7.4379');
  50. t('1.5', '-1.5');
  51. t('6.0399', '-6.0399');
  52. t('109.07', '-109.070');
  53. t('1582', '-1582');
  54. t('-772', '772');
  55. t('-6.7824e-14', '0.000000000000067824');
  56. t('-1.819e-8', '0.00000001819');
  57. t('-3e-15', '0.0000000000000030');
  58. t('-424120', '424120');
  59. t('-1814.54', '1814.54');
  60. t('-4.295e-17', '0.00000000000000004295');
  61. t('-5', '5');
  62. t('2152', '-2152');
  63. t('4.6', '-4.6');
  64. t('1.9', '-1.9');
  65. t('-2', '2.0');
  66. t('-0.00036', '0.00036');
  67. t('-0.000006962', '0.000006962');
  68. t('3.6', '-3.6');
  69. t('-1.1495e-14', '0.000000000000011495');
  70. t('-312.4', '312.4');
  71. t('4.3e-10', '-0.00000000043');
  72. t('5', '-5');
  73. t('-1.8911e-8', '0.000000018911');
  74. t('4963.53', '-4963.53');
  75. t('-4.3934e-10', '0.00000000043934');
  76. t('-1.3', '1.30');
  77. t('-1', '1.0');
  78. t('-68.32', '68.32');
  79. t('0.014836', '-0.014836');
  80. t('8', '-8');
  81. t('2.1351', '-2.13510');
  82. t('162224', '-162224');
  83. t('3e-19', '-0.00000000000000000030');
  84. t('0.00004985', '-0.00004985');
  85. t('28.9321', '-28.9321');
  86. t('-2', '2');
  87. t('-16688', '16688');
  88. t('-1', '1');
  89. t('5', '-5');
  90. t('-20', '20.0');
  91. t('-1.9', '1.9');
  92. t('3', '-3');
  93. t('185640', '-185640');
  94. t('-0.0000058', '0.0000058');
  95. t('9.67e-13', '-0.000000000000967');
  96. t('-707.98', '707.98');
  97. t('2.57917', '-2.57917');
  98. t('-1.3', '1.3');
  99. t('-4.2655', '4.2655');
  100. t('-149.6', '149.6');
  101. t('-1.32383', '1.32383');
  102. t('-26.925', '26.925');
  103. t('-0.00013', '0.00013');
  104. t('-6868', '6868');
  105. t('7', '-7');
  106. t('-5e-9', '0.0000000050');
  107. t('3.2555e-16', '-0.00000000000000032555');
  108. t('1.42768e-13', '-0.000000000000142768');
  109. t('11.2962', '-11.2962');
  110. t('3186.7', '-3186.7');
  111. t('-6.9', '6.9');
  112. t('-6.2618e-7', '0.00000062618');
  113. t('8', '-8');
  114. t('-8.04', '8.04');
  115. t('-22', '22');
  116. t('-750.6', '750.6');
  117. t('12.803', '-12.803');
  118. t('-20513.4', '20513.4');
  119. t('114781', '-114781');
  120. t('-16.9046', '16.9046');
  121. t('4.6e-7', '-0.00000046');
  122. t('-31399', '31399');
  123. t('1.04', '-1.04');
  124. t('-51.2544', '51.2544');
  125. t('1.023e-15', '-0.000000000000001023');
  126. t('281', '-281');
  127. t('-128315', '128315');
  128. t('20.2', '-20.2');
  129. t('9', '-9');
  130. t('-10', '10');
  131. t('-1.92262e-17', '0.0000000000000000192262');
  132. t('-0.0023', '0.0023');
  133. t('5', '-5');
  134. t('7', '-7');
  135. t('13.72', '-13.72');
  136. t('98068', '-98068');
  137. t('3.2', '-3.2');
  138. t('1.1', '-1.1');
  139. t('-3.97e-18', '0.000000000000000003970');
  140. t('0.00334824', '-0.00334824');
  141. t('-5.4892e-8', '0.000000054892');
  142. t('-1', '1.0');
  143. t('-2.8135e-8', '0.000000028135');
  144. t('-1.816e-13', '0.0000000000001816');
  145. t('199724', '-199724');
  146. t('-19.4', '19.40');
  147. t('-12.74', '12.74');
  148. t('-2171.8', '2171.8');
  149. t('-2.7', '2.7');
  150. t('1', '-1.0');
  151. t('21779', '-21779');
  152. t('8.9e-12', '-0.0000000000089');
  153. t('-4.51', '4.51');
  154. t('2.6', '-2.6');
  155. t('-0.00016', '0.000160');
  156. t('6', '-6');
  157. t('50.566', '-50.566');
  158. t('-16.2', '16.2');
  159. t('-7.9156e-20', '0.000000000000000000079156');
  160. t('-2262.4', '2262.4');
  161. t('6468.59', '-6468.59');
  162. t('0.077', '-0.077');
  163. t('-465.83', '465.83');
  164. t('-604.59', '604.59');
  165. t('-0.0014917', '0.0014917');
  166. t('-2.8954', '2.8954');
  167. t('1', '-1');
  168. t('1942', '-1942');
  169. t('-182.308', '182.308');
  170. t('-17.8', '17.8');
  171. t('39472.5', '-39472.5');
  172. t('214.21', '-214.21');
  173. t('-40.11', '40.11');
  174. t('-3', '3');
  175. t('141149', '-141149');
  176. t('-8', '8.0');
  177. t('-2.9', '2.9');
  178. t('44.51', '-44.51');
  179. t('-5.3', '5.3');
  180. t('0.05498', '-0.054980');
  181. t('7', '-7');
  182. t('-922', '922.0');
  183. t('-1.5146e-14', '0.000000000000015146');
  184. t('-0.000008117', '0.000008117');
  185. t('1', '-1');
  186. t('5452.81', '-5452.81');
  187. t('751745', '-751745');
  188. t('-2.7', '2.7');
  189. t('5.1', '-5.1');
  190. t('-1', '1');
  191. t('-524124', '524124');
  192. t('-183.5', '183.50');
  193. t('44856.8', '-44856.8');
  194. t('0.00000387', '-0.00000387');
  195. t('-3.0544e-14', '0.000000000000030544');
  196. t('1.3', '-1.3');
  197. t('-0.0019273', '0.0019273');
  198. t('75428', '-75428');
  199. t('-91.7925', '91.7925');
  200. t('44.5', '-44.5');
  201. t('-2', '2');
  202. t('5.3', '-5.3');
  203. t('-57', '57');
  204. t('-2.53e-9', '0.00000000253');
  205. t('18258', '-18258');
  206. t('0.829', '-0.829');
  207. t('-4', '4');
  208. t('-1', '1');
  209. t('10.289', '-10.289');
  210. t('319', '-319');
  211. t('2.4', '-2.4');
  212. t('89.9207', '-89.9207');
  213. t('-9.06122e-17', '0.0000000000000000906122');
  214. t('-102.639', '102.639');
  215. t('948.5', '-948.50');
  216. t('-610.7', '610.7');
  217. t('-1.61', '1.61');
  218. t('-99.042', '99.042');
  219. t('3.0232', '-3.0232');
  220. t('-15', '15');
  221. t('-3.835', '3.835');
  222. t('-7', '7');
  223. t('1', '-1');
  224. t('21.46', '-21.46');
  225. t('2', '-2');
  226. t('-2077.79', '2077.79');
  227. t('-14.7446', '14.7446');
  228. t('-9.11e-12', '0.00000000000911');
  229. t('1.2', '-1.2');
  230. t('-105851', '105851');
  231. t('24.561', '-24.561');
  232. t('780', '-780');
  233. t('3.82122', '-3.82122');
  234. t('9564', '-9564');
  235. t('-13.21', '13.21');
  236. t('25020.5', '-25020.5');
  237. t('-5678.6', '5678.6');
  238. t('1', '-1.0');
  239. t('2.6', '-2.6');
  240. t('9.6e-16', '-0.000000000000000960');
  241. t('12.6', '-12.6');
  242. t('-5', '5');
  243. t('-537', '537');
  244. t('-85', '85');
  245. t('758.15', '-758.15');
  246. t('-67.55', '67.55');
  247. t('-9444', '9444');
  248. t('21.4', '-21.4');
  249. t('2.5', '-2.5');
  250. t('489311', '-489311');
  251. t('6.8', '-6.8');
  252. t('4.29', '-4.29');
  253. t('23982', '-23982.0');
  254. t('-0.0111781', '0.0111781');
  255. t('4.96e-20', '-0.0000000000000000000496');
  256. t('-40.5481', '40.5481');
  257. t('-32.52', '32.52');
  258. t('-7.4', '7.4');
  259. t('1008', '-1008');
  260. t('1.2', '-1.2');
  261. t('-5', '5.0');
  262. t('-2463.4', '2463.4');
  263. t('7.363', '-7.363');
  264. t('2.8', '-2.8');
  265. t('-14498', '14498');
  266. t('201', '-201');
  267. t('3.2', '-3.2');
  268. t('-3.05', '3.05');
  269. t('1.1', '-1.1');
  270. t('-380.4', '380.4');
  271. t('13399', '-13399');
  272. t('-20.44', '20.44');
  273. t('1.6', '-1.6');
  274. t('2.1234e-10', '-0.00000000021234');
  275. t('4404.1', '-4404.1');
  276. t('2.4345', '-2.4345');
  277. t('-117.256', '117.256');
  278. t('-6.025', '6.025');
  279. t('18.43', '-18.43');
  280. t('-47.5', '47.5');
  281. t('45.1', '-45.1');
  282. t('-3806.5', '3806.5');
  283. t('-4.6', '4.6');
  284. t('-1.3', '1.3');
  285. t('-74.6', '74.60');
  286. t('-16.2088', '16.2088');
  287. t('788.6', '-788.6');
  288. t('-0.29', '0.29');
  289. t('1', '-1');
  290. t('-4.058', '4.058');
  291. t('5', '-5.0');
  292. t('0.00612', '-0.00612');
  293. t('-14317', '14317');
  294. t('-1.1801', '1.1801');
  295. t('-32.6', '32.6');
  296. t('57248', '-57248');
  297. t('-103', '103');
  298. t('-1.4', '1.4');
  299. t('228', '-228');
  300. t('92.8', '-92.8');
  301. t('3.46e-17', '-0.0000000000000000346');
  302. t('-15747', '15747');
  303. t('16.36', '-16.360');
  304. t('0.00223', '-0.00223');
  305. t('244', '-244');
  306. t('3.8', '-3.8');
  307. t('-604.2', '604.2');
  308. t('1.03', '-1.03');
  309. t('1487', '-1487');
  310. t('7', '-7');
  311. t('45', '-45.00');
  312. t('2.55374e-10', '-0.000000000255374');
  313. t('3', '-3');
  314. t('-5.5', '5.5');
  315. t('-5.4', '5.4');
  316. t('-9', '9');
  317. t('-1627.2', '1627.2');
  318. t('1.0805e-16', '-0.00000000000000010805');
  319. t('-14.0548', '14.0548');
  320. t('-207137', '207137');
  321. t('3.8', '-3.8');
  322. t('-33.4785', '33.4785');
  323. t('4.28626', '-4.28626');
  324. t('-4', '4');
  325. t('-6', '6');
  326. t('-1', '1');
  327. t('-44.951', '44.951');
  328. t('29.7', '-29.7');
  329. t('-121.17', '121.17');
  330. t('480', '-480');
  331. t('-2.696', '2.696');
  332. t('-3708.62', '3708.62');
  333. t('2.8', '-2.8');
  334. t('17842', '-17842');
  335. t('-3', '3');
  336. t('-2', '2');
  337. t('-1.855', '1.855');
  338. t('246866', '-246866');
  339. t('-0.0022', '0.0022');
  340. t('-1', '1');
  341. t('1283', '-1283');
  342. t('2.1', '-2.1');
  343. t('3.289e-12', '-0.000000000003289');
  344. t('-1656', '1656');
  345. t('3.9', '-3.9');
  346. t('1.12', '-1.12');
  347. t('3.54e-16', '-0.000000000000000354');
  348. t('-0.001123', '0.001123');
  349. t('2.06551e-14', '-0.0000000000000206551');
  350. t('-19319.3', '19319.3');
  351. t('3', '-3');
  352. t('-6', '6');
  353. t('5.747e-17', '-0.00000000000000005747');
  354. t('-1.756', '1.756');
  355. t('2.71004e-15', '-0.00000000000000271004');
  356. t('1.4', '-1.4');
  357. t('-0.0000019', '0.00000190');
  358. t('-6', '6');
  359. t('-31.4', '31.4');
  360. t('1', '-1');
  361. t('-39.954', '39.9540');
  362. t('8.4', '-8.40');
  363. t('5.3382e-17', '-0.0000000000000000533820');
  364. t('8.4', '-8.4');
  365. t('-106', '106');
  366. t('905', '-905');
  367. t('-2030.8', '2030.8');
  368. t('0.19358', '-0.193580');
  369. t('50057.4', '-50057.4');
  370. t('8.0731e-15', '-0.0000000000000080731');
  371. t('2.4', '-2.4');
  372. t('-1', '1');
  373. t('0.026038', '-0.026038');
  374. t('-22', '22');
  375. t('-2.8', '2.8');
  376. t('0.00110001', '-0.00110001');
  377. t('7', '-7');
  378. t('-705', '705');
  379. t('-36046', '36046');
  380. t('2.42', '-2.42');
  381. t('-1.225', '1.225');
  382. t('36.8', '-36.8');
  383. t('6.8926', '-6.8926');
  384. t('163575', '-163575');
  385. t('3.29e-16', '-0.000000000000000329');
  386. t('-3.9612e-20', '0.000000000000000000039612');
  387. t('6.3', '-6.3');
  388. t('1.1', '-1.1');
  389. t('-53', '53');
  390. t('-6.3', '6.3');
  391. t('-3.73', '3.73');
  392. t('5.99e-13', '-0.000000000000599');
  393. t('-0.0453', '0.0453');
  394. t('6.2', '-6.2');
  395. t('5', '-5');
  396. t('4.85599e-7', '-0.000000485599');
  397. t('-6.554e-19', '0.0000000000000000006554');
  398. t('245.2', '-245.20');
  399. t('-12.557', '12.557');
  400. t('8.7', '-8.7');
  401. t('-38.7', '38.7');
  402. t('1.1291', '-1.1291');
  403. t('-3', '3');
  404. t('40533.9', '-40533.9');
  405. t('135.1', '-135.1');
  406. t('-213', '213');
  407. t('-271352', '271352');
  408. t('-159.9', '159.9');
  409. t('-103632', '103632');
  410. t('-0.00000225418', '0.00000225418');
  411. t('-2.1e-16', '0.00000000000000021');
  412. t('14.5', '-14.5');
  413. t('48016', '-48016');
  414. t('282', '-282.0');
  415. t('9.3552e-18', '-0.0000000000000000093552');
  416. t('237', '-237');
  417. t('-21.1', '21.1');
  418. t('2.281', '-2.281');
  419. t('-4.68312', '4.68312');
  420. t('7', '-7');
  421. t('6', '-6');
  422. t('5.3', '-5.3');
  423. t('-681.586', '681.586');
  424. t('-1.59e-16', '0.0000000000000001590');
  425. t('-2.94', '2.94');
  426. t('-1', '1');
  427. t('7.03', '-7.03');
  428. t('5.73608e-13', '-0.000000000000573608');
  429. t('2', '-2');
  430. t('-1.26e-18', '0.00000000000000000126');
  431. t('-1.5e-14', '0.000000000000015');
  432. t('2', '-2');
  433. t('-44', '44');
  434. t('-1.3928', '1.3928');
  435. t('18811.4', '-18811.4');
  436. t('6.6', '-6.6');
  437. t('1.99', '-1.99');
  438. t('-6.6496e-14', '0.000000000000066496');
  439. t('27.184', '-27.184');
  440. t('0.00007614', '-0.00007614');
  441. t('5478', '-5478.0');
  442. t('-30.6432', '30.6432');
  443. t('-108', '108');
  444. t('-1', '1');
  445. t('-61', '61');
  446. t('4', '-4');
  447. t('-0.032192', '0.032192');
  448. t('2.6e-8', '-0.000000026');
  449. BigNumber.config({EXPONENTIAL_AT: 0});
  450. t('-5.0600621890668482322956892808849303e+20', '5.0600621890668482322956892808849303e+20');
  451. t('7e+0', '-7e+0');
  452. t('-6.1095374220609e+13', '6.1095374220609e+13');
  453. t('9.01e+2', '-9.01e+2');
  454. t('-1.016984074247269470395836690098169093010136836967e+39', '1.016984074247269470395836690098169093010136836967e+39');
  455. t('-1.497639134680472576e+18', '1.497639134680472576e+18');
  456. t('-4.1717657571404248e+16', '4.1717657571404248e+16');
  457. t('8.983272e+1', '-8.983272e+1');
  458. t('-5.308416e+6', '5.308416e+6');
  459. t('-2.09764e+3', '2.09764e+3');
  460. t('-3.83432050166120236679168e+23', '3.83432050166120236679168e+23');
  461. t('-4.096e+3', '4.096e+3');
  462. t('2.679971527468745095582058350756311201706813294321409e+51', '-2.679971527468745095582058350756311201706813294321409e+51');
  463. t('-5.067853299870089529116832768e+2', '5.067853299870089529116832768e+2');
  464. t('-3.48822062687911109850066182676769e+32', '3.48822062687911109850066182676769e+32');
  465. t('-1e+0', '1e+0');
  466. t('4.2773e+0', '-4.2773e+0');
  467. t('5.8169306081172252508071119604378757744768e+12', '-5.8169306081172252508071119604378757744768e+12');
  468. t('-1e+0', '1e+0');
  469. t('1.51655708279450944384385164853883404204414169862685507e+46', '-1.51655708279450944384385164853883404204414169862685507e+46');
  470. t('-8.1e+1', '8.1e+1');
  471. t('-1.296e+3', '1.296e+3');
  472. t('-2.9e+0', '2.9e+0');
  473. t('-1.764e+3', '1.764e+3');
  474. t('9.3418332730097368870513138581415704704611459349313e+49', '-9.3418332730097368870513138581415704704611459349313e+49');
  475. t('-Infinity', Infinity);
  476. t('-Infinity', 'Infinity');
  477. t('Infinity', -Infinity);
  478. t('Infinity', '-Infinity');
  479. t('NaN', NaN);
  480. t('NaN', 'NaN');
  481. BigNumber.config({EXPONENTIAL_AT: 1e+9});
  482. Test.areEqual(-1, new BigNumber(2).negated().s);
  483. Test.areEqual(1, new BigNumber(-2).negated().s);
  484. Test.areEqual(null, new BigNumber(NaN).negated().s);
  485. Test.areEqual(null, new BigNumber('-NaN').negated().s);
  486. Test.areEqual(-1, new BigNumber(Infinity).negated().s);
  487. Test.areEqual(1, new BigNumber('-Infinity').negated().s);
  488. Test.areEqual('-1', new BigNumber(1).negated().valueOf());
  489. Test.areEqual('-0', new BigNumber(0).negated().valueOf());
  490. Test.areEqual('-0', new BigNumber(0).negated().valueOf());
  491. Test.areEqual('-0', new BigNumber('0.00000').negated().valueOf());
  492. Test.areEqual('-0', new BigNumber('+0.0').negated().valueOf());
  493. Test.areEqual('0', new BigNumber(-0).negated().valueOf());
  494. Test.areEqual('0', new BigNumber('-0').negated().valueOf());
  495. });