div.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /* div.c: bcmath library file. */
  2. /*
  3. Copyright (C) 1991, 1992, 1993, 1994, 1997 Free Software Foundation, Inc.
  4. Copyright (C) 2000 Philip A. Nelson
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details. (LICENSE)
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to:
  15. The Free Software Foundation, Inc.
  16. 59 Temple Place, Suite 330
  17. Boston, MA 02111-1307 USA.
  18. You may contact the author by:
  19. e-mail: philnelson@acm.org
  20. us-mail: Philip A. Nelson
  21. Computer Science Department, 9062
  22. Western Washington University
  23. Bellingham, WA 98226-9062
  24. *************************************************************************/
  25. #include <config.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <stdarg.h>
  30. #include "bcmath.h"
  31. #include "private.h"
  32. /* Some utility routines for the divide: First a one digit multiply.
  33. NUM (with SIZE digits) is multiplied by DIGIT and the result is
  34. placed into RESULT. It is written so that NUM and RESULT can be
  35. the same pointers. */
  36. static void _one_mult (unsigned char *num, int size, int digit, unsigned char *result)
  37. {
  38. int carry, value;
  39. unsigned char *nptr, *rptr;
  40. if (digit == 0)
  41. memset (result, 0, size);
  42. else
  43. {
  44. if (digit == 1)
  45. memcpy (result, num, size);
  46. else
  47. {
  48. /* Initialize */
  49. nptr = (unsigned char *) (num+size-1);
  50. rptr = (unsigned char *) (result+size-1);
  51. carry = 0;
  52. while (size-- > 0)
  53. {
  54. value = *nptr-- * digit + carry;
  55. *rptr-- = value % BASE;
  56. carry = value / BASE;
  57. }
  58. if (carry != 0) *rptr = carry;
  59. }
  60. }
  61. }
  62. /* The full division routine. This computes N1 / N2. It returns
  63. 0 if the division is ok and the result is in QUOT. The number of
  64. digits after the decimal point is SCALE. It returns -1 if division
  65. by zero is tried. The algorithm is found in Knuth Vol 2. p237. */
  66. int
  67. bc_divide (bc_num n1, bc_num n2, bc_num *quot, int scale)
  68. {
  69. bc_num qval;
  70. unsigned char *num1, *num2;
  71. unsigned char *ptr1, *ptr2, *n2ptr, *qptr;
  72. int scale1, val;
  73. unsigned int len1, len2, scale2, qdigits, extra, count;
  74. unsigned int qdig, qguess, borrow, carry;
  75. unsigned char *mval;
  76. char zero;
  77. unsigned int norm;
  78. /* Test for divide by zero. */
  79. if (bc_is_zero (n2)) return -1;
  80. /* Test for divide by 1. If it is we must truncate. */
  81. if (n2->n_scale == 0)
  82. {
  83. if (n2->n_len == 1 && *n2->n_value == 1)
  84. {
  85. qval = bc_new_num (n1->n_len, scale);
  86. qval->n_sign = (n1->n_sign == n2->n_sign ? PLUS : MINUS);
  87. memset (&qval->n_value[n1->n_len],0,scale);
  88. memcpy (qval->n_value, n1->n_value,
  89. n1->n_len + MIN(n1->n_scale,scale));
  90. bc_free_num (quot);
  91. *quot = qval;
  92. }
  93. }
  94. /* Set up the divide. Move the decimal point on n1 by n2's scale.
  95. Remember, zeros on the end of num2 are wasted effort for dividing. */
  96. scale2 = n2->n_scale;
  97. n2ptr = (unsigned char *) n2->n_value+n2->n_len+scale2-1;
  98. while ((scale2 > 0) && (*n2ptr-- == 0)) scale2--;
  99. len1 = n1->n_len + scale2;
  100. scale1 = n1->n_scale - scale2;
  101. if (scale1 < scale)
  102. extra = scale - scale1;
  103. else
  104. extra = 0;
  105. num1 = (unsigned char *) safe_emalloc (1, n1->n_len+n1->n_scale, extra+2);
  106. memset (num1, 0, n1->n_len+n1->n_scale+extra+2);
  107. memcpy (num1+1, n1->n_value, n1->n_len+n1->n_scale);
  108. len2 = n2->n_len + scale2;
  109. num2 = (unsigned char *) safe_emalloc (1, len2, 1);
  110. memcpy (num2, n2->n_value, len2);
  111. *(num2+len2) = 0;
  112. n2ptr = num2;
  113. while (*n2ptr == 0)
  114. {
  115. n2ptr++;
  116. len2--;
  117. }
  118. /* Calculate the number of quotient digits. */
  119. if (len2 > len1+scale)
  120. {
  121. qdigits = scale+1;
  122. zero = TRUE;
  123. }
  124. else
  125. {
  126. zero = FALSE;
  127. if (len2>len1)
  128. qdigits = scale+1; /* One for the zero integer part. */
  129. else
  130. qdigits = len1-len2+scale+1;
  131. }
  132. /* Allocate and zero the storage for the quotient. */
  133. qval = bc_new_num (qdigits-scale,scale);
  134. memset (qval->n_value, 0, qdigits);
  135. /* Allocate storage for the temporary storage mval. */
  136. mval = (unsigned char *) safe_emalloc (1, len2, 1);
  137. /* Now for the full divide algorithm. */
  138. if (!zero)
  139. {
  140. /* Normalize */
  141. norm = 10 / ((int)*n2ptr + 1);
  142. if (norm != 1)
  143. {
  144. _one_mult (num1, len1+scale1+extra+1, norm, num1);
  145. _one_mult (n2ptr, len2, norm, n2ptr);
  146. }
  147. /* Initialize divide loop. */
  148. qdig = 0;
  149. if (len2 > len1)
  150. qptr = (unsigned char *) qval->n_value+len2-len1;
  151. else
  152. qptr = (unsigned char *) qval->n_value;
  153. /* Loop */
  154. while (qdig <= len1+scale-len2)
  155. {
  156. /* Calculate the quotient digit guess. */
  157. if (*n2ptr == num1[qdig])
  158. qguess = 9;
  159. else
  160. qguess = (num1[qdig]*10 + num1[qdig+1]) / *n2ptr;
  161. /* Test qguess. */
  162. if (n2ptr[1]*qguess >
  163. (num1[qdig]*10 + num1[qdig+1] - *n2ptr*qguess)*10
  164. + num1[qdig+2])
  165. {
  166. qguess--;
  167. /* And again. */
  168. if (n2ptr[1]*qguess >
  169. (num1[qdig]*10 + num1[qdig+1] - *n2ptr*qguess)*10
  170. + num1[qdig+2])
  171. qguess--;
  172. }
  173. /* Multiply and subtract. */
  174. borrow = 0;
  175. if (qguess != 0)
  176. {
  177. *mval = 0;
  178. _one_mult (n2ptr, len2, qguess, mval+1);
  179. ptr1 = (unsigned char *) num1+qdig+len2;
  180. ptr2 = (unsigned char *) mval+len2;
  181. for (count = 0; count < len2+1; count++)
  182. {
  183. val = (int) *ptr1 - (int) *ptr2-- - borrow;
  184. if (val < 0)
  185. {
  186. val += 10;
  187. borrow = 1;
  188. }
  189. else
  190. borrow = 0;
  191. *ptr1-- = val;
  192. }
  193. }
  194. /* Test for negative result. */
  195. if (borrow == 1)
  196. {
  197. qguess--;
  198. ptr1 = (unsigned char *) num1+qdig+len2;
  199. ptr2 = (unsigned char *) n2ptr+len2-1;
  200. carry = 0;
  201. for (count = 0; count < len2; count++)
  202. {
  203. val = (int) *ptr1 + (int) *ptr2-- + carry;
  204. if (val > 9)
  205. {
  206. val -= 10;
  207. carry = 1;
  208. }
  209. else
  210. carry = 0;
  211. *ptr1-- = val;
  212. }
  213. if (carry == 1) *ptr1 = (*ptr1 + 1) % 10;
  214. }
  215. /* We now know the quotient digit. */
  216. *qptr++ = qguess;
  217. qdig++;
  218. }
  219. }
  220. /* Clean up and return the number. */
  221. qval->n_sign = ( n1->n_sign == n2->n_sign ? PLUS : MINUS );
  222. if (bc_is_zero (qval)) qval->n_sign = PLUS;
  223. _bc_rm_leading_zeros (qval);
  224. bc_free_num (quot);
  225. *quot = qval;
  226. /* Clean up temporary storage. */
  227. efree (mval);
  228. efree (num1);
  229. efree (num2);
  230. return 0; /* Everything is OK. */
  231. }