fraction.test.js 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. const Fraction = require('fraction.js');
  2. const assert = require('assert');
  3. var DivisionByZero = function () { return new Error("Division by Zero"); };
  4. var InvalidParameter = function () { return new Error("Invalid argument"); };
  5. var NonIntegerParameter = function () { return new Error("Parameters must be integer"); };
  6. var tests = [{
  7. set: "",
  8. expectError: InvalidParameter()
  9. }, {
  10. set: "foo",
  11. expectError: InvalidParameter()
  12. }, {
  13. set: " 123",
  14. expectError: InvalidParameter()
  15. }, {
  16. set: 0,
  17. expect: 0
  18. }, {
  19. set: .2,
  20. expect: "0.2"
  21. }, {
  22. set: .333,
  23. expect: "0.333"
  24. }, {
  25. set: 1.1,
  26. expect: "1.1"
  27. }, {
  28. set: 1.2,
  29. expect: "1.2"
  30. }, {
  31. set: 1.3,
  32. expect: "1.3"
  33. }, {
  34. set: 1.4,
  35. expect: "1.4"
  36. }, {
  37. set: 1.5,
  38. expect: "1.5"
  39. }, {
  40. set: 2.555,
  41. expect: "2.555"
  42. }, {
  43. set: 1e12,
  44. expect: "1000000000000"
  45. }, {
  46. set: " - ",
  47. expectError: InvalidParameter()
  48. }, {
  49. set: ".5",
  50. expect: "0.5"
  51. }, {
  52. set: "2_000_000",
  53. expect: "2000000"
  54. }, {
  55. set: "-.5",
  56. expect: "-0.5"
  57. }, {
  58. set: "123",
  59. expect: "123"
  60. }, {
  61. set: "-123",
  62. expect: "-123"
  63. }, {
  64. set: "123.4",
  65. expect: "123.4"
  66. }, {
  67. set: "-123.4",
  68. expect: "-123.4"
  69. }, {
  70. set: "123.",
  71. expect: "123"
  72. }, {
  73. set: "-123.",
  74. expect: "-123"
  75. }, {
  76. set: "123.4(56)",
  77. expect: "123.4(56)"
  78. }, {
  79. set: "-123.4(56)",
  80. expect: "-123.4(56)"
  81. }, {
  82. set: "123.(4)",
  83. expect: "123.(4)"
  84. }, {
  85. set: "-123.(4)",
  86. expect: "-123.(4)"
  87. }, {
  88. set: "0/0",
  89. expectError: DivisionByZero()
  90. }, {
  91. set: "9/0",
  92. expectError: DivisionByZero()
  93. }, {
  94. label: "0/1+0/1",
  95. set: "0/1",
  96. param: "0/1",
  97. expect: "0"
  98. }, {
  99. label: "1/9+0/1",
  100. set: "1/9",
  101. param: "0/1",
  102. expect: "0.(1)"
  103. }, {
  104. set: "123/456",
  105. expect: "0.269(736842105263157894)"
  106. }, {
  107. set: "-123/456",
  108. expect: "-0.269(736842105263157894)"
  109. }, {
  110. set: "19 123/456",
  111. expect: "19.269(736842105263157894)"
  112. }, {
  113. set: "-19 123/456",
  114. expect: "-19.269(736842105263157894)"
  115. }, {
  116. set: "123.(22)123",
  117. expectError: InvalidParameter()
  118. }, {
  119. set: "+33.3(3)",
  120. expect: "33.(3)"
  121. }, {
  122. set: "3.'09009'",
  123. expect: "3.(09009)"
  124. }, {
  125. set: "123.(((",
  126. expectError: InvalidParameter()
  127. }, {
  128. set: "123.((",
  129. expectError: InvalidParameter()
  130. }, {
  131. set: "123.()",
  132. expectError: InvalidParameter()
  133. }, {
  134. set: null,
  135. expect: "0" // I would say it's just fine
  136. }, {
  137. set: [22, 7],
  138. expect: '3.(142857)' // We got Pi! - almost ;o
  139. }, {
  140. set: "355/113",
  141. expect: "3.(1415929203539823008849557522123893805309734513274336283185840707964601769911504424778761061946902654867256637168)" // Yay, a better PI
  142. }, {
  143. set: "3 1/7",
  144. expect: '3.(142857)'
  145. }, {
  146. set: [36, -36],
  147. expect: "-1"
  148. }, {
  149. set: [1n, 3n],
  150. expect: "0.(3)"
  151. }, {
  152. set: 1n,
  153. set2: 3n,
  154. expect: "0.(3)"
  155. }, {
  156. set: { n: 1n, d: 3n },
  157. expect: "0.(3)"
  158. }, {
  159. set: { n: 1n, d: 3n },
  160. expect: "0.(3)"
  161. }, {
  162. set: [1n, 3n],
  163. expect: "0.(3)"
  164. }, {
  165. set: "9/12",
  166. expect: "0.75"
  167. }, {
  168. set: "0.09(33)",
  169. expect: "0.09(3)"
  170. }, {
  171. set: 1 / 2,
  172. expect: "0.5"
  173. }, {
  174. set: 1 / 3,
  175. expect: "0.(3)"
  176. }, {
  177. set: "0.'3'",
  178. expect: "0.(3)"
  179. }, {
  180. set: "0.00002",
  181. expect: "0.00002"
  182. }, {
  183. set: 7 / 8,
  184. expect: "0.875"
  185. }, {
  186. set: 0.003,
  187. expect: "0.003"
  188. }, {
  189. set: 4,
  190. expect: "4"
  191. }, {
  192. set: -99,
  193. expect: "-99"
  194. }, {
  195. set: "-92332.1192",
  196. expect: "-92332.1192"
  197. }, {
  198. set: '88.92933(12111)',
  199. expect: "88.92933(12111)"
  200. }, {
  201. set: '-192322.823(123)',
  202. expect: "-192322.8(231)"
  203. }, {
  204. label: "-99.12 % 0.09(34)",
  205. set: '-99.12',
  206. fn: "mod",
  207. param: "0.09(34)",
  208. expect: "-0.07(95)"
  209. }, {
  210. label: "0.4 / 0.1",
  211. set: .4,
  212. fn: "div",
  213. param: ".1",
  214. expect: "4"
  215. }, {
  216. label: "1 / -.1",
  217. set: 1,
  218. fn: "div",
  219. param: "-.1",
  220. expect: "-10"
  221. }, {
  222. label: "1 - (-1)",
  223. set: 1,
  224. fn: "sub",
  225. param: "-1",
  226. expect: "2"
  227. }, {
  228. label: "1 + (-1)",
  229. set: 1,
  230. fn: "add",
  231. param: "-1",
  232. expect: "0"
  233. }, {
  234. label: "-187 % 12",
  235. set: '-187',
  236. fn: "mod",
  237. param: "12",
  238. expect: "-7"
  239. }, {
  240. label: "Negate by 99 * -1",
  241. set: '99',
  242. fn: "mul",
  243. param: "-1",
  244. expect: "-99"
  245. }, {
  246. label: "0.5050000000000000000000000",
  247. set: "0.5050000000000000000000000",
  248. expect: "101/200",
  249. fn: "toFraction",
  250. param: true
  251. }, {
  252. label: "0.505000000(0000000000)",
  253. set: "0.505000000(0000000000)",
  254. expect: "101/200",
  255. fn: "toFraction",
  256. param: true
  257. }, {
  258. set: [20, -5],
  259. expect: "-4",
  260. fn: "toFraction",
  261. param: true
  262. }, {
  263. set: [-10, -7],
  264. expect: "1 3/7",
  265. fn: "toFraction",
  266. param: true
  267. }, {
  268. set: [21, -6],
  269. expect: "-3 1/2",
  270. fn: "toFraction",
  271. param: true
  272. }, {
  273. set: "10/78",
  274. expect: "5/39",
  275. fn: "toFraction",
  276. param: true
  277. }, {
  278. set: "0/91",
  279. expect: "0",
  280. fn: "toFraction",
  281. param: true
  282. }, {
  283. set: "-0/287",
  284. expect: "0",
  285. fn: "toFraction",
  286. param: true
  287. }, {
  288. set: "-5/20",
  289. expect: "-1/4",
  290. fn: "toFraction",
  291. param: true
  292. }, {
  293. set: "42/9",
  294. expect: "4 2/3",
  295. fn: "toFraction",
  296. param: true
  297. }, {
  298. set: "71/23",
  299. expect: "3 2/23",
  300. fn: "toFraction",
  301. param: true
  302. }, {
  303. set: "6/3",
  304. expect: "2",
  305. fn: "toFraction",
  306. param: true
  307. }, {
  308. set: "28/4",
  309. expect: "7",
  310. fn: "toFraction",
  311. param: true
  312. }, {
  313. set: "105/35",
  314. expect: "3",
  315. fn: "toFraction",
  316. param: true
  317. }, {
  318. set: "4/6",
  319. expect: "2/3",
  320. fn: "toFraction",
  321. param: true
  322. }, {
  323. label: "99.(9) + 66",
  324. set: '99.(999999)',
  325. fn: "add",
  326. param: "66",
  327. expect: "166"
  328. }, {
  329. label: "123.32 / 33.'9821'",
  330. set: '123.32',
  331. fn: "div",
  332. param: "33.'9821'",
  333. expect: "3.628958880242975"
  334. }, {
  335. label: "-82.124 / 66.(3)",
  336. set: '-82.124',
  337. fn: "div",
  338. param: "66.(3)",
  339. expect: "-1.238(050251256281407035175879396984924623115577889447236180904522613065326633165829145728643216080402010)"
  340. }, {
  341. label: "100 - .91",
  342. set: '100',
  343. fn: "sub",
  344. param: ".91",
  345. expect: "99.09"
  346. }, {
  347. label: "381.(33411) % 11.119(356)",
  348. set: '381.(33411)',
  349. fn: "mod",
  350. param: "11.119(356)",
  351. expect: "3.275(997225017295217)"
  352. }, {
  353. label: "13/26 mod 1",
  354. set: '13/26',
  355. fn: "mod",
  356. param: "1.000",
  357. expect: "0.5"
  358. }, {
  359. label: "381.(33411) % 1", // Extract fraction part of a number
  360. set: '381.(33411)',
  361. fn: "mod",
  362. param: "1",
  363. expect: "0.(33411)"
  364. }, {
  365. label: "-222/3",
  366. set: {
  367. n: 3,
  368. d: 222,
  369. s: -1
  370. },
  371. fn: "inverse",
  372. param: null,
  373. expect: "-74"
  374. }, {
  375. label: "inverse",
  376. set: 1 / 2,
  377. fn: "inverse",
  378. param: null,
  379. expect: "2"
  380. }, {
  381. label: "abs(-222/3)",
  382. set: {
  383. n: -222,
  384. d: 3
  385. },
  386. fn: "abs",
  387. param: null,
  388. expect: "74"
  389. }, {
  390. label: "9 % -2",
  391. set: 9,
  392. fn: "mod",
  393. param: "-2",
  394. expect: "1"
  395. }, {
  396. label: "-9 % 2",
  397. set: '-9',
  398. fn: "mod",
  399. param: "-2",
  400. expect: "-1"
  401. }, {
  402. label: "1 / 195312500",
  403. set: '1',
  404. fn: "div",
  405. param: "195312500",
  406. expect: "0.00000000512"
  407. }, {
  408. label: "10 / 0",
  409. set: 10,
  410. fn: "div",
  411. param: 0,
  412. expectError: DivisionByZero()
  413. }, {
  414. label: "-3 / 4",
  415. set: [-3, 4],
  416. fn: "inverse",
  417. param: null,
  418. expect: "-1.(3)"
  419. }, {
  420. label: "-19.6",
  421. set: [-98, 5],
  422. fn: "equals",
  423. param: '-19.6',
  424. expect: "true" // actually, we get a real bool but we call toString() in the test below
  425. }, {
  426. label: "-19.6",
  427. set: [98, -5],
  428. fn: "equals",
  429. param: '-19.6',
  430. expect: "true"
  431. }, {
  432. label: "99/88",
  433. set: [99, 88],
  434. fn: "equals",
  435. param: [88, 99],
  436. expect: "false"
  437. }, {
  438. label: "99/88",
  439. set: [99, -88],
  440. fn: "equals",
  441. param: [9, 8],
  442. expect: "false"
  443. }, {
  444. label: "12.5",
  445. set: 12.5,
  446. fn: "add",
  447. param: 0,
  448. expect: "12.5"
  449. }, {
  450. label: "0/1 -> 1/0",
  451. set: 0,
  452. fn: "inverse",
  453. param: null,
  454. expectError: DivisionByZero()
  455. }, {
  456. label: "abs(-100.25)",
  457. set: -100.25,
  458. fn: "abs",
  459. param: null,
  460. expect: "100.25"
  461. }, {
  462. label: "0.022222222",
  463. set: '0.0(22222222)',
  464. fn: "abs",
  465. param: null,
  466. expect: "0.0(2)"
  467. }, {
  468. label: "1.5 | 100.5",
  469. set: 100.5,
  470. fn: "divisible",
  471. param: '1.5',
  472. expect: "true"
  473. }, {
  474. label: "1.5 | 100.6",
  475. set: 100.6,
  476. fn: "divisible",
  477. param: 1.6,
  478. expect: "false"
  479. }, {
  480. label: "(1/6) | (2/3)", // == 4
  481. set: [2, 3],
  482. fn: "divisible",
  483. param: [1, 6],
  484. expect: "true"
  485. }, {
  486. label: "(1/6) | (2/5)",
  487. set: [2, 5],
  488. fn: "divisible",
  489. param: [1, 6],
  490. expect: "false"
  491. }, {
  492. label: "0 | (2/5)",
  493. set: [2, 5],
  494. fn: "divisible",
  495. param: 0,
  496. expect: "false"
  497. }, {
  498. label: "6 | 0",
  499. set: 0,
  500. fn: "divisible",
  501. param: 6,
  502. expect: "true"
  503. }, {
  504. label: "fmod(4.55, 0.05)", // http://phpjs.org/functions/fmod/ (comment section)
  505. set: 4.55,
  506. fn: "mod",
  507. param: 0.05,
  508. expect: "0"
  509. }, {
  510. label: "fmod(99.12, 0.4)",
  511. set: 99.12,
  512. fn: "mod",
  513. param: "0.4",
  514. expect: "0.32"
  515. }, {
  516. label: "fmod(fmod(1.0,0.1))", // http://stackoverflow.com/questions/4218961/why-fmod1-0-0-1-1
  517. set: 1.0,
  518. fn: "mod",
  519. param: 0.1,
  520. expect: "0"
  521. }, {
  522. label: "bignum",
  523. set: [5385020324, 1673196525],
  524. fn: "add",
  525. param: 0,
  526. expect: "3.21(840276592733181776121606516006839065124164060763872313206005492988936251824931324190982287630557922656455433410609073551596098372245902196097377144624418820138297860736950789447760776337973807350574075570710380240599651018280712721418065340531352107607323652551812465663589637206543923464101146157950573080469432602963360804254598843372567965379918536467197121390148715495330113717514444395585868193217769203770011415724163065662594535928766646225254382476081224230369471990147720394052336440275597631903998844367669243157195775313960803259497565595290726533154854597848271290188102679689703515252041298615534717298077104242133182771222884293284077911887845930112722413166618308629346454087334421161315763550250022184333666363549254920906556389124702491239037207539024741878423396797336762338781453063321417070239253574830368476888869943116813489676593728283053898883754853602746993512910863832926021645903191198654921901657666901979730085800889408373591978384009612977172541043856160291750546158945674358246709841810124486123947693472528578195558946669459524487119048971249805817042322628538808374587079661786890216019304725725509141850506771761314768448972244907094819599867385572056456428511886850828834945135927771544947477105237234460548500123140047759781236696030073335228807028510891749551057667897081007863078128255137273847732859712937785356684266362554153643129279150277938809369688357439064129062782986595074359241811119587401724970711375341877428295519559485099934689381452068220139292962014728066686607540019843156200674036183526020650801913421377683054893985032630879985)"
  527. }, {
  528. label: "ceil(0.4)",
  529. set: 0.4,
  530. fn: "ceil",
  531. param: null,
  532. expect: "1"
  533. },
  534. {
  535. label: "1 < 2",
  536. set: 1,
  537. fn: "lt",
  538. param: 2,
  539. expect: "true"
  540. }, {
  541. label: "2 < 2",
  542. set: 2,
  543. fn: "lt",
  544. param: 2,
  545. expect: "false"
  546. }, {
  547. label: "3 > 2",
  548. set: 3,
  549. fn: "gt",
  550. param: 2,
  551. expect: "true"
  552. }, {
  553. label: "2 > 2",
  554. set: 2,
  555. fn: "gt",
  556. param: 2,
  557. expect: "false"
  558. }, {
  559. label: "1 <= 2",
  560. set: 1,
  561. fn: "lte",
  562. param: 2,
  563. expect: "true"
  564. }, {
  565. label: "2 <= 2",
  566. set: 2,
  567. fn: "lte",
  568. param: 2,
  569. expect: "true"
  570. }, {
  571. label: "3 <= 2",
  572. set: 3,
  573. fn: "lte",
  574. param: 2,
  575. expect: "false"
  576. }, {
  577. label: "3 >= 2",
  578. set: 3,
  579. fn: "gte",
  580. param: 2,
  581. expect: "true"
  582. }, {
  583. label: "2 >= 2",
  584. set: 2,
  585. fn: "gte",
  586. param: 2,
  587. expect: "true"
  588. }, {
  589. label: "ceil(0.5)",
  590. set: 0.5,
  591. fn: "ceil",
  592. param: null,
  593. expect: "1"
  594. }, {
  595. label: "ceil(0.23, 2)",
  596. set: 0.23,
  597. fn: "ceil",
  598. param: 2,
  599. expect: "0.23"
  600. }, {
  601. label: "ceil(0.6)",
  602. set: 0.6,
  603. fn: "ceil",
  604. param: null,
  605. expect: "1"
  606. }, {
  607. label: "ceil(-0.4)",
  608. set: -0.4,
  609. fn: "ceil",
  610. param: null,
  611. expect: "0"
  612. }, {
  613. label: "ceil(-0.5)",
  614. set: -0.5,
  615. fn: "ceil",
  616. param: null,
  617. expect: "0"
  618. }, {
  619. label: "ceil(-0.6)",
  620. set: -0.6,
  621. fn: "ceil",
  622. param: null,
  623. expect: "0"
  624. }, {
  625. label: "floor(0.4)",
  626. set: 0.4,
  627. fn: "floor",
  628. param: null,
  629. expect: "0"
  630. }, {
  631. label: "floor(0.4, 1)",
  632. set: 0.4,
  633. fn: "floor",
  634. param: 1,
  635. expect: "0.4"
  636. }, {
  637. label: "floor(0.5)",
  638. set: 0.5,
  639. fn: "floor",
  640. param: null,
  641. expect: "0"
  642. }, {
  643. label: "floor(0.6)",
  644. set: 0.6,
  645. fn: "floor",
  646. param: null,
  647. expect: "0"
  648. }, {
  649. label: "floor(-0.4)",
  650. set: -0.4,
  651. fn: "floor",
  652. param: null,
  653. expect: "-1"
  654. }, {
  655. label: "floor(-0.5)",
  656. set: -0.5,
  657. fn: "floor",
  658. param: null,
  659. expect: "-1"
  660. }, {
  661. label: "floor(-0.6)",
  662. set: -0.6,
  663. fn: "floor",
  664. param: null,
  665. expect: "-1"
  666. }, {
  667. label: "floor(10.4)",
  668. set: 10.4,
  669. fn: "floor",
  670. param: null,
  671. expect: "10"
  672. }, {
  673. label: "floor(10.4, 1)",
  674. set: 10.4,
  675. fn: "floor",
  676. param: 1,
  677. expect: "10.4"
  678. }, {
  679. label: "floor(10.5)",
  680. set: 10.5,
  681. fn: "floor",
  682. param: null,
  683. expect: "10"
  684. }, {
  685. label: "floor(10.6)",
  686. set: 10.6,
  687. fn: "floor",
  688. param: null,
  689. expect: "10"
  690. }, {
  691. label: "floor(-10.4)",
  692. set: -10.4,
  693. fn: "floor",
  694. param: null,
  695. expect: "-11"
  696. }, {
  697. label: "floor(-10.5)",
  698. set: -10.5,
  699. fn: "floor",
  700. param: null,
  701. expect: "-11"
  702. }, {
  703. label: "floor(-10.6)",
  704. set: -10.6,
  705. fn: "floor",
  706. param: null,
  707. expect: "-11"
  708. }, {
  709. label: "floor(-10.543,3)",
  710. set: -10.543,
  711. fn: "floor",
  712. param: 3,
  713. expect: "-10.543"
  714. }, {
  715. label: "floor(10.543,3)",
  716. set: 10.543,
  717. fn: "floor",
  718. param: 3,
  719. expect: "10.543"
  720. }, {
  721. label: "round(-10.543,3)",
  722. set: -10.543,
  723. fn: "round",
  724. param: 3,
  725. expect: "-10.543"
  726. }, {
  727. label: "round(10.543,3)",
  728. set: 10.543,
  729. fn: "round",
  730. param: 3,
  731. expect: "10.543"
  732. }, {
  733. label: "round(10.4)",
  734. set: 10.4,
  735. fn: "round",
  736. param: null,
  737. expect: "10"
  738. }, {
  739. label: "round(10.5)",
  740. set: 10.5,
  741. fn: "round",
  742. param: null,
  743. expect: "11"
  744. }, {
  745. label: "round(10.5, 1)",
  746. set: 10.5,
  747. fn: "round",
  748. param: 1,
  749. expect: "10.5"
  750. }, {
  751. label: "round(10.6)",
  752. set: 10.6,
  753. fn: "round",
  754. param: null,
  755. expect: "11"
  756. }, {
  757. label: "round(-10.4)",
  758. set: -10.4,
  759. fn: "round",
  760. param: null,
  761. expect: "-10"
  762. }, {
  763. label: "round(-10.5)",
  764. set: -10.5,
  765. fn: "round",
  766. param: null,
  767. expect: "-10"
  768. }, {
  769. label: "round(-10.6)",
  770. set: -10.6,
  771. fn: "round",
  772. param: null,
  773. expect: "-11"
  774. }, {
  775. label: "round(-0.4)",
  776. set: -0.4,
  777. fn: "round",
  778. param: null,
  779. expect: "0"
  780. }, {
  781. label: "round(-0.5)",
  782. set: -0.5,
  783. fn: "round",
  784. param: null,
  785. expect: "0"
  786. }, {
  787. label: "round(-0.6)",
  788. set: -0.6,
  789. fn: "round",
  790. param: null,
  791. expect: "-1"
  792. }, {
  793. label: "round(-0)",
  794. set: -0,
  795. fn: "round",
  796. param: null,
  797. expect: "0"
  798. }, {
  799. label: "round(big fraction)",
  800. set: [
  801. '409652136432929109317120'.repeat(100),
  802. '63723676445298091081155'.repeat(100)
  803. ],
  804. fn: "round",
  805. param: null,
  806. expect: "6428570341270001560623330590225448467479093479780591305451264291405695842465355472558570608574213642"
  807. }, {
  808. label: "round(big numerator)",
  809. set: ['409652136432929109317'.repeat(100), 10],
  810. fn: "round",
  811. param: null,
  812. expect: '409652136432929109317'.repeat(99) + '40965213643292910932'
  813. }, {
  814. label: "17402216385200408/5539306332998545",
  815. set: [17402216385200408, 5539306332998545],
  816. fn: "add",
  817. param: 0,
  818. expect: "3.141587653589870"
  819. }, {
  820. label: "17402216385200401/553930633299855",
  821. set: [17402216385200401, 553930633299855],
  822. fn: "add",
  823. param: 0,
  824. expect: "31.415876535898660"
  825. }, {
  826. label: "1283191/418183",
  827. set: [1283191, 418183],
  828. fn: "add",
  829. param: 0,
  830. expect: "3.068491545567371"
  831. }, {
  832. label: "1.001",
  833. set: "1.001",
  834. fn: "add",
  835. param: 0,
  836. expect: "1.001"
  837. }, {
  838. label: "99+1",
  839. set: [99, 1],
  840. fn: "add",
  841. param: 1,
  842. expect: "100"
  843. }, {
  844. label: "gcd(5/8, 3/7)",
  845. set: [5, 8],
  846. fn: "gcd",
  847. param: [3, 7],
  848. expect: "0.017(857142)" // == 1/56
  849. }, {
  850. label: "gcd(52, 39)",
  851. set: 52,
  852. fn: "gcd",
  853. param: 39,
  854. expect: "13"
  855. }, {
  856. label: "gcd(51357, 3819)",
  857. set: 51357,
  858. fn: "gcd",
  859. param: 3819,
  860. expect: "57"
  861. }, {
  862. label: "gcd(841, 299)",
  863. set: 841,
  864. fn: "gcd",
  865. param: 299,
  866. expect: "1"
  867. }, {
  868. label: "gcd(2/3, 7/5)",
  869. set: [2, 3],
  870. fn: "gcd",
  871. param: [7, 5],
  872. expect: "0.0(6)" // == 1/15
  873. }, {
  874. label: "lcm(-3, 3)",
  875. set: -3,
  876. fn: "lcm",
  877. param: 3,
  878. expect: "3"
  879. }, {
  880. label: "lcm(3,-3)",
  881. set: 3,
  882. fn: "lcm",
  883. param: -3,
  884. expect: "3"
  885. }, {
  886. label: "lcm(0,3)",
  887. set: 0,
  888. fn: "lcm",
  889. param: 3,
  890. expect: "0"
  891. }, {
  892. label: "lcm(3, 0)",
  893. set: 3,
  894. fn: "lcm",
  895. param: 0,
  896. expect: "0"
  897. }, {
  898. label: "lcm(0, 0)",
  899. set: 0,
  900. fn: "lcm",
  901. param: 0,
  902. expect: "0"
  903. }, {
  904. label: "lcm(200, 333)",
  905. set: 200,
  906. fn: "lcm",
  907. param: 333, expect: "66600"
  908. },
  909. {
  910. label: "1 + -1",
  911. set: 1,
  912. fn: "add",
  913. param: -1,
  914. expect: "0"
  915. }, {
  916. label: "3/10+3/14",
  917. set: "3/10",
  918. fn: "add",
  919. param: "3/14",
  920. expect: "0.5(142857)"
  921. }, {
  922. label: "3/10-3/14",
  923. set: "3/10",
  924. fn: "sub",
  925. param: "3/14",
  926. expect: "0.0(857142)"
  927. }, {
  928. label: "3/10*3/14",
  929. set: "3/10",
  930. fn: "mul",
  931. param: "3/14",
  932. expect: "0.06(428571)"
  933. }, {
  934. label: "3/10 / 3/14",
  935. set: "3/10",
  936. fn: "div",
  937. param: "3/14",
  938. expect: "1.4"
  939. }, {
  940. label: "1-2",
  941. set: "1",
  942. fn: "sub",
  943. param: "2",
  944. expect: "-1"
  945. }, {
  946. label: "1--1",
  947. set: "1",
  948. fn: "sub",
  949. param: "-1",
  950. expect: "2"
  951. }, {
  952. label: "0/1*1/3",
  953. set: "0/1",
  954. fn: "mul",
  955. param: "1/3",
  956. expect: "0"
  957. }, {
  958. label: "3/10 * 8/12",
  959. set: "3/10",
  960. fn: "mul",
  961. param: "8/12",
  962. expect: "0.2"
  963. }, {
  964. label: ".5+5",
  965. set: ".5",
  966. fn: "add",
  967. param: 5, expect: "5.5"
  968. },
  969. {
  970. label: "10/12-5/60",
  971. set: "10/12",
  972. fn: "sub",
  973. param: "5/60",
  974. expect: "0.75"
  975. }, {
  976. label: "10/15 / 3/4",
  977. set: "10/15",
  978. fn: "div",
  979. param: "3/4",
  980. expect: "0.(8)"
  981. }, {
  982. label: "1/4 + 3/8",
  983. set: "1/4",
  984. fn: "add",
  985. param: "3/8",
  986. expect: "0.625"
  987. }, {
  988. label: "2-1/3",
  989. set: "2",
  990. fn: "sub",
  991. param: "1/3",
  992. expect: "1.(6)"
  993. }, {
  994. label: "5*6",
  995. set: "5",
  996. fn: "mul",
  997. param: 6,
  998. expect: "30"
  999. }, {
  1000. label: "1/2-1/5",
  1001. set: "1/2",
  1002. fn: "sub",
  1003. param: "1/5",
  1004. expect: "0.3"
  1005. }, {
  1006. label: "1/2-5",
  1007. set: "1/2",
  1008. fn: "add",
  1009. param: -5,
  1010. expect: "-4.5"
  1011. }, {
  1012. label: "1*-1",
  1013. set: "1",
  1014. fn: "mul",
  1015. param: -1,
  1016. expect: "-1"
  1017. }, {
  1018. label: "5/10",
  1019. set: 5.0,
  1020. fn: "div",
  1021. param: 10,
  1022. expect: "0.5"
  1023. }, {
  1024. label: "1/-1",
  1025. set: "1",
  1026. fn: "div",
  1027. param: -1,
  1028. expect: "-1"
  1029. }, {
  1030. label: "4/5 + 13/2",
  1031. set: "4/5",
  1032. fn: "add",
  1033. param: "13/2",
  1034. expect: "7.3"
  1035. }, {
  1036. label: "4/5 + 61/2",
  1037. set: "4/5",
  1038. fn: "add",
  1039. param: "61/2",
  1040. expect: "31.3"
  1041. }, {
  1042. label: "0.8 + 6.5",
  1043. set: "0.8",
  1044. fn: "add",
  1045. param: "6.5",
  1046. expect: "7.3"
  1047. }, {
  1048. label: "2/7 inverse",
  1049. set: "2/7",
  1050. fn: "inverse",
  1051. param: null,
  1052. expect: "3.5"
  1053. }, {
  1054. label: "neg 1/3",
  1055. set: "1/3",
  1056. fn: "neg",
  1057. param: null,
  1058. expect: "-0.(3)"
  1059. }, {
  1060. label: "1/2+1/3",
  1061. set: "1/2",
  1062. fn: "add",
  1063. param: "1/3",
  1064. expect: "0.8(3)"
  1065. }, {
  1066. label: "1/2+3",
  1067. set: ".5",
  1068. fn: "add",
  1069. param: 3,
  1070. expect: "3.5"
  1071. }, {
  1072. label: "1/2+3.14",
  1073. set: "1/2",
  1074. fn: "add",
  1075. param: "3.14",
  1076. expect: "3.64"
  1077. }, {
  1078. label: "3.5 < 4.1",
  1079. set: 3.5,
  1080. fn: "compare",
  1081. param: 4.1,
  1082. expect: "-1"
  1083. }, {
  1084. label: "3.5 > 4.1",
  1085. set: 4.1,
  1086. fn: "compare",
  1087. param: 3.1,
  1088. expect: "1"
  1089. }, {
  1090. label: "-3.5 > -4.1",
  1091. set: -3.5,
  1092. fn: "compare",
  1093. param: -4.1,
  1094. expect: "1"
  1095. }, {
  1096. label: "-3.5 > -4.1",
  1097. set: -4.1,
  1098. fn: "compare",
  1099. param: -3.5,
  1100. expect: "-1"
  1101. }, {
  1102. label: "4.3 == 4.3",
  1103. set: 4.3,
  1104. fn: "compare",
  1105. param: 4.3,
  1106. expect: "0"
  1107. }, {
  1108. label: "-4.3 == -4.3",
  1109. set: -4.3,
  1110. fn: "compare",
  1111. param: -4.3,
  1112. expect: "0"
  1113. }, {
  1114. label: "-4.3 < 4.3",
  1115. set: -4.3,
  1116. fn: "compare",
  1117. param: 4.3,
  1118. expect: "-1"
  1119. }, {
  1120. label: "4.3 == -4.3",
  1121. set: 4.3,
  1122. fn: "compare",
  1123. param: -4.3,
  1124. expect: "1"
  1125. }, {
  1126. label: "2^0.5",
  1127. set: 2,
  1128. fn: "pow",
  1129. param: 0.5,
  1130. expect: "null"
  1131. }, {
  1132. label: "(-8/27)^(1/3)",
  1133. set: [-8, 27],
  1134. fn: "pow",
  1135. param: [1, 3],
  1136. expect: "null"
  1137. }, {
  1138. label: "(-8/27)^(2/3)",
  1139. set: [-8, 27],
  1140. fn: "pow",
  1141. param: [2, 3],
  1142. expect: "null"
  1143. }, {
  1144. label: "(-32/243)^(5/3)",
  1145. set: [-32, 243],
  1146. fn: "pow",
  1147. param: [5, 3],
  1148. expect: "null"
  1149. }, {
  1150. label: "sqrt(0)",
  1151. set: 0,
  1152. fn: "pow",
  1153. param: 0.5,
  1154. expect: "0"
  1155. }, {
  1156. label: "27^(2/3)",
  1157. set: 27,
  1158. fn: "pow",
  1159. param: "2/3",
  1160. expect: "9"
  1161. }, {
  1162. label: "(243/1024)^(2/5)",
  1163. set: "243/1024",
  1164. fn: "pow",
  1165. param: "2/5",
  1166. expect: "0.5625"
  1167. }, {
  1168. label: "-0.5^-3",
  1169. set: -0.5,
  1170. fn: "pow",
  1171. param: -3,
  1172. expect: "-8"
  1173. }, {
  1174. label: "",
  1175. set: -3,
  1176. fn: "pow",
  1177. param: -3,
  1178. expect: "-0.(037)"
  1179. }, {
  1180. label: "-3",
  1181. set: -3,
  1182. fn: "pow",
  1183. param: 2,
  1184. expect: "9"
  1185. }, {
  1186. label: "-3",
  1187. set: -3,
  1188. fn: "pow",
  1189. param: 3,
  1190. expect: "-27"
  1191. }, {
  1192. label: "0^0",
  1193. set: 0,
  1194. fn: "pow",
  1195. param: 0,
  1196. expect: "1"
  1197. }, {
  1198. label: "2/3^7",
  1199. set: [2, 3],
  1200. fn: "pow",
  1201. param: 7,
  1202. expect: "0.(058527663465935070873342478280749885688157293095564700502972107910379515317786922725194330132601737540009144947416552354823959762231367169638774577046181984453589391860996799268404206675811614083219021490626428898033836305441243712848651120256)"
  1203. }, {
  1204. label: "-0.6^4",
  1205. set: -0.6,
  1206. fn: "pow",
  1207. param: 4,
  1208. expect: "0.1296"
  1209. }, {
  1210. label: "8128371:12394 - 8128371/12394",
  1211. set: "8128371:12394",
  1212. fn: "sub",
  1213. param: "8128371/12394",
  1214. expect: "0"
  1215. }, {
  1216. label: "3/4 + 1/4",
  1217. set: "3/4",
  1218. fn: "add",
  1219. param: "1/4",
  1220. expect: "1"
  1221. }, {
  1222. label: "1/10 + 2/10",
  1223. set: "1/10",
  1224. fn: "add",
  1225. param: "2/10",
  1226. expect: "0.3"
  1227. }, {
  1228. label: "5/10 + 2/10",
  1229. set: "5/10",
  1230. fn: "add",
  1231. param: "2/10",
  1232. expect: "0.7"
  1233. }, {
  1234. label: "18/10 + 2/10",
  1235. set: "18/10",
  1236. fn: "add",
  1237. param: "2/10",
  1238. expect: "2"
  1239. }, {
  1240. label: "1/3 + 1/6",
  1241. set: "1/3",
  1242. fn: "add",
  1243. param: "1/6",
  1244. expect: "0.5"
  1245. }, {
  1246. label: "1/3 + 2/6",
  1247. set: "1/3",
  1248. fn: "add",
  1249. param: "2/6",
  1250. expect: "0.(6)"
  1251. }, {
  1252. label: "3/4 / 1/4",
  1253. set: "3/4",
  1254. fn: "div",
  1255. param: "1/4",
  1256. expect: "3"
  1257. }, {
  1258. label: "1/10 / 2/10",
  1259. set: "1/10",
  1260. fn: "div",
  1261. param: "2/10",
  1262. expect: "0.5"
  1263. }, {
  1264. label: "5/10 / 2/10",
  1265. set: "5/10",
  1266. fn: "div",
  1267. param: "2/10",
  1268. expect: "2.5"
  1269. }, {
  1270. label: "18/10 / 2/10",
  1271. set: "18/10",
  1272. fn: "div",
  1273. param: "2/10",
  1274. expect: "9"
  1275. }, {
  1276. label: "1/3 / 1/6",
  1277. set: "1/3",
  1278. fn: "div",
  1279. param: "1/6",
  1280. expect: "2"
  1281. }, {
  1282. label: "1/3 / 2/6",
  1283. set: "1/3",
  1284. fn: "div",
  1285. param: "2/6",
  1286. expect: "1"
  1287. }, {
  1288. label: "3/4 * 1/4",
  1289. set: "3/4",
  1290. fn: "mul",
  1291. param: "1/4",
  1292. expect: "0.1875"
  1293. }, {
  1294. label: "1/10 * 2/10",
  1295. set: "1/10",
  1296. fn: "mul",
  1297. param: "2/10",
  1298. expect: "0.02"
  1299. }, {
  1300. label: "5/10 * 2/10",
  1301. set: "5/10",
  1302. fn: "mul",
  1303. param: "2/10",
  1304. expect: "0.1"
  1305. }, {
  1306. label: "18/10 * 2/10",
  1307. set: "18/10",
  1308. fn: "mul",
  1309. param: "2/10",
  1310. expect: "0.36"
  1311. }, {
  1312. label: "1/3 * 1/6",
  1313. set: "1/3",
  1314. fn: "mul",
  1315. param: "1/6",
  1316. expect: "0.0(5)"
  1317. }, {
  1318. label: "1/3 * 2/6",
  1319. set: "1/3",
  1320. fn: "mul",
  1321. param: "2/6",
  1322. expect: "0.(1)"
  1323. }, {
  1324. label: "5/4 - 1/4",
  1325. set: "5/4",
  1326. fn: "sub",
  1327. param: "1/4",
  1328. expect: "1"
  1329. }, {
  1330. label: "5/10 - 2/10",
  1331. set: "5/10",
  1332. fn: "sub",
  1333. param: "2/10",
  1334. expect: "0.3"
  1335. }, {
  1336. label: "9/10 - 2/10",
  1337. set: "9/10",
  1338. fn: "sub",
  1339. param: "2/10",
  1340. expect: "0.7"
  1341. }, {
  1342. label: "22/10 - 2/10",
  1343. set: "22/10",
  1344. fn: "sub",
  1345. param: "2/10",
  1346. expect: "2"
  1347. }, {
  1348. label: "2/3 - 1/6",
  1349. set: "2/3",
  1350. fn: "sub",
  1351. param: "1/6",
  1352. expect: "0.5"
  1353. }, {
  1354. label: "3/3 - 2/6",
  1355. set: "3/3",
  1356. fn: "sub",
  1357. param: "2/6",
  1358. expect: "0.(6)"
  1359. }, {
  1360. label: "0.006999999999999999",
  1361. set: 0.006999999999999999,
  1362. fn: "add",
  1363. param: 0,
  1364. expect: "0.007"
  1365. }, {
  1366. label: "1/7 - 1",
  1367. set: 1 / 7,
  1368. fn: "add",
  1369. param: -1,
  1370. expect: "-0.(857142)"
  1371. }, {
  1372. label: "0.0065 + 0.0005",
  1373. set: 0.0065,
  1374. fn: "add",
  1375. param: 0.0005,
  1376. expect: "0.007"
  1377. }, {
  1378. label: "6.5/.5",
  1379. set: 6.5,
  1380. fn: "div",
  1381. param: .5,
  1382. expect: "13"
  1383. }, {
  1384. label: "0.999999999999999999999999999",
  1385. set: 0.999999999999999999999999999,
  1386. fn: "sub",
  1387. param: 1,
  1388. expect: "0"
  1389. }, {
  1390. label: "0.5833333333333334",
  1391. set: 0.5833333333333334,
  1392. fn: "add",
  1393. param: 0,
  1394. expect: "0.58(3)"
  1395. }, {
  1396. label: "1.75/3",
  1397. set: 1.75 / 3,
  1398. fn: "add",
  1399. param: 0,
  1400. expect: "0.58(3)"
  1401. }, {
  1402. label: "3.3333333333333",
  1403. set: 3.3333333333333,
  1404. fn: "add",
  1405. param: 0,
  1406. expect: "3.(3)"
  1407. }, {
  1408. label: "4.285714285714285714285714",
  1409. set: 4.285714285714285714285714,
  1410. fn: "add",
  1411. param: 0,
  1412. expect: "4.(285714)"
  1413. }, {
  1414. label: "-4",
  1415. set: -4,
  1416. fn: "neg",
  1417. param: 0,
  1418. expect: "4"
  1419. }, {
  1420. label: "4",
  1421. set: 4,
  1422. fn: "neg",
  1423. param: 0,
  1424. expect: "-4"
  1425. }, {
  1426. label: "0",
  1427. set: 0,
  1428. fn: "neg",
  1429. param: 0,
  1430. expect: "0"
  1431. }, {
  1432. label: "6869570742453802/5329686054127205",
  1433. set: "6869570742453802/5329686054127205",
  1434. fn: "neg",
  1435. param: 0,
  1436. expect: "-1.288925965373540"
  1437. }, {
  1438. label: "686970702/53212205",
  1439. set: "686970702/53212205",
  1440. fn: "neg",
  1441. param: 0,
  1442. expect: "-12.910021338149772"
  1443. }, {
  1444. label: "1/3000000000000000",
  1445. set: "1/3000000000000000",
  1446. fn: "add",
  1447. param: 0,
  1448. expect: "0.000000000000000(3)"
  1449. }, {
  1450. label: "toString(15) .0000000000000003",
  1451. set: ".0000000000000003",
  1452. fn: "toString",
  1453. param: 15,
  1454. expect: "0.000000000000000"
  1455. }, {
  1456. label: "toString(16) .0000000000000003",
  1457. set: ".0000000000000003",
  1458. fn: "toString",
  1459. param: 16,
  1460. expect: "0.0000000000000003"
  1461. }, {
  1462. label: "12 / 4.3",
  1463. set: 12,
  1464. set2: 4.3,
  1465. fn: "toString",
  1466. param: null,
  1467. expectError: NonIntegerParameter()
  1468. }, {
  1469. label: "12.5 / 4",
  1470. set: 12.5,
  1471. set2: 4,
  1472. fn: "toString",
  1473. param: null,
  1474. expectError: NonIntegerParameter()
  1475. }, {
  1476. label: "0.9 round to multiple of 1/8",
  1477. set: .9,
  1478. fn: "roundTo",
  1479. param: "1/8",
  1480. expect: "0.875"
  1481. }, {
  1482. label: "1/3 round to multiple of 1/16",
  1483. set: 1 / 3,
  1484. fn: "roundTo",
  1485. param: "1/16",
  1486. expect: "0.3125"
  1487. }, {
  1488. label: "1/3 round to multiple of 1/16",
  1489. set: -1 / 3,
  1490. fn: "roundTo",
  1491. param: "1/16",
  1492. expect: "-0.3125"
  1493. }, {
  1494. label: "1/2 round to multiple of 1/4",
  1495. set: 1 / 2,
  1496. fn: "roundTo",
  1497. param: "1/4",
  1498. expect: "0.5"
  1499. }, {
  1500. label: "1/4 round to multiple of 1/2",
  1501. set: 1 / 4,
  1502. fn: "roundTo",
  1503. param: "1/2",
  1504. expect: "0.5"
  1505. }, {
  1506. label: "10/3 round to multiple of 1/2",
  1507. set: "10/3",
  1508. fn: "roundTo",
  1509. param: "1/2",
  1510. expect: "3.5"
  1511. }, {
  1512. label: "-10/3 round to multiple of 1/2",
  1513. set: "-10/3",
  1514. fn: "roundTo",
  1515. param: "1/2",
  1516. expect: "-3.5"
  1517. }, {
  1518. label: "log_2(8)",
  1519. set: "8",
  1520. fn: "log",
  1521. param: "2",
  1522. expect: "3" // because 2^3 = 8
  1523. }, {
  1524. label: "log_2(3)",
  1525. set: "3",
  1526. fn: "log",
  1527. param: "2",
  1528. expect: 'null' // because 2^(p/q) != 3
  1529. }, {
  1530. label: "log_10(1000)",
  1531. set: "1000",
  1532. fn: "log",
  1533. param: "10",
  1534. expect: "3" // because 10^3 = 1000
  1535. }, {
  1536. label: "log_27(81)",
  1537. set: "81",
  1538. fn: "log",
  1539. param: "27",
  1540. expect: "1.(3)" // because 27^(4/3) = 81
  1541. }, {
  1542. label: "log_27(9)",
  1543. set: "9",
  1544. fn: "log",
  1545. param: "27",
  1546. expect: "0.(6)" // because 27^(2/3) = 9
  1547. }, {
  1548. label: "log_9/4(27/8)",
  1549. set: "27/8",
  1550. fn: "log",
  1551. param: "9/4",
  1552. expect: "1.5" // because (9/4)^(3/2) = 27/8
  1553. }];
  1554. describe('Fraction', function () {
  1555. for (var i = 0; i < tests.length; i++) {
  1556. (function (i) {
  1557. var action;
  1558. if (tests[i].fn) {
  1559. action = function () {
  1560. var x = Fraction(tests[i].set, tests[i].set2)[tests[i].fn](tests[i].param);
  1561. if (x === null) return "null";
  1562. return x.toString();
  1563. };
  1564. } else {
  1565. action = function () {
  1566. var x = new Fraction(tests[i].set, tests[i].set2);
  1567. if (x === null) return "null";
  1568. return x.toString();
  1569. };
  1570. }
  1571. it(String(tests[i].label || tests[i].set), function () {
  1572. if (tests[i].expectError) {
  1573. assert.throws(action, tests[i].expectError);
  1574. } else {
  1575. assert.equal(action(), tests[i].expect);
  1576. }
  1577. });
  1578. })(i);
  1579. }
  1580. });
  1581. describe('JSON', function () {
  1582. it("Should be possible to stringify the object", function () {
  1583. if (typeof Fraction(1).n !== 'number') {
  1584. return;
  1585. }
  1586. assert.equal('{"s":1,"n":14623,"d":330}', JSON.stringify(new Fraction("44.3(12)")));
  1587. assert.equal('{"s":-1,"n":2,"d":1}', JSON.stringify(new Fraction(-1 / 2).inverse()));
  1588. });
  1589. });
  1590. describe('Arguments', function () {
  1591. it("Should be possible to use different kind of params", function () {
  1592. // String
  1593. var fraction = new Fraction("0.1");
  1594. assert.equal("1/10", fraction.n + "/" + fraction.d);
  1595. var fraction = new Fraction("6234/6460");
  1596. assert.equal("3117/3230", fraction.n + "/" + fraction.d);
  1597. // Two params
  1598. var fraction = new Fraction(1, 2);
  1599. assert.equal("1/2", fraction.n + "/" + fraction.d);
  1600. // Object
  1601. var fraction = new Fraction({ n: 1, d: 3 });
  1602. assert.equal("1/3", fraction.n + "/" + fraction.d);
  1603. // Array
  1604. var fraction = new Fraction([1, 4]);
  1605. assert.equal("1/4", fraction.n + "/" + fraction.d);
  1606. });
  1607. });
  1608. describe('fractions', function () {
  1609. it("Should pass 0.08 = 2/25", function () {
  1610. var fraction = new Fraction("0.08");
  1611. assert.equal("2/25", fraction.n + "/" + fraction.d);
  1612. });
  1613. it("Should pass 0.200 = 1/5", function () {
  1614. var fraction = new Fraction("0.200");
  1615. assert.equal("1/5", fraction.n + "/" + fraction.d);
  1616. });
  1617. it("Should pass 0.125 = 1/8", function () {
  1618. var fraction = new Fraction("0.125");
  1619. assert.equal("1/8", fraction.n + "/" + fraction.d);
  1620. });
  1621. it("Should pass 8.36 = 209/25", function () {
  1622. var fraction = new Fraction(8.36);
  1623. assert.equal("209/25", fraction.n + "/" + fraction.d);
  1624. });
  1625. });
  1626. describe('constructors', function () {
  1627. it("Should pass 0.08 = 2/25", function () {
  1628. var tmp = new Fraction({ d: 4, n: 2, s: -1 });
  1629. assert.equal("-1/2", tmp.s * tmp.n + "/" + tmp.d);
  1630. var tmp = new Fraction(-88.3);
  1631. assert.equal("-883/10", tmp.s * tmp.n + "/" + tmp.d);
  1632. var tmp = new Fraction(-88.3).clone();
  1633. assert.equal("-883/10", tmp.s * tmp.n + "/" + tmp.d);
  1634. var tmp = new Fraction("123.'3'");
  1635. assert.equal("370/3", tmp.s * tmp.n + "/" + tmp.d);
  1636. var tmp = new Fraction("123.'3'").clone();
  1637. assert.equal("370/3", tmp.s * tmp.n + "/" + tmp.d);
  1638. var tmp = new Fraction([-1023461776, 334639305]);
  1639. tmp = tmp.add([4, 25]);
  1640. assert.equal("-4849597436/1673196525", tmp.s * tmp.n + "/" + tmp.d);
  1641. });
  1642. });
  1643. describe('Latex Output', function () {
  1644. it("Should pass 123.'3' = \\frac{370}{3}", function () {
  1645. var tmp = new Fraction("123.'3'");
  1646. assert.equal("\\frac{370}{3}", tmp.toLatex());
  1647. });
  1648. it("Should pass 1.'3' = \\frac{4}{3}", function () {
  1649. var tmp = new Fraction("1.'3'");
  1650. assert.equal("\\frac{4}{3}", tmp.toLatex());
  1651. });
  1652. it("Should pass -1.0000000000 = -1", function () {
  1653. var tmp = new Fraction("-1.0000000000");
  1654. assert.equal('-1', tmp.toLatex());
  1655. });
  1656. it("Should pass -0.0000000000 = 0", function () {
  1657. var tmp = new Fraction("-0.0000000000");
  1658. assert.equal('0', tmp.toLatex());
  1659. });
  1660. });
  1661. describe('Fraction Output', function () {
  1662. it("Should pass 123.'3' = 123 1/3", function () {
  1663. var tmp = new Fraction("123.'3'");
  1664. assert.equal('370/3', tmp.toFraction());
  1665. });
  1666. it("Should pass 1.'3' = 1 1/3", function () {
  1667. var tmp = new Fraction("1.'3'");
  1668. assert.equal('4/3', tmp.toFraction());
  1669. });
  1670. it("Should pass -1.0000000000 = -1", function () {
  1671. var tmp = new Fraction("-1.0000000000");
  1672. assert.equal('-1', tmp.toFraction());
  1673. });
  1674. it("Should pass -0.0000000000 = 0", function () {
  1675. var tmp = new Fraction("-0.0000000000");
  1676. assert.equal('0', tmp.toFraction());
  1677. });
  1678. it("Should pass 1/-99/293 = -1/29007", function () {
  1679. var tmp = new Fraction(-99).inverse().div(293);
  1680. assert.equal('-1/29007', tmp.toFraction());
  1681. });
  1682. it('Should work with large calculations', function () {
  1683. var x = Fraction(1123875);
  1684. var y = Fraction(1238750184);
  1685. var z = Fraction(1657134);
  1686. var r = Fraction(77344464613500, 92063);
  1687. assert.equal(x.mul(y).div(z).toFraction(), r.toFraction());
  1688. });
  1689. });
  1690. describe('Fraction toContinued', function () {
  1691. it("Should pass 415/93", function () {
  1692. var tmp = new Fraction(415, 93);
  1693. assert.equal('4,2,6,7', tmp.toContinued().toString());
  1694. });
  1695. it("Should pass 0/2", function () {
  1696. var tmp = new Fraction(0, 2);
  1697. assert.equal('0', tmp.toContinued().toString());
  1698. });
  1699. it("Should pass 1/7", function () {
  1700. var tmp = new Fraction(1, 7);
  1701. assert.equal('0,7', tmp.toContinued().toString());
  1702. });
  1703. it("Should pass 23/88", function () {
  1704. var tmp = new Fraction('23/88');
  1705. assert.equal('0,3,1,4,1,3', tmp.toContinued().toString());
  1706. });
  1707. it("Should pass 1/99", function () {
  1708. var tmp = new Fraction('1/99');
  1709. assert.equal('0,99', tmp.toContinued().toString());
  1710. });
  1711. it("Should pass 1768/99", function () {
  1712. var tmp = new Fraction('1768/99');
  1713. assert.equal('17,1,6,14', tmp.toContinued().toString());
  1714. });
  1715. it("Should pass 1768/99", function () {
  1716. var tmp = new Fraction('7/8');
  1717. assert.equal('0,1,7', tmp.toContinued().toString());
  1718. });
  1719. });
  1720. describe('Fraction simplify', function () {
  1721. it("Should pass 415/93", function () {
  1722. var tmp = new Fraction(415, 93);
  1723. assert.equal('9/2', tmp.simplify(0.1).toFraction());
  1724. assert.equal('58/13', tmp.simplify(0.01).toFraction());
  1725. assert.equal('415/93', tmp.simplify(0.0001).toFraction());
  1726. });
  1727. });