|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r73612 - in sandbox/big_number: boost/math/big_number libs/math/test
From: john_at_[hidden]
Date: 2011-08-08 13:13:51
Author: johnmaddock
Date: 2011-08-08 13:13:50 EDT (Mon, 08 Aug 2011)
New Revision: 73612
URL: http://svn.boost.org/trac/boost/changeset/73612
Log:
Add ldexp and frexp.
Text files modified:
sandbox/big_number/boost/math/big_number/gmp.hpp | 24 ++++++++++++++++++++++++
sandbox/big_number/libs/math/test/test_arithmetic.cpp | 15 +++++++++++++++
2 files changed, 39 insertions(+), 0 deletions(-)
Modified: sandbox/big_number/boost/math/big_number/gmp.hpp
==============================================================================
--- sandbox/big_number/boost/math/big_number/gmp.hpp (original)
+++ sandbox/big_number/boost/math/big_number/gmp.hpp 2011-08-08 13:13:50 EDT (Mon, 08 Aug 2011)
@@ -413,6 +413,30 @@
mpf_trunc(result.backend().data(), val.backend().data());
return result;
}
+template <unsigned Digits10>
+big_number<gmp_real<Digits10> > ldexp(const big_number<gmp_real<Digits10> >& val, long e)
+{
+ big_number<gmp_real<Digits10> > result;
+ if(e > 0)
+ mpf_mul_2exp(result.backend().data(), val.backend().data(), e);
+ else if(e < 0)
+ mpf_div_2exp(result.backend().data(), val.backend().data(), -e);
+ return result;
+}
+template <unsigned Digits10>
+big_number<gmp_real<Digits10> > frexp(const big_number<gmp_real<Digits10> >& val, int* e)
+{
+ long v;
+ mpf_get_d_2exp(&v, val.backend().data());
+ *e = v;
+ return ldexp(val, -v);
+}
+template <unsigned Digits10>
+big_number<gmp_real<Digits10> > frexp(const big_number<gmp_real<Digits10> >& val, long* e)
+{
+ mpf_get_d_2exp(e, val.backend().data());
+ return ldexp(val, -*v);
+}
struct gmp_int
{
Modified: sandbox/big_number/libs/math/test/test_arithmetic.cpp
==============================================================================
--- sandbox/big_number/libs/math/test/test_arithmetic.cpp (original)
+++ sandbox/big_number/libs/math/test/test_arithmetic.cpp 2011-08-08 13:13:50 EDT (Mon, 08 Aug 2011)
@@ -85,6 +85,21 @@
BOOST_TEST(ceil(Real(-5) / 2) == -2);
BOOST_TEST(trunc(Real(5) / 2) == 2);
BOOST_TEST(trunc(Real(-5) / 2) == -2);
+
+ //
+ // ldexp and frexp, these pretty much have to implemented by each backend:
+ //
+ BOOST_TEST(ldexp(Real(2), 5) == 64);
+ BOOST_TEST(ldexp(Real(2), -5) == Real(2) / 32);
+ Real v(512);
+ int exp;
+ Real r = frexp(v, &exp);
+ BOOST_TEST(r == 0.5);
+ BOOST_TEST(exp == 10);
+ v = 1 / v;
+ r = frexp(v, &exp);
+ BOOST_TEST(r == 0.5);
+ BOOST_TEST(exp == -8);
}
template <class Real, class Num>
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