From 7cfbe579dc19483b1ad3aab549e285704a6e6258 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Mon, 28 Mar 2022 15:17:32 -0400 Subject: modernize bigscan() --- big.c | 64 ++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/big.c b/big.c index 24e5278..bb72224 100644 --- a/big.c +++ b/big.c @@ -362,7 +362,7 @@ word bigdiv(word x, word y) word s2 = s1; /* make x,y positive and remember signs */ - if (s1 = big_is_negative(y)) { + if (s1) { y = make(INT, digit0(y), rest(y)); } @@ -673,7 +673,7 @@ word dbltobig(double x) the exact integers. There are inherent deficiences in 64 bit fp, no point in trying to mask this */ -static double big__log(word x, double logbase, const char *name, double (*fn)(double)) +static double big__log(word x, double logbase, char *name, double (*fn)(double)) { word n = 0; double r = digit(x); @@ -713,37 +713,53 @@ double biglog10(word x) return big__log(x, log10IBASE, "log10", log10); } -word bigscan(p) /* read a big number (in decimal) */ - /* NB does NOT check for malformed number, assumes already done */ -char *p; /* p is a pointer to a null terminated string of digits */ +/* read a big number (in decimal) */ +/* NB does NOT check for malformed number, assumes already done */ +/* p is a pointer to a null terminated string of digits */ +word bigscan(char *p) { - word s = 0, r = make(INT, 0, 0); - if (*p == '-') - s = 1, p++; /* optional leading `-' (for NUMVAL) */ + word s = 0; + word r = make(INT, 0, 0); + + /* optional leading `-' (for NUMVAL) */ + if (*p == '-') { + s = 1; + p++; + } + while (*p) { word d = *p - '0', f = 10; p++; - while (*p && f < PTEN) - d = 10 * d + *p - '0', f = 10 * f, p++; + + while (*p && f < PTEN) { + d = 10 * d + *p - '0'; + f = 10 * f; + p++; + } + /* rest of loop does r=f*r+d; (in situ) */ d = f * digit(r) + d; - { - word carry = d >> DIGITWIDTH; - word *x = &rest(r); - digit(r) = d & MAXDIGIT; - while (*x) - d = f * digit(*x) + carry, - digit(*x) = d & MAXDIGIT, carry = d >> DIGITWIDTH, x = &rest(*x); - if (carry) - *x = make(INT, carry, 0); + + word carry = d >> DIGITWIDTH; + word *x = &rest(r); + digit(r) = d & MAXDIGIT; + while (*x) { + d = f * digit(*x) + carry; + digit(*x) = d & MAXDIGIT; + carry = d >> DIGITWIDTH; + x = &rest(*x); + } + + if (carry) { + *x = make(INT, carry, 0); } } -/*if(*p=='e') - { int s=bigscan(p+1); - r = bigtimes(r,bigpow(make(INT,10,0),s); } */ - if (s && !bigzero(r)) + + if (s && !bigzero(r)) { digit(r) = digit(r) | SIGNBIT; - return (r); + } + + return r; } /* code to handle (unsigned) exponent commented out */ -- cgit v1.2.1