fraction.test.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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. });