[Gmp-commit] /var/hg/gmp: mpz/rootrem.c: Correctly handle odd roots of negati...

mercurial at gmplib.org mercurial at gmplib.org
Fri May 18 16:13:11 CEST 2012


details:   /var/hg/gmp/rev/a898dcc37122
changeset: 14985:a898dcc37122
user:      Marco Bodrato <bodrato at mail.dm.unipi.it>
date:      Fri May 18 16:13:07 2012 +0200
description:
mpz/rootrem.c: Correctly handle odd roots of negatives.

diffstat:

 ChangeLog          |   5 ++++-
 mpz/rootrem.c      |  10 +++++-----
 tests/mpz/t-root.c |  11 +++++++++--
 3 files changed, 18 insertions(+), 8 deletions(-)

diffs (77 lines):

diff -r c894673fe97a -r a898dcc37122 ChangeLog
--- a/ChangeLog	Fri May 18 12:29:07 2012 +0200
+++ b/ChangeLog	Fri May 18 16:13:07 2012 +0200
@@ -15,7 +15,10 @@
 	* tests/mpz/t-get_d_2exp.c (check_zero): New check.
 	* tests/mpz/t-inp_str.c: A few more cases.
 	* tests/mpz/t-cmp_d.c: More bases and symbols, a few cases.
-	
+
+	* mpz/rootrem.c: Correctly handle odd roots of negatives.
+	* tests/mpz/t-root.c: Test it.
+
 2012-05-16  Torbjorn Granlund  <tege at gmplib.org>
 
 	* tests/mpf/t-eq.c (check_random): New function, meat from old main().
diff -r c894673fe97a -r a898dcc37122 mpz/rootrem.c
--- a/mpz/rootrem.c	Fri May 18 12:29:07 2012 +0200
+++ b/mpz/rootrem.c	Fri May 18 16:13:07 2012 +0200
@@ -1,7 +1,7 @@
-/* mpz_rootrem(root, rem, u, nth) --  Set ROOT to floor(U^(1/nth)) and
+/* mpz_rootrem(root, rem, u, nth) --  Set ROOT to trunc(U^(1/nth)) and
    set REM to the remainder.
 
-Copyright 1999, 2000, 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
+Copyright 1999, 2000, 2001, 2002, 2003, 2005, 2012 Free Software Foundation, Inc.
 
 This file is part of the GNU MP Library.
 
@@ -81,10 +81,10 @@
       SIZ(root) = us >= 0 ? rootn : -rootn;
       if (u == root)
 	MPN_COPY (up, rootp, rootn);
-      else if (u == rem)
-	MPN_COPY (up, remp, remn);
     }
 
-  SIZ(rem) = remn;
+  if (u == rem)
+    MPN_COPY (up, remp, remn);
+  SIZ(rem) = us >= 0 ? remn : -remn;
   TMP_FREE;
 }
diff -r c894673fe97a -r a898dcc37122 tests/mpz/t-root.c
--- a/tests/mpz/t-root.c	Fri May 18 12:29:07 2012 +0200
+++ b/tests/mpz/t-root.c	Fri May 18 16:13:07 2012 +0200
@@ -49,7 +49,7 @@
   mpz_add (temp2, temp, rem2);
 
   /* Is power of result > argument?  */
-  if (mpz_cmp (root1, root2) != 0 || mpz_cmp (x2, temp2) != 0 || mpz_cmp (temp, x2) > 0)
+  if (mpz_cmp (root1, root2) != 0 || mpz_cmp (x2, temp2) != 0 || mpz_cmpabs (temp, x2) > 0)
     {
       fprintf (stderr, "ERROR after test %d\n", i);
       debug_mp (x2, 10);
@@ -68,7 +68,7 @@
       abort ();
     }
 
-  if (nth <= 10000)		/* skip too expensive test */
+  if (nth <= 10000 && mpz_sgn(x2) > 0)		/* skip too expensive test */
     {
       mpz_add_ui (temp2, root1, 1L);
       mpz_pow_ui (temp2, temp2, nth);
@@ -150,6 +150,13 @@
 	}
 
       check_one (root1, x2, nth, i);
+
+      if (((nth & 1) != 0) && ((bsi & 2) != 0))
+	{
+	  mpz_neg (x2, x2);
+	  mpz_neg (root1, root1);
+	  check_one (root1, x2, nth, i);
+	}
     }
 
   mpz_clear (bs);


More information about the gmp-commit mailing list