/usr/include/boost/multiprecision
Edit: /usr/include/boost/multiprecision/mpfr.hpp (128421B)
///////////////////////////////////////////////////////////////////////////////
// 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_0.txt)
#ifndef BOOST_MATH_BN_MPFR_HPP
#define BOOST_MATH_BN_MPFR_HPP
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#ifndef BOOST_MULTIPRECISION_MPFR_DEFAULT_PRECISION
#define BOOST_MULTIPRECISION_MPFR_DEFAULT_PRECISION 20
#endif
namespace boost {
namespace multiprecision {
enum mpfr_allocation_type
{
allocate_stack,
allocate_dynamic
};
namespace backends {
template
struct mpfr_float_backend;
template <>
struct mpfr_float_backend<0, allocate_stack>;
} // namespace backends
template
struct number_category > : public mpl::int_
{};
namespace backends {
namespace detail {
template
struct mpfr_cleanup
{
struct initializer
{
initializer() {}
~initializer() { mpfr_free_cache(); }
void force_instantiate() const {}
};
static const initializer init;
static void force_instantiate() { init.force_instantiate(); }
};
template
typename mpfr_cleanup::initializer const mpfr_cleanup::init;
inline void mpfr_copy_precision(mpfr_t dest, const mpfr_t src)
{
mpfr_prec_t p_dest = mpfr_get_prec(dest);
mpfr_prec_t p_src = mpfr_get_prec(src);
if (p_dest != p_src)
mpfr_set_prec(dest, p_src);
}
inline void mpfr_copy_precision(mpfr_t dest, const mpfr_t src1, const mpfr_t src2)
{
mpfr_prec_t p_dest = mpfr_get_prec(dest);
mpfr_prec_t p_src1 = mpfr_get_prec(src1);
mpfr_prec_t p_src2 = mpfr_get_prec(src2);
if (p_src2 > p_src1)
p_src1 = p_src2;
if (p_dest != p_src1)
mpfr_set_prec(dest, p_src1);
}
template
struct mpfr_float_imp;
template
struct mpfr_float_imp
{
#ifdef BOOST_HAS_LONG_LONG
typedef mpl::list signed_types;
typedef mpl::list unsigned_types;
#else
typedef mpl::list signed_types;
typedef mpl::list unsigned_types;
#endif
typedef mpl::list float_types;
typedef long exponent_type;
mpfr_float_imp()
{
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
mpfr_set_ui(m_data, 0u, GMP_RNDN);
}
mpfr_float_imp(unsigned digits2)
{
mpfr_init2(m_data, digits2);
mpfr_set_ui(m_data, 0u, GMP_RNDN);
}
mpfr_float_imp(const mpfr_float_imp& o)
{
mpfr_init2(m_data, mpfr_get_prec(o.m_data));
if (o.m_data[0]._mpfr_d)
mpfr_set(m_data, o.m_data, GMP_RNDN);
}
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
mpfr_float_imp(mpfr_float_imp&& o) BOOST_NOEXCEPT
{
m_data[0] = o.m_data[0];
o.m_data[0]._mpfr_d = 0;
}
#endif
mpfr_float_imp& operator=(const mpfr_float_imp& o)
{
if ((o.m_data[0]._mpfr_d) && (this != &o))
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, mpfr_get_prec(o.m_data));
mpfr_set(m_data, o.m_data, GMP_RNDN);
}
return *this;
}
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
mpfr_float_imp& operator=(mpfr_float_imp&& o) BOOST_NOEXCEPT
{
mpfr_swap(m_data, o.m_data);
return *this;
}
#endif
#ifdef BOOST_HAS_LONG_LONG
#ifdef _MPFR_H_HAVE_INTMAX_T
mpfr_float_imp& operator=(boost::ulong_long_type i)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
mpfr_set_uj(m_data, i, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(boost::long_long_type i)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
mpfr_set_sj(m_data, i, GMP_RNDN);
return *this;
}
#else
mpfr_float_imp& operator=(boost::ulong_long_type i)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits::digits - 1)) - 1) << 1) | 1uLL);
unsigned shift = 0;
mpfr_t t;
mpfr_init2(t, (std::max)(static_cast(std::numeric_limits::digits), mpfr_get_prec(m_data)));
mpfr_set_ui(m_data, 0, GMP_RNDN);
while (i)
{
mpfr_set_ui(t, static_cast(i & mask), GMP_RNDN);
if (shift)
mpfr_mul_2exp(t, t, shift, GMP_RNDN);
mpfr_add(m_data, m_data, t, GMP_RNDN);
shift += std::numeric_limits::digits;
i >>= std::numeric_limits::digits;
}
mpfr_clear(t);
return *this;
}
mpfr_float_imp& operator=(boost::long_long_type i)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
bool neg = i < 0;
*this = boost::multiprecision::detail::unsigned_abs(i);
if (neg)
mpfr_neg(m_data, m_data, GMP_RNDN);
return *this;
}
#endif
#endif
mpfr_float_imp& operator=(unsigned long i)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
mpfr_set_ui(m_data, i, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(long i)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
mpfr_set_si(m_data, i, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(double d)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
mpfr_set_d(m_data, d, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(long double a)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
mpfr_set_ld(m_data, a, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(const char* s)
{
if (m_data[0]._mpfr_d == 0)
mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
if (mpfr_set_str(m_data, s, 10, GMP_RNDN) != 0)
{
BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Unable to parse string \"") + s + std::string("\"as a valid floating point number.")));
}
return *this;
}
void swap(mpfr_float_imp& o) BOOST_NOEXCEPT
{
mpfr_swap(m_data, o.m_data);
}
std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
{
BOOST_ASSERT(m_data[0]._mpfr_d);
bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
std::streamsize org_digits(digits);
if (scientific && digits)
++digits;
std::string result;
mp_exp_t e;
if (mpfr_inf_p(m_data))
{
if (mpfr_sgn(m_data) < 0)
result = "-inf";
else if (f & std::ios_base::showpos)
result = "+inf";
else
result = "inf";
return result;
}
if (mpfr_nan_p(m_data))
{
result = "nan";
return result;
}
if (mpfr_zero_p(m_data))
{
e = 0;
result = "0";
}
else
{
char* ps = mpfr_get_str(0, &e, 10, static_cast(digits), m_data, GMP_RNDN);
--e; // To match with what our formatter expects.
if (fixed && e != -1)
{
// Oops we actually need a different number of digits to what we asked for:
mpfr_free_str(ps);
digits += e + 1;
if (digits == 0)
{
// We need to get *all* the digits and then possibly round up,
// we end up with either "0" or "1" as the result.
ps = mpfr_get_str(0, &e, 10, 0, m_data, GMP_RNDN);
--e;
unsigned offset = *ps == '-' ? 1 : 0;
if (ps[offset] > '5')
{
++e;
ps[offset] = '1';
ps[offset + 1] = 0;
}
else if (ps[offset] == '5')
{
unsigned i = offset + 1;
bool round_up = false;
while (ps[i] != 0)
{
if (ps[i] != '0')
{
round_up = true;
break;
}
++i;
}
if (round_up)
{
++e;
ps[offset] = '1';
ps[offset + 1] = 0;
}
else
{
ps[offset] = '0';
ps[offset + 1] = 0;
}
}
else
{
ps[offset] = '0';
ps[offset + 1] = 0;
}
}
else if (digits > 0)
{
mp_exp_t old_e = e;
ps = mpfr_get_str(0, &e, 10, static_cast(digits), m_data, GMP_RNDN);
--e; // To match with what our formatter expects.
if (old_e > e)
{
// in some cases, when we ask for more digits of precision, it will
// change the number of digits to the left of the decimal, if that
// happens, account for it here.
// example: cout << fixed << setprecision(3) << mpf_float_50("99.9809")
digits -= old_e - e;
ps = mpfr_get_str(0, &e, 10, static_cast(digits), m_data, GMP_RNDN);
--e; // To match with what our formatter expects.
}
}
else
{
ps = mpfr_get_str(0, &e, 10, 1, m_data, GMP_RNDN);
--e;
unsigned offset = *ps == '-' ? 1 : 0;
ps[offset] = '0';
ps[offset + 1] = 0;
}
}
result = ps ? ps : "0";
if (ps)
mpfr_free_str(ps);
}
boost::multiprecision::detail::format_float_string(result, e, org_digits, f, 0 != mpfr_zero_p(m_data));
return result;
}
~mpfr_float_imp() BOOST_NOEXCEPT
{
if (m_data[0]._mpfr_d)
mpfr_clear(m_data);
detail::mpfr_cleanup::force_instantiate();
}
void negate() BOOST_NOEXCEPT
{
BOOST_ASSERT(m_data[0]._mpfr_d);
mpfr_neg(m_data, m_data, GMP_RNDN);
}
template
int compare(const mpfr_float_backend& o) const BOOST_NOEXCEPT
{
BOOST_ASSERT(m_data[0]._mpfr_d && o.m_data[0]._mpfr_d);
return mpfr_cmp(m_data, o.m_data);
}
int compare(long i) const BOOST_NOEXCEPT
{
BOOST_ASSERT(m_data[0]._mpfr_d);
return mpfr_cmp_si(m_data, i);
}
int compare(unsigned long i) const BOOST_NOEXCEPT
{
BOOST_ASSERT(m_data[0]._mpfr_d);
return mpfr_cmp_ui(m_data, i);
}
template
int compare(V v) const BOOST_NOEXCEPT
{
mpfr_float_backend d(0uL, mpfr_get_prec(m_data));
d = v;
return compare(d);
}
mpfr_t& data() BOOST_NOEXCEPT
{
BOOST_ASSERT(m_data[0]._mpfr_d);
return m_data;
}
const mpfr_t& data() const BOOST_NOEXCEPT
{
BOOST_ASSERT(m_data[0]._mpfr_d);
return m_data;
}
protected:
mpfr_t m_data;
static boost::multiprecision::detail::precision_type& get_default_precision() BOOST_NOEXCEPT
{
static boost::multiprecision::detail::precision_type val(BOOST_MULTIPRECISION_MPFR_DEFAULT_PRECISION);
return val;
}
};
#ifdef BOOST_MSVC
#pragma warning(push)
#pragma warning(disable : 4127) // Conditional expression is constant
#endif
template
struct mpfr_float_imp
{
#ifdef BOOST_HAS_LONG_LONG
typedef mpl::list signed_types;
typedef mpl::list unsigned_types;
#else
typedef mpl::list signed_types;
typedef mpl::list unsigned_types;
#endif
typedef mpl::list float_types;
typedef long exponent_type;
static const unsigned digits2 = (digits10 * 1000uL) / 301uL + ((digits10 * 1000uL) % 301 ? 2u : 1u);
static const unsigned limb_count = mpfr_custom_get_size(digits2) / sizeof(mp_limb_t);
~mpfr_float_imp() BOOST_NOEXCEPT
{
detail::mpfr_cleanup::force_instantiate();
}
mpfr_float_imp()
{
mpfr_custom_init(m_buffer, digits2);
mpfr_custom_init_set(m_data, MPFR_NAN_KIND, 0, digits2, m_buffer);
mpfr_set_ui(m_data, 0u, GMP_RNDN);
}
mpfr_float_imp(const mpfr_float_imp& o)
{
mpfr_custom_init(m_buffer, digits2);
mpfr_custom_init_set(m_data, MPFR_NAN_KIND, 0, digits2, m_buffer);
mpfr_set(m_data, o.m_data, GMP_RNDN);
}
mpfr_float_imp& operator=(const mpfr_float_imp& o)
{
mpfr_set(m_data, o.m_data, GMP_RNDN);
return *this;
}
#ifdef BOOST_HAS_LONG_LONG
#ifdef _MPFR_H_HAVE_INTMAX_T
mpfr_float_imp& operator=(boost::ulong_long_type i)
{
mpfr_set_uj(m_data, i, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(boost::long_long_type i)
{
mpfr_set_sj(m_data, i, GMP_RNDN);
return *this;
}
#else
mpfr_float_imp& operator=(boost::ulong_long_type i)
{
boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits::digits - 1)) - 1) << 1) | 1uL);
unsigned shift = 0;
mpfr_t t;
mp_limb_t t_limbs[limb_count];
mpfr_custom_init(t_limbs, digits2);
mpfr_custom_init_set(t, MPFR_NAN_KIND, 0, digits2, t_limbs);
mpfr_set_ui(m_data, 0, GMP_RNDN);
while (i)
{
mpfr_set_ui(t, static_cast(i & mask), GMP_RNDN);
if (shift)
mpfr_mul_2exp(t, t, shift, GMP_RNDN);
mpfr_add(m_data, m_data, t, GMP_RNDN);
shift += std::numeric_limits::digits;
i >>= std::numeric_limits::digits;
}
return *this;
}
mpfr_float_imp& operator=(boost::long_long_type i)
{
bool neg = i < 0;
*this = boost::multiprecision::detail::unsigned_abs(i);
if (neg)
mpfr_neg(m_data, m_data, GMP_RNDN);
return *this;
}
#endif
#endif
mpfr_float_imp& operator=(unsigned long i)
{
mpfr_set_ui(m_data, i, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(long i)
{
mpfr_set_si(m_data, i, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(double d)
{
mpfr_set_d(m_data, d, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(long double a)
{
mpfr_set_ld(m_data, a, GMP_RNDN);
return *this;
}
mpfr_float_imp& operator=(const char* s)
{
if (mpfr_set_str(m_data, s, 10, GMP_RNDN) != 0)
{
BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Unable to parse string \"") + s + std::string("\"as a valid floating point number.")));
}
return *this;
}
void swap(mpfr_float_imp& o) BOOST_NOEXCEPT
{
// We have to swap by copying:
mpfr_float_imp t(*this);
*this = o;
o = t;
}
std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
{
BOOST_ASSERT(m_data[0]._mpfr_d);
bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
std::streamsize org_digits(digits);
if (scientific && digits)
++digits;
std::string result;
mp_exp_t e;
if (mpfr_inf_p(m_data))
{
if (mpfr_sgn(m_data) < 0)
result = "-inf";
else if (f & std::ios_base::showpos)
result = "+inf";
else
result = "inf";
return result;
}
if (mpfr_nan_p(m_data))
{
result = "nan";
return result;
}
if (mpfr_zero_p(m_data))
{
e = 0;
result = "0";
}
else
{
char* ps = mpfr_get_str(0, &e, 10, static_cast(digits), m_data, GMP_RNDN);
--e; // To match with what our formatter expects.
if (fixed && e != -1)
{
// Oops we actually need a different number of digits to what we asked for:
mpfr_free_str(ps);
digits += e + 1;
if (digits == 0)
{
// We need to get *all* the digits and then possibly round up,
// we end up with either "0" or "1" as the result.
ps = mpfr_get_str(0, &e, 10, 0, m_data, GMP_RNDN);
--e;
unsigned offset = *ps == '-' ? 1 : 0;
if (ps[offset] > '5')
{
++e;
ps[offset] = '1';
ps[offset + 1] = 0;
}
else if (ps[offset] == '5')
{
unsigned i = offset + 1;
bool round_up = false;
while (ps[i] != 0)
{
if (ps[i] != '0')
{
round_up = true;
break;
}
}
if (round_up)
{
++e;
ps[offset] = '1';
ps[offset + 1] = 0;
}
else
{
ps[offset] = '0';
ps[offset + 1] = 0;
}
}
else
{
ps[offset] = '0';
ps[offset + 1] = 0;
}
}
else if (digits > 0)
{
ps = mpfr_get_str(0, &e, 10, static_cast(digits), m_data, GMP_RNDN);
--e; // To match with what our formatter expects.
}
else
{
ps = mpfr_get_str(0, &e, 10, 1, m_data, GMP_RNDN);
--e;
unsigned offset = *ps == '-' ? 1 : 0;
ps[offset] = '0';
ps[offset + 1] = 0;
}
}
result = ps ? ps : "0";
if (ps)
mpfr_free_str(ps);
}
boost::multiprecision::detail::format_float_string(result, e, org_digits, f, 0 != mpfr_zero_p(m_data));
return result;
}
void negate() BOOST_NOEXCEPT
{
mpfr_neg(m_data, m_data, GMP_RNDN);
}
template
int compare(const mpfr_float_backend& o) const BOOST_NOEXCEPT
{
return mpfr_cmp(m_data, o.m_data);
}
int compare(long i) const BOOST_NOEXCEPT
{
return mpfr_cmp_si(m_data, i);
}
int compare(unsigned long i) const BOOST_NOEXCEPT
{
return mpfr_cmp_ui(m_data, i);
}
template
int compare(V v) const BOOST_NOEXCEPT
{
mpfr_float_backend d;
d = v;
return compare(d);
}
mpfr_t& data() BOOST_NOEXCEPT
{
return m_data;
}
const mpfr_t& data() const BOOST_NOEXCEPT
{
return m_data;
}
protected:
mpfr_t m_data;
mp_limb_t m_buffer[limb_count];
};
#ifdef BOOST_MSVC
#pragma warning(pop)
#endif
} // namespace detail
template
struct mpfr_float_backend : public detail::mpfr_float_imp
{
mpfr_float_backend() : detail::mpfr_float_imp() {}
mpfr_float_backend(const mpfr_float_backend& o) : detail::mpfr_float_imp(o) {}
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
mpfr_float_backend(mpfr_float_backend&& o) BOOST_NOEXCEPT : detail::mpfr_float_imp(static_cast&&>(o))
{}
#endif
template
mpfr_float_backend(const mpfr_float_backend& val, typename enable_if_c::type* = 0)
: detail::mpfr_float_imp()
{
mpfr_set(this->m_data, val.data(), GMP_RNDN);
}
template
explicit mpfr_float_backend(const mpfr_float_backend& val, typename disable_if_c::type* = 0)
: detail::mpfr_float_imp()
{
mpfr_set(this->m_data, val.data(), GMP_RNDN);
}
template
mpfr_float_backend(const gmp_float& val, typename enable_if_c::type* = 0)
: detail::mpfr_float_imp()
{
mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
}
template
mpfr_float_backend(const gmp_float& val, typename disable_if_c::type* = 0)
: detail::mpfr_float_imp()
{
mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
}
mpfr_float_backend(const gmp_int& val)
: detail::mpfr_float_imp()
{
mpfr_set_z(this->m_data, val.data(), GMP_RNDN);
}
mpfr_float_backend(const gmp_rational& val)
: detail::mpfr_float_imp()
{
mpfr_set_q(this->m_data, val.data(), GMP_RNDN);
}
mpfr_float_backend(const mpfr_t val)
: detail::mpfr_float_imp()
{
mpfr_set(this->m_data, val, GMP_RNDN);
}
mpfr_float_backend(const mpf_t val)
: detail::mpfr_float_imp()
{
mpfr_set_f(this->m_data, val, GMP_RNDN);
}
mpfr_float_backend(const mpz_t val)
: detail::mpfr_float_imp()
{
mpfr_set_z(this->m_data, val, GMP_RNDN);
}
mpfr_float_backend(const mpq_t val)
: detail::mpfr_float_imp()
{
mpfr_set_q(this->m_data, val, GMP_RNDN);
}
// Construction with precision: we ignore the precision here.
template
mpfr_float_backend(const V& o, unsigned)
{
*this = o;
}
mpfr_float_backend& operator=(const mpfr_float_backend& o)
{
*static_cast*>(this) = static_cast const&>(o);
return *this;
}
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
mpfr_float_backend& operator=(mpfr_float_backend&& o) BOOST_NOEXCEPT
{
*static_cast*>(this) = static_cast&&>(o);
return *this;
}
#endif
template
mpfr_float_backend& operator=(const V& v)
{
*static_cast*>(this) = v;
return *this;
}
mpfr_float_backend& operator=(const mpfr_t val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
mpfr_set(this->m_data, val, GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const mpf_t val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
mpfr_set_f(this->m_data, val, GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const mpz_t val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
mpfr_set_z(this->m_data, val, GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const mpq_t val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
mpfr_set_q(this->m_data, val, GMP_RNDN);
return *this;
}
// We don't change our precision here, this is a fixed precision type:
template
mpfr_float_backend& operator=(const mpfr_float_backend& val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
mpfr_set(this->m_data, val.data(), GMP_RNDN);
return *this;
}
template
mpfr_float_backend& operator=(const gmp_float& val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const gmp_int& val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
mpfr_set_z(this->m_data, val.data(), GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const gmp_rational& val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
mpfr_set_q(this->m_data, val.data(), GMP_RNDN);
return *this;
}
};
template <>
struct mpfr_float_backend<0, allocate_dynamic> : public detail::mpfr_float_imp<0, allocate_dynamic>
{
mpfr_float_backend() : detail::mpfr_float_imp<0, allocate_dynamic>() {}
mpfr_float_backend(const mpfr_t val)
: detail::mpfr_float_imp<0, allocate_dynamic>((unsigned)mpfr_get_prec(val))
{
mpfr_set(this->m_data, val, GMP_RNDN);
}
mpfr_float_backend(const mpf_t val)
: detail::mpfr_float_imp<0, allocate_dynamic>((unsigned)mpf_get_prec(val))
{
mpfr_set_f(this->m_data, val, GMP_RNDN);
}
mpfr_float_backend(const mpz_t val)
: detail::mpfr_float_imp<0, allocate_dynamic>()
{
mpfr_set_z(this->m_data, val, GMP_RNDN);
}
mpfr_float_backend(const mpq_t val)
: detail::mpfr_float_imp<0, allocate_dynamic>()
{
mpfr_set_q(this->m_data, val, GMP_RNDN);
}
mpfr_float_backend(const mpfr_float_backend& o) : detail::mpfr_float_imp<0, allocate_dynamic>(o) {}
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
mpfr_float_backend(mpfr_float_backend&& o) BOOST_NOEXCEPT : detail::mpfr_float_imp<0, allocate_dynamic>(static_cast&&>(o))
{}
#endif
template
mpfr_float_backend(const V& o, unsigned digits10)
: detail::mpfr_float_imp<0, allocate_dynamic>(multiprecision::detail::digits10_2_2(digits10))
{
*this = o;
}
#ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
mpfr_float_backend(const std::string_view& o, unsigned digits10)
: detail::mpfr_float_imp<0, allocate_dynamic>(multiprecision::detail::digits10_2_2(digits10))
{
std::string s(o);
*this = s.c_str();
}
#endif
template
mpfr_float_backend(const gmp_float& val, unsigned digits10)
: detail::mpfr_float_imp<0, allocate_dynamic>(multiprecision::detail::digits10_2_2(digits10))
{
mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
}
template
mpfr_float_backend(const mpfr_float_backend& val, unsigned digits10)
: detail::mpfr_float_imp<0, allocate_dynamic>(multiprecision::detail::digits10_2_2(digits10))
{
mpfr_set(this->m_data, val.data(), GMP_RNDN);
}
template
mpfr_float_backend(const mpfr_float_backend& val)
: detail::mpfr_float_imp<0, allocate_dynamic>(mpfr_get_prec(val.data()))
{
mpfr_set(this->m_data, val.data(), GMP_RNDN);
}
template
mpfr_float_backend(const gmp_float& val)
: detail::mpfr_float_imp<0, allocate_dynamic>(mpf_get_prec(val.data()))
{
mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
}
mpfr_float_backend(const gmp_int& val)
: detail::mpfr_float_imp<0, allocate_dynamic>()
{
mpfr_set_z(this->m_data, val.data(), GMP_RNDN);
}
mpfr_float_backend(const gmp_rational& val)
: detail::mpfr_float_imp<0, allocate_dynamic>()
{
mpfr_set_q(this->m_data, val.data(), GMP_RNDN);
}
mpfr_float_backend& operator=(const mpfr_float_backend& o)
{
if (this != &o)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, mpfr_get_prec(o.data()));
else
detail::mpfr_copy_precision(this->m_data, o.data());
mpfr_set(this->m_data, o.data(), GMP_RNDN);
}
return *this;
}
#ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
mpfr_float_backend& operator=(mpfr_float_backend&& o) BOOST_NOEXCEPT
{
*static_cast*>(this) = static_cast&&>(o);
return *this;
}
#endif
template
mpfr_float_backend& operator=(const V& v)
{
*static_cast*>(this) = v;
return *this;
}
mpfr_float_backend& operator=(const mpfr_t val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, mpfr_get_prec(val));
else
mpfr_set_prec(this->m_data, mpfr_get_prec(val));
mpfr_set(this->m_data, val, GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const mpf_t val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, (mpfr_prec_t)mpf_get_prec(val));
else
mpfr_set_prec(this->m_data, (unsigned)mpf_get_prec(val));
mpfr_set_f(this->m_data, val, GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const mpz_t val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(get_default_precision()));
mpfr_set_z(this->m_data, val, GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const mpq_t val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(get_default_precision()));
mpfr_set_q(this->m_data, val, GMP_RNDN);
return *this;
}
template
mpfr_float_backend& operator=(const mpfr_float_backend& val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, mpfr_get_prec(val.data()));
else
mpfr_set_prec(this->m_data, mpfr_get_prec(val.data()));
mpfr_set(this->m_data, val.data(), GMP_RNDN);
return *this;
}
template
mpfr_float_backend& operator=(const gmp_float& val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, mpf_get_prec(val.data()));
else
mpfr_set_prec(this->m_data, mpf_get_prec(val.data()));
mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const gmp_int& val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(get_default_precision()));
mpfr_set_z(this->m_data, val.data(), GMP_RNDN);
return *this;
}
mpfr_float_backend& operator=(const gmp_rational& val)
{
if (this->m_data[0]._mpfr_d == 0)
mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(get_default_precision()));
mpfr_set_q(this->m_data, val.data(), GMP_RNDN);
return *this;
}
static unsigned default_precision() BOOST_NOEXCEPT
{
return get_default_precision();
}
static void default_precision(unsigned v) BOOST_NOEXCEPT
{
get_default_precision() = v;
}
unsigned precision() const BOOST_NOEXCEPT
{
return multiprecision::detail::digits2_2_10(mpfr_get_prec(this->m_data));
}
void precision(unsigned digits10) BOOST_NOEXCEPT
{
mpfr_prec_round(this->m_data, multiprecision::detail::digits10_2_2((digits10)), GMP_RNDN);
}
};
template
inline typename enable_if, bool>::type eval_eq(const mpfr_float_backend& a, const T& b) BOOST_NOEXCEPT
{
return a.compare(b) == 0;
}
template
inline typename enable_if, bool>::type eval_lt(const mpfr_float_backend& a, const T& b) BOOST_NOEXCEPT
{
return a.compare(b) < 0;
}
template
inline typename enable_if, bool>::type eval_gt(const mpfr_float_backend& a, const T& b) BOOST_NOEXCEPT
{
return a.compare(b) > 0;
}
template
inline void eval_add(mpfr_float_backend& result, const mpfr_float_backend& o)
{
mpfr_add(result.data(), result.data(), o.data(), GMP_RNDN);
}
template
inline void eval_subtract(mpfr_float_backend& result, const mpfr_float_backend& o)
{
mpfr_sub(result.data(), result.data(), o.data(), GMP_RNDN);
}
template
inline void eval_multiply(mpfr_float_backend& result, const mpfr_float_backend& o)
{
if ((void*)&o == (void*)&result)
mpfr_sqr(result.data(), o.data(), GMP_RNDN);
else
mpfr_mul(result.data(), result.data(), o.data(), GMP_RNDN);
}
template
inline void eval_divide(mpfr_float_backend& result, const mpfr_float_backend& o)
{
mpfr_div(result.data(), result.data(), o.data(), GMP_RNDN);
}
template
inline void eval_add(mpfr_float_backend& result, unsigned long i)
{
mpfr_add_ui(result.data(), result.data(), i, GMP_RNDN);
}
template
inline void eval_subtract(mpfr_float_backend& result, unsigned long i)
{
mpfr_sub_ui(result.data(), result.data(), i, GMP_RNDN);
}
template
inline void eval_multiply(mpfr_float_backend& result, unsigned long i)
{
mpfr_mul_ui(result.data(), result.data(), i, GMP_RNDN);
}
template
inline void eval_divide(mpfr_float_backend& result, unsigned long i)
{
mpfr_div_ui(result.data(), result.data(), i, GMP_RNDN);
}
template
inline void eval_add(mpfr_float_backend& result, long i)
{
if (i > 0)
mpfr_add_ui(result.data(), result.data(), i, GMP_RNDN);
else
mpfr_sub_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
}
template
inline void eval_subtract(mpfr_float_backend& result, long i)
{
if (i > 0)
mpfr_sub_ui(result.data(), result.data(), i, GMP_RNDN);
else
mpfr_add_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
}
template
inline void eval_multiply(mpfr_float_backend& result, long i)
{
mpfr_mul_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
if (i < 0)
mpfr_neg(result.data(), result.data(), GMP_RNDN);
}
template
inline void eval_divide(mpfr_float_backend& result, long i)
{
mpfr_div_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
if (i < 0)
mpfr_neg(result.data(), result.data(), GMP_RNDN);
}
//
// Specialised 3 arg versions of the basic operators:
//
template
inline void eval_add(mpfr_float_backend& a, const mpfr_float_backend& x, const mpfr_float_backend& y)
{
mpfr_add(a.data(), x.data(), y.data(), GMP_RNDN);
}
template
inline void eval_add(mpfr_float_backend& a, const mpfr_float_backend& x, unsigned long y)
{
mpfr_add_ui(a.data(), x.data(), y, GMP_RNDN);
}
template
inline void eval_add(mpfr_float_backend& a, const mpfr_float_backend& x, long y)
{
if (y < 0)
mpfr_sub_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
else
mpfr_add_ui(a.data(), x.data(), y, GMP_RNDN);
}
template
inline void eval_add(mpfr_float_backend& a, unsigned long x, const mpfr_float_backend& y)
{
mpfr_add_ui(a.data(), y.data(), x, GMP_RNDN);
}
template
inline void eval_add(mpfr_float_backend& a, long x, const mpfr_float_backend& y)
{
if (x < 0)
{
mpfr_ui_sub(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data(), GMP_RNDN);
mpfr_neg(a.data(), a.data(), GMP_RNDN);
}
else
mpfr_add_ui(a.data(), y.data(), x, GMP_RNDN);
}
template
inline void eval_subtract(mpfr_float_backend& a, const mpfr_float_backend& x, const mpfr_float_backend& y)
{
mpfr_sub(a.data(), x.data(), y.data(), GMP_RNDN);
}
template
inline void eval_subtract(mpfr_float_backend& a, const mpfr_float_backend& x, unsigned long y)
{
mpfr_sub_ui(a.data(), x.data(), y, GMP_RNDN);
}
template
inline void eval_subtract(mpfr_float_backend& a, const mpfr_float_backend& x, long y)
{
if (y < 0)
mpfr_add_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
else
mpfr_sub_ui(a.data(), x.data(), y, GMP_RNDN);
}
template
inline void eval_subtract(mpfr_float_backend& a, unsigned long x, const mpfr_float_backend& y)
{
mpfr_ui_sub(a.data(), x, y.data(), GMP_RNDN);
}
template
inline void eval_subtract(mpfr_float_backend& a, long x, const mpfr_float_backend& y)
{
if (x < 0)
{
mpfr_add_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x), GMP_RNDN);
mpfr_neg(a.data(), a.data(), GMP_RNDN);
}
else
mpfr_ui_sub(a.data(), x, y.data(), GMP_RNDN);
}
template
inline void eval_multiply(mpfr_float_backend& a, const mpfr_float_backend& x, const mpfr_float_backend& y)
{
if ((void*)&x == (void*)&y)
mpfr_sqr(a.data(), x.data(), GMP_RNDN);
else
mpfr_mul(a.data(), x.data(), y.data(), GMP_RNDN);
}
template
inline void eval_multiply(mpfr_float_backend& a, const mpfr_float_backend& x, unsigned long y)
{
mpfr_mul_ui(a.data(), x.data(), y, GMP_RNDN);
}
template
inline void eval_multiply(mpfr_float_backend& a, const mpfr_float_backend& x, long y)
{
if (y < 0)
{
mpfr_mul_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
a.negate();
}
else
mpfr_mul_ui(a.data(), x.data(), y, GMP_RNDN);
}
template
inline void eval_multiply(mpfr_float_backend& a, unsigned long x, const mpfr_float_backend& y)
{
mpfr_mul_ui(a.data(), y.data(), x, GMP_RNDN);
}
template
inline void eval_multiply(mpfr_float_backend& a, long x, const mpfr_float_backend& y)
{
if (x < 0)
{
mpfr_mul_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x), GMP_RNDN);
mpfr_neg(a.data(), a.data(), GMP_RNDN);
}
else
mpfr_mul_ui(a.data(), y.data(), x, GMP_RNDN);
}
template
inline void eval_divide(mpfr_float_backend& a, const mpfr_float_backend& x, const mpfr_float_backend& y)
{
mpfr_div(a.data(), x.data(), y.data(), GMP_RNDN);
}
template
inline void eval_divide(mpfr_float_backend& a, const mpfr_float_backend& x, unsigned long y)
{
mpfr_div_ui(a.data(), x.data(), y, GMP_RNDN);
}
template
inline void eval_divide(mpfr_float_backend& a, const mpfr_float_backend& x, long y)
{
if (y < 0)
{
mpfr_div_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
a.negate();
}
else
mpfr_div_ui(a.data(), x.data(), y, GMP_RNDN);
}
template
inline void eval_divide(mpfr_float_backend& a, unsigned long x, const mpfr_float_backend& y)
{
mpfr_ui_div(a.data(), x, y.data(), GMP_RNDN);
}
template
inline void eval_divide(mpfr_float_backend& a, long x, const mpfr_float_backend& y)
{
if (x < 0)
{
mpfr_ui_div(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data(), GMP_RNDN);
mpfr_neg(a.data(), a.data(), GMP_RNDN);
}
else
mpfr_ui_div(a.data(), x, y.data(), GMP_RNDN);
}
template
inline bool eval_is_zero(const mpfr_float_backend& val) BOOST_NOEXCEPT
{
return 0 != mpfr_zero_p(val.data());
}
template
inline int eval_get_sign(const mpfr_float_backend& val) BOOST_NOEXCEPT
{
return mpfr_sgn(val.data());
}
template
inline void eval_convert_to(unsigned long* result, const mpfr_float_backend& val)
{
if (mpfr_nan_p(val.data()))
{
BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
}
*result = mpfr_get_ui(val.data(), GMP_RNDZ);
}
template
inline void eval_convert_to(long* result, const mpfr_float_backend& val)
{
if (mpfr_nan_p(val.data()))
{
BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
}
*result = mpfr_get_si(val.data(), GMP_RNDZ);
}
#ifdef _MPFR_H_HAVE_INTMAX_T
template
inline void eval_convert_to(boost::ulong_long_type* result, const mpfr_float_backend& val)
{
if (mpfr_nan_p(val.data()))
{
BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
}
*result = mpfr_get_uj(val.data(), GMP_RNDZ);
}
template
inline void eval_convert_to(boost::long_long_type* result, const mpfr_float_backend& val)
{
if (mpfr_nan_p(val.data()))
{
BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
}
*result = mpfr_get_sj(val.data(), GMP_RNDZ);
}
#endif
template
inline void eval_convert_to(float* result, const mpfr_float_backend& val) BOOST_NOEXCEPT
{
*result = mpfr_get_flt(val.data(), GMP_RNDN);
}
template
inline void eval_convert_to(double* result, const mpfr_float_backend& val) BOOST_NOEXCEPT
{
*result = mpfr_get_d(val.data(), GMP_RNDN);
}
template
inline void eval_convert_to(long double* result, const mpfr_float_backend& val) BOOST_NOEXCEPT
{
*result = mpfr_get_ld(val.data(), GMP_RNDN);
}
//
// Native non-member operations:
//
template
inline void eval_sqrt(mpfr_float_backend& result, const mpfr_float_backend& val)
{
mpfr_sqrt(result.data(), val.data(), GMP_RNDN);
}
template
inline void eval_abs(mpfr_float_backend& result, const mpfr_float_backend& val)
{
mpfr_abs(result.data(), val.data(), GMP_RNDN);
}
template
inline void eval_fabs(mpfr_float_backend& result, const mpfr_float_backend& val)
{
mpfr_abs(result.data(), val.data(), GMP_RNDN);
}
template
inline void eval_ceil(mpfr_float_backend& result, const mpfr_float_backend& val)
{
mpfr_ceil(result.data(), val.data());
}
template
inline void eval_floor(mpfr_float_backend& result, const mpfr_float_backend& val)
{
mpfr_floor(result.data(), val.data());
}
template
inline void eval_trunc(mpfr_float_backend& result, const mpfr_float_backend& val)
{
mpfr_trunc(result.data(), val.data());
}
template