[Gmp-commit] /var/hg/gmp: Add C++ functions gcd and lcm.

mercurial at gmplib.org mercurial at gmplib.org
Tue Jun 17 19:35:03 UTC 2014


details:   /var/hg/gmp/rev/30b6a3b346bc
changeset: 16442:30b6a3b346bc
user:      Marc Glisse <marc.glisse at inria.fr>
date:      Tue Jun 17 21:34:59 2014 +0200
description:
Add C++ functions gcd and lcm.

diffstat:

 ChangeLog           |   7 +++++++
 NEWS                |   6 ++++++
 doc/gmp.texi        |   3 +++
 gmpxx.h             |  38 ++++++++++++++++++++++++++++++++++++++
 tests/cxx/t-ops2.cc |  12 ++++++++++++
 5 files changed, 66 insertions(+), 0 deletions(-)

diffs (120 lines):

diff -r 7d46dceebe4c -r 30b6a3b346bc ChangeLog
--- a/ChangeLog	Mon Jun 16 15:46:27 2014 +0200
+++ b/ChangeLog	Tue Jun 17 21:34:59 2014 +0200
@@ -1,3 +1,10 @@
+2014-06-17  Marc Glisse  <marc.glisse at inria.fr>
+
+	* gmpxx.h (__gmp_gcd_function, __gmp_lcm_function): New classes.
+	(gcd, lcm): New functions.
+	* doc/gmp.texi (C++ Interface Integers): Document them.
+	* tests/cxx/t-ops2.cc (checkz): Test them.
+
 2014-06-16  Torbjörn Granlund  <tege at gmplib.org>
 
 	* mpn/generic/perfpow.c (perfpow): Combine TMP_ALLOCs.
diff -r 7d46dceebe4c -r 30b6a3b346bc NEWS
--- a/NEWS	Mon Jun 16 15:46:27 2014 +0200
+++ b/NEWS	Tue Jun 17 21:34:59 2014 +0200
@@ -4,6 +4,12 @@
 medium, provided this notice is preserved.
 
 
+Changes between GMP version 6.0.* and 6.1.0
+
+  FEATURES
+  * New C++ functions gcd and lcm for mpz_class.
+
+
 Changes between GMP version 5.1.* and 6.0.0
 
   BUGS FIXED
diff -r 7d46dceebe4c -r 30b6a3b346bc doc/gmp.texi
--- a/doc/gmp.texi	Mon Jun 16 15:46:27 2014 +0200
+++ b/doc/gmp.texi	Tue Jun 17 21:34:59 2014 +0200
@@ -6843,6 +6843,9 @@
 @deftypefunx int sgn (mpz_class @var{op})
 @deftypefunx mpz_class sqrt (mpz_class @var{op})
 @maybepagebreak
+ at deftypefunx mpz_class gcd (mpz_class @var{op1}, mpz_class @var{op2})
+ at deftypefunx mpz_class lcm (mpz_class @var{op1}, mpz_class @var{op2})
+ at maybepagebreak
 @deftypefunx void mpz_class::swap (mpz_class& @var{op})
 @deftypefunx void swap (mpz_class& @var{op1}, mpz_class& @var{op2})
 These functions provide a C++ class interface to the corresponding GMP C
diff -r 7d46dceebe4c -r 30b6a3b346bc gmpxx.h
--- a/gmpxx.h	Mon Jun 16 15:46:27 2014 +0200
+++ b/gmpxx.h	Tue Jun 17 21:34:59 2014 +0200
@@ -1105,6 +1105,42 @@
   { return -mpf_cmp_d(f, d); }
 };
 
