[Gmp-commit] /var/hg/gmp: Reject negative operands for primorial, after Marco...

mercurial at gmplib.org mercurial at gmplib.org
Tue Nov 10 21:20:43 UTC 2015


details:   /var/hg/gmp/rev/7db76d280783
changeset: 16957:7db76d280783
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Tue Nov 10 22:20:39 2015 +0100
description:
Reject negative operands for primorial, after Marco's suggestion.

diffstat:

 ChangeLog            |   5 +++++
 gmpxx.h              |  12 +++++-------
 tests/cxx/t-ops2z.cc |   6 ++++--
 3 files changed, 14 insertions(+), 9 deletions(-)

diffs (56 lines):

diff -r 1974b38cf7e8 -r 7db76d280783 ChangeLog
--- a/ChangeLog	Tue Nov 10 05:50:45 2015 +0100
+++ b/ChangeLog	Tue Nov 10 22:20:39 2015 +0100
@@ -1,3 +1,8 @@
+2015-11-10  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (__gmp_primorial_function): Throw on negative operands.
+	* tests/cxx/t-ops2z.cc: Test it.
+
 2015-11-09 Marco Bodrato <bodrato at mail.dm.unipi.it>
 
 	* mpz: Experimental, lazy allocation.
diff -r 1974b38cf7e8 -r 7db76d280783 gmpxx.h
--- a/gmpxx.h	Tue Nov 10 05:50:45 2015 +0100
+++ b/gmpxx.h	Tue Nov 10 22:20:39 2015 +0100
@@ -1222,19 +1222,17 @@
   static void eval(mpz_ptr z, signed long l)
   {
     if (l < 0)
-      mpz_set_ui(z, 1);
-    else
-      eval(z, static_cast<unsigned long>(l));
+      throw std::domain_error ("primorial(negative)");
+    eval(z, static_cast<unsigned long>(l));
   }
   static void eval(mpz_ptr z, mpz_srcptr w)
   {
     if (!mpz_fits_ulong_p(w))
       if (mpz_sgn(w) < 0)
-	mpz_set_ui(z, 1);
+	throw std::domain_error ("primorial(negative)");
       else
-	throw std::bad_alloc(); // or std::overflow_error ("factorial")?
-    else
-      eval(z, mpz_get_ui(w));
+	throw std::bad_alloc(); // or std::overflow_error ("primorial")?
+    eval(z, mpz_get_ui(w));
   }
   static void eval(mpz_ptr z, double d)
   {  __GMPXX_TMPZ_D;    eval (z, temp); }
diff -r 1974b38cf7e8 -r 7db76d280783 tests/cxx/t-ops2z.cc
--- a/tests/cxx/t-ops2z.cc	Tue Nov 10 05:50:45 2015 +0100
+++ b/tests/cxx/t-ops2z.cc	Tue Nov 10 22:20:39 2015 +0100
@@ -99,8 +99,10 @@
   ASSERT_ALWAYS(mpz_class::primorial(3)==6);
   ASSERT_ALWAYS(mpz_class::primorial(3ul)==6);
   ASSERT_ALWAYS(mpz_class::primorial(3.f)==6);
-  ASSERT_ALWAYS(mpz_class::primorial(-mpz_class(3))==1);
-  ASSERT_ALWAYS(mpz_class::primorial(-5)==1);
+  try { ret=primorial(-mpz_class(3)); ASSERT_ALWAYS(0); }
+  catch (std::domain_error) {}
+  try { ret=mpz_class::primorial(-5); ASSERT_ALWAYS(0); }
+  catch (std::domain_error) {}
   try { ret=primorial(mpz_class(1)<<300); ASSERT_ALWAYS(0); }
   catch (std::bad_alloc) {}
 }


More information about the gmp-commit mailing list