imrat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. Name: imrat.c
  3. Purpose: Arbitrary precision rational arithmetic routines.
  4. Author: M. J. Fromberger
  5. Copyright (C) 2002-2007 Michael J. Fromberger, All Rights Reserved.
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE.
  21. */
  22. #include "imrat.h"
  23. #include <assert.h>
  24. #include <ctype.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #define MP_NUMER_SIGN(Q) (MP_NUMER_P(Q)->sign)
  28. #define MP_DENOM_SIGN(Q) (MP_DENOM_P(Q)->sign)
  29. #define TEMP(K) (temp + (K))
  30. #define SETUP(E, C) \
  31. do { \
  32. if ((res = (E)) != MP_OK) goto CLEANUP; \
  33. ++(C); \
  34. } while (0)
  35. /* Reduce the given rational, in place, to lowest terms and canonical form.
  36. Zero is represented as 0/1, one as 1/1. Signs are adjusted so that the sign
  37. of the numerator is definitive. */
  38. static mp_result s_rat_reduce(mp_rat r);
  39. /* Common code for addition and subtraction operations on rationals. */
  40. static mp_result s_rat_combine(mp_rat a, mp_rat b, mp_rat c,
  41. mp_result (*comb_f)(mp_int, mp_int, mp_int));
  42. mp_result mp_rat_init(mp_rat r) {
  43. mp_result res;
  44. if ((res = mp_int_init(MP_NUMER_P(r))) != MP_OK) return res;
  45. if ((res = mp_int_init(MP_DENOM_P(r))) != MP_OK) {
  46. mp_int_clear(MP_NUMER_P(r));
  47. return res;
  48. }
  49. return mp_int_set_value(MP_DENOM_P(r), 1);
  50. }
  51. mp_rat mp_rat_alloc(void) {
  52. mp_rat out = malloc(sizeof(*out));
  53. if (out != NULL) {
  54. if (mp_rat_init(out) != MP_OK) {
  55. free(out);
  56. return NULL;
  57. }
  58. }
  59. return out;
  60. }
  61. mp_result mp_rat_reduce(mp_rat r) { return s_rat_reduce(r); }
  62. mp_result mp_rat_init_size(mp_rat r, mp_size n_prec, mp_size d_prec) {
  63. mp_result res;
  64. if ((res = mp_int_init_size(MP_NUMER_P(r), n_prec)) != MP_OK) {
  65. return res;
  66. }
  67. if ((res = mp_int_init_size(MP_DENOM_P(r), d_prec)) != MP_OK) {
  68. mp_int_clear(MP_NUMER_P(r));
  69. return res;
  70. }
  71. return mp_int_set_value(MP_DENOM_P(r), 1);
  72. }
  73. mp_result mp_rat_init_copy(mp_rat r, mp_rat old) {
  74. mp_result res;
  75. if ((res = mp_int_init_copy(MP_NUMER_P(r), MP_NUMER_P(old))) != MP_OK) {
  76. return res;
  77. }
  78. if ((res = mp_int_init_copy(MP_DENOM_P(r), MP_DENOM_P(old))) != MP_OK)
  79. mp_int_clear(MP_NUMER_P(r));
  80. return res;
  81. }
  82. mp_result mp_rat_set_value(mp_rat r, mp_small numer, mp_small denom) {
  83. mp_result res;
  84. if (denom == 0) return MP_UNDEF;
  85. if ((res = mp_int_set_value(MP_NUMER_P(r), numer)) != MP_OK) {
  86. return res;
  87. }
  88. if ((res = mp_int_set_value(MP_DENOM_P(r), denom)) != MP_OK) {
  89. return res;
  90. }
  91. return s_rat_reduce(r);
  92. }
  93. mp_result mp_rat_set_uvalue(mp_rat r, mp_usmall numer, mp_usmall denom) {
  94. mp_result res;
  95. if (denom == 0) return MP_UNDEF;
  96. if ((res = mp_int_set_uvalue(MP_NUMER_P(r), numer)) != MP_OK) {
  97. return res;
  98. }
  99. if ((res = mp_int_set_uvalue(MP_DENOM_P(r), denom)) != MP_OK) {
  100. return res;
  101. }
  102. return s_rat_reduce(r);
  103. }
  104. void mp_rat_clear(mp_rat r) {
  105. mp_int_clear(MP_NUMER_P(r));
  106. mp_int_clear(MP_DENOM_P(r));
  107. }
  108. void mp_rat_free(mp_rat r) {
  109. assert(r != NULL);
  110. if (r->num.digits != NULL) mp_rat_clear(r);
  111. free(r);
  112. }
  113. mp_result mp_rat_numer(mp_rat r, mp_int z) {
  114. return mp_int_copy(MP_NUMER_P(r), z);
  115. }
  116. mp_int mp_rat_numer_ref(mp_rat r) { return MP_NUMER_P(r); }
  117. mp_result mp_rat_denom(mp_rat r, mp_int z) {
  118. return mp_int_copy(MP_DENOM_P(r), z);
  119. }
  120. mp_int mp_rat_denom_ref(mp_rat r) { return MP_DENOM_P(r); }
  121. mp_sign mp_rat_sign(mp_rat r) { return MP_NUMER_SIGN(r); }
  122. mp_result mp_rat_copy(mp_rat a, mp_rat c) {
  123. mp_result res;
  124. if ((res = mp_int_copy(MP_NUMER_P(a), MP_NUMER_P(c))) != MP_OK) {
  125. return res;
  126. }
  127. res = mp_int_copy(MP_DENOM_P(a), MP_DENOM_P(c));
  128. return res;
  129. }
  130. void mp_rat_zero(mp_rat r) {
  131. mp_int_zero(MP_NUMER_P(r));
  132. mp_int_set_value(MP_DENOM_P(r), 1);
  133. }
  134. mp_result mp_rat_abs(mp_rat a, mp_rat c) {
  135. mp_result res;
  136. if ((res = mp_int_abs(MP_NUMER_P(a), MP_NUMER_P(c))) != MP_OK) {
  137. return res;
  138. }
  139. res = mp_int_abs(MP_DENOM_P(a), MP_DENOM_P(c));
  140. return res;
  141. }
  142. mp_result mp_rat_neg(mp_rat a, mp_rat c) {
  143. mp_result res;
  144. if ((res = mp_int_neg(MP_NUMER_P(a), MP_NUMER_P(c))) != MP_OK) {
  145. return res;
  146. }
  147. res = mp_int_copy(MP_DENOM_P(a), MP_DENOM_P(c));
  148. return res;
  149. }
  150. mp_result mp_rat_recip(mp_rat a, mp_rat c) {
  151. mp_result res;
  152. if (mp_rat_compare_zero(a) == 0) return MP_UNDEF;
  153. if ((res = mp_rat_copy(a, c)) != MP_OK) return res;
  154. mp_int_swap(MP_NUMER_P(c), MP_DENOM_P(c));
  155. /* Restore the signs of the swapped elements */
  156. {
  157. mp_sign tmp = MP_NUMER_SIGN(c);
  158. MP_NUMER_SIGN(c) = MP_DENOM_SIGN(c);
  159. MP_DENOM_SIGN(c) = tmp;
  160. }
  161. return MP_OK;
  162. }
  163. mp_result mp_rat_add(mp_rat a, mp_rat b, mp_rat c) {
  164. return s_rat_combine(a, b, c, mp_int_add);
  165. }
  166. mp_result mp_rat_sub(mp_rat a, mp_rat b, mp_rat c) {
  167. return s_rat_combine(a, b, c, mp_int_sub);
  168. }
  169. mp_result mp_rat_mul(mp_rat a, mp_rat b, mp_rat c) {
  170. mp_result res;
  171. if ((res = mp_int_mul(MP_NUMER_P(a), MP_NUMER_P(b), MP_NUMER_P(c))) != MP_OK)
  172. return res;
  173. if (mp_int_compare_zero(MP_NUMER_P(c)) != 0) {
  174. res = mp_int_mul(MP_DENOM_P(a), MP_DENOM_P(b), MP_DENOM_P(c));
  175. if (res != MP_OK) {
  176. return res;
  177. }
  178. }
  179. return s_rat_reduce(c);
  180. }
  181. mp_result mp_rat_div(mp_rat a, mp_rat b, mp_rat c) {
  182. mp_result res = MP_OK;
  183. if (mp_rat_compare_zero(b) == 0) return MP_UNDEF;
  184. if (c == a || c == b) {
  185. mpz_t tmp;
  186. if ((res = mp_int_init(&tmp)) != MP_OK) return res;
  187. if ((res = mp_int_mul(MP_NUMER_P(a), MP_DENOM_P(b), &tmp)) != MP_OK) {
  188. goto CLEANUP;
  189. }
  190. if ((res = mp_int_mul(MP_DENOM_P(a), MP_NUMER_P(b), MP_DENOM_P(c))) !=
  191. MP_OK) {
  192. goto CLEANUP;
  193. }
  194. res = mp_int_copy(&tmp, MP_NUMER_P(c));
  195. CLEANUP:
  196. mp_int_clear(&tmp);
  197. } else {
  198. if ((res = mp_int_mul(MP_NUMER_P(a), MP_DENOM_P(b), MP_NUMER_P(c))) !=
  199. MP_OK) {
  200. return res;
  201. }
  202. if ((res = mp_int_mul(MP_DENOM_P(a), MP_NUMER_P(b), MP_DENOM_P(c))) !=
  203. MP_OK) {
  204. return res;
  205. }
  206. }
  207. if (res != MP_OK) {
  208. return res;
  209. } else {
  210. return s_rat_reduce(c);
  211. }
  212. }
  213. mp_result mp_rat_add_int(mp_rat a, mp_int b, mp_rat c) {
  214. mpz_t tmp;
  215. mp_result res;
  216. if ((res = mp_int_init_copy(&tmp, b)) != MP_OK) {
  217. return res;
  218. }
  219. if ((res = mp_int_mul(&tmp, MP_DENOM_P(a), &tmp)) != MP_OK) {
  220. goto CLEANUP;
  221. }
  222. if ((res = mp_rat_copy(a, c)) != MP_OK) {
  223. goto CLEANUP;
  224. }
  225. if ((res = mp_int_add(MP_NUMER_P(c), &tmp, MP_NUMER_P(c))) != MP_OK) {
  226. goto CLEANUP;
  227. }
  228. res = s_rat_reduce(c);
  229. CLEANUP:
  230. mp_int_clear(&tmp);
  231. return res;
  232. }
  233. mp_result mp_rat_sub_int(mp_rat a, mp_int b, mp_rat c) {
  234. mpz_t tmp;
  235. mp_result res;
  236. if ((res = mp_int_init_copy(&tmp, b)) != MP_OK) {
  237. return res;
  238. }
  239. if ((res = mp_int_mul(&tmp, MP_DENOM_P(a), &tmp)) != MP_OK) {
  240. goto CLEANUP;
  241. }
  242. if ((res = mp_rat_copy(a, c)) != MP_OK) {
  243. goto CLEANUP;
  244. }
  245. if ((res = mp_int_sub(MP_NUMER_P(c), &tmp, MP_NUMER_P(c))) != MP_OK) {
  246. goto CLEANUP;
  247. }
  248. res = s_rat_reduce(c);
  249. CLEANUP:
  250. mp_int_clear(&tmp);
  251. return res;
  252. }
  253. mp_result mp_rat_mul_int(mp_rat a, mp_int b, mp_rat c) {
  254. mp_result res;
  255. if ((res = mp_rat_copy(a, c)) != MP_OK) {
  256. return res;
  257. }
  258. if ((res = mp_int_mul(MP_NUMER_P(c), b, MP_NUMER_P(c))) != MP_OK) {
  259. return res;
  260. }
  261. return s_rat_reduce(c);
  262. }
  263. mp_result mp_rat_div_int(mp_rat a, mp_int b, mp_rat c) {
  264. mp_result res;
  265. if (mp_int_compare_zero(b) == 0) {
  266. return MP_UNDEF;
  267. }
  268. if ((res = mp_rat_copy(a, c)) != MP_OK) {
  269. return res;
  270. }
  271. if ((res = mp_int_mul(MP_DENOM_P(c), b, MP_DENOM_P(c))) != MP_OK) {
  272. return res;
  273. }
  274. return s_rat_reduce(c);
  275. }
  276. mp_result mp_rat_expt(mp_rat a, mp_small b, mp_rat c) {
  277. mp_result res;
  278. /* Special cases for easy powers. */
  279. if (b == 0) {
  280. return mp_rat_set_value(c, 1, 1);
  281. } else if (b == 1) {
  282. return mp_rat_copy(a, c);
  283. }
  284. /* Since rationals are always stored in lowest terms, it is not necessary to
  285. reduce again when raising to an integer power. */
  286. if ((res = mp_int_expt(MP_NUMER_P(a), b, MP_NUMER_P(c))) != MP_OK) {
  287. return res;
  288. }
  289. return mp_int_expt(MP_DENOM_P(a), b, MP_DENOM_P(c));
  290. }
  291. int mp_rat_compare(mp_rat a, mp_rat b) {
  292. /* Quick check for opposite signs. Works because the sign of the numerator
  293. is always definitive. */
  294. if (MP_NUMER_SIGN(a) != MP_NUMER_SIGN(b)) {
  295. if (MP_NUMER_SIGN(a) == MP_ZPOS) {
  296. return 1;
  297. } else {
  298. return -1;
  299. }
  300. } else {
  301. /* Compare absolute magnitudes; if both are positive, the answer stands,
  302. otherwise it needs to be reflected about zero. */
  303. int cmp = mp_rat_compare_unsigned(a, b);
  304. if (MP_NUMER_SIGN(a) == MP_ZPOS) {
  305. return cmp;
  306. } else {
  307. return -cmp;
  308. }
  309. }
  310. }
  311. int mp_rat_compare_unsigned(mp_rat a, mp_rat b) {
  312. /* If the denominators are equal, we can quickly compare numerators without
  313. multiplying. Otherwise, we actually have to do some work. */
  314. if (mp_int_compare_unsigned(MP_DENOM_P(a), MP_DENOM_P(b)) == 0) {
  315. return mp_int_compare_unsigned(MP_NUMER_P(a), MP_NUMER_P(b));
  316. }
  317. else {
  318. mpz_t temp[2];
  319. mp_result res;
  320. int cmp = INT_MAX, last = 0;
  321. /* t0 = num(a) * den(b), t1 = num(b) * den(a) */
  322. SETUP(mp_int_init_copy(TEMP(last), MP_NUMER_P(a)), last);
  323. SETUP(mp_int_init_copy(TEMP(last), MP_NUMER_P(b)), last);
  324. if ((res = mp_int_mul(TEMP(0), MP_DENOM_P(b), TEMP(0))) != MP_OK ||
  325. (res = mp_int_mul(TEMP(1), MP_DENOM_P(a), TEMP(1))) != MP_OK) {
  326. goto CLEANUP;
  327. }
  328. cmp = mp_int_compare_unsigned(TEMP(0), TEMP(1));
  329. CLEANUP:
  330. while (--last >= 0) {
  331. mp_int_clear(TEMP(last));
  332. }
  333. return cmp;
  334. }
  335. }
  336. int mp_rat_compare_zero(mp_rat r) { return mp_int_compare_zero(MP_NUMER_P(r)); }
  337. int mp_rat_compare_value(mp_rat r, mp_small n, mp_small d) {
  338. mpq_t tmp;
  339. mp_result res;
  340. int out = INT_MAX;
  341. if ((res = mp_rat_init(&tmp)) != MP_OK) {
  342. return out;
  343. }
  344. if ((res = mp_rat_set_value(&tmp, n, d)) != MP_OK) {
  345. goto CLEANUP;
  346. }
  347. out = mp_rat_compare(r, &tmp);
  348. CLEANUP:
  349. mp_rat_clear(&tmp);
  350. return out;
  351. }
  352. bool mp_rat_is_integer(mp_rat r) {
  353. return (mp_int_compare_value(MP_DENOM_P(r), 1) == 0);
  354. }
  355. mp_result mp_rat_to_ints(mp_rat r, mp_small *num, mp_small *den) {
  356. mp_result res;
  357. if ((res = mp_int_to_int(MP_NUMER_P(r), num)) != MP_OK) {
  358. return res;
  359. }
  360. res = mp_int_to_int(MP_DENOM_P(r), den);
  361. return res;
  362. }
  363. mp_result mp_rat_to_string(mp_rat r, mp_size radix, char *str, int limit) {
  364. /* Write the numerator. The sign of the rational number is written by the
  365. underlying integer implementation. */
  366. mp_result res;
  367. if ((res = mp_int_to_string(MP_NUMER_P(r), radix, str, limit)) != MP_OK) {
  368. return res;
  369. }
  370. /* If the value is zero, don't bother writing any denominator */
  371. if (mp_int_compare_zero(MP_NUMER_P(r)) == 0) {
  372. return MP_OK;
  373. }
  374. /* Locate the end of the numerator, and make sure we are not going to exceed
  375. the limit by writing a slash. */
  376. int len = strlen(str);
  377. char *start = str + len;
  378. limit -= len;
  379. if (limit == 0) return MP_TRUNC;
  380. *start++ = '/';
  381. limit -= 1;
  382. return mp_int_to_string(MP_DENOM_P(r), radix, start, limit);
  383. }
  384. mp_result mp_rat_to_decimal(mp_rat r, mp_size radix, mp_size prec,
  385. mp_round_mode round, char *str, int limit) {
  386. mpz_t temp[3];
  387. mp_result res;
  388. int last = 0;
  389. SETUP(mp_int_init_copy(TEMP(last), MP_NUMER_P(r)), last);
  390. SETUP(mp_int_init(TEMP(last)), last);
  391. SETUP(mp_int_init(TEMP(last)), last);
  392. /* Get the unsigned integer part by dividing denominator into the absolute
  393. value of the numerator. */
  394. mp_int_abs(TEMP(0), TEMP(0));
  395. if ((res = mp_int_div(TEMP(0), MP_DENOM_P(r), TEMP(0), TEMP(1))) != MP_OK) {
  396. goto CLEANUP;
  397. }
  398. /* Now: T0 = integer portion, unsigned;
  399. T1 = remainder, from which fractional part is computed. */
  400. /* Count up leading zeroes after the radix point. */
  401. int zprec = (int)prec;
  402. int lead_0;
  403. for (lead_0 = 0; lead_0 < zprec && mp_int_compare(TEMP(1), MP_DENOM_P(r)) < 0;
  404. ++lead_0) {
  405. if ((res = mp_int_mul_value(TEMP(1), radix, TEMP(1))) != MP_OK) {
  406. goto CLEANUP;
  407. }
  408. }
  409. /* Multiply remainder by a power of the radix sufficient to get the right
  410. number of significant figures. */
  411. if (zprec > lead_0) {
  412. if ((res = mp_int_expt_value(radix, zprec - lead_0, TEMP(2))) != MP_OK) {
  413. goto CLEANUP;
  414. }
  415. if ((res = mp_int_mul(TEMP(1), TEMP(2), TEMP(1))) != MP_OK) {
  416. goto CLEANUP;
  417. }
  418. }
  419. if ((res = mp_int_div(TEMP(1), MP_DENOM_P(r), TEMP(1), TEMP(2))) != MP_OK) {
  420. goto CLEANUP;
  421. }
  422. /* Now: T1 = significant digits of fractional part;
  423. T2 = leftovers, to use for rounding.
  424. At this point, what we do depends on the rounding mode. The default is
  425. MP_ROUND_DOWN, for which everything is as it should be already.
  426. */
  427. switch (round) {
  428. int cmp;
  429. case MP_ROUND_UP:
  430. if (mp_int_compare_zero(TEMP(2)) != 0) {
  431. if (prec == 0) {
  432. res = mp_int_add_value(TEMP(0), 1, TEMP(0));
  433. } else {
  434. res = mp_int_add_value(TEMP(1), 1, TEMP(1));
  435. }
  436. }
  437. break;
  438. case MP_ROUND_HALF_UP:
  439. case MP_ROUND_HALF_DOWN:
  440. if ((res = mp_int_mul_pow2(TEMP(2), 1, TEMP(2))) != MP_OK) {
  441. goto CLEANUP;
  442. }
  443. cmp = mp_int_compare(TEMP(2), MP_DENOM_P(r));
  444. if (round == MP_ROUND_HALF_UP) cmp += 1;
  445. if (cmp > 0) {
  446. if (prec == 0) {
  447. res = mp_int_add_value(TEMP(0), 1, TEMP(0));
  448. } else {
  449. res = mp_int_add_value(TEMP(1), 1, TEMP(1));
  450. }
  451. }
  452. break;
  453. case MP_ROUND_DOWN:
  454. break; /* No action required */
  455. default:
  456. return MP_BADARG; /* Invalid rounding specifier */
  457. }
  458. if (res != MP_OK) {
  459. goto CLEANUP;
  460. }
  461. /* The sign of the output should be the sign of the numerator, but if all the
  462. displayed digits will be zero due to the precision, a negative shouldn't
  463. be shown. */
  464. char *start = str;
  465. int left = limit;
  466. if (MP_NUMER_SIGN(r) == MP_NEG && (mp_int_compare_zero(TEMP(0)) != 0 ||
  467. mp_int_compare_zero(TEMP(1)) != 0)) {
  468. *start++ = '-';
  469. left -= 1;
  470. }
  471. if ((res = mp_int_to_string(TEMP(0), radix, start, left)) != MP_OK) {
  472. goto CLEANUP;
  473. }
  474. int len = strlen(start);
  475. start += len;
  476. left -= len;
  477. if (prec == 0) goto CLEANUP;
  478. *start++ = '.';
  479. left -= 1;
  480. if (left < zprec + 1) {
  481. res = MP_TRUNC;
  482. goto CLEANUP;
  483. }
  484. memset(start, '0', lead_0 - 1);
  485. left -= lead_0;
  486. start += lead_0 - 1;
  487. res = mp_int_to_string(TEMP(1), radix, start, left);
  488. CLEANUP:
  489. while (--last >= 0) mp_int_clear(TEMP(last));
  490. return res;
  491. }
  492. mp_result mp_rat_string_len(mp_rat r, mp_size radix) {
  493. mp_result d_len = 0;
  494. mp_result n_len = mp_int_string_len(MP_NUMER_P(r), radix);
  495. if (mp_int_compare_zero(MP_NUMER_P(r)) != 0) {
  496. d_len = mp_int_string_len(MP_DENOM_P(r), radix);
  497. }
  498. /* Though simplistic, this formula is correct. Space for the sign flag is
  499. included in n_len, and the space for the NUL that is counted in n_len
  500. counts for the separator here. The space for the NUL counted in d_len
  501. counts for the final terminator here. */
  502. return n_len + d_len;
  503. }
  504. mp_result mp_rat_decimal_len(mp_rat r, mp_size radix, mp_size prec) {
  505. int f_len;
  506. int z_len = mp_int_string_len(MP_NUMER_P(r), radix);
  507. if (prec == 0) {
  508. f_len = 1; /* terminator only */
  509. } else {
  510. f_len = 1 + prec + 1; /* decimal point, digits, terminator */
  511. }
  512. return z_len + f_len;
  513. }
  514. mp_result mp_rat_read_string(mp_rat r, mp_size radix, const char *str) {
  515. return mp_rat_read_cstring(r, radix, str, NULL);
  516. }
  517. mp_result mp_rat_read_cstring(mp_rat r, mp_size radix, const char *str,
  518. char **end) {
  519. mp_result res;
  520. char *endp;
  521. if ((res = mp_int_read_cstring(MP_NUMER_P(r), radix, str, &endp)) != MP_OK &&
  522. (res != MP_TRUNC)) {
  523. return res;
  524. }
  525. /* Skip whitespace between numerator and (possible) separator */
  526. while (isspace((unsigned char)*endp)) {
  527. ++endp;
  528. }
  529. /* If there is no separator, we will stop reading at this point. */
  530. if (*endp != '/') {
  531. mp_int_set_value(MP_DENOM_P(r), 1);
  532. if (end != NULL) *end = endp;
  533. return res;
  534. }
  535. ++endp; /* skip separator */
  536. if ((res = mp_int_read_cstring(MP_DENOM_P(r), radix, endp, end)) != MP_OK) {
  537. return res;
  538. }
  539. /* Make sure the value is well-defined */
  540. if (mp_int_compare_zero(MP_DENOM_P(r)) == 0) {
  541. return MP_UNDEF;
  542. }
  543. /* Reduce to lowest terms */
  544. return s_rat_reduce(r);
  545. }
  546. /* Read a string and figure out what format it's in. The radix may be supplied
  547. as zero to use "default" behaviour.
  548. This function will accept either a/b notation or decimal notation.
  549. */
  550. mp_result mp_rat_read_ustring(mp_rat r, mp_size radix, const char *str,
  551. char **end) {
  552. char *endp = "";
  553. mp_result res;
  554. if (radix == 0) radix = 10; /* default to decimal input */
  555. res = mp_rat_read_cstring(r, radix, str, &endp);
  556. if (res == MP_TRUNC && *endp == '.') {
  557. res = mp_rat_read_cdecimal(r, radix, str, &endp);
  558. }
  559. if (end != NULL) *end = endp;
  560. return res;
  561. }
  562. mp_result mp_rat_read_decimal(mp_rat r, mp_size radix, const char *str) {
  563. return mp_rat_read_cdecimal(r, radix, str, NULL);
  564. }
  565. mp_result mp_rat_read_cdecimal(mp_rat r, mp_size radix, const char *str,
  566. char **end) {
  567. mp_result res;
  568. mp_sign osign;
  569. char *endp;
  570. while (isspace((unsigned char)*str)) ++str;
  571. switch (*str) {
  572. case '-':
  573. osign = MP_NEG;
  574. break;
  575. default:
  576. osign = MP_ZPOS;
  577. }
  578. if ((res = mp_int_read_cstring(MP_NUMER_P(r), radix, str, &endp)) != MP_OK &&
  579. (res != MP_TRUNC)) {
  580. return res;
  581. }
  582. /* This needs to be here. */
  583. (void)mp_int_set_value(MP_DENOM_P(r), 1);
  584. if (*endp != '.') {
  585. if (end != NULL) *end = endp;
  586. return res;
  587. }
  588. /* If the character following the decimal point is whitespace or a sign flag,
  589. we will consider this a truncated value. This special case is because
  590. mp_int_read_string() will consider whitespace or sign flags to be valid
  591. starting characters for a value, and we do not want them following the
  592. decimal point.
  593. Once we have done this check, it is safe to read in the value of the
  594. fractional piece as a regular old integer.
  595. */
  596. ++endp;
  597. if (*endp == '\0') {
  598. if (end != NULL) *end = endp;
  599. return MP_OK;
  600. } else if (isspace((unsigned char)*endp) || *endp == '-' || *endp == '+') {
  601. return MP_TRUNC;
  602. } else {
  603. mpz_t frac;
  604. mp_result save_res;
  605. char *save = endp;
  606. int num_lz = 0;
  607. /* Make a temporary to hold the part after the decimal point. */
  608. if ((res = mp_int_init(&frac)) != MP_OK) {
  609. return res;
  610. }
  611. if ((res = mp_int_read_cstring(&frac, radix, endp, &endp)) != MP_OK &&
  612. (res != MP_TRUNC)) {
  613. goto CLEANUP;
  614. }
  615. /* Save this response for later. */
  616. save_res = res;
  617. if (mp_int_compare_zero(&frac) == 0) goto FINISHED;
  618. /* Discard trailing zeroes (somewhat inefficiently) */
  619. while (mp_int_divisible_value(&frac, radix)) {
  620. if ((res = mp_int_div_value(&frac, radix, &frac, NULL)) != MP_OK) {
  621. goto CLEANUP;
  622. }
  623. }
  624. /* Count leading zeros after the decimal point */
  625. while (save[num_lz] == '0') {
  626. ++num_lz;
  627. }
  628. /* Find the least power of the radix that is at least as large as the
  629. significant value of the fractional part, ignoring leading zeroes. */
  630. (void)mp_int_set_value(MP_DENOM_P(r), radix);
  631. while (mp_int_compare(MP_DENOM_P(r), &frac) < 0) {
  632. if ((res = mp_int_mul_value(MP_DENOM_P(r), radix, MP_DENOM_P(r))) !=
  633. MP_OK) {
  634. goto CLEANUP;
  635. }
  636. }
  637. /* Also shift by enough to account for leading zeroes */
  638. while (num_lz > 0) {
  639. if ((res = mp_int_mul_value(MP_DENOM_P(r), radix, MP_DENOM_P(r))) !=
  640. MP_OK) {
  641. goto CLEANUP;
  642. }
  643. --num_lz;
  644. }
  645. /* Having found this power, shift the numerator leftward that many, digits,
  646. and add the nonzero significant digits of the fractional part to get the
  647. result. */
  648. if ((res = mp_int_mul(MP_NUMER_P(r), MP_DENOM_P(r), MP_NUMER_P(r))) !=
  649. MP_OK) {
  650. goto CLEANUP;
  651. }
  652. { /* This addition needs to be unsigned. */
  653. MP_NUMER_SIGN(r) = MP_ZPOS;
  654. if ((res = mp_int_add(MP_NUMER_P(r), &frac, MP_NUMER_P(r))) != MP_OK) {
  655. goto CLEANUP;
  656. }
  657. MP_NUMER_SIGN(r) = osign;
  658. }
  659. if ((res = s_rat_reduce(r)) != MP_OK) goto CLEANUP;
  660. /* At this point, what we return depends on whether reading the fractional
  661. part was truncated or not. That information is saved from when we
  662. called mp_int_read_string() above. */
  663. FINISHED:
  664. res = save_res;
  665. if (end != NULL) *end = endp;
  666. CLEANUP:
  667. mp_int_clear(&frac);
  668. return res;
  669. }
  670. }
  671. /* Private functions for internal use. Make unchecked assumptions about format
  672. and validity of inputs. */
  673. static mp_result s_rat_reduce(mp_rat r) {
  674. mpz_t gcd;
  675. mp_result res = MP_OK;
  676. if (mp_int_compare_zero(MP_NUMER_P(r)) == 0) {
  677. mp_int_set_value(MP_DENOM_P(r), 1);
  678. return MP_OK;
  679. }
  680. /* If the greatest common divisor of the numerator and denominator is greater
  681. than 1, divide it out. */
  682. if ((res = mp_int_init(&gcd)) != MP_OK) return res;
  683. if ((res = mp_int_gcd(MP_NUMER_P(r), MP_DENOM_P(r), &gcd)) != MP_OK) {
  684. goto CLEANUP;
  685. }
  686. if (mp_int_compare_value(&gcd, 1) != 0) {
  687. if ((res = mp_int_div(MP_NUMER_P(r), &gcd, MP_NUMER_P(r), NULL)) != MP_OK) {
  688. goto CLEANUP;
  689. }
  690. if ((res = mp_int_div(MP_DENOM_P(r), &gcd, MP_DENOM_P(r), NULL)) != MP_OK) {
  691. goto CLEANUP;
  692. }
  693. }
  694. /* Fix up the signs of numerator and denominator */
  695. if (MP_NUMER_SIGN(r) == MP_DENOM_SIGN(r)) {
  696. MP_NUMER_SIGN(r) = MP_DENOM_SIGN(r) = MP_ZPOS;
  697. } else {
  698. MP_NUMER_SIGN(r) = MP_NEG;
  699. MP_DENOM_SIGN(r) = MP_ZPOS;
  700. }
  701. CLEANUP:
  702. mp_int_clear(&gcd);
  703. return res;
  704. }
  705. static mp_result s_rat_combine(mp_rat a, mp_rat b, mp_rat c,
  706. mp_result (*comb_f)(mp_int, mp_int, mp_int)) {
  707. mp_result res;
  708. /* Shortcut when denominators are already common */
  709. if (mp_int_compare(MP_DENOM_P(a), MP_DENOM_P(b)) == 0) {
  710. if ((res = (comb_f)(MP_NUMER_P(a), MP_NUMER_P(b), MP_NUMER_P(c))) !=
  711. MP_OK) {
  712. return res;
  713. }
  714. if ((res = mp_int_copy(MP_DENOM_P(a), MP_DENOM_P(c))) != MP_OK) {
  715. return res;
  716. }
  717. return s_rat_reduce(c);
  718. } else {
  719. mpz_t temp[2];
  720. int last = 0;
  721. SETUP(mp_int_init_copy(TEMP(last), MP_NUMER_P(a)), last);
  722. SETUP(mp_int_init_copy(TEMP(last), MP_NUMER_P(b)), last);
  723. if ((res = mp_int_mul(TEMP(0), MP_DENOM_P(b), TEMP(0))) != MP_OK) {
  724. goto CLEANUP;
  725. }
  726. if ((res = mp_int_mul(TEMP(1), MP_DENOM_P(a), TEMP(1))) != MP_OK) {
  727. goto CLEANUP;
  728. }
  729. if ((res = (comb_f)(TEMP(0), TEMP(1), MP_NUMER_P(c))) != MP_OK) {
  730. goto CLEANUP;
  731. }
  732. res = mp_int_mul(MP_DENOM_P(a), MP_DENOM_P(b), MP_DENOM_P(c));
  733. CLEANUP:
  734. while (--last >= 0) {
  735. mp_int_clear(TEMP(last));
  736. }
  737. if (res == MP_OK) {
  738. return s_rat_reduce(c);
  739. } else {
  740. return res;
  741. }
  742. }
  743. }
  744. /* Here there be dragons */