Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r75279 - in sandbox/big_number/libs/multiprecision: doc test
From: john_at_[hidden]
Date: 2011-11-02 14:15:01


Author: johnmaddock
Date: 2011-11-02 14:15:00 EDT (Wed, 02 Nov 2011)
New Revision: 75279
URL: http://svn.boost.org/trac/boost/changeset/75279

Log:
Add conversion tests and update docs.
Added:
   sandbox/big_number/libs/multiprecision/test/test_gmp_conversions.cpp (contents, props changed)
   sandbox/big_number/libs/multiprecision/test/test_mpfr_conversions.cpp (contents, props changed)
Text files modified:
   sandbox/big_number/libs/multiprecision/doc/multiprecision.qbk | 43 +++++++++++++++++++++++++++++----------
   sandbox/big_number/libs/multiprecision/test/Jamfile.v2 | 13 ++++++++++++
   2 files changed, 45 insertions(+), 11 deletions(-)

Modified: sandbox/big_number/libs/multiprecision/doc/multiprecision.qbk
==============================================================================
--- sandbox/big_number/libs/multiprecision/doc/multiprecision.qbk (original)
+++ sandbox/big_number/libs/multiprecision/doc/multiprecision.qbk 2011-11-02 14:15:00 EDT (Wed, 02 Nov 2011)
@@ -115,7 +115,7 @@
 respectively. The typedef mpf_float provides a variable precision type whose precision can be controlled via the
 `mp_number`'s member functions.
 
-[note This type only provides standard library and numeric_limits support when the precision is fixed at compile time.]
+[note This type only provides standard library and `numeric_limits` support when the precision is fixed at compile time.]
 
 As well as the usual conversions from arithmetic and string types, instances of `mp_number<mpf_float<N> >` are
 copy constructible and assignable from:
@@ -149,27 +149,36 @@
    namespace boost{ namespace multiprecision{
 
    template <unsigned Digits10>
- class mpfr_float;
+ class mpfr_float_backend;
 
- typedef mp_number<mpfr_float<50> > mpfr_float_50;
- typedef mp_number<mpfr_float<100> > mpfr_float_100;
- typedef mp_number<mpfr_float<500> > mpfr_float_500;
- typedef mp_number<mpfr_float<1000> > mpfr_float_1000;
- typedef mp_number<mpfr_float<0> > mpfr_float;
+ typedef mp_number<mpfr_float_backend<50> > mpfr_float_50;
+ typedef mp_number<mpfr_float_backend<100> > mpfr_float_100;
+ typedef mp_number<mpfr_float_backend<500> > mpfr_float_500;
+ typedef mp_number<mpfr_float_backend<1000> > mpfr_float_1000;
+ typedef mp_number<mpfr_float_backend<0> > mpfr_float;
 
    }} // namespaces
 
-The `mpfr_float` backend is used in conjunction with `mp_number`: It acts as a thin wrapper around the MPFR `mpfr_t`
+The `mpfr_float_backend` type is used in conjunction with `mp_number`: It acts as a thin wrapper around the MPFR `mpfr_t`
 to provide an real-number type that is a drop-in replacement for the native C++ floating-point types, but with
 much greater precision.
 
-Type `mpfr_float` can be used at fixed precision by specifying a non-zero `Digits10` template parameter, or
+Type `mpfr_float_backend` can be used at fixed precision by specifying a non-zero `Digits10` template parameter, or
 at variable precision by setting the template argument to zero. The typedefs mpfr_float_50, mpfr_float_100,
 mpfr_float_500, mpfr_float_1000 provide arithmetic types at 50, 100, 500 and 1000 decimal digits precision
 respectively. The typedef mpfr_float provides a variable precision type whose precision can be controlled via the
 `mp_number`'s member functions.
 
