Browse Source

real: fix a rounding bug in real number printing.

legacy
Sam Hocevar sam 13 years ago
parent
commit
578b35c806
1 changed files with 8 additions and 23 deletions
  1. +8
    -23
      src/real.cpp

+ 8
- 23
src/real.cpp View File

@@ -1268,35 +1268,20 @@ void real::print(int ndigits) const
}

/* Normalise x so that mantissa is in [1..9.999] */
int exponent = 0;
if (x.m_signexp)
/* FIXME: better use int64_t when the cast is implemented */
int exponent = ceil(log10(x));
x /= pow(R_10, (real)exponent);

if (x < R_1)
{
for (real div = R_1, newdiv; true; div = newdiv)
{
newdiv = div * R_10;
if (x < newdiv)
{
x /= div;
break;
}
exponent++;
}
for (real mul = 1, newx; true; mul *= R_10)
{
newx = x * mul;
if (newx >= R_1)
{
x = newx;
break;
}
exponent--;
}
x *= R_10;
exponent--;
}

/* Print digits */
for (int i = 0; i < ndigits; i++)
{
int digit = (int)x;
int digit = (int)floor(x);
printf("%i", digit);
if (i == 0)
printf(".");


Loading…
Cancel
Save