diff options
author | Jakob Kaivo <jkk@ung.org> | 2022-03-28 15:17:32 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2022-03-28 15:17:32 -0400 |
commit | 7cfbe579dc19483b1ad3aab549e285704a6e6258 (patch) | |
tree | 0e400f7260a7d186522880710e2a687098c1ba58 | |
parent | 51d1dee7efbf0e76febfcfc03b32a2f5733461d2 (diff) |
modernize bigscan()
-rw-r--r-- | big.c | 64 |
1 files changed, 40 insertions, 24 deletions
@@ -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 */ |