Commit 95b93c28 authored by Ken Thompson's avatar Ken Thompson

1. got 29 (Mpscale) more bits of precision

out of floating constant multiply
2. added rounding code to "const fix=float"
to allow up to 29 (Mpscale) bits of
slop and still get an exact fixed constant.

fixes #931

R=rsc
CC=golang-dev
https://golang.org/cl/1692055
parent b693847e
......@@ -156,10 +156,11 @@ mpmovefixflt(Mpflt *a, Mpint *b)
// convert (truncate) b to a.
// return -1 (but still convert) if b was non-integer.
int
mpmovefltfix(Mpint *a, Mpflt *b)
static int
mpexactfltfix(Mpint *a, Mpflt *b)
{
Mpflt f;
*a = b->val;
mpshiftfix(a, b->exp);
if(b->exp < 0) {
......@@ -172,6 +173,35 @@ mpmovefltfix(Mpint *a, Mpflt *b)
return 0;
}
int
mpmovefltfix(Mpint *a, Mpflt *b)
{
Mpflt f;
int i;
if(mpexactfltfix(a, b) == 0)
return 0;
// try rounding down a little
f = *b;
f.val.a[0] = 0;
if(mpexactfltfix(a, &f) == 0)
return 0;
// try rounding up a little
for(i=1; i<Mpprec; i++) {
f.val.a[i]++;
if(f.val.a[i] != Mpbase)
break;
f.val.a[i] = 0;
}
mpnorm(&f);
if(mpexactfltfix(a, &f) == 0)
return 0;
return -1;
}
void
mpmovefixfix(Mpint *a, Mpint *b)
{
......
......@@ -319,7 +319,11 @@ mpmulfract(Mpint *a, Mpint *b)
s.neg = 0;
mpmovecfix(&q, 0);
for(i=0; i<Mpprec; i++) {
x = *--a1;
if(x != 0)
yyerror("mpmulfract not normal");
for(i=0; i<Mpprec-1; i++) {
x = *--a1;
if(x == 0) {
mprshw(&s);
......
......@@ -109,7 +109,7 @@ mpmulfltflt(Mpflt *a, Mpflt *b)
}
mpmulfract(&a->val, &b->val);
a->exp = (a->exp + b->exp) + Mpscale*Mpprec - 1;
a->exp = (a->exp + b->exp) + Mpscale*Mpprec - Mpscale - 1;
mpnorm(a);
if(Mpdebug)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment