[Gmp-commit] /var/hg/gmp: Make test case less dependent on float precision.

mercurial at gmplib.org mercurial at gmplib.org
Wed Feb 20 22:02:16 CET 2013


details:   /var/hg/gmp/rev/d5352c3cce8b
changeset: 15468:d5352c3cce8b
user:      Niels M?ller <nisse at lysator.liu.se>
date:      Wed Feb 20 22:01:47 2013 +0100
description:
Make test case less dependent on float precision.

diffstat:

 ChangeLog           |   6 ++++++
 tests/mpq/t-get_d.c |  43 ++++++++++++++++++++++++++++++-------------
 2 files changed, 36 insertions(+), 13 deletions(-)

diffs (84 lines):

diff -r 693ac3cb4f8d -r d5352c3cce8b ChangeLog
--- a/ChangeLog	Tue Feb 19 16:29:41 2013 +0100
+++ b/ChangeLog	Wed Feb 20 22:01:47 2013 +0100
@@ -1,3 +1,9 @@
+2013-02-20  Niels Möller  <nisse at lysator.liu.se>
+
+	* tests/mpq/t-get_d.c (check_random): Rewrote to make test less
+	dependent on float operations. Fixes problem with m68k-linux and
+	extended float precision.
+
 2013-02-19 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
         * mini-gmp/mini-gmp.c: Move asserts to work-around a compiler bug.
diff -r 693ac3cb4f8d -r d5352c3cce8b tests/mpq/t-get_d.c
--- a/tests/mpq/t-get_d.c	Tue Feb 19 16:29:41 2013 +0100
+++ b/tests/mpq/t-get_d.c	Wed Feb 20 22:01:47 2013 +0100
@@ -163,9 +163,13 @@
 void
 check_random (int argc, char **argv)
 {
-  double d, d2, nd, dd;
+  gmp_randstate_ptr rands = RANDS;
+  
+  double d;
   mpq_t q;
-  mp_limb_t rp[LIMBS_PER_DOUBLE + 1];
+  mpz_t a, t;
+  int exp;
+
   int test, reps = 100000;
   int i;
 
@@ -173,27 +177,40 @@
      reps = 100 * atoi (argv[1]);
 
   mpq_init (q);
-
+  mpz_init (a);
+  mpz_init (t);
+  
   for (test = 0; test < reps; test++)
     {
-      mpn_random2 (rp, LIMBS_PER_DOUBLE + 1);
-      d = 0.0;
-      for (i = LIMBS_PER_DOUBLE - 1; i >= 0; i--)
-	d = d * MP_BASE_AS_DOUBLE + rp[i];
-      d = my_ldexp (d, (int) (rp[LIMBS_PER_DOUBLE] % (2 * MAXEXP)) - MAXEXP);
+      mpz_rrandomb (a, rands, 53);
+      mpz_urandomb (t, rands, 32);
+      exp = mpz_get_ui (t) % (2*MAXEXP) - MAXEXP;
+
+      d = my_ldexp (mpz_get_d (a), exp);
       mpq_set_d (q, d);
-      nd = mpz_get_d (mpq_numref (q));
-      dd = mpz_get_d (mpq_denref (q));
-      d2 = nd / dd;
-      if (d != d2)
+      /* Check that n/d = a * 2^exp, or
+	 d*a 2^{exp} = n */
+      mpz_mul (t, a, mpq_denref (q));
+      if (exp > 0)
+	mpz_mul_2exp (t, t, exp);
+      else
 	{
+	  if (!mpz_divisible_2exp_p (t, -exp))
+	    goto fail;
+	  mpz_div_2exp (t, t, -exp);
+	}
+      if (mpz_cmp (t, mpq_numref (q)) != 0)
+	{
+	fail:
 	  printf ("ERROR (check_random test %d): bad mpq_set_d results\n", test);
 	  printf ("%.16g\n", d);
-	  printf ("%.16g\n", d2);
+	  gmp_printf ("%Qd\n", q);
 	  abort ();
 	}
     }
   mpq_clear (q);
+  mpz_clear (t);
+  mpz_clear (a);
 }
 
 void


More information about the gmp-commit mailing list