diff options
author | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2017-12-09 21:59:21 +0000 |
---|---|---|
committer | zimmerma <zimmerma@280ebfd0-de03-0410-8827-d642c229c3f4> | 2017-12-09 21:59:21 +0000 |
commit | 4b96c94982e139915c74515ca987bc88c51a454f (patch) | |
tree | ffda61acddc0687fded4a4442a748bed549cb0d2 | |
parent | 0512b1c5116267a1e64d8239dad3c6de3f0f359f (diff) | |
download | mpfr-4b96c94982e139915c74515ca987bc88c51a454f.tar.gz |
[src/get_q.c] make result of mpfr_get_q in canonical form
[tests/tget_q.c] added non-regression test
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@11951 280ebfd0-de03-0410-8827-d642c229c3f4
-rw-r--r-- | src/get_q.c | 1 | ||||
-rw-r--r-- | tests/tget_q.c | 34 |
2 files changed, 35 insertions, 0 deletions
diff --git a/src/get_q.c b/src/get_q.c index bdd274c39..9fe6eef91 100644 --- a/src/get_q.c +++ b/src/get_q.c @@ -56,6 +56,7 @@ mpfr_get_q (mpq_ptr q, mpfr_srcptr f) MPFR_ASSERTN (-exp <= (mp_bitcnt_t) -1); mpz_mul_2exp (v, v, -exp); } + mpq_canonicalize (q); } } #endif diff --git a/tests/tget_q.c b/tests/tget_q.c index fa2d5bb8b..8a577ac36 100644 --- a/tests/tget_q.c +++ b/tests/tget_q.c @@ -100,6 +100,38 @@ random_tests (void) mpfr_clear (g); } +/* Check results are in canonical form. + See https://sympa.inria.fr/sympa/arc/mpfr/2017-12/msg00029.html */ +static void +check_canonical (void) +{ + mpfr_t x; + mpq_t q; + mpz_t z; + + mpfr_init2 (x, 53); + mpfr_set_ui (x, 3, MPFR_RNDN); + mpq_init (q); + mpfr_get_q (q, x); + /* check the denominator is positive */ + if (mpz_sgn (mpq_denref (q)) <= 0) + { + printf ("Error, the denominator of mpfr_get_q should be positive\n"); + MPFR_ASSERTN (0); + } + mpz_init (z); + mpz_gcd (z, mpq_numref (q), mpq_denref (q)); + /* check the numerator and denominator are coprime */ + if (mpz_cmp_ui (z, 1) != 0) + { + printf ("Error, numerator and denominator of mpfr_get_q should be coprime\n"); + MPFR_ASSERTN (0); + } + mpfr_clear (x); + mpq_clear (q); + mpz_clear (z); +} + int main (void) { @@ -108,6 +140,8 @@ main (void) special (); random_tests (); + check_canonical (); + tests_end_mpfr (); return 0; } |