From 578b35c80660548b0bdea799db46f8d1b96ca0bd Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 2 Jan 2012 03:14:18 +0000 Subject: [PATCH] real: fix a rounding bug in real number printing. --- src/real.cpp | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/src/real.cpp b/src/real.cpp index 836cec86..6319b5a3 100644 --- a/src/real.cpp +++ b/src/real.cpp @@ -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(".");