summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2021-04-23 08:22:25 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2021-04-23 08:22:25 +0000
commit94e93e6acb6da4a5bb7c17a73ff506a860878d41 (patch)
treeed41608a010395fbd79cba7a7dfd37c2aae5cf7a
parent027e70786131be4c0ac09a54068496634e7e772e (diff)
downloadmpfr-94e93e6acb6da4a5bb7c17a73ff506a860878d41.tar.gz
Fixed bug in mpfr_get_str_ndigits.
* src/get_str.c: use MPFR_SAVE_EXPO_MARK / MPFR_SAVE_EXPO_FREE to fix 2 issues in mpfr_get_str_ndigits: the inexact flag could be raised (bug reported by Pierre Chatelier[*]); undefined behavior in a very reduced exponent range. * tests/tget_str.c: check the above issues in the tests. [*] https://sympa.inria.fr/sympa/arc/mpfr/2021-04/msg00000.html (merged changeset r14488 from the trunk) git-svn-id: https://scm.gforge.inria.fr/anonscm/svn/mpfr/branches/4.1@14490 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--src/get_str.c17
-rw-r--r--tests/tget_str.c61
2 files changed, 59 insertions, 19 deletions
diff --git a/src/get_str.c b/src/get_str.c
index f9e4d779c..11c85f946 100644
--- a/src/get_str.c
+++ b/src/get_str.c
@@ -2484,6 +2484,8 @@ mpfr_ceil_mul (mpfr_exp_t e, int beta, int i)
size_t
mpfr_get_str_ndigits (int b, mpfr_prec_t p)
{
+ MPFR_SAVE_EXPO_DECL (expo);
+
MPFR_ASSERTN (2 <= b && b <= 62);
/* deal first with power of two bases, since even for those, mpfr_ceil_mul
@@ -2497,17 +2499,26 @@ mpfr_get_str_ndigits (int b, mpfr_prec_t p)
return 1 + (p + k - 2) / k;
}
+ MPFR_SAVE_EXPO_MARK (expo);
+
/* the value returned by mpfr_ceil_mul is guaranteed to be
1 + ceil(p*log(2)/log(b)) for p < 186564318007 (it returns one more
for p=186564318007 and b=7 or 49) */
MPFR_STAT_STATIC_ASSERT (MPFR_PREC_BITS >= 64 || MPFR_PREC_BITS <= 32);
+ if
#if MPFR_PREC_BITS >= 64
/* 64-bit numbers are supported by the C implementation, so that we can
use the large constant below. If MPFR_PREC_BITS <= 32, the condition
is always satisfied, so that we do not need any test. */
- if (MPFR_LIKELY (p < 186564318007))
+ (MPFR_LIKELY (p < 186564318007))
+#else
+ (1)
#endif
- return 1 + mpfr_ceil_mul (IS_POW2(b) ? p - 1 : p, b, 1);
+ {
+ size_t ret = 1 + mpfr_ceil_mul (IS_POW2(b) ? p - 1 : p, b, 1);
+ MPFR_SAVE_EXPO_FREE (expo);
+ return ret;
+ }
/* Now p is large and b is not a power of two. The code below works for any
value of p and b, as long as b is not a power of two. Indeed, in such a
@@ -2541,6 +2552,8 @@ mpfr_get_str_ndigits (int b, mpfr_prec_t p)
mpfr_clear (d);
mpfr_clear (u);
}
+
+ MPFR_SAVE_EXPO_FREE (expo);
return 1 + ret;
}
}
diff --git a/tests/tget_str.c b/tests/tget_str.c
index c6a31de6e..4181a28c8 100644
--- a/tests/tget_str.c
+++ b/tests/tget_str.c
@@ -1311,6 +1311,33 @@ coverage (void)
mpfr_clear (x);
}
+static void
+test_ndigits_aux (int b, mpfr_prec_t p, size_t expected_m)
+{
+ size_t m;
+ mpfr_exp_t old_emin, old_emax, e[] = { MPFR_EMIN_MIN, 0, MPFR_EMAX_MAX };
+ mpfr_flags_t flags;
+ int i;
+
+ old_emin = mpfr_get_emin ();
+ old_emax = mpfr_get_emax ();
+
+ i = randlimb () % (numberof (e) + 1);
+ if (i < numberof (e))
+ {
+ set_emin (e[i]);
+ set_emax (e[i]);
+ }
+
+ __gmpfr_flags = flags = randlimb () & MPFR_FLAGS_ALL;
+ m = mpfr_get_str_ndigits (b, p);
+ MPFR_ASSERTN (m == expected_m);
+ MPFR_ASSERTN (__gmpfr_flags == flags);
+
+ set_emin (old_emin);
+ set_emax (old_emax);
+}
+
/* test of mpfr_get_str_ndigits */
static void
test_ndigits (void)
@@ -1319,61 +1346,61 @@ test_ndigits (void)
/* for b=2, we have 1 + ceil((p-1)*log(2)/log(b)) = p */
for (p = MPFR_PREC_MIN; p <= 1024; p++)
- MPFR_ASSERTN(mpfr_get_str_ndigits (2, p) == p);
+ test_ndigits_aux (2, p, p);
/* for b=4, we have 1 + ceil((p-1)*log(2)/log(b)) = 1 + ceil((p-1)/2)
= 1 + floor(p/2) */
for (p = MPFR_PREC_MIN; p <= 1024; p++)
- MPFR_ASSERTN(mpfr_get_str_ndigits (4, p) == 1 + (p / 2));
+ test_ndigits_aux (4, p, 1 + (p / 2));
/* for b=8, we have 1 + ceil((p-1)*log(2)/log(b)) = 1 + ceil((p-1)/3)
= 1 + floor((p+1)/3) */
for (p = MPFR_PREC_MIN; p <= 1024; p++)
- MPFR_ASSERTN(mpfr_get_str_ndigits (8, p) == 1 + ((p + 1) / 3));
+ test_ndigits_aux (8, p, 1 + ((p + 1) / 3));
/* for b=16, we have 1 + ceil((p-1)*log(2)/log(b)) = 1 + ceil((p-1)/4)
= 1 + floor((p+2)/4) */
for (p = MPFR_PREC_MIN; p <= 1024; p++)
- MPFR_ASSERTN(mpfr_get_str_ndigits (16, p) == 1 + ((p + 2) / 4));
+ test_ndigits_aux (16, p, 1 + ((p + 2) / 4));
/* for b=32, we have 1 + ceil((p-1)*log(2)/log(b)) = 1 + ceil((p-1)/5)
= 1 + floor((p+3)/5) */
for (p = MPFR_PREC_MIN; p <= 1024; p++)
- MPFR_ASSERTN(mpfr_get_str_ndigits (32, p) == 1 + ((p + 3) / 5));
+ test_ndigits_aux (32, p, 1 + ((p + 3) / 5));
/* error < 1e-3 */
- MPFR_ASSERTN(mpfr_get_str_ndigits (57, 35) == 8);
+ test_ndigits_aux (57, 35, 8);
/* error < 1e-4 */
- MPFR_ASSERTN(mpfr_get_str_ndigits (31, 649) == 133);
+ test_ndigits_aux (31, 649, 133);
/* error < 1e-5 */
- MPFR_ASSERTN(mpfr_get_str_ndigits (43, 5041) == 931);
+ test_ndigits_aux (43, 5041, 931);
/* error < 1e-6 */
- MPFR_ASSERTN(mpfr_get_str_ndigits (41, 17771) == 3319);
+ test_ndigits_aux (41, 17771, 3319);
/* 20th convergent of log(2)/log(3) */
- MPFR_ASSERTN(mpfr_get_str_ndigits (3, 630138897) == 397573381);
+ test_ndigits_aux (3, 630138897, 397573381);
#if MPFR_PREC_BITS >= 64
/* 21st convergent of log(2)/log(3) */
- MPFR_ASSERTN(mpfr_get_str_ndigits (3, 9809721694) == 6189245292);
+ test_ndigits_aux (3, 9809721694, 6189245292);
/* 22nd convergent of log(2)/log(3) */
- MPFR_ASSERTN(mpfr_get_str_ndigits (3, 10439860591) == 6586818672);
+ test_ndigits_aux (3, 10439860591, 6586818672);
/* 23rd convergent of log(2)/log(3) */
- MPFR_ASSERTN(mpfr_get_str_ndigits (3, 103768467013) == 65470613322);
+ test_ndigits_aux (3, 103768467013, 65470613322);
/* 24th convergent of log(2)/log(3) */
- MPFR_ASSERTN(mpfr_get_str_ndigits (3, 217976794617) == 137528045314);
+ test_ndigits_aux (3, 217976794617, 137528045314);
- MPFR_ASSERTN(mpfr_get_str_ndigits (3, 1193652440098) == 753110839882);
+ test_ndigits_aux (3, 1193652440098, 753110839882);
- MPFR_ASSERTN(mpfr_get_str_ndigits (3, 683381996816440) == 431166034846569);
+ test_ndigits_aux (3, 683381996816440, 431166034846569);
- MPFR_ASSERTN(mpfr_get_str_ndigits (7, 186564318007) == 66455550933);
+ test_ndigits_aux (7, 186564318007, 66455550933);
#endif
}