From 8468609eda9dbb6197728596a8c46d431dc6b96b Mon Sep 17 00:00:00 2001 From: ceriel Date: Tue, 4 Dec 1990 11:33:01 +0000 Subject: [PATCH] improved for numbers that can be represented exactly --- modules/src/flt_arith/.distr | 1 + modules/src/flt_arith/Makefile | 4 ++ modules/src/flt_arith/flt_str2fl.c | 65 +++++++++++++++++++++++------- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/modules/src/flt_arith/.distr b/modules/src/flt_arith/.distr index ae0649c34..1b45ce6b6 100644 --- a/modules/src/flt_arith/.distr +++ b/modules/src/flt_arith/.distr @@ -17,3 +17,4 @@ misc.h ucmp.c split.c flt_arith.3 +test.c diff --git a/modules/src/flt_arith/Makefile b/modules/src/flt_arith/Makefile index cef5f6043..c1d5ebbcf 100644 --- a/modules/src/flt_arith/Makefile +++ b/modules/src/flt_arith/Makefile @@ -26,6 +26,10 @@ OBJ = flt_ar2flt.$(SUF) flt_div.$(SUF) flt_flt2ar.$(SUF) flt_modf.$(SUF) \ all: $(LIBFLT) +test: $(LIBFLT) test.c + $(CC) -o test test.c $(LIBFLT) + ./test + $(LIBFLT): $(OBJ) rm -f $(LIBFLT) $(AR) r $(LIBFLT) $(OBJ) diff --git a/modules/src/flt_arith/flt_str2fl.c b/modules/src/flt_arith/flt_str2fl.c index 61c205e61..11551ff6a 100644 --- a/modules/src/flt_arith/flt_str2fl.c +++ b/modules/src/flt_arith/flt_str2fl.c @@ -300,6 +300,7 @@ flt_ecvt(e, ndigit, decpt, sign) static char buf[NDIGITS+1]; register char *p = buf; register char *pe; + register int findex = 0; if (ndigit < 0) ndigit = 0; if (ndigit > NDIGITS) ndigit = NDIGITS; @@ -316,21 +317,26 @@ flt_ecvt(e, ndigit, decpt, sign) if (e->m1 != 0) { register flt_arith *pp = &big_10pow[1]; + findex = 1; while (flt_cmp(e, &big_10pow[BIGSZ-1]) >= 0) { flt_mul(e,&r_big_10pow[BIGSZ-1],e); *decpt += (BIGSZ-1)*SMALLSZ; } while (flt_cmp(e,pp) >= 0) { pp++; + findex++; } - pp--; - flt_mul(e,&r_big_10pow[pp-big_10pow],e); - *decpt += (pp - big_10pow)*SMALLSZ; + findex--; + flt_mul(e,&r_big_10pow[findex],e); + *decpt += findex*SMALLSZ; pp = &s10pow[1]; - while (pp < &s10pow[SMALLSZ] && flt_cmp(e, pp) >= 0) pp++; - pp--; - flt_mul(e, &r_10pow[pp-s10pow], e); - *decpt += pp - s10pow; + findex = 1; + while (pp < &s10pow[SMALLSZ] && flt_cmp(e, pp) >= 0) { + pp++; + findex++; + } + findex--; + *decpt += findex; if (flt_cmp(e, &s10pow[0]) < 0) { while (flt_cmp(e, &r_big_10pow[BIGSZ-1]) < 0) { @@ -338,21 +344,50 @@ flt_ecvt(e, ndigit, decpt, sign) *decpt -= (BIGSZ-1)*SMALLSZ; } pp = &r_big_10pow[1]; - while(flt_cmp(e,pp) < 0) pp++; - pp--; - flt_mul(e,&big_10pow[pp-r_big_10pow],e); - *decpt -= (pp - r_big_10pow)*SMALLSZ; + findex = 1; + while(flt_cmp(e,pp) < 0) { + pp++; + findex++; + } + findex--; + flt_mul(e,&big_10pow[findex],e); + *decpt -= findex*SMALLSZ; /* here, value >= 10 ** -28 */ flt_mul(e, &s10pow[1], e); (*decpt)--; pp = &r_10pow[0]; - while(flt_cmp(e, pp) < 0) pp++; - flt_mul(e, &s10pow[pp-r_10pow], e); - *decpt -= pp - r_10pow; + findex = 0; + while(flt_cmp(e, pp) < 0) { + pp++; + findex++; + } + flt_mul(e, &s10pow[findex], e); + *decpt -= findex; + findex = 0; } (*decpt)++; /* because now value in [1.0, 10.0) */ } while (p <= pe) { + if (findex) { + flt_arith tc, oldtc; + int count = 0; + + oldtc.flt_exp = 0; + oldtc.flt_sign = 0; + oldtc.m1 = 0; + oldtc.m2 = 0; + tc = s10pow[findex]; + while (flt_cmp(e, &tc) >= 0) { + oldtc = tc; + flt_add(&tc, &s10pow[findex], &tc); + count++; + } + *p++ = count + '0'; + oldtc.flt_sign = 1; + flt_add(e, &oldtc, e); + findex--; + continue; + } if (e->flt_exp >= 0 && e->m1 != 0) { flt_arith x; @@ -367,7 +402,7 @@ flt_ecvt(e, ndigit, decpt, sign) } } else *p++ = '0'; - flt_mul(e, &s10pow[1], e); + if (e->m1) flt_mul(e, &s10pow[1], e); } if (pe >= buf) { p = pe;