summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-03-28 15:35:10 -0400
committerJakob Kaivo <jkk@ung.org>2022-03-28 15:35:10 -0400
commit03adbf0757f7bd96576d8235c29f08e0e78d1003 (patch)
tree74bff0b998852376eaac31513e47352c1babf444
parentb1f19a49015fca42571c5d2bfea01cb52bb7e53f (diff)
modernize bigtostr()
-rw-r--r--big.c63
1 files changed, 44 insertions, 19 deletions
diff --git a/big.c b/big.c
index 04817db..d7ffd02 100644
--- a/big.c
+++ b/big.c
@@ -879,12 +879,12 @@ word strtobig(word z, int base)
return r;
}
-extern char *dicp;
-word bigtostr(x) /* number to decimal string (as Miranda list) */
-word x;
+/* number to decimal string (as Miranda list) */
+word bigtostr(word x)
{
word x1, sign, s = NIL;
+
#ifdef DEBUG
extern int debug;
if (debug & 04) { /* print octally */
@@ -892,45 +892,68 @@ word x;
sign = big_is_negative(x);
for (;;) {
word i = OCTW;
- while (i-- || d)
- s = cons('0' + (d & 07), s), d >>= 3;
+
+ while (i-- || d) {
+ s = cons('0' + (d & 07), s);
+ d >>= 3;
+ }
+
x = rest(x);
- if (x)
+ if (x) {
s = cons(' ', s), d = digit(x);
- else
+ } else {
return (sign ? cons('-', s) : s);
+ }
}
}
#endif
+
if (rest(x) == 0) {
extern char *dicp;
sprintf(dicp, "%ld", getsmallint(x));
return (str_conv(dicp));
}
+
sign = big_is_negative(x);
+
x1 = make(INT, digit0(x), 0); /* reverse x into x1 */
- while (x = rest(x))
+ while (x = rest(x)) {
x1 = make(INT, digit(x), x1);
+ }
x = x1;
- for (;;) { /* in situ division of (reversed order) x by PTEN */
- word d = digit(x), rem = d % PTEN;
+
+ /* in situ division of (reversed order) x by PTEN */
+ for (;;) {
+ word d = digit(x);
+ word rem = d % PTEN;
d = d / PTEN;
x1 = rest(x);
- if (d)
+
+ if (d) {
digit(x) = d;
- else
+ } else {
x = x1; /* remove leading zero from result */
- while (x1)
- d = rem * IBASE + digit(x1),
- digit(x1) = d / PTEN, rem = d % PTEN, x1 = rest(x1);
+ }
+
+ while (x1) {
+ d = rem * IBASE + digit(x1);
+ digit(x1) = d / PTEN;
+ rem = d % PTEN;
+ x1 = rest(x1);
+ }
+
/* end of in situ division (also uses x1 as temporary) */
if (x) {
word i = TENW;
- while (i--)
- s = cons('0' + rem % 10, s), rem = rem / 10;
+ while (i--) {
+ s = cons('0' + rem % 10, s);
+ rem = rem / 10;
+ }
} else {
- while (rem)
- s = cons('0' + rem % 10, s), rem = rem / 10;
+ while (rem) {
+ s = cons('0' + rem % 10, s);
+ rem = rem / 10;
+ }
return (sign ? cons('-', s) : s);
}
}
@@ -939,6 +962,7 @@ word x;
word bigtostrx(x) /* integer to hexadecimal string (as Miranda list) */
word x;
{
+ extern char *dicp;
word r = NIL, s = big_is_negative(x);
while (x) {
word count = 4; /* 60 bits => 20 octal digits => 4 bignum digits */
@@ -965,6 +989,7 @@ word x;
word bigtostr8(x) /* integer to octal string (as Miranda list) */
word x;
{
+ extern char *dicp;
word r = NIL, s = big_is_negative(x);
while (x) {
char *q = dicp + 5;