summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-03-28 14:49:10 -0400
committerJakob Kaivo <jkk@ung.org>2022-03-28 14:49:10 -0400
commit495690d6be9325a5eeae02412adb1a0cfa622432 (patch)
treeff8a146d82a76ad1a2ab7b3d80143283d369f57f
parent579c7a34c96e51429a8cba7e9bfb5e4c23b37a78 (diff)
modernize shortdiv()
-rw-r--r--big.c55
1 files changed, 35 insertions, 20 deletions
diff --git a/big.c b/big.c
index fc9a452..806cd11 100644
--- a/big.c
+++ b/big.c
@@ -438,29 +438,44 @@ word bigmod(word x, word y)
magnitudes invariant under change of sign, remainder has sign of
dividend, quotient negative if signs of divi(sor/dend) mixed */
-word shortdiv(x, n) /* divide big x by single digit n returning big quotient
- and setting external b_rem as side effect */
- /* may assume - x>=0,n>0 */
-word x, n;
-{
- word d = digit(x), s_rem, q = 0;
- while (x = rest(x)) /* reverse rest(x) into q */
- q = make(INT, d, q), d = digit(x); /* leaving most sig. digit in d */
- {
- word tmp;
- x = q;
+/* divide big x by single digit n returning big quotient
+ and setting external b_rem as side effect */
+/* may assume - x>=0,n>0 */
+word shortdiv(word x, word n)
+{
+ word d = digit(x);
+ word q = 0;
+
+ /* reverse rest(x) into q */
+ while (x = rest(x)) {
+ q = make(INT, d, q);
+ d = digit(x); /* leaving most sig. digit in d */
+ }
+
+ x = q;
+ word s_rem = d % n;
+ d = d / n;
+
+ /* put back first digit (if not leading 0) */
+ if (d || !q) {
+ q = make(INT, d, 0);
+ } else {
+ q = 0;
+ }
+
+ /* in situ division of q by n AND destructive reversal */
+ while (x) {
+ d = s_rem * IBASE + digit(x);
+ digit(x) = d / n;
s_rem = d % n;
- d = d / n;
- if (d || !q)
- q = make(INT, d, 0); /* put back first digit (if not leading 0) */
- else
- q = 0;
- while (x) /* in situ division of q by n AND destructive reversal */
- d = s_rem * IBASE + digit(x), digit(x) = d / n, s_rem = d % n,
- tmp = x, x = rest(x), rest(tmp) = q, q = tmp;
+ word tmp = x;
+ x = rest(x);
+ rest(tmp) = q;
+ q = tmp;
}
+
b_rem = make(INT, s_rem, 0);
- return (q);
+ return q;
}
word longdiv(x, y) /* divide big x by big y returning quotient, leaving