diff --git a/modules/src/flt_arith/Makefile b/modules/src/flt_arith/Makefile index 5457b1c54..b76f9d962 100644 --- a/modules/src/flt_arith/Makefile +++ b/modules/src/flt_arith/Makefile @@ -26,8 +26,8 @@ OBJ = flt_ar2flt.$(SUF) flt_div.$(SUF) flt_flt2ar.$(SUF) flt_modf.$(SUF) \ all: $(LIBFLT) -test: $(LIBFLT) test.c - $(CC) -o tst test.c $(LIBFLT) +test: $(LIBFLT) test.o + $(CC) -o tst test.o $(LIBFLT) ./tst $(LIBFLT): $(OBJ) diff --git a/modules/src/flt_arith/flt_ar2flt.c b/modules/src/flt_arith/flt_ar2flt.c index 376181012..8b3c4edf0 100644 --- a/modules/src/flt_arith/flt_ar2flt.c +++ b/modules/src/flt_arith/flt_ar2flt.c @@ -21,22 +21,20 @@ flt_arith2flt(n, e, uns) n = -n; } else e->flt_sign = 0; - e->m1 = e->m2 = 0; + switch(sizeof(arith)) { + case 4: + e->m1 = n; e->m2 = 0; break; + default: + e->m1 = (n >> (sizeof(arith)*8 - 32)) & 0xFFFFFFFF; + e->m2 = n << 32; + break; + } if (n == 0) { e->flt_exp = 0; return; } e->flt_exp = 63; - for (i = 64; i > 0 && n != 0; i--) { - flt_b64_sft(&(e->flt_mantissa),1); - e->m1 |= (n & 1) << 31; - n = (n >> 1) & ~(0x80L << 8*(sizeof(arith)-1)); - } - - if (i > 0) { - flt_b64_sft(&(e->flt_mantissa), i); - } flt_status = 0; flt_nrm(e); } diff --git a/modules/src/flt_arith/flt_str2fl.c b/modules/src/flt_arith/flt_str2fl.c index 0c86aead2..6612639c9 100644 --- a/modules/src/flt_arith/flt_str2fl.c +++ b/modules/src/flt_arith/flt_str2fl.c @@ -288,23 +288,21 @@ flt_str2flt(s, e) flt_chk(e); } -#define NDIGITS 128 +#define NDIG 18 static char * -flt_ecvt(e, ndigit, decpt, sign) +flt_ecvt(e, decpt, sign) register flt_arith *e; - int ndigit, *decpt, *sign; + int *decpt, *sign; { /* Like ecvt(), but for extended precision */ - static char buf[NDIGITS+1]; + static char buf[NDIG+1]; register char *p = buf; register char *pe; register int findex = 0; - if (ndigit < 0) ndigit = 0; - if (ndigit > NDIGITS) ndigit = NDIGITS; - pe = &buf[ndigit]; + pe = &buf[NDIG]; buf[0] = '\0'; *sign = 0; @@ -416,6 +414,7 @@ flt_ecvt(e, ndigit, decpt, sign) } } *pe = '\0'; + while (--pe > buf && *pe == '0') *pe = '\0'; } return buf; } @@ -425,7 +424,6 @@ flt_flt2str(e, buf, bufsize) char *buf; { -#define NDIG 19 int sign, dp; register int i; register char *s1; @@ -435,14 +433,17 @@ flt_flt2str(e, buf, bufsize) e1 = *e; flt_status = 0; - s1 = flt_ecvt(&e1,NDIG,&dp,&sign); + s1 = flt_ecvt(&e1,&dp,&sign); if (sign) *s++ = '-'; *s++ = *s1++; *s++ = '.'; for (i = NDIG-1; i > 0; i--) { if (*s1) *s++ = *s1++; - else *s++ = '0'; + else { + if (i == NDIG-1) *s++ = '0'; + break; + } } if (e->m1 | e->m2) { --dp ; diff --git a/modules/src/flt_arith/test.c b/modules/src/flt_arith/test.c index 35904edbe..bf9c43c69 100644 --- a/modules/src/flt_arith/test.c +++ b/modules/src/flt_arith/test.c @@ -5,17 +5,20 @@ struct tests { int oper; char *result; } tests[] = { - { "1.0", 0, 0, "1.000000000000000000" }, - { "-1.0", 0, 0, "-1.000000000000000000" }, - { "0.0", 0, 0, "0.000000000000000000" }, - { "1.234567", 0, 0, "1.234567000000000000" }, - { "1.234567", 0, 'D', "1.000000000000000000" }, - { "1.234567", 0, 'R', "2.345670000000000000e-1" }, - { "32768", "32768", '+', "6.553600000000000000e+4" }, - { "32768", "32767", '-', "1.000000000000000000" }, - { "32768", "32768", '*', "1.073741824000000000e+9" }, - { "32768", "32768", '/', "1.000000000000000000" }, - { "1.234567e20", "-1.234567e20", '+', "0.000000000000000000" }, + { "1.0", 0, 0, "1.0" }, + { "-1.0", 0, 0, "-1.0" }, + { "0.0", 0, 0, "0.0" }, + { "1.234567", 0, 0, "1.234567" }, + { "1.234567", 0, 'D', "1.0" }, + { "1.234567", 0, 'R', "2.34567e-1" }, + { "32768", "32768", '+', "6.5536e+4" }, + { "32768", "32767", '-', "1.0" }, + { "32768", "32768", '*', "1.073741824e+9" }, + { "32768", "32768", '/', "1.0" }, + { "1.234567e20", "-1.234567e20", '+', "0.0" }, + { "1e100", "1e100", '+', "2.0e+100" }, + { "1e110", "10", '*', "1.0e+111" }, + { "0.5e100", "0.5e100", '*', "2.5e+199" }, { 0, 0, 0, 0} }; @@ -79,6 +82,6 @@ dotest(p) if (! strcmp(buf, p->result)) return 1; - printf("Test number %d failed\n", testno); + printf("Test number %d failed: result = %s, should be %s\n", testno, buf, p->result); return 0; }