-[note This type only provides numeric_limits support when the precision is fixed at compile time.]
+[note This type only provides `numeric_limits` support when the precision is fixed at compile time.]
+
+As well as the usual conversions from arithmetic and string types, instances of `mp_number<mpfr_float_backend<N> >` are
+copy constructible and assignable from:
+
+* The GMP native types `mpf_t`, `mpz_t`, `mpq_t`.
+* The MPFR native type `mpfr_t`.
+* The `mp_number` wrappers around those types: `mp_number<mpfr_float_backend<M> >`, `mp_number<mpf_float<M> >`, `mp_number<gmp_int>`, `mp_number<gmp_rational>`.
+
+It's also possible to access the underlying `mpf_t` via the data() member function of `gmp_float`.
 
 [h5 MPFR example:]
 
@@ -186,6 +195,10 @@
    std::cout << std::numeric_limits<mpfr_float_100>::digits << std::endl;
    std::cout << log(b) << std::endl; // print log(2)
 
+ // Access the underlying data:
+ mpfr_t r;
+ mpfr_set(r, b.backend().data());
+
 [h4 mp_float]
 
    namespace boost{ namespace multiprecision{
@@ -206,7 +219,7 @@
 The typedefs mp_float_50 and mp_float_100 provide arithmetic types at 50 and 100 decimal digits precision
 respectively.
 
-There is full standard library and numeric_limits support available for this type.
+There is full standard library and `numeric_limits` support available for this type.
 
 [h5 mp_float example:]
 
@@ -305,6 +318,9 @@
       int compare(const mp_number<Backend>& o)const;
       template <class V>
       typename enable_if<is_arithmetic<V>, int>::type compare(const V& o)const;
+ // Access to the underlying implementation:
+ Backend& backend();
+ const Backend& backend()const;
    };
 
    // iostream support:
@@ -408,6 +424,11 @@
 * A value greater that 0 for *this > other
 * Zero for *this == other
 
+ Backend& backend();
+ const Backend& backend()const;
+
+Returns the underlying backend instance used by *this.
+
 [h4 Iostream Support]
 
    template <class Backend>

Modified: sandbox/big_number/libs/multiprecision/test/Jamfile.v2
==============================================================================
--- sandbox/big_number/libs/multiprecision/test/Jamfile.v2 (original)
+++ sandbox/big_number/libs/multiprecision/test/Jamfile.v2 2011-11-02 14:15:00 EDT (Wed, 02 Nov 2011)
@@ -426,3 +426,16 @@
               <define>TEST_MPFR_50
          [ check-target-builds ../config//has_mpfr : : <build>no ]
         : test_atan_mpfr_50 ;
+
+run test_gmp_conversions.cpp gmp
+ : # command line
+ : # input files
+ : # requirements
+ [ check-target-builds ../config//has_gmp : : <build>no ] ;
+
+run test_mpfr_conversions.cpp gmp mpfr
+ : # command line
+ : # input files
+ : # requirements
+ [ check-target-builds ../config//has_mpfr : : <build>no ] ;
+

Added: sandbox/big_number/libs/multiprecision/test/test_gmp_conversions.cpp
==============================================================================
--- (empty file)
+++ sandbox/big_number/libs/multiprecision/test/test_gmp_conversions.cpp 2011-11-02 14:15:00 EDT (Wed, 02 Nov 2011)
@@ -0,0 +1,131 @@
+///////////////////////////////////////////////////////////////
+// Copyright Christopher Kormanyos 2002 - 2011.
+// Copyright 2011 John Maddock. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
+//
+// This work is based on an earlier work:
+// "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
+// in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
+
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/array.hpp>
+#include "test.hpp"
+
+#include <boost/multiprecision/gmp.hpp>
+
+int main()
+{
+ using namespace boost::multiprecision;
+ //
+ // Test interconversions between GMP supported backends:
+ //
+ mpf_t mpf;
+ mpz_t mpz;
+ mpq_t mpq;
+ mpf_init2(mpf, 100);
+ mpf_set_ui(mpf, 2u);
+ mpz_init(mpz);
+ mpz_set_ui(mpz, 2u);
+ mpq_init(mpq);
+ mpq_set_ui(mpq, 2u, 1u);
+
+ BOOST_TEST(mpf_float(mpf) == 2);
+ BOOST_TEST(mpf_float_50(mpf) == 2);
+ BOOST_TEST(mpf_float(mpz) == 2);
+ BOOST_TEST(mpf_float_50(mpz) == 2);
+ BOOST_TEST(mpf_float(mpq) == 2);
+ BOOST_TEST(mpf_float_50(mpq) == 2);
+
+ mpf_float f0;
+ mpf_float_50 f50;
+ f0 = mpf;
+ BOOST_TEST(f0 == 2);
+ f0 = 0;
+ f0 = mpz;
+ BOOST_TEST(f0 == 2);
+ f0 = 0;
+ f0 = mpq;
+ BOOST_TEST(f0 == 2);
+
+ f50 = mpf;
+ BOOST_TEST(f50 == 2);
+ f50 = 0;
+ f50 = mpz;
+ BOOST_TEST(f50 == 2);
+ f50 = 0;
+ f50 = mpq;
+ BOOST_TEST(f50 == 2);
+
+ f50 = 4;
+ f0 = f50;
+ BOOST_TEST(f0 == 4);
+ f0 = 3;
+ f50 = f0;
+ BOOST_TEST(f50 == 3);
+ f50 = 4;
+ BOOST_TEST(mpf_float(f50) == 4);
+ BOOST_TEST(mpf_float_50(f0) == 3);
+
+ mpz_int iz(2);
+ mpq_rational rat(2);
+ f50 = iz;
+ BOOST_TEST(f50 == 2);
+ f0 = iz;
+ BOOST_TEST(f0 == 2);
+ BOOST_TEST(mpf_float(iz) == 2);
+ BOOST_TEST(mpf_float_50(iz) == 2);
+ BOOST_TEST(mpf_float(rat) == 2);
+ BOOST_TEST(mpf_float_50(rat) == 2);
+
+ BOOST_TEST(mpz_int(mpf) == 2);
+ BOOST_TEST(mpz_int(mpz) == 2);
+ BOOST_TEST(mpz_int(mpq) == 2);
+ iz = 3;
+ iz = mpf;
+ BOOST_TEST(iz == 2);
+ iz = 3;
+ iz = mpz;
+ BOOST_TEST(iz == 2);
+ iz = 4;
+ iz = mpq;
+ BOOST_TEST(iz == 2);
+ f0 = 2;
+ f50 = 2;
+ BOOST_TEST(mpz_int(f0) == 2);
+ BOOST_TEST(mpz_int(f50) == 2);
+ rat = 2;
+ BOOST_TEST(mpz_int(rat) == 2);
+ iz = 3;
+ iz = f0;
+ BOOST_TEST(iz == 2);
+ iz = 3;
+ iz = f50;
+ BOOST_TEST(iz == 2);
+ iz = 3;
+ iz = rat;
+ BOOST_TEST(iz == 2);
+
+ BOOST_TEST(mpq_rational(mpz) == 2);
+ BOOST_TEST(mpq_rational(mpq) == 2);
+ BOOST_TEST(mpq_rational(iz) == 2);
+ rat = 3;
+ rat = mpz;
+ BOOST_TEST(rat == 2);
+ rat = 3;
+ rat = mpq;
+ BOOST_TEST(rat == 2);
+ rat = 3;
+ rat = iz;
+ BOOST_TEST(rat == 2);
+
+ iz = numerator(rat);
+ BOOST_TEST(iz == 2);
+ iz = denominator(rat);
+ BOOST_TEST(iz == 1);
+
+ return boost::report_errors();
+}
+
+
+

Added: sandbox/big_number/libs/multiprecision/test/test_mpfr_conversions.cpp
==============================================================================
--- (empty file)
+++ sandbox/big_number/libs/multiprecision/test/test_mpfr_conversions.cpp 2011-11-02 14:15:00 EDT (Wed, 02 Nov 2011)
@@ -0,0 +1,104 @@
+///////////////////////////////////////////////////////////////
+// Copyright Christopher Kormanyos 2002 - 2011.
+// Copyright 2011 John Maddock. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
+//
+// This work is based on an earlier work:
+// "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
+// in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
+
+#include <boost/detail/lightweight_test.hpp>
+#include <boost/array.hpp>
+#include "test.hpp"
+
+#include <boost/multiprecision/mpfr.hpp>
+#include <boost/multiprecision/gmp.hpp>
+
+int main()
+{
+ using namespace boost::multiprecision;
+ //
+ // Test interconversions between GMP supported backends:
+ //
+ mpf_t mpf;
+ mpz_t mpz;
+ mpq_t mpq;
+ mpfr_t mpfr;
+ mpf_init2(mpf, 100);
+ mpf_set_ui(mpf, 2u);
+ mpz_init(mpz);
+ mpz_set_ui(mpz, 2u);
+ mpq_init(mpq);
+ mpq_set_ui(mpq, 2u, 1u);
+ mpfr_init(mpfr);
+ mpfr_set_ui(mpfr, 2u, GMP_RNDN);
+
+ BOOST_TEST(mpfr_float(mpf) == 2);
+ BOOST_TEST(mpfr_float_50(mpf) == 2);
+ BOOST_TEST(mpfr_float(mpz) == 2);
+ BOOST_TEST(mpfr_float_50(mpz) == 2);
+ BOOST_TEST(mpfr_float(mpq) == 2);
+ BOOST_TEST(mpfr_float_50(mpq) == 2);
+ BOOST_TEST(mpfr_float(mpfr) == 2);
+ BOOST_TEST(mpfr_float_50(mpfr) == 2);
+
+ mpfr_float f0;
+ mpfr_float_50 f50;
+ f0 = mpf;
+ BOOST_TEST(f0 == 2);
+ f0 = 0;
+ f0 = mpz;
+ BOOST_TEST(f0 == 2);
+ f0 = 0;
+ f0 = mpq;
+ BOOST_TEST(f0 == 2);
+ f0 = mpfr;
+ BOOST_TEST(f0 == 2);
+
+ f50 = mpf;
+ BOOST_TEST(f50 == 2);
+ f50 = 0;
+ f50 = mpz;
+ BOOST_TEST(f50 == 2);
+ f50 = 0;
+ f50 = mpq;
+ BOOST_TEST(f50 == 2);
+ f50 = mpfr;
+ BOOST_TEST(f50 == 2);
+
+ f50 = 4;
+ f0 = f50;
+ BOOST_TEST(f0 == 4);
+ f0 = 3;
+ f50 = f0;
+ BOOST_TEST(f50 == 3);
+ f50 = 4;
+ BOOST_TEST(mpfr_float(f50) == 4);
+ BOOST_TEST(mpfr_float_50(f0) == 3);
+
+ mpz_int iz(2);
+ mpq_rational rat(2);
+ mpf_float gf(2);
+ f50 = 3;
+ f50 = iz;
+ BOOST_TEST(f50 == 2);
+ f50 = 3;
+ f0 = iz;
+ BOOST_TEST(f0 == 2);
+ f50 = 3;
+ f0 = gf;
+ BOOST_TEST(f0 == 2);
+ BOOST_TEST(mpfr_float(iz) == 2);
+ BOOST_TEST(mpfr_float_50(iz) == 2);
+ BOOST_TEST(mpfr_float(rat) == 2);
+ BOOST_TEST(mpfr_float_50(rat) == 2);
+ BOOST_TEST(mpfr_float(gf) == 2);
+ BOOST_TEST(mpfr_float_50(gf) == 2);
+
+
+ return boost::report_errors();
+}
+
+
+


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk