summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2018-04-26 15:17:30 +0000
committervlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4>2018-04-26 15:17:30 +0000
commitf329fe20abb1cbfda97ce2ac9208e517c9ba97c1 (patch)
tree04e0ba4ec60f8c751046b602a72bd91147d69fe4
parent9b6f4ff7400be62af83e94a365068435d30dbd5b (diff)
downloadmpfr-f329fe20abb1cbfda97ce2ac9208e517c9ba97c1.tar.gz
[src/gmp_op.c] Fixed a bug in mpfr_cmp_q on NaN rational: the NaN flag
was set by mpfr_set_q, but the flags were not restored just after. [tests/tgmpop.c] For mpfr_cmp_q, improved the test where x is NaN by checking all the flags (not just erange) and added a test where y is a NaN rational. (merged changesets r12343,12677 from the trunk) git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/branches/4.0@12679 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r--src/gmp_op.c6
-rw-r--r--tests/tgmpop.c31
2 files changed, 32 insertions, 5 deletions
diff --git a/src/gmp_op.c b/src/gmp_op.c
index eab65479c..dc00527f5 100644
--- a/src/gmp_op.c
+++ b/src/gmp_op.c
@@ -455,8 +455,12 @@ mpfr_cmp_q (mpfr_srcptr x, mpq_srcptr q)
if (MPFR_UNLIKELY (mpz_sgn (mpq_denref (q)) == 0))
{
/* q is an infinity or NaN */
- mpfr_init2 (t, 2);
+ mpfr_flags_t old_flags;
+
+ mpfr_init2 (t, MPFR_PREC_MIN);
+ old_flags = __gmpfr_flags;
mpfr_set_q (t, q, MPFR_RNDN);
+ __gmpfr_flags = old_flags;
res = mpfr_cmp (x, t);
mpfr_clear (t);
return res;
diff --git a/tests/tgmpop.c b/tests/tgmpop.c
index 27a169026..f1a36fbd2 100644
--- a/tests/tgmpop.c
+++ b/tests/tgmpop.c
@@ -307,16 +307,39 @@ test_cmp_q (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax)
mpfr_init2 (z, MPFR_PREC_MIN);
mpq_init (y);
- /* check the erange flag when x is NaN */
+ /* Check the flags when x is NaN: the erange flags must be set, and
+ only this one. */
mpfr_set_nan (x);
mpq_set_ui (y, 17, 1);
- mpfr_clear_erangeflag ();
+ mpfr_clear_flags ();
res1 = mpfr_cmp_q (x, y);
- if (res1 != 0 || mpfr_erangeflag_p () == 0)
+ if (res1 != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE)
{
printf ("Error for mpfr_cmp_q (NaN, 17)\n");
printf ("Return value: expected 0, got %d\n", res1);
- printf ("Erange flag: expected set, got %d\n", mpfr_erangeflag_p ());
+ printf ("Expected flags:");
+ flags_out (MPFR_FLAGS_ERANGE);
+ printf ("Got flags: ");
+ flags_out (__gmpfr_flags);
+ exit (1);
+ }
+
+ /* Check the flags when y is NaN: the erange flags must be set, and
+ only this one. */
+ mpfr_set_ui (x, 42, MPFR_RNDN);
+ /* A NaN rational is represented by 0/0 (MPFR extension). */
+ mpz_set_ui (mpq_numref (y), 0);
+ mpz_set_ui (mpq_denref (y), 0);
+ mpfr_clear_flags ();
+ res1 = mpfr_cmp_q (x, y);
+ if (res1 != 0 || __gmpfr_flags != MPFR_FLAGS_ERANGE)
+ {
+ printf ("Error for mpfr_cmp_q (42, NaN)\n");
+ printf ("Return value: expected 0, got %d\n", res1);
+ printf ("Expected flags:");
+ flags_out (MPFR_FLAGS_ERANGE);
+ printf ("Got flags: ");
+ flags_out (__gmpfr_flags);
exit (1);
}