+struct __gmp_gcd_function
+{
+  static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
+  { mpz_gcd(z, w, v); }
+  static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
+  { mpz_gcd_ui(z, w, l); }
+  static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
+  { eval(z, w, l); }
+  static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
+  { eval(z, w, __gmpxx_abs_ui(l)); }
+  static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
+  { eval(z, w, l); }
+  static void eval(mpz_ptr z, mpz_srcptr w, double d)
+  {  __GMPXX_TMPZ_D;    mpz_gcd (z, w, temp); }
+  static void eval(mpz_ptr z, double d, mpz_srcptr w)
+  { eval(z, w, d); }
+};
+
+struct __gmp_lcm_function
+{
+  static void eval(mpz_ptr z, mpz_srcptr w, mpz_srcptr v)
+  { mpz_lcm(z, w, v); }
+  static void eval(mpz_ptr z, mpz_srcptr w, unsigned long int l)
+  { mpz_lcm_ui(z, w, l); }
+  static void eval(mpz_ptr z, unsigned long int l, mpz_srcptr w)
+  { eval(z, w, l); }
+  static void eval(mpz_ptr z, mpz_srcptr w, signed long int l)
+  { eval(z, w, __gmpxx_abs_ui(l)); }
+  static void eval(mpz_ptr z, signed long int l, mpz_srcptr w)
+  { eval(z, w, l); }
+  static void eval(mpz_ptr z, mpz_srcptr w, double d)
+  {  __GMPXX_TMPZ_D;    mpz_lcm (z, w, temp); }
+  static void eval(mpz_ptr z, double d, mpz_srcptr w)
+  { eval(z, w, d); }
+};
+
 struct __gmp_rand_function
 {
   static void eval(mpz_ptr z, gmp_randstate_t s, mp_bitcnt_t l)
@@ -2984,6 +3020,8 @@
 __GMP_DEFINE_UNARY_FUNCTION(ceil, __gmp_ceil_function)
 __GMP_DEFINE_UNARY_FUNCTION(sqrt, __gmp_sqrt_function)
 __GMP_DEFINE_BINARY_FUNCTION(hypot, __gmp_hypot_function)
+__GMP_DEFINE_BINARY_FUNCTION(gcd, __gmp_gcd_function)
+__GMP_DEFINE_BINARY_FUNCTION(lcm, __gmp_lcm_function)
 
 __GMP_DEFINE_UNARY_TYPE_FUNCTION(int, sgn, __gmp_sgn_function)
 __GMP_DEFINE_BINARY_TYPE_FUNCTION(int, cmp, __gmp_cmp_function)
diff -r 7d46dceebe4c -r 30b6a3b346bc tests/cxx/t-ops2.cc
--- a/tests/cxx/t-ops2.cc	Mon Jun 16 15:46:27 2014 +0200
+++ b/tests/cxx/t-ops2.cc	Tue Jun 17 21:34:59 2014 +0200
@@ -132,6 +132,18 @@
   ASSERT_ALWAYS(mpz_class(1)+DBL_MAX>2);
   ASSERT_ALWAYS(mpz_class(1)+DBL_MIN<2);
   ASSERT_ALWAYS(mpz_class(1)+std::numeric_limits<double>::denorm_min()<2);
+  ASSERT_ALWAYS(gcd(mpz_class(6),mpz_class(8))==2);
+  ASSERT_ALWAYS(gcd(-mpz_class(6),mpz_class(8))==2);
+  ASSERT_ALWAYS(gcd(-mpz_class(6),-mpz_class(8))==2);
+  ASSERT_ALWAYS(gcd(mpz_class(6),8.f)==2);
+  ASSERT_ALWAYS(gcd(-mpz_class(6),static_cast<unsigned char>(8))==2);
+  ASSERT_ALWAYS(gcd(static_cast<long>(-6),mpz_class(5)+3)==2);
+  ASSERT_ALWAYS(lcm(mpz_class(6),mpz_class(8))==24);
+  ASSERT_ALWAYS(lcm(-mpz_class(6),mpz_class(8))==24);
+  ASSERT_ALWAYS(lcm(-mpz_class(6),-mpz_class(8))==24);
+  ASSERT_ALWAYS(lcm(mpz_class(6),static_cast<short>(8))==24);
+  ASSERT_ALWAYS(lcm(-mpz_class(6),static_cast<unsigned char>(8))==24);
+  ASSERT_ALWAYS(lcm(-6.,mpz_class(5)+3)==24);
 }
 
 template<class T>


More information about the gmp-commit mailing list