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

mercurial at gmplib.org mercurial at gmplib.org
Sat May 18 00:06:19 CEST 2013


details:   /var/hg/gmp-5.1/rev/ef1495feb4fe
changeset: 15419:ef1495feb4fe
user:      Niels Möller
date:      Sat May 18 00:06:11 2013 +0200
description:
Make test case less dependent on float precision.

diffstat:

 ChangeLog           |   6 ++++++
 tests/mpq/t-get_d.c |  46 +++++++++++++++++++++++++++++++---------------
 2 files changed, 37 insertions(+), 15 deletions(-)

diffs (96 lines):

diff -r 453e6006ff0b -r ef1495feb4fe ChangeLog
--- a/ChangeLog	Thu May 16 20:16:11 2013 +0200
+++ b/ChangeLog	Sat May 18 00:06:11 2013 +0200
@@ -71,6 +71,12 @@
 	the mini-gmp allocation functions, including uses of mpz_get_str
 	for various test failure messages.
 
+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 453e6006ff0b -r ef1495feb4fe tests/mpq/t-get_d.c
--- a/tests/mpq/t-get_d.c	Thu May 16 20:16:11 2013 +0200
+++ b/tests/mpq/t-get_d.c	Sat May 18 00:06:11 2013 +0200
@@ -1,7 +1,7 @@
 /* Test mpq_get_d and mpq_set_d
 
-Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002, 2003, 2012 Free Software
-Foundation, Inc.
+Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002, 2003, 2012, 2013 Free
+Software Foundation, Inc.
 
 This file is part of the GNU MP Library test suite.
 
@@ -163,37 +163,53 @@
 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;
 
   if (argc == 2)
      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