AbaKus – a complex calculator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1810 lines
41KB

  1. /* number.c: Implements arbitrary precision numbers. */
  2. /*
  3. Copyright (C) 1991, 1992, 1993, 1994, 1997, 2000 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License , or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; see the file COPYING. If not, write to:
  14. The Free Software Foundation, Inc.
  15. 51 Franklin Street, Fifth Floor
  16. Boston, MA 02110-1301 USA.
  17. You may contact the author by:
  18. e-mail: philnelson@acm.org
  19. us-mail: Philip A. Nelson
  20. Computer Science Department, 9062
  21. Western Washington University
  22. Bellingham, WA 98226-9062
  23. *************************************************************************/
  24. #include "number.h"
  25. #include <stdio.h>
  26. #include <assert.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <ctype.h>/* Prototypes needed for external utility routines. */
  30. #define bc_rt_warn rt_warn
  31. #define bc_rt_error rt_error
  32. #define bc_out_of_memory out_of_memory
  33. _PROTOTYPE(void rt_warn, (char *mesg ,...));
  34. _PROTOTYPE(void rt_error, (char *mesg ,...));
  35. _PROTOTYPE(void out_of_memory, (void));
  36. void out_of_memory(void){
  37. return;
  38. }
  39. void rt_warn(char *mesg ,...){
  40. return;
  41. }
  42. void rt_error(char *mesg ,...){
  43. return;
  44. }
  45. /* Storage used for special numbers. */
  46. bc_num _zero_;
  47. bc_num _one_;
  48. bc_num _two_;
  49. static bc_num _bc_Free_list = NULL;
  50. /* new_num allocates a number and sets fields to known values. */
  51. bc_num
  52. bc_new_num (length, scale)
  53. int length, scale;
  54. {
  55. bc_num temp;
  56. if (_bc_Free_list != NULL) {
  57. temp = _bc_Free_list;
  58. _bc_Free_list = temp->n_next;
  59. } else {
  60. temp = (bc_num) malloc (sizeof(bc_struct));
  61. if (temp == NULL) bc_out_of_memory ();
  62. }
  63. temp->n_sign = PLUS;
  64. temp->n_len = length;
  65. temp->n_scale = scale;
  66. temp->n_refs = 1;
  67. temp->n_ptr = (char *) malloc (length+scale+1);
  68. if (temp->n_ptr == NULL) bc_out_of_memory();
  69. temp->n_value = temp->n_ptr;
  70. memset (temp->n_ptr, 0, length+scale);
  71. return temp;
  72. }
  73. /* "Frees" a bc_num NUM. Actually decreases reference count and only
  74. frees the storage if reference count is zero. */
  75. void
  76. bc_free_num (num)
  77. bc_num *num;
  78. {
  79. if (*num == NULL) return;
  80. (*num)->n_refs--;
  81. if ((*num)->n_refs == 0) {
  82. if ((*num)->n_ptr)
  83. free ((*num)->n_ptr);
  84. (*num)->n_next = _bc_Free_list;
  85. _bc_Free_list = *num;
  86. }
  87. *num = NULL;
  88. }
  89. /* Intitialize the number package! */
  90. void
  91. bc_init_numbers ()
  92. {
  93. _zero_ = bc_new_num (1,0);
  94. _one_ = bc_new_num (1,0);
  95. _one_->n_value[0] = 1;
  96. _two_ = bc_new_num (1,0);
  97. _two_->n_value[0] = 2;
  98. }
  99. /* Make a copy of a number! Just increments the reference count! */
  100. bc_num
  101. bc_copy_num (num)
  102. bc_num num;
  103. {
  104. num->n_refs++;
  105. return num;
  106. }
  107. /* Initialize a number NUM by making it a copy of zero. */
  108. void
  109. bc_init_num (num)
  110. bc_num *num;
  111. {
  112. *num = bc_copy_num (_zero_);
  113. }
  114. /* For many things, we may have leading zeros in a number NUM.
  115. _bc_rm_leading_zeros just moves the data "value" pointer to the
  116. correct place and adjusts the length. */
  117. static void
  118. _bc_rm_leading_zeros (num)
  119. bc_num num;
  120. {
  121. /* We can move n_value to point to the first non zero digit! */
  122. while (*num->n_value == 0 && num->n_len > 1) {
  123. num->n_value++;
  124. num->n_len--;
  125. }
  126. }
  127. /* Compare two bc numbers. Return value is 0 if equal, -1 if N1 is less
  128. than N2 and +1 if N1 is greater than N2. If USE_SIGN is false, just
  129. compare the magnitudes. */
  130. static int
  131. _bc_do_compare (n1, n2, use_sign, ignore_last)
  132. bc_num n1, n2;
  133. int use_sign;
  134. int ignore_last;
  135. {
  136. char *n1ptr, *n2ptr;
  137. int count;
  138. /* First, compare signs. */
  139. if (use_sign && n1->n_sign != n2->n_sign)
  140. {
  141. if (n1->n_sign == PLUS)
  142. return (1); /* Positive N1 > Negative N2 */
  143. else
  144. return (-1); /* Negative N1 < Positive N1 */
  145. }
  146. /* Now compare the magnitude. */
  147. if (n1->n_len != n2->n_len)
  148. {
  149. if (n1->n_len > n2->n_len)
  150. {
  151. /* Magnitude of n1 > n2. */
  152. if (!use_sign || n1->n_sign == PLUS)
  153. return (1);
  154. else
  155. return (-1);
  156. }
  157. else
  158. {
  159. /* Magnitude of n1 < n2. */
  160. if (!use_sign || n1->n_sign == PLUS)
  161. return (-1);
  162. else
  163. return (1);
  164. }
  165. }
  166. /* If we get here, they have the same number of integer digits.
  167. check the integer part and the equal length part of the fraction. */
  168. count = n1->n_len + MIN (n1->n_scale, n2->n_scale);
  169. n1ptr = n1->n_value;
  170. n2ptr = n2->n_value;
  171. while ((count > 0) && (*n1ptr == *n2ptr))
  172. {
  173. n1ptr++;
  174. n2ptr++;
  175. count--;
  176. }
  177. if (ignore_last && count == 1 && n1->n_scale == n2->n_scale)
  178. return (0);
  179. if (count != 0)
  180. {
  181. if (*n1ptr > *n2ptr)
  182. {
  183. /* Magnitude of n1 > n2. */
  184. if (!use_sign || n1->n_sign == PLUS)
  185. return (1);
  186. else
  187. return (-1);
  188. }
  189. else
  190. {
  191. /* Magnitude of n1 < n2. */
  192. if (!use_sign || n1->n_sign == PLUS)
  193. return (-1);
  194. else
  195. return (1);
  196. }
  197. }
  198. /* They are equal up to the last part of the equal part of the fraction. */
  199. if (n1->n_scale != n2->n_scale)
  200. {
  201. if (n1->n_scale > n2->n_scale)
  202. {
  203. for (count = n1->n_scale-n2->n_scale; count>0; count--)
  204. if (*n1ptr++ != 0)
  205. {
  206. /* Magnitude of n1 > n2. */
  207. if (!use_sign || n1->n_sign == PLUS)
  208. return (1);
  209. else
  210. return (-1);
  211. }
  212. }
  213. else
  214. {
  215. for (count = n2->n_scale-n1->n_scale; count>0; count--)
  216. if (*n2ptr++ != 0)
  217. {
  218. /* Magnitude of n1 < n2. */
  219. if (!use_sign || n1->n_sign == PLUS)
  220. return (-1);
  221. else
  222. return (1);
  223. }
  224. }
  225. }
  226. /* They must be equal! */
  227. return (0);
  228. }
  229. /* This is the "user callable" routine to compare numbers N1 and N2. */
  230. int
  231. bc_compare (n1, n2)
  232. bc_num n1, n2;
  233. {
  234. return _bc_do_compare (n1, n2, TRUE, FALSE);
  235. }
  236. /* In some places we need to check if the number is negative. */
  237. char
  238. bc_is_neg (num)
  239. bc_num num;
  240. {
  241. return num->n_sign == MINUS;
  242. }
  243. /* In some places we need to check if the number NUM is zero. */
  244. char
  245. bc_is_zero (num)
  246. bc_num num;
  247. {
  248. int count;
  249. char *nptr;
  250. /* Quick check. */
  251. if (num == _zero_) return TRUE;
  252. /* Initialize */
  253. count = num->n_len + num->n_scale;
  254. nptr = num->n_value;
  255. /* The check */
  256. while ((count > 0) && (*nptr++ == 0)) count--;
  257. if (count != 0)
  258. return FALSE;
  259. else
  260. return TRUE;
  261. }
  262. /* In some places we need to check if the number NUM is almost zero.
  263. Specifically, all but the last digit is 0 and the last digit is 1.
  264. Last digit is defined by scale. */
  265. char
  266. bc_is_near_zero (num, scale)
  267. bc_num num;
  268. int scale;
  269. {
  270. int count;
  271. char *nptr;
  272. /* Error checking */
  273. if (scale > num->n_scale)
  274. scale = num->n_scale;
  275. /* Initialize */
  276. count = num->n_len + scale;
  277. nptr = num->n_value;
  278. /* The check */
  279. while ((count > 0) && (*nptr++ == 0)) count--;
  280. if (count != 0 && (count != 1 || *--nptr != 1))
  281. return FALSE;
  282. else
  283. return TRUE;
  284. }
  285. /* Perform addition: N1 is added to N2 and the value is
  286. returned. The signs of N1 and N2 are ignored.
  287. SCALE_MIN is to set the minimum scale of the result. */
  288. static bc_num
  289. _bc_do_add (n1, n2, scale_min)
  290. bc_num n1, n2;
  291. int scale_min;
  292. {
  293. bc_num sum;
  294. int sum_scale, sum_digits;
  295. char *n1ptr, *n2ptr, *sumptr;
  296. int carry, n1bytes, n2bytes;
  297. int count;
  298. /* Prepare sum. */
  299. sum_scale = MAX (n1->n_scale, n2->n_scale);
  300. sum_digits = MAX (n1->n_len, n2->n_len) + 1;
  301. sum = bc_new_num (sum_digits, MAX(sum_scale, scale_min));
  302. /* Zero extra digits made by scale_min. */
  303. if (scale_min > sum_scale)
  304. {
  305. sumptr = (char *) (sum->n_value + sum_scale + sum_digits);
  306. for (count = scale_min - sum_scale; count > 0; count--)
  307. *sumptr++ = 0;
  308. }
  309. /* Start with the fraction part. Initialize the pointers. */
  310. n1bytes = n1->n_scale;
  311. n2bytes = n2->n_scale;
  312. n1ptr = (char *) (n1->n_value + n1->n_len + n1bytes - 1);
  313. n2ptr = (char *) (n2->n_value + n2->n_len + n2bytes - 1);
  314. sumptr = (char *) (sum->n_value + sum_scale + sum_digits - 1);
  315. /* Add the fraction part. First copy the longer fraction.*/
  316. if (n1bytes != n2bytes)
  317. {
  318. if (n1bytes > n2bytes)
  319. while (n1bytes>n2bytes)
  320. { *sumptr-- = *n1ptr--; n1bytes--;}
  321. else
  322. while (n2bytes>n1bytes)
  323. { *sumptr-- = *n2ptr--; n2bytes--;}
  324. }
  325. /* Now add the remaining fraction part and equal size integer parts. */
  326. n1bytes += n1->n_len;
  327. n2bytes += n2->n_len;
  328. carry = 0;
  329. while ((n1bytes > 0) && (n2bytes > 0))
  330. {
  331. *sumptr = *n1ptr-- + *n2ptr-- + carry;
  332. if (*sumptr > (BASE-1))
  333. {
  334. carry = 1;
  335. *sumptr -= BASE;
  336. }
  337. else
  338. carry = 0;
  339. sumptr--;
  340. n1bytes--;
  341. n2bytes--;
  342. }
  343. /* Now add carry the longer integer part. */
  344. if (n1bytes == 0)
  345. { n1bytes = n2bytes; n1ptr = n2ptr; }
  346. while (n1bytes-- > 0)
  347. {
  348. *sumptr = *n1ptr-- + carry;
  349. if (*sumptr > (BASE-1))
  350. {
  351. carry = 1;
  352. *sumptr -= BASE;
  353. }
  354. else
  355. carry = 0;
  356. sumptr--;
  357. }
  358. /* Set final carry. */
  359. if (carry == 1)
  360. *sumptr += 1;
  361. /* Adjust sum and return. */
  362. _bc_rm_leading_zeros (sum);
  363. return sum;
  364. }
  365. /* Perform subtraction: N2 is subtracted from N1 and the value is
  366. returned. The signs of N1 and N2 are ignored. Also, N1 is
  367. assumed to be larger than N2. SCALE_MIN is the minimum scale
  368. of the result. */
  369. static bc_num
  370. _bc_do_sub (n1, n2, scale_min)
  371. bc_num n1, n2;
  372. int scale_min;
  373. {
  374. bc_num diff;
  375. int diff_scale, diff_len;
  376. int min_scale, min_len;
  377. char *n1ptr, *n2ptr, *diffptr;
  378. int borrow, count, val;
  379. /* Allocate temporary storage. */
  380. diff_len = MAX (n1->n_len, n2->n_len);
  381. diff_scale = MAX (n1->n_scale, n2->n_scale);
  382. min_len = MIN (n1->n_len, n2->n_len);
  383. min_scale = MIN (n1->n_scale, n2->n_scale);
  384. diff = bc_new_num (diff_len, MAX(diff_scale, scale_min));
  385. /* Zero extra digits made by scale_min. */
  386. if (scale_min > diff_scale)
  387. {
  388. diffptr = (char *) (diff->n_value + diff_len + diff_scale);
  389. for (count = scale_min - diff_scale; count > 0; count--)
  390. *diffptr++ = 0;
  391. }
  392. /* Initialize the subtract. */
  393. n1ptr = (char *) (n1->n_value + n1->n_len + n1->n_scale -1);
  394. n2ptr = (char *) (n2->n_value + n2->n_len + n2->n_scale -1);
  395. diffptr = (char *) (diff->n_value + diff_len + diff_scale -1);
  396. /* Subtract the numbers. */
  397. borrow = 0;
  398. /* Take care of the longer scaled number. */
  399. if (n1->n_scale != min_scale)
  400. {
  401. /* n1 has the longer scale */
  402. for (count = n1->n_scale - min_scale; count > 0; count--)
  403. *diffptr-- = *n1ptr--;
  404. }
  405. else
  406. {
  407. /* n2 has the longer scale */
  408. for (count = n2->n_scale - min_scale; count > 0; count--)
  409. {
  410. val = - *n2ptr-- - borrow;
  411. if (val < 0)
  412. {
  413. val += BASE;
  414. borrow = 1;
  415. }
  416. else
  417. borrow = 0;
  418. *diffptr-- = val;
  419. }
  420. }
  421. /* Now do the equal length scale and integer parts. */
  422. for (count = 0; count < min_len + min_scale; count++)
  423. {
  424. val = *n1ptr-- - *n2ptr-- - borrow;
  425. if (val < 0)
  426. {
  427. val += BASE;
  428. borrow = 1;
  429. }
  430. else
  431. borrow = 0;
  432. *diffptr-- = val;
  433. }
  434. /* If n1 has more digits then n2, we now do that subtract. */
  435. if (diff_len != min_len)
  436. {
  437. for (count = diff_len - min_len; count > 0; count--)
  438. {
  439. val = *n1ptr-- - borrow;
  440. if (val < 0)
  441. {
  442. val += BASE;
  443. borrow = 1;
  444. }
  445. else
  446. borrow = 0;
  447. *diffptr-- = val;
  448. }
  449. }
  450. /* Clean up and return. */
  451. _bc_rm_leading_zeros (diff);
  452. return diff;
  453. }
  454. /* Here is the full subtract routine that takes care of negative numbers.
  455. N2 is subtracted from N1 and the result placed in RESULT. SCALE_MIN
  456. is the minimum scale for the result. */
  457. void
  458. bc_sub (n1, n2, result, scale_min)
  459. bc_num n1, n2, *result;
  460. int scale_min;
  461. {
  462. bc_num diff = NULL;
  463. int cmp_res;
  464. int res_scale;
  465. if (n1->n_sign != n2->n_sign)
  466. {
  467. diff = _bc_do_add (n1, n2, scale_min);
  468. diff->n_sign = n1->n_sign;
  469. }
  470. else
  471. {
  472. /* subtraction must be done. */
  473. /* Compare magnitudes. */
  474. cmp_res = _bc_do_compare (n1, n2, FALSE, FALSE);
  475. switch (cmp_res)
  476. {
  477. case -1:
  478. /* n1 is less than n2, subtract n1 from n2. */
  479. diff = _bc_do_sub (n2, n1, scale_min);
  480. diff->n_sign = (n2->n_sign == PLUS ? MINUS : PLUS);
  481. break;
  482. case 0:
  483. /* They are equal! return zero! */
  484. res_scale = MAX (scale_min, MAX(n1->n_scale, n2->n_scale));
  485. diff = bc_new_num (1, res_scale);
  486. memset (diff->n_value, 0, res_scale+1);
  487. break;
  488. case 1:
  489. /* n2 is less than n1, subtract n2 from n1. */
  490. diff = _bc_do_sub (n1, n2, scale_min);
  491. diff->n_sign = n1->n_sign;
  492. break;
  493. }
  494. }
  495. /* Clean up and return. */
  496. bc_free_num (result);
  497. *result = diff;
  498. }
  499. /* Here is the full add routine that takes care of negative numbers.
  500. N1 is added to N2 and the result placed into RESULT. SCALE_MIN
  501. is the minimum scale for the result. */
  502. void
  503. bc_add (n1, n2, result, scale_min)
  504. bc_num n1, n2, *result;
  505. int scale_min;
  506. {
  507. bc_num sum = NULL;
  508. int cmp_res;
  509. int res_scale;
  510. if (n1->n_sign == n2->n_sign)
  511. {
  512. sum = _bc_do_add (n1, n2, scale_min);
  513. sum->n_sign = n1->n_sign;
  514. }
  515. else
  516. {
  517. /* subtraction must be done. */
  518. cmp_res = _bc_do_compare (n1, n2, FALSE, FALSE); /* Compare magnitudes. */
  519. switch (cmp_res)
  520. {
  521. case -1:
  522. /* n1 is less than n2, subtract n1 from n2. */
  523. sum = _bc_do_sub (n2, n1, scale_min);
  524. sum->n_sign = n2->n_sign;
  525. break;
  526. case 0:
  527. /* They are equal! return zero with the correct scale! */
  528. res_scale = MAX (scale_min, MAX(n1->n_scale, n2->n_scale));
  529. sum = bc_new_num (1, res_scale);
  530. memset (sum->n_value, 0, res_scale+1);
  531. break;
  532. case 1:
  533. /* n2 is less than n1, subtract n2 from n1. */
  534. sum = _bc_do_sub (n1, n2, scale_min);
  535. sum->n_sign = n1->n_sign;
  536. }
  537. }
  538. /* Clean up and return. */
  539. bc_free_num (result);
  540. *result = sum;
  541. }
  542. /* Recursive vs non-recursive multiply crossover ranges. */
  543. #if defined(MULDIGITS)
  544. #include "muldigits.h"
  545. #else
  546. #define MUL_BASE_DIGITS 80
  547. #endif
  548. int mul_base_digits = MUL_BASE_DIGITS;
  549. #define MUL_SMALL_DIGITS mul_base_digits/4
  550. /* Multiply utility routines */
  551. static bc_num
  552. new_sub_num (length, scale, value)
  553. int length, scale;
  554. char *value;
  555. {
  556. bc_num temp;
  557. if (_bc_Free_list != NULL) {
  558. temp = _bc_Free_list;
  559. _bc_Free_list = temp->n_next;
  560. } else {
  561. temp = (bc_num) malloc (sizeof(bc_struct));
  562. if (temp == NULL) bc_out_of_memory ();
  563. }
  564. temp->n_sign = PLUS;
  565. temp->n_len = length;
  566. temp->n_scale = scale;
  567. temp->n_refs = 1;
  568. temp->n_ptr = NULL;
  569. temp->n_value = value;
  570. return temp;
  571. }
  572. static void
  573. _bc_simp_mul (bc_num n1, int n1len, bc_num n2, int n2len, bc_num *prod,
  574. int full_scale)
  575. {
  576. char *n1ptr, *n2ptr, *pvptr;
  577. char *n1end, *n2end; /* To the end of n1 and n2. */
  578. int indx, sum, prodlen;
  579. prodlen = n1len+n2len+1;
  580. *prod = bc_new_num (prodlen, 0);
  581. n1end = (char *) (n1->n_value + n1len - 1);
  582. n2end = (char *) (n2->n_value + n2len - 1);
  583. pvptr = (char *) ((*prod)->n_value + prodlen - 1);
  584. sum = 0;
  585. /* Here is the loop... */
  586. for (indx = 0; indx < prodlen-1; indx++)
  587. {
  588. n1ptr = (char *) (n1end - MAX(0, indx-n2len+1));
  589. n2ptr = (char *) (n2end - MIN(indx, n2len-1));
  590. while ((n1ptr >= n1->n_value) && (n2ptr <= n2end))
  591. sum += *n1ptr-- * *n2ptr++;
  592. *pvptr-- = sum % BASE;
  593. sum = sum / BASE;
  594. }
  595. *pvptr = sum;
  596. }
  597. /* A special adder/subtractor for the recursive divide and conquer
  598. multiply algorithm. Note: if sub is called, accum must
  599. be larger that what is being subtracted. Also, accum and val
  600. must have n_scale = 0. (e.g. they must look like integers. *) */
  601. static void
  602. _bc_shift_addsub (bc_num accum, bc_num val, int shift, int sub)
  603. {
  604. signed char *accp, *valp;
  605. int count, carry;
  606. count = val->n_len;
  607. if (val->n_value[0] == 0)
  608. count--;
  609. assert (accum->n_len+accum->n_scale >= shift+count);
  610. /* Set up pointers and others */
  611. accp = (signed char *)(accum->n_value +
  612. accum->n_len + accum->n_scale - shift - 1);
  613. valp = (signed char *)(val->n_value + val->n_len - 1);
  614. carry = 0;
  615. if (sub) {
  616. /* Subtraction, carry is really borrow. */
  617. while (count--) {
  618. *accp -= *valp-- + carry;
  619. if (*accp < 0) {
  620. carry = 1;
  621. *accp-- += BASE;
  622. } else {
  623. carry = 0;
  624. accp--;
  625. }
  626. }
  627. while (carry) {
  628. *accp -= carry;
  629. if (*accp < 0)
  630. *accp-- += BASE;
  631. else
  632. carry = 0;
  633. }
  634. } else {
  635. /* Addition */
  636. while (count--) {
  637. *accp += *valp-- + carry;
  638. if (*accp > (BASE-1)) {
  639. carry = 1;
  640. *accp-- -= BASE;
  641. } else {
  642. carry = 0;
  643. accp--;
  644. }
  645. }
  646. while (carry) {
  647. *accp += carry;
  648. if (*accp > (BASE-1))
  649. *accp-- -= BASE;
  650. else
  651. carry = 0;
  652. }
  653. }
  654. }
  655. /* Recursive divide and conquer multiply algorithm.
  656. Based on
  657. Let u = u0 + u1*(b^n)
  658. Let v = v0 + v1*(b^n)
  659. Then uv = (B^2n+B^n)*u1*v1 + B^n*(u1-u0)*(v0-v1) + (B^n+1)*u0*v0
  660. B is the base of storage, number of digits in u1,u0 close to equal.
  661. */
  662. static void
  663. _bc_rec_mul (bc_num u, int ulen, bc_num v, int vlen, bc_num *prod,
  664. int full_scale)
  665. {
  666. bc_num u0, u1, v0, v1;
  667. int u0len, v0len;
  668. bc_num m1, m2, m3, d1, d2;
  669. int n, prodlen, m1zero;
  670. int d1len, d2len;
  671. /* Base case? */
  672. if ((ulen+vlen) < mul_base_digits
  673. || ulen < MUL_SMALL_DIGITS
  674. || vlen < MUL_SMALL_DIGITS ) {
  675. _bc_simp_mul (u, ulen, v, vlen, prod, full_scale);
  676. return;
  677. }
  678. /* Calculate n -- the u and v split point in digits. */
  679. n = (MAX(ulen, vlen)+1) / 2;
  680. /* Split u and v. */
  681. if (ulen < n) {
  682. u1 = bc_copy_num (_zero_);
  683. u0 = new_sub_num (ulen,0, u->n_value);
  684. } else {
  685. u1 = new_sub_num (ulen-n, 0, u->n_value);
  686. u0 = new_sub_num (n, 0, u->n_value+ulen-n);
  687. }
  688. if (vlen < n) {
  689. v1 = bc_copy_num (_zero_);
  690. v0 = new_sub_num (vlen,0, v->n_value);
  691. } else {
  692. v1 = new_sub_num (vlen-n, 0, v->n_value);
  693. v0 = new_sub_num (n, 0, v->n_value+vlen-n);
  694. }
  695. _bc_rm_leading_zeros (u1);
  696. _bc_rm_leading_zeros (u0);
  697. u0len = u0->n_len;
  698. _bc_rm_leading_zeros (v1);
  699. _bc_rm_leading_zeros (v0);
  700. v0len = v0->n_len;
  701. m1zero = bc_is_zero(u1) || bc_is_zero(v1);
  702. /* Calculate sub results ... */
  703. bc_init_num(&d1);
  704. bc_init_num(&d2);
  705. bc_sub (u1, u0, &d1, 0);
  706. d1len = d1->n_len;
  707. bc_sub (v0, v1, &d2, 0);
  708. d2len = d2->n_len;
  709. /* Do recursive multiplies and shifted adds. */
  710. if (m1zero)
  711. m1 = bc_copy_num (_zero_);
  712. else
  713. _bc_rec_mul (u1, u1->n_len, v1, v1->n_len, &m1, 0);
  714. if (bc_is_zero(d1) || bc_is_zero(d2))
  715. m2 = bc_copy_num (_zero_);
  716. else
  717. _bc_rec_mul (d1, d1len, d2, d2len, &m2, 0);
  718. if (bc_is_zero(u0) || bc_is_zero(v0))
  719. m3 = bc_copy_num (_zero_);
  720. else
  721. _bc_rec_mul (u0, u0->n_len, v0, v0->n_len, &m3, 0);
  722. /* Initialize product */
  723. prodlen = ulen+vlen+1;
  724. *prod = bc_new_num(prodlen, 0);
  725. if (!m1zero) {
  726. _bc_shift_addsub (*prod, m1, 2*n, 0);
  727. _bc_shift_addsub (*prod, m1, n, 0);
  728. }
  729. _bc_shift_addsub (*prod, m3, n, 0);
  730. _bc_shift_addsub (*prod, m3, 0, 0);
  731. _bc_shift_addsub (*prod, m2, n, d1->n_sign != d2->n_sign);
  732. /* Now clean up! */
  733. bc_free_num (&u1);
  734. bc_free_num (&u0);
  735. bc_free_num (&v1);
  736. bc_free_num (&m1);
  737. bc_free_num (&v0);
  738. bc_free_num (&m2);
  739. bc_free_num (&m3);
  740. bc_free_num (&d1);
  741. bc_free_num (&d2);
  742. }
  743. /* The multiply routine. N2 times N1 is put int PROD with the scale of
  744. the result being MIN(N2 scale+N1 scale, MAX (SCALE, N2 scale, N1 scale)).
  745. */
  746. void
  747. bc_multiply (n1, n2, prod, scale)
  748. bc_num n1, n2, *prod;
  749. int scale;
  750. {
  751. bc_num pval;
  752. int len1, len2;
  753. int full_scale, prod_scale;
  754. /* Initialize things. */
  755. len1 = n1->n_len + n1->n_scale;
  756. len2 = n2->n_len + n2->n_scale;
  757. full_scale = n1->n_scale + n2->n_scale;
  758. prod_scale = MIN(full_scale,MAX(scale,MAX(n1->n_scale,n2->n_scale)));
  759. /* Do the multiply */
  760. _bc_rec_mul (n1, len1, n2, len2, &pval, full_scale);
  761. /* Assign to prod and clean up the number. */
  762. pval->n_sign = ( n1->n_sign == n2->n_sign ? PLUS : MINUS );
  763. pval->n_value = pval->n_ptr;
  764. pval->n_len = len2 + len1 + 1 - full_scale;
  765. pval->n_scale = prod_scale;
  766. _bc_rm_leading_zeros (pval);
  767. if (bc_is_zero (pval))
  768. pval->n_sign = PLUS;
  769. bc_free_num (prod);
  770. *prod = pval;
  771. }
  772. /* Some utility routines for the divide: First a one digit multiply.
  773. NUM (with SIZE digits) is multiplied by DIGIT and the result is
  774. placed into RESULT. It is written so that NUM and RESULT can be
  775. the same pointers. */
  776. static void
  777. _one_mult (num, size, digit, result)
  778. unsigned char *num;
  779. int size, digit;
  780. unsigned char *result;
  781. {
  782. int carry, value;
  783. unsigned char *nptr, *rptr;
  784. if (digit == 0)
  785. memset (result, 0, size);
  786. else
  787. {
  788. if (digit == 1)
  789. memcpy (result, num, size);
  790. else
  791. {
  792. /* Initialize */
  793. nptr = (unsigned char *) (num+size-1);
  794. rptr = (unsigned char *) (result+size-1);
  795. carry = 0;
  796. while (size-- > 0)
  797. {
  798. value = *nptr-- * digit + carry;
  799. *rptr-- = value % BASE;
  800. carry = value / BASE;
  801. }
  802. if (carry != 0) *rptr = carry;
  803. }
  804. }
  805. }
  806. /* The full division routine. This computes N1 / N2. It returns
  807. 0 if the division is ok and the result is in TQUOT. The number of
  808. digits after the decimal point is SCALE. It returns -1 if division
  809. by zero is tried. The algorithm is found in Knuth Vol 2. p237. */
  810. int
  811. bc_divide (n1, n2, quot, scale)
  812. bc_num n1, n2, *quot;
  813. int scale;
  814. {
  815. bc_num qval;
  816. unsigned char *num1, *num2;
  817. unsigned char *ptr1, *ptr2, *n2ptr, *qptr;
  818. int scale1, val;
  819. unsigned int len1, len2, scale2, qdigits, extra, count;
  820. unsigned int qdig, qguess, borrow, carry;
  821. unsigned char *mval;
  822. char zero;
  823. unsigned int norm;
  824. /* Test for divide by zero. */
  825. if (bc_is_zero (n2)) return -1;
  826. /* Test for divide by 1. If it is we must truncate. */
  827. if (n2->n_scale == 0)
  828. {
  829. if (n2->n_len == 1 && *n2->n_value == 1)
  830. {
  831. qval = bc_new_num (n1->n_len, scale);
  832. qval->n_sign = (n1->n_sign == n2->n_sign ? PLUS : MINUS);
  833. memset (&qval->n_value[n1->n_len],0,scale);
  834. memcpy (qval->n_value, n1->n_value,
  835. n1->n_len + MIN(n1->n_scale,scale));
  836. bc_free_num (quot);
  837. *quot = qval;
  838. }
  839. }
  840. /* Set up the divide. Move the decimal point on n1 by n2's scale.
  841. Remember, zeros on the end of num2 are wasted effort for dividing. */
  842. scale2 = n2->n_scale;
  843. n2ptr = (unsigned char *) n2->n_value+n2->n_len+scale2-1;
  844. while ((scale2 > 0) && (*n2ptr-- == 0)) scale2--;
  845. len1 = n1->n_len + scale2;
  846. scale1 = n1->n_scale - scale2;
  847. if (scale1 < scale)
  848. extra = scale - scale1;
  849. else
  850. extra = 0;
  851. num1 = (unsigned char *) malloc (n1->n_len+n1->n_scale+extra+2);
  852. if (num1 == NULL) bc_out_of_memory();
  853. memset (num1, 0, n1->n_len+n1->n_scale+extra+2);
  854. memcpy (num1+1, n1->n_value, n1->n_len+n1->n_scale);
  855. len2 = n2->n_len + scale2;
  856. num2 = (unsigned char *) malloc (len2+1);
  857. if (num2 == NULL) bc_out_of_memory();
  858. memcpy (num2, n2->n_value, len2);
  859. *(num2+len2) = 0;
  860. n2ptr = num2;
  861. while (*n2ptr == 0)
  862. {
  863. n2ptr++;
  864. len2--;
  865. }
  866. /* Calculate the number of quotient digits. */
  867. if (len2 > len1+scale)
  868. {
  869. qdigits = scale+1;
  870. zero = TRUE;
  871. }
  872. else
  873. {
  874. zero = FALSE;
  875. if (len2>len1)
  876. qdigits = scale+1; /* One for the zero integer part. */
  877. else
  878. qdigits = len1-len2+scale+1;
  879. }
  880. /* Allocate and zero the storage for the quotient. */
  881. qval = bc_new_num (qdigits-scale,scale);
  882. memset (qval->n_value, 0, qdigits);
  883. /* Allocate storage for the temporary storage mval. */
  884. mval = (unsigned char *) malloc (len2+1);
  885. if (mval == NULL) bc_out_of_memory ();
  886. /* Now for the full divide algorithm. */
  887. if (!zero)
  888. {
  889. /* Normalize */
  890. norm = 10 / ((int)*n2ptr + 1);
  891. if (norm != 1)
  892. {
  893. _one_mult (num1, len1+scale1+extra+1, norm, num1);
  894. _one_mult (n2ptr, len2, norm, n2ptr);
  895. }
  896. /* Initialize divide loop. */
  897. qdig = 0;
  898. if (len2 > len1)
  899. qptr = (unsigned char *) qval->n_value+len2-len1;
  900. else
  901. qptr = (unsigned char *) qval->n_value;
  902. /* Loop */
  903. while (qdig <= len1+scale-len2)
  904. {
  905. /* Calculate the quotient digit guess. */
  906. if (*n2ptr == num1[qdig])
  907. qguess = 9;
  908. else
  909. qguess = (num1[qdig]*10 + num1[qdig+1]) / *n2ptr;
  910. /* Test qguess. */
  911. if (n2ptr[1]*qguess >
  912. (num1[qdig]*10 + num1[qdig+1] - *n2ptr*qguess)*10
  913. + num1[qdig+2])
  914. {
  915. qguess--;
  916. /* And again. */
  917. if (n2ptr[1]*qguess >
  918. (num1[qdig]*10 + num1[qdig+1] - *n2ptr*qguess)*10
  919. + num1[qdig+2])
  920. qguess--;
  921. }
  922. /* Multiply and subtract. */
  923. borrow = 0;
  924. if (qguess != 0)
  925. {
  926. *mval = 0;
  927. _one_mult (n2ptr, len2, qguess, mval+1);
  928. ptr1 = (unsigned char *) num1+qdig+len2;
  929. ptr2 = (unsigned char *) mval+len2;
  930. for (count = 0; count < len2+1; count++)
  931. {
  932. val = (int) *ptr1 - (int) *ptr2-- - borrow;
  933. if (val < 0)
  934. {
  935. val += 10;
  936. borrow = 1;
  937. }
  938. else
  939. borrow = 0;
  940. *ptr1-- = val;
  941. }
  942. }
  943. /* Test for negative result. */
  944. if (borrow == 1)
  945. {
  946. qguess--;
  947. ptr1 = (unsigned char *) num1+qdig+len2;
  948. ptr2 = (unsigned char *) n2ptr+len2-1;
  949. carry = 0;
  950. for (count = 0; count < len2; count++)
  951. {
  952. val = (int) *ptr1 + (int) *ptr2-- + carry;
  953. if (val > 9)
  954. {
  955. val -= 10;
  956. carry = 1;
  957. }
  958. else
  959. carry = 0;
  960. *ptr1-- = val;
  961. }
  962. if (carry == 1) *ptr1 = (*ptr1 + 1) % 10;
  963. }
  964. /* We now know the quotient digit. */
  965. *qptr++ = qguess;
  966. qdig++;
  967. }
  968. }
  969. /* Clean up and return the number. */
  970. qval->n_sign = ( n1->n_sign == n2->n_sign ? PLUS : MINUS );
  971. if (bc_is_zero (qval)) qval->n_sign = PLUS;
  972. _bc_rm_leading_zeros (qval);
  973. bc_free_num (quot);
  974. *quot = qval;
  975. /* Clean up temporary storage. */
  976. free (mval);
  977. free (num1);
  978. free (num2);
  979. return 0; /* Everything is OK. */
  980. }
  981. /* Division *and* modulo for numbers. This computes both NUM1 / NUM2 and
  982. NUM1 % NUM2 and puts the results in TQUOT and REM, except that if TQUOT
  983. is NULL then that store will be omitted.
  984. */
  985. int
  986. bc_divmod (num1, num2, quot, rem, scale)
  987. bc_num num1, num2, *quot, *rem;
  988. int scale;
  989. {
  990. bc_num quotient = NULL;
  991. bc_num temp;
  992. int rscale;
  993. /* Check for correct numbers. */
  994. if (bc_is_zero (num2)) return -1;
  995. /* Calculate final scale. */
  996. rscale = MAX (num1->n_scale, num2->n_scale+scale);
  997. bc_init_num(&temp);
  998. /* Calculate it. */
  999. bc_divide (num1, num2, &temp, scale);
  1000. if (quot)
  1001. quotient = bc_copy_num (temp);
  1002. bc_multiply (temp, num2, &temp, rscale);
  1003. bc_sub (num1, temp, rem, rscale);
  1004. bc_free_num (&temp);
  1005. if (quot)
  1006. {
  1007. bc_free_num (quot);
  1008. *quot = quotient;
  1009. }
  1010. return 0; /* Everything is OK. */
  1011. }
  1012. /* Modulo for numbers. This computes NUM1 % NUM2 and puts the
  1013. result in RESULT. */
  1014. int
  1015. bc_modulo (num1, num2, result, scale)
  1016. bc_num num1, num2, *result;
  1017. int scale;
  1018. {
  1019. return bc_divmod (num1, num2, NULL, result, scale);
  1020. }
  1021. /* Raise BASE to the EXPO power, reduced modulo MOD. The result is
  1022. placed in RESULT. If a EXPO is not an integer,
  1023. only the integer part is used. */
  1024. int
  1025. bc_raisemod (base, expo, mod, result, scale)
  1026. bc_num base, expo, mod, *result;
  1027. int scale;
  1028. {
  1029. bc_num power, exponent, parity, temp;
  1030. int rscale;
  1031. /* Check for correct numbers. */
  1032. if (bc_is_zero(mod)) return -1;
  1033. if (bc_is_neg(expo)) return -1;
  1034. /* Set initial values. */
  1035. power = bc_copy_num (base);
  1036. exponent = bc_copy_num (expo);
  1037. temp = bc_copy_num (_one_);
  1038. bc_init_num(&parity);
  1039. /* Check the base for scale digits. */
  1040. if (base->n_scale != 0)
  1041. bc_rt_warn ("non-zero scale in base");
  1042. /* Check the exponent for scale digits. */
  1043. if (exponent->n_scale != 0)
  1044. {
  1045. bc_rt_warn ("non-zero scale in exponent");
  1046. bc_divide (exponent, _one_, &exponent, 0); /*truncate */
  1047. }
  1048. /* Check the modulus for scale digits. */
  1049. if (mod->n_scale != 0)
  1050. bc_rt_warn ("non-zero scale in modulus");
  1051. /* Do the calculation. */
  1052. rscale = MAX(scale, base->n_scale);
  1053. while ( !bc_is_zero(exponent) )
  1054. {
  1055. (void) bc_divmod (exponent, _two_, &exponent, &parity, 0);
  1056. if ( !bc_is_zero(parity) )
  1057. {
  1058. bc_multiply (temp, power, &temp, rscale);
  1059. (void) bc_modulo (temp, mod, &temp, scale);
  1060. }
  1061. bc_multiply (power, power, &power, rscale);
  1062. (void) bc_modulo (power, mod, &power, scale);
  1063. }
  1064. /* Assign the value. */
  1065. bc_free_num (&power);
  1066. bc_free_num (&exponent);
  1067. bc_free_num (result);
  1068. *result = temp;
  1069. return 0; /* Everything is OK. */
  1070. }
  1071. /* Raise NUM1 to the NUM2 power. The result is placed in RESULT.
  1072. Maximum exponent is LONG_MAX. If a NUM2 is not an integer,
  1073. only the integer part is used. */
  1074. void
  1075. bc_raise (num1, num2, result, scale)
  1076. bc_num num1, num2, *result;
  1077. int scale;
  1078. {
  1079. bc_num temp, power;
  1080. long exponent;
  1081. int rscale;
  1082. int pwrscale;
  1083. int calcscale;
  1084. char neg;
  1085. /* Check the exponent for scale digits and convert to a long. */
  1086. if (num2->n_scale != 0)
  1087. bc_rt_warn ("non-zero scale in exponent");
  1088. exponent = bc_num2long (num2);
  1089. if (exponent == 0 && (num2->n_len > 1 || num2->n_value[0] != 0))
  1090. bc_rt_error ("exponent too large in raise");
  1091. /* Special case if exponent is a zero. */
  1092. if (exponent == 0)
  1093. {
  1094. bc_free_num (result);
  1095. *result = bc_copy_num (_one_);
  1096. return;
  1097. }
  1098. /* Other initializations. */
  1099. if (exponent < 0)
  1100. {
  1101. neg = TRUE;
  1102. exponent = -exponent;
  1103. rscale = scale;
  1104. }
  1105. else
  1106. {
  1107. neg = FALSE;
  1108. rscale = MIN (num1->n_scale*exponent, MAX(scale, num1->n_scale));
  1109. }
  1110. /* Set initial value of temp. */
  1111. power = bc_copy_num (num1);
  1112. pwrscale = num1->n_scale;
  1113. while ((exponent & 1) == 0)
  1114. {
  1115. pwrscale = 2*pwrscale;
  1116. bc_multiply (power, power, &power, pwrscale);
  1117. exponent = exponent >> 1;
  1118. }
  1119. temp = bc_copy_num (power);
  1120. calcscale = pwrscale;
  1121. exponent = exponent >> 1;
  1122. /* Do the calculation. */
  1123. while (exponent > 0)
  1124. {
  1125. pwrscale = 2*pwrscale;
  1126. bc_multiply (power, power, &power, pwrscale);
  1127. if ((exponent & 1) == 1) {
  1128. calcscale = pwrscale + calcscale;
  1129. bc_multiply (temp, power, &temp, calcscale);
  1130. }
  1131. exponent = exponent >> 1;
  1132. }
  1133. /* Assign the value. */
  1134. if (neg)
  1135. {
  1136. bc_divide (_one_, temp, result, rscale);
  1137. bc_free_num (&temp);
  1138. }
  1139. else
  1140. {
  1141. bc_free_num (result);
  1142. *result = temp;
  1143. if ((*result)->n_scale > rscale)
  1144. (*result)->n_scale = rscale;
  1145. }
  1146. bc_free_num (&power);
  1147. }
  1148. /* Take the square root NUM and return it in NUM with SCALE digits
  1149. after the decimal place. */
  1150. int
  1151. bc_sqrt (num, scale)
  1152. bc_num *num;
  1153. int scale;
  1154. {
  1155. int rscale, cmp_res, done;
  1156. int cscale;
  1157. bc_num guess, guess1, point5, diff;
  1158. /* Initial checks. */
  1159. cmp_res = bc_compare (*num, _zero_);
  1160. if (cmp_res < 0)
  1161. return 0; /* error */
  1162. else
  1163. {
  1164. if (cmp_res == 0)
  1165. {
  1166. bc_free_num (num);
  1167. *num = bc_copy_num (_zero_);
  1168. return 1;
  1169. }
  1170. }
  1171. cmp_res = bc_compare (*num, _one_);
  1172. if (cmp_res == 0)
  1173. {
  1174. bc_free_num (num);
  1175. *num = bc_copy_num (_one_);
  1176. return 1;
  1177. }
  1178. /* Initialize the variables. */
  1179. rscale = MAX (scale, (*num)->n_scale);
  1180. bc_init_num(&guess);
  1181. bc_init_num(&guess1);
  1182. bc_init_num(&diff);
  1183. point5 = bc_new_num (1,1);
  1184. point5->n_value[1] = 5;
  1185. /* Calculate the initial guess. */
  1186. if (cmp_res < 0)
  1187. {
  1188. /* The number is between 0 and 1. Guess should start at 1. */
  1189. guess = bc_copy_num (_one_);
  1190. cscale = (*num)->n_scale;
  1191. }
  1192. else
  1193. {
  1194. /* The number is greater than 1. Guess should start at 10^(exp/2). */
  1195. bc_int2num (&guess,10);
  1196. bc_int2num (&guess1,(*num)->n_len);
  1197. bc_multiply (guess1, point5, &guess1, 0);
  1198. guess1->n_scale = 0;
  1199. bc_raise (guess, guess1, &guess, 0);
  1200. bc_free_num (&guess1);
  1201. cscale = 3;
  1202. }
  1203. /* Find the square root using Newton's algorithm. */
  1204. done = FALSE;
  1205. while (!done)
  1206. {
  1207. bc_free_num (&guess1);
  1208. guess1 = bc_copy_num (guess);
  1209. bc_divide (*num, guess, &guess, cscale);
  1210. bc_add (guess, guess1, &guess, 0);
  1211. bc_multiply (guess, point5, &guess, cscale);
  1212. bc_sub (guess, guess1, &diff, cscale+1);
  1213. if (bc_is_near_zero (diff, cscale))
  1214. {
  1215. if (cscale < rscale+1)
  1216. cscale = MIN (cscale*3, rscale+1);
  1217. else
  1218. done = TRUE;
  1219. }
  1220. }
  1221. /* Assign the number and clean up. */
  1222. bc_free_num (num);
  1223. bc_divide (guess,_one_,num,rscale);
  1224. bc_free_num (&guess);
  1225. bc_free_num (&guess1);
  1226. bc_free_num (&point5);
  1227. bc_free_num (&diff);
  1228. return 1;
  1229. }
  1230. /* The following routines provide output for bcd numbers package
  1231. using the rules of POSIX bc for output. */
  1232. /* This structure is used for saving digits in the conversion process. */
  1233. typedef struct stk_rec {
  1234. long digit;
  1235. struct stk_rec *next;
  1236. } stk_rec;
  1237. /* The reference string for digits. */
  1238. static char ref_str[] = "0123456789ABCDEF";
  1239. /* A special output routine for "multi-character digits." Exactly
  1240. SIZE characters must be output for the value VAL. If SPACE is
  1241. non-zero, we must output one space before the number. OUT_CHAR
  1242. is the actual routine for writing the characters. */
  1243. void
  1244. bc_out_long (val, size, space, out_char)
  1245. long val;
  1246. int size, space;
  1247. #ifdef NUMBER__STDC__
  1248. void (*out_char)(int);
  1249. #else
  1250. void (*out_char)();
  1251. #endif
  1252. {
  1253. char digits[40];
  1254. int len, ix;
  1255. if (space) (*out_char) (' ');
  1256. sprintf (digits, "%ld", val);
  1257. len = strlen (digits);
  1258. while (size > len)
  1259. {
  1260. (*out_char) ('0');
  1261. size--;
  1262. }
  1263. for (ix=0; ix < len; ix++)
  1264. (*out_char) (digits[ix]);
  1265. }
  1266. /* Output of a bcd number. NUM is written in base O_BASE using OUT_CHAR
  1267. as the routine to do the actual output of the characters. */
  1268. void
  1269. bc_out_num (num, o_base, out_char, leading_zero)
  1270. bc_num num;
  1271. int o_base;
  1272. #ifdef NUMBER__STDC__
  1273. void (*out_char)(int);
  1274. #else
  1275. void (*out_char)();
  1276. #endif
  1277. int leading_zero;
  1278. {
  1279. char *nptr;
  1280. int index, fdigit, pre_space;
  1281. stk_rec *digits, *temp;
  1282. bc_num int_part, frac_part, base, cur_dig, t_num, max_o_digit;
  1283. /* The negative sign if needed. */
  1284. if (num->n_sign == MINUS) (*out_char) ('-');
  1285. /* Output the number. */
  1286. if (bc_is_zero (num))
  1287. (*out_char) ('0');
  1288. else
  1289. if (o_base == 10)
  1290. {
  1291. /* The number is in base 10, do it the fast way. */
  1292. nptr = num->n_value;
  1293. if (num->n_len > 1 || *nptr != 0)
  1294. for (index=num->n_len; index>0; index--)
  1295. (*out_char) (BCD_CHAR(*nptr++));
  1296. else
  1297. nptr++;
  1298. if (leading_zero && bc_is_zero (num))
  1299. (*out_char) ('0');
  1300. /* Now the fraction. */
  1301. if (num->n_scale > 0)
  1302. {
  1303. (*out_char) ('.');
  1304. for (index=0; index<num->n_scale; index++)
  1305. (*out_char) (BCD_CHAR(*nptr++));
  1306. }
  1307. }
  1308. else
  1309. {
  1310. /* special case ... */
  1311. if (leading_zero && bc_is_zero (num))
  1312. (*out_char) ('0');
  1313. /* The number is some other base. */
  1314. digits = NULL;
  1315. bc_init_num (&int_part);
  1316. bc_divide (num, _one_, &int_part, 0);
  1317. bc_init_num (&frac_part);
  1318. bc_init_num (&cur_dig);
  1319. bc_init_num (&base);
  1320. bc_sub (num, int_part, &frac_part, 0);
  1321. /* Make the INT_PART and FRAC_PART positive. */
  1322. int_part->n_sign = PLUS;
  1323. frac_part->n_sign = PLUS;
  1324. bc_int2num (&base, o_base);
  1325. bc_init_num (&max_o_digit);
  1326. bc_int2num (&max_o_digit, o_base-1);
  1327. /* Get the digits of the integer part and push them on a stack. */
  1328. while (!bc_is_zero (int_part))
  1329. {
  1330. bc_modulo (int_part, base, &cur_dig, 0);
  1331. temp = (stk_rec *) malloc (sizeof(stk_rec));
  1332. if (temp == NULL) bc_out_of_memory();
  1333. temp->digit = bc_num2long (cur_dig);
  1334. temp->next = digits;
  1335. digits = temp;
  1336. bc_divide (int_part, base, &int_part, 0);
  1337. }
  1338. /* Print the digits on the stack. */
  1339. if (digits != NULL)
  1340. {
  1341. /* Output the digits. */
  1342. while (digits != NULL)
  1343. {
  1344. temp = digits;
  1345. digits = digits->next;
  1346. if (o_base <= 16)
  1347. (*out_char) (ref_str[ (int) temp->digit]);
  1348. else
  1349. bc_out_long (temp->digit, max_o_digit->n_len, 1, out_char);
  1350. free (temp);
  1351. }
  1352. }
  1353. /* Get and print the digits of the fraction part. */
  1354. if (num->n_scale > 0)
  1355. {
  1356. (*out_char) ('.');
  1357. pre_space = 0;
  1358. t_num = bc_copy_num (_one_);
  1359. while (t_num->n_len <= num->n_scale) {
  1360. bc_multiply (frac_part, base, &frac_part, num->n_scale);
  1361. fdigit = bc_num2long (frac_part);
  1362. bc_int2num (&int_part, fdigit);
  1363. bc_sub (frac_part, int_part, &frac_part, 0);
  1364. if (o_base <= 16)
  1365. (*out_char) (ref_str[fdigit]);
  1366. else {
  1367. bc_out_long (fdigit, max_o_digit->n_len, pre_space, out_char);
  1368. pre_space = 1;
  1369. }
  1370. bc_multiply (t_num, base, &t_num, 0);
  1371. }
  1372. bc_free_num (&t_num);
  1373. }
  1374. /* Clean up. */
  1375. bc_free_num (&int_part);
  1376. bc_free_num (&frac_part);
  1377. bc_free_num (&base);
  1378. bc_free_num (&cur_dig);
  1379. bc_free_num (&max_o_digit);
  1380. }
  1381. }
  1382. /* Convert a number NUM to a long. The function returns only the integer
  1383. part of the number. For numbers that are too large to represent as
  1384. a long, this function returns a zero. This can be detected by checking
  1385. the NUM for zero after having a zero returned. */
  1386. long
  1387. bc_num2long (num)
  1388. bc_num num;
  1389. {
  1390. long val;
  1391. char *nptr;
  1392. int index;
  1393. /* Extract the int value, ignore the fraction. */
  1394. val = 0;
  1395. nptr = num->n_value;
  1396. for (index=num->n_len; (index>0) && (val<=(LONG_MAX/BASE)); index--)
  1397. val = val*BASE + *nptr++;
  1398. /* Check for overflow. If overflow, return zero. */
  1399. if (index>0) val = 0;
  1400. if (val < 0) val = 0;
  1401. /* Return the value. */
  1402. if (num->n_sign == PLUS)
  1403. return (val);
  1404. else
  1405. return (-val);
  1406. }
  1407. /* Convert an integer VAL to a bc number NUM. */
  1408. void
  1409. bc_int2num (num, val)
  1410. bc_num *num;
  1411. int val;
  1412. {
  1413. char buffer[30];
  1414. char *bptr, *vptr;
  1415. int ix = 1;
  1416. char neg = 0;
  1417. /* Sign. */
  1418. if (val < 0)
  1419. {
  1420. neg = 1;
  1421. val = -val;
  1422. }
  1423. /* Get things going. */
  1424. bptr = buffer;
  1425. *bptr++ = val % BASE;
  1426. val = val / BASE;
  1427. /* Extract remaining digits. */
  1428. while (val != 0)
  1429. {
  1430. *bptr++ = val % BASE;
  1431. val = val / BASE;
  1432. ix++; /* Count the digits. */
  1433. }
  1434. /* Make the number. */
  1435. bc_free_num (num);
  1436. *num = bc_new_num (ix, 0);
  1437. if (neg) (*num)->n_sign = MINUS;
  1438. /* Assign the digits. */
  1439. vptr = (*num)->n_value;
  1440. while (ix-- > 0)
  1441. *vptr++ = *--bptr;
  1442. }
  1443. /* Convert a numbers to a string. Base 10 only.*/
  1444. char
  1445. *bc_num2str (num)
  1446. bc_num num;
  1447. {
  1448. char *str, *sptr;
  1449. char *nptr;
  1450. int index, signch;
  1451. /* Allocate the string memory. */
  1452. signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */
  1453. if (num->n_scale > 0)
  1454. str = (char *) malloc (num->n_len + num->n_scale + 2 + signch);
  1455. else
  1456. str = (char *) malloc (num->n_len + 1 + signch);
  1457. if (str == NULL) bc_out_of_memory();
  1458. /* The negative sign if needed. */
  1459. sptr = str;
  1460. if (signch) *sptr++ = '-';
  1461. /* Load the whole number. */
  1462. nptr = num->n_value;
  1463. for (index=num->n_len; index>0; index--)
  1464. *sptr++ = BCD_CHAR(*nptr++);
  1465. /* Now the fraction. */
  1466. if (num->n_scale > 0)
  1467. {
  1468. *sptr++ = '.';
  1469. for (index=0; index<num->n_scale; index++)
  1470. *sptr++ = BCD_CHAR(*nptr++);
  1471. }
  1472. /* Terminate the string and return it! */
  1473. *sptr = '\0';
  1474. return (str);
  1475. }
  1476. /* Convert strings to bc numbers. Base 10 only.*/
  1477. void
  1478. bc_str2num (num, str, scale)
  1479. bc_num *num;
  1480. char *str;
  1481. int scale;
  1482. {
  1483. int digits, strscale;
  1484. char *ptr, *nptr;
  1485. char zero_int;
  1486. /* Prepare num. */
  1487. bc_free_num (num);
  1488. /* Check for valid number and count digits. */
  1489. ptr = str;
  1490. digits = 0;
  1491. strscale = 0;
  1492. zero_int = FALSE;
  1493. if ( (*ptr == '+') || (*ptr == '-')) ptr++; /* Sign */
  1494. while (*ptr == '0') ptr++; /* Skip leading zeros. */
  1495. while (isdigit((int)*ptr)) ptr++, digits++; /* digits */
  1496. if (*ptr == '.') ptr++; /* decimal point */
  1497. while (isdigit((int)*ptr)) ptr++, strscale++; /* digits */
  1498. if ((*ptr != '\0') || (digits+strscale == 0))
  1499. {
  1500. *num = bc_copy_num (_zero_);
  1501. return;
  1502. }
  1503. /* Adjust numbers and allocate storage and initialize fields. */
  1504. strscale = MIN(strscale, scale);
  1505. if (digits == 0)
  1506. {
  1507. zero_int = TRUE;
  1508. digits = 1;
  1509. }
  1510. *num = bc_new_num (digits, strscale);
  1511. /* Build the whole number. */
  1512. ptr = str;
  1513. if (*ptr == '-')
  1514. {
  1515. (*num)->n_sign = MINUS;
  1516. ptr++;
  1517. }
  1518. else
  1519. {
  1520. (*num)->n_sign = PLUS;
  1521. if (*ptr == '+') ptr++;
  1522. }
  1523. while (*ptr == '0') ptr++; /* Skip leading zeros. */
  1524. nptr = (*num)->n_value;
  1525. if (zero_int)
  1526. {
  1527. *nptr++ = 0;
  1528. digits = 0;
  1529. }
  1530. for (;digits > 0; digits--)
  1531. *nptr++ = CH_VAL(*ptr++);
  1532. /* Build the fractional part. */
  1533. if (strscale > 0)
  1534. {
  1535. ptr++; /* skip the decimal point! */
  1536. for (;strscale > 0; strscale--)
  1537. *nptr++ = CH_VAL(*ptr++);
  1538. }
  1539. }
  1540. /* pn prints the number NUM in base 10. */
  1541. static void
  1542. out_char (int c)
  1543. {
  1544. putchar(c);
  1545. }
  1546. void
  1547. pn (num)
  1548. bc_num num;
  1549. {
  1550. bc_out_num (num, 10, out_char, 0);
  1551. out_char ('\n');
  1552. }
  1553. /* pv prints a character array as if it was a string of bcd digits. */
  1554. void
  1555. pv (name, num, len)
  1556. char *name;
  1557. unsigned char *num;
  1558. int len;
  1559. {
  1560. int i;
  1561. printf ("%s=", name);
  1562. for (i=0; i<len; i++) printf ("%c",BCD_CHAR(num[i]));
  1563. printf ("\n");
  1564. }
  1565. // vim: set et sw=2 ts=8: