Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/multiprecision/detail/default_ops.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 ///////////////////////////////////////////////////////////////////////////////
0002 //  Copyright 2011-21 John Maddock.
0003 //  Copyright 2021 Iskandarov Lev. Distributed under the Boost
0004 //  Software License, Version 1.0. (See accompanying file
0005 //  LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 
0007 #ifndef BOOST_MP_DEFAULT_OPS
0008 #define BOOST_MP_DEFAULT_OPS
0009 
0010 #include <boost/multiprecision/detail/standalone_config.hpp>
0011 #include <boost/multiprecision/detail/no_exceptions_support.hpp>
0012 #include <boost/multiprecision/detail/number_base.hpp>
0013 #include <boost/multiprecision/detail/assert.hpp>
0014 #include <boost/multiprecision/traits/is_backend.hpp>
0015 #include <boost/multiprecision/detail/fpclassify.hpp>
0016 #include <cstdint>
0017 #include <complex>
0018 #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
0019 #include <string_view>
0020 #endif
0021 
0022 #ifdef BOOST_MP_MATH_AVAILABLE
0023 #include <boost/math/special_functions/fpclassify.hpp>
0024 #include <boost/math/special_functions/next.hpp>
0025 #include <boost/math/special_functions/hypot.hpp>
0026 #include <boost/math/policies/error_handling.hpp>
0027 #endif
0028 
0029 #ifndef INSTRUMENT_BACKEND
0030 #ifndef BOOST_MP_INSTRUMENT
0031 #define INSTRUMENT_BACKEND(x)
0032 #else
0033 #define INSTRUMENT_BACKEND(x) \
0034    std::cout << BOOST_STRINGIZE(x) << " = " << x.str(0, std::ios_base::scientific) << std::endl;
0035 #endif
0036 #endif
0037 
0038 namespace boost {
0039 namespace multiprecision {
0040 
0041 namespace detail {
0042 
0043 template <class To, class From>
0044 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/);
0045 template <class To, class From>
0046 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/);
0047 template <class To, class From>
0048 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/);
0049 template <class To, class From>
0050 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/);
0051 template <class To, class From>
0052 void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/);
0053 
0054 template <class Integer>
0055 BOOST_MP_CXX14_CONSTEXPR Integer karatsuba_sqrt(const Integer& x, Integer& r, size_t bits);
0056 
0057 } // namespace detail
0058 
0059 namespace default_ops {
0060 
0061 template <class T>
0062 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_backend<T>::value, int>::type eval_signbit(const T& val);
0063 
0064 template <class T>
0065 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!boost::multiprecision::detail::is_backend<T>::value, int>::type eval_signbit(const T& val) { return val < 0; }
0066 
0067 inline int eval_signbit(float val) { return (std::signbit)(val); }
0068 inline int eval_signbit(double val) { return (std::signbit)(val); }
0069 inline int eval_signbit(long double val) { return (std::signbit)(val); }
0070 #ifdef BOOST_HAS_FLOAT128
0071 extern "C" int signbitq(float128_type) throw();
0072 inline int            eval_signbit(float128_type val) { return signbitq(val); }
0073 #endif
0074 
0075 template <class T>
0076 BOOST_MP_CXX14_CONSTEXPR bool eval_is_zero(const T& val);
0077 
0078 #ifdef BOOST_MSVC
0079 // warning C4127: conditional expression is constant
0080 // warning C4146: unary minus operator applied to unsigned type, result still unsigned
0081 #pragma warning(push)
0082 #pragma warning(disable : 4127 4146)
0083 #endif
0084 //
0085 // Default versions of mixed arithmetic, these just construct a temporary
0086 // from the arithmetic value and then do the arithmetic on that, two versions
0087 // of each depending on whether the backend can be directly constructed from type V.
0088 //
0089 // Note that we have to provide *all* the template parameters to class number when used in
0090 // enable_if as MSVC-10 won't compile the code if we rely on a computed-default parameter.
0091 // Since the result of the test doesn't depend on whether expression templates are on or off
0092 // we just use et_on everywhere.  We could use a BOOST_WORKAROUND but that just obfuscates the
0093 // code even more....
0094 //
0095 template <class T, class V>
0096 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_convertible<V, T>::value>::type
0097 eval_add(T& result, V const& v)
0098 {
0099    T t;
0100    t = v;
0101    eval_add(result, t);
0102 }
0103 template <class T, class V>
0104 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, T>::value>::type
0105 eval_add(T& result, V const& v)
0106 {
0107    T t(v);
0108    eval_add(result, t);
0109 }
0110 template <class T, class V>
0111 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_convertible<V, T>::value>::type
0112 eval_subtract(T& result, V const& v)
0113 {
0114    T t;
0115    t = v;
0116    eval_subtract(result, t);
0117 }
0118 template <class T, class V>
0119 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, T>::value>::type
0120 eval_subtract(T& result, V const& v)
0121 {
0122    T t(v);
0123    eval_subtract(result, t);
0124 }
0125 template <class T, class V>
0126 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_convertible<V, T>::value>::type
0127 eval_multiply(T& result, V const& v)
0128 {
0129    T t;
0130    t = v;
0131    eval_multiply(result, t);
0132 }
0133 template <class T, class V>
0134 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, T>::value>::type
0135 eval_multiply(T& result, V const& v)
0136 {
0137    T t(v);
0138    eval_multiply(result, t);
0139 }
0140 
0141 template <class T, class U, class V>
0142 BOOST_MP_CXX14_CONSTEXPR void eval_multiply(T& t, const U& u, const V& v);
0143 
0144 template <class T, class U, class V>
0145 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!std::is_same<T, U>::value && std::is_same<T, V>::value)>::type eval_multiply_add(T& t, const U& u, const V& v)
0146 {
0147    T z;
0148    eval_multiply(z, u, v);
0149    eval_add(t, z);
0150 }
0151 template <class T, class U, class V>
0152 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value && std::is_same<T, V>::value>::type eval_multiply_add(T& t, const U& u, const V& v)
0153 {
0154    eval_multiply_add(t, v, u);
0155 }
0156 template <class T, class U, class V>
0157 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!std::is_same<T, U>::value && std::is_same<T, V>::value)>::type eval_multiply_subtract(T& t, const U& u, const V& v)
0158 {
0159    T z;
0160    eval_multiply(z, u, v);
0161    eval_subtract(t, z);
0162 }
0163 template <class T, class U, class V>
0164 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value && std::is_same<T, V>::value>::type eval_multiply_subtract(T& t, const U& u, const V& v)
0165 {
0166    eval_multiply_subtract(t, v, u);
0167 }
0168 template <class T, class V>
0169 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
0170 eval_divide(T& result, V const& v)
0171 {
0172    T t;
0173    t = v;
0174    eval_divide(result, t);
0175 }
0176 template <class T, class V>
0177 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
0178 eval_divide(T& result, V const& v)
0179 {
0180    T t(v);
0181    eval_divide(result, t);
0182 }
0183 template <class T, class V>
0184 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
0185 eval_modulus(T& result, V const& v)
0186 {
0187    T t;
0188    t = v;
0189    eval_modulus(result, t);
0190 }
0191 template <class T, class V>
0192 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
0193 eval_modulus(T& result, V const& v)
0194 {
0195    T t(v);
0196    eval_modulus(result, t);
0197 }
0198 template <class T, class V>
0199 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
0200 eval_bitwise_and(T& result, V const& v)
0201 {
0202    T t;
0203    t = v;
0204    eval_bitwise_and(result, t);
0205 }
0206 template <class T, class V>
0207 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
0208 eval_bitwise_and(T& result, V const& v)
0209 {
0210    T t(v);
0211    eval_bitwise_and(result, t);
0212 }
0213 template <class T, class V>
0214 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
0215 eval_bitwise_or(T& result, V const& v)
0216 {
0217    T t;
0218    t = v;
0219    eval_bitwise_or(result, t);
0220 }
0221 template <class T, class V>
0222 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
0223 eval_bitwise_or(T& result, V const& v)
0224 {
0225    T t(v);
0226    eval_bitwise_or(result, t);
0227 }
0228 template <class T, class V>
0229 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
0230 eval_bitwise_xor(T& result, V const& v)
0231 {
0232    T t;
0233    t = v;
0234    eval_bitwise_xor(result, t);
0235 }
0236 template <class T, class V>
0237 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
0238 eval_bitwise_xor(T& result, V const& v)
0239 {
0240    T t(v);
0241    eval_bitwise_xor(result, t);
0242 }
0243 
0244 template <class T, class V>
0245 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
0246 eval_complement(T& result, V const& v)
0247 {
0248    T t;
0249    t = v;
0250    eval_complement(result, t);
0251 }
0252 template <class T, class V>
0253 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
0254 eval_complement(T& result, V const& v)
0255 {
0256    T t(v);
0257    eval_complement(result, t);
0258 }
0259 
0260 //
0261 // Default versions of 3-arg arithmetic functions, these mostly just forward to the 2 arg versions:
0262 //
0263 template <class T, class U, class V>
0264 BOOST_MP_CXX14_CONSTEXPR void eval_add(T& t, const U& u, const V& v);
0265 
0266 template <class T>
0267 inline BOOST_MP_CXX14_CONSTEXPR void eval_add_default(T& t, const T& u, const T& v)
0268 {
0269    if (&t == &v)
0270    {
0271       eval_add(t, u);
0272    }
0273    else if (&t == &u)
0274    {
0275       eval_add(t, v);
0276    }
0277    else
0278    {
0279       t = u;
0280       eval_add(t, v);
0281    }
0282 }
0283 template <class T, class U>
0284 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_add_default(T& t, const T& u, const U& v)
0285 {
0286    T vv;
0287    vv = v;
0288    eval_add(t, u, vv);
0289 }
0290 template <class T, class U>
0291 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_add_default(T& t, const T& u, const U& v)
0292 {
0293    T vv(v);
0294    eval_add(t, u, vv);
0295 }
0296 template <class T, class U>
0297 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_add_default(T& t, const U& u, const T& v)
0298 {
0299    eval_add(t, v, u);
0300 }
0301 template <class T, class U, class V>
0302 inline BOOST_MP_CXX14_CONSTEXPR void eval_add_default(T& t, const U& u, const V& v)
0303 {
0304    BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
0305    {
0306       if ((void*)&t == (void*)&v)
0307       {
0308          eval_add(t, u);
0309       }
0310       else
0311       {
0312          t = u;
0313          eval_add(t, v);
0314       }
0315    }
0316    else
0317    {
0318       t = u;
0319       eval_add(t, v);
0320    }
0321 }
0322 template <class T, class U, class V>
0323 inline BOOST_MP_CXX14_CONSTEXPR void eval_add(T& t, const U& u, const V& v)
0324 {
0325    eval_add_default(t, u, v);
0326 }
0327 
0328 template <class T, class U, class V>
0329 void BOOST_MP_CXX14_CONSTEXPR eval_subtract(T& t, const U& u, const V& v);
0330 
0331 template <class T>
0332 inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract_default(T& t, const T& u, const T& v)
0333 {
0334    if ((&t == &v) && is_signed_number<T>::value)
0335    {
0336       eval_subtract(t, u);
0337       t.negate();
0338    }
0339    else if (&t == &u)
0340    {
0341       eval_subtract(t, v);
0342    }
0343    else
0344    {
0345       t = u;
0346       eval_subtract(t, v);
0347    }
0348 }
0349 template <class T, class U>
0350 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_subtract_default(T& t, const T& u, const U& v)
0351 {
0352    T vv;
0353    vv = v;
0354    eval_subtract(t, u, vv);
0355 }
0356 template <class T, class U>
0357 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_subtract_default(T& t, const T& u, const U& v)
0358 {
0359    T vv(v);
0360    eval_subtract(t, u, vv);
0361 }
0362 template <class T, class U>
0363 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && is_signed_number<T>::value && (number_category<T>::value != number_kind_complex)>::type eval_subtract_default(T& t, const U& u, const T& v)
0364 {
0365    eval_subtract(t, v, u);
0366    if(!eval_is_zero(t) || (eval_signbit(u) != eval_signbit(v)))
0367       t.negate();
0368 }
0369 template <class T, class U>
0370 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && is_signed_number<T>::value && (number_category<T>::value == number_kind_complex)>::type eval_subtract_default(T& t, const U& u, const T& v)
0371 {
0372    eval_subtract(t, v, u);
0373    t.negate();
0374 }
0375 template <class T, class U>
0376 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value && is_unsigned_number<T>::value>::type eval_subtract_default(T& t, const U& u, const T& v)
0377 {
0378    T temp;
0379    temp = u;
0380    eval_subtract(t, temp, v);
0381 }
0382 template <class T, class U>
0383 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value && is_unsigned_number<T>::value>::type eval_subtract_default(T& t, const U& u, const T& v)
0384 {
0385    T temp(u);
0386    eval_subtract(t, temp, v);
0387 }
0388 template <class T, class U, class V>
0389 inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract_default(T& t, const U& u, const V& v)
0390 {
0391    BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
0392    {
0393       if ((void*)&t == (void*)&v)
0394       {
0395          eval_subtract(t, u);
0396          t.negate();
0397       }
0398       else
0399       {
0400          t = u;
0401          eval_subtract(t, v);
0402       }
0403    }
0404    else
0405    {
0406       t = u;
0407       eval_subtract(t, v);
0408    }
0409 }
0410 template <class T, class U, class V>
0411 inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(T& t, const U& u, const V& v)
0412 {
0413    eval_subtract_default(t, u, v);
0414 }
0415 
0416 template <class T>
0417 inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply_default(T& t, const T& u, const T& v)
0418 {
0419    if (&t == &v)
0420    {
0421       eval_multiply(t, u);
0422    }
0423    else if (&t == &u)
0424    {
0425       eval_multiply(t, v);
0426    }
0427    else
0428    {
0429       t = u;
0430       eval_multiply(t, v);
0431    }
0432 }
0433 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
0434 template <class T, class U>
0435 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_multiply_default(T& t, const T& u, const U& v)
0436 {
0437    T vv;
0438    vv = v;
0439    eval_multiply(t, u, vv);
0440 }
0441 template <class T, class U>
0442 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_multiply_default(T& t, const T& u, const U& v)
0443 {
0444    T vv(v);
0445    eval_multiply(t, u, vv);
0446 }
0447 template <class T, class U>
0448 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_multiply_default(T& t, const U& u, const T& v)
0449 {
0450    eval_multiply(t, v, u);
0451 }
0452 #endif
0453 template <class T, class U, class V>
0454 inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply_default(T& t, const U& u, const V& v)
0455 {
0456    BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
0457    {
0458       if ((void*)&t == (void*)&v)
0459       {
0460          eval_multiply(t, u);
0461       }
0462       else
0463       {
0464          t = number<T>::canonical_value(u);
0465          eval_multiply(t, v);
0466       }
0467    }
0468    else
0469    {
0470       t = number<T>::canonical_value(u);
0471       eval_multiply(t, v);
0472    }
0473 }
0474 template <class T, class U, class V>
0475 inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(T& t, const U& u, const V& v)
0476 {
0477    eval_multiply_default(t, u, v);
0478 }
0479 
0480 template <class T>
0481 inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply_add(T& t, const T& u, const T& v, const T& x)
0482 {
0483    if ((void*)&x == (void*)&t)
0484    {
0485       T z;
0486       z = number<T>::canonical_value(x);
0487       eval_multiply_add(t, u, v, z);
0488    }
0489    else
0490    {
0491       eval_multiply(t, u, v);
0492       eval_add(t, x);
0493    }
0494 }
0495 
0496 template <class T, class U>
0497 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_same<T, U>::value, T>::type make_T(const U& u)
0498 {
0499    T t;
0500    t = number<T>::canonical_value(u);
0501    return t;
0502 }
0503 template <class T>
0504 inline BOOST_MP_CXX14_CONSTEXPR const T& make_T(const T& t)
0505 {
0506    return t;
0507 }
0508 
0509 template <class T, class U, class V, class X>
0510 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!std::is_same<T, U>::value && std::is_same<T, V>::value)>::type eval_multiply_add(T& t, const U& u, const V& v, const X& x)
0511 {
0512    eval_multiply_add(t, make_T<T>(u), make_T<T>(v), make_T<T>(x));
0513 }
0514 template <class T, class U, class V, class X>
0515 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value && std::is_same<T, V>::value>::type eval_multiply_add(T& t, const U& u, const V& v, const X& x)
0516 {
0517    eval_multiply_add(t, v, u, x);
0518 }
0519 template <class T, class U, class V, class X>
0520 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!std::is_same<T, U>::value && std::is_same<T, V>::value)>::type eval_multiply_subtract(T& t, const U& u, const V& v, const X& x)
0521 {
0522    if ((void*)&x == (void*)&t)
0523    {
0524       T z;
0525       z = x;
0526       eval_multiply_subtract(t, u, v, z);
0527    }
0528    else
0529    {
0530       eval_multiply(t, u, v);
0531       eval_subtract(t, x);
0532    }
0533 }
0534 template <class T, class U, class V, class X>
0535 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value && std::is_same<T, V>::value>::type eval_multiply_subtract(T& t, const U& u, const V& v, const X& x)
0536 {
0537    eval_multiply_subtract(t, v, u, x);
0538 }
0539 
0540 template <class T, class U, class V>
0541 BOOST_MP_CXX14_CONSTEXPR void eval_divide(T& t, const U& u, const V& v);
0542 
0543 template <class T>
0544 inline BOOST_MP_CXX14_CONSTEXPR void eval_divide_default(T& t, const T& u, const T& v)
0545 {
0546    if (&t == &u)
0547       eval_divide(t, v);
0548    else if (&t == &v)
0549    {
0550       T temp;
0551       eval_divide(temp, u, v);
0552       temp.swap(t);
0553    }
0554    else
0555    {
0556       t = u;
0557       eval_divide(t, v);
0558    }
0559 }
0560 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
0561 template <class T, class U>
0562 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_divide_default(T& t, const T& u, const U& v)
0563 {
0564    T vv;
0565    vv = v;
0566    eval_divide(t, u, vv);
0567 }
0568 template <class T, class U>
0569 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_divide_default(T& t, const T& u, const U& v)
0570 {
0571    T vv(v);
0572    eval_divide(t, u, vv);
0573 }
0574 template <class T, class U>
0575 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_divide_default(T& t, const U& u, const T& v)
0576 {
0577    T uu;
0578    uu = u;
0579    eval_divide(t, uu, v);
0580 }
0581 template <class T, class U>
0582 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_divide_default(T& t, const U& u, const T& v)
0583 {
0584    T uu(u);
0585    eval_divide(t, uu, v);
0586 }
0587 #endif
0588 template <class T, class U, class V>
0589 inline BOOST_MP_CXX14_CONSTEXPR void eval_divide_default(T& t, const U& u, const V& v)
0590 {
0591    BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
0592    {
0593       if ((void*)&t == (void*)&v)
0594       {
0595          T temp;
0596          temp = u;
0597          eval_divide(temp, v);
0598          t = temp;
0599       }
0600       else
0601       {
0602          t = u;
0603          eval_divide(t, v);
0604       }
0605    }
0606    else
0607    {
0608       t = u;
0609       eval_divide(t, v);
0610    }
0611 }
0612 template <class T, class U, class V>
0613 inline BOOST_MP_CXX14_CONSTEXPR void eval_divide(T& t, const U& u, const V& v)
0614 {
0615    eval_divide_default(t, u, v);
0616 }
0617 
0618 template <class T, class U, class V>
0619 BOOST_MP_CXX14_CONSTEXPR void eval_modulus(T& t, const U& u, const V& v);
0620 
0621 template <class T>
0622 inline BOOST_MP_CXX14_CONSTEXPR void eval_modulus_default(T& t, const T& u, const T& v)
0623 {
0624    if (&t == &u)
0625       eval_modulus(t, v);
0626    else if (&t == &v)
0627    {
0628       T temp;
0629       eval_modulus(temp, u, v);
0630       temp.swap(t);
0631    }
0632    else
0633    {
0634       t = u;
0635       eval_modulus(t, v);
0636    }
0637 }
0638 template <class T, class U>
0639 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_modulus_default(T& t, const T& u, const U& v)
0640 {
0641    T vv;
0642    vv = v;
0643    eval_modulus(t, u, vv);
0644 }
0645 template <class T, class U>
0646 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_modulus_default(T& t, const T& u, const U& v)
0647 {
0648    T vv(v);
0649    eval_modulus(t, u, vv);
0650 }
0651 template <class T, class U>
0652 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_modulus_default(T& t, const U& u, const T& v)
0653 {
0654    T uu;
0655    uu = u;
0656    eval_modulus(t, uu, v);
0657 }
0658 template <class T, class U>
0659 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_modulus_default(T& t, const U& u, const T& v)
0660 {
0661    T uu(u);
0662    eval_modulus(t, uu, v);
0663 }
0664 template <class T, class U, class V>
0665 inline BOOST_MP_CXX14_CONSTEXPR void eval_modulus_default(T& t, const U& u, const V& v)
0666 {
0667    BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
0668    {
0669       if ((void*)&t == (void*)&v)
0670       {
0671          T temp(u);
0672          eval_modulus(temp, v);
0673          t = temp;
0674       }
0675       else
0676       {
0677          t = u;
0678          eval_modulus(t, v);
0679       }
0680    }
0681    else
0682    {
0683       t = u;
0684       eval_modulus(t, v);
0685    }
0686 }
0687 template <class T, class U, class V>
0688 inline BOOST_MP_CXX14_CONSTEXPR void eval_modulus(T& t, const U& u, const V& v)
0689 {
0690    eval_modulus_default(t, u, v);
0691 }
0692 
0693 template <class T, class U, class V>
0694 BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_and(T& t, const U& u, const V& v);
0695 
0696 template <class T>
0697 inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_and_default(T& t, const T& u, const T& v)
0698 {
0699    if (&t == &v)
0700    {
0701       eval_bitwise_and(t, u);
0702    }
0703    else if (&t == &u)
0704    {
0705       eval_bitwise_and(t, v);
0706    }
0707    else
0708    {
0709       t = u;
0710       eval_bitwise_and(t, v);
0711    }
0712 }
0713 template <class T, class U>
0714 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_convertible<U, T>::value>::type eval_bitwise_and_default(T& t, const T& u, const U& v)
0715 {
0716    T vv;
0717    vv = v;
0718    eval_bitwise_and(t, u, vv);
0719 }
0720 template <class T, class U>
0721 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, T>::value>::type eval_bitwise_and_default(T& t, const T& u, const U& v)
0722 {
0723    T vv(v);
0724    eval_bitwise_and(t, u, vv);
0725 }
0726 template <class T, class U>
0727 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_and_default(T& t, const U& u, const T& v)
0728 {
0729    eval_bitwise_and(t, v, u);
0730 }
0731 template <class T, class U, class V>
0732 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value || std::is_same<T, V>::value>::type eval_bitwise_and_default(T& t, const U& u, const V& v)
0733 {
0734    t = u;
0735    eval_bitwise_and(t, v);
0736 }
0737 template <class T, class U, class V>
0738 inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_and(T& t, const U& u, const V& v)
0739 {
0740    eval_bitwise_and_default(t, u, v);
0741 }
0742 
0743 template <class T, class U, class V>
0744 BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_or(T& t, const U& u, const V& v);
0745 
0746 template <class T>
0747 inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_or_default(T& t, const T& u, const T& v)
0748 {
0749    if (&t == &v)
0750    {
0751       eval_bitwise_or(t, u);
0752    }
0753    else if (&t == &u)
0754    {
0755       eval_bitwise_or(t, v);
0756    }
0757    else
0758    {
0759       t = u;
0760       eval_bitwise_or(t, v);
0761    }
0762 }
0763 template <class T, class U>
0764 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_bitwise_or_default(T& t, const T& u, const U& v)
0765 {
0766    T vv;
0767    vv = v;
0768    eval_bitwise_or(t, u, vv);
0769 }
0770 template <class T, class U>
0771 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_bitwise_or_default(T& t, const T& u, const U& v)
0772 {
0773    T vv(v);
0774    eval_bitwise_or(t, u, vv);
0775 }
0776 template <class T, class U>
0777 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_or_default(T& t, const U& u, const T& v)
0778 {
0779    eval_bitwise_or(t, v, u);
0780 }
0781 template <class T, class U, class V>
0782 inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_or_default(T& t, const U& u, const V& v)
0783 {
0784    BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
0785    {
0786       if ((void*)&t == (void*)&v)
0787       {
0788          eval_bitwise_or(t, u);
0789       }
0790       else
0791       {
0792          t = u;
0793          eval_bitwise_or(t, v);
0794       }
0795    }
0796    else
0797    {
0798       t = u;
0799       eval_bitwise_or(t, v);
0800    }
0801 }
0802 template <class T, class U, class V>
0803 inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_or(T& t, const U& u, const V& v)
0804 {
0805    eval_bitwise_or_default(t, u, v);
0806 }
0807 
0808 template <class T, class U, class V>
0809 BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_xor(T& t, const U& u, const V& v);
0810 
0811 template <class T>
0812 inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_xor_default(T& t, const T& u, const T& v)
0813 {
0814    if (&t == &v)
0815    {
0816       eval_bitwise_xor(t, u);
0817    }
0818    else if (&t == &u)
0819    {
0820       eval_bitwise_xor(t, v);
0821    }
0822    else
0823    {
0824       t = u;
0825       eval_bitwise_xor(t, v);
0826    }
0827 }
0828 template <class T, class U>
0829 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_bitwise_xor_default(T& t, const T& u, const U& v)
0830 {
0831    T vv;
0832    vv = v;
0833    eval_bitwise_xor(t, u, vv);
0834 }
0835 template <class T, class U>
0836 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_bitwise_xor_default(T& t, const T& u, const U& v)
0837 {
0838    T vv(v);
0839    eval_bitwise_xor(t, u, vv);
0840 }
0841 template <class T, class U>
0842 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_xor_default(T& t, const U& u, const T& v)
0843 {
0844    eval_bitwise_xor(t, v, u);
0845 }
0846 template <class T, class U, class V>
0847 inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_xor_default(T& t, const U& u, const V& v)
0848 {
0849    BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
0850    {
0851       if ((void*)&t == (void*)&v)
0852       {
0853          eval_bitwise_xor(t, u);
0854       }
0855       else
0856       {
0857          t = u;
0858          eval_bitwise_xor(t, v);
0859       }
0860    }
0861    else
0862    {
0863       t = u;
0864       eval_bitwise_xor(t, v);
0865    }
0866 }
0867 template <class T, class U, class V>
0868 inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_xor(T& t, const U& u, const V& v)
0869 {
0870    eval_bitwise_xor_default(t, u, v);
0871 }
0872 
0873 template <class T>
0874 inline BOOST_MP_CXX14_CONSTEXPR void eval_increment(T& val)
0875 {
0876    using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
0877    eval_add(val, static_cast<ui_type>(1u));
0878 }
0879 template <class T>
0880 inline BOOST_MP_CXX14_CONSTEXPR void eval_decrement(T& val)
0881 {
0882    using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
0883    eval_subtract(val, static_cast<ui_type>(1u));
0884 }
0885 
0886 template <class T, class U, class V>
0887 inline BOOST_MP_CXX14_CONSTEXPR void eval_left_shift(T& result, const U& arg, const V val)
0888 {
0889    result = arg;
0890    eval_left_shift(result, val);
0891 }
0892 
0893 template <class T, class U, class V>
0894 inline BOOST_MP_CXX14_CONSTEXPR void eval_right_shift(T& result, const U& arg, const V val)
0895 {
0896    result = arg;
0897    eval_right_shift(result, val);
0898 }
0899 
0900 template <class T>
0901 inline BOOST_MP_CXX14_CONSTEXPR bool eval_is_zero(const T& val)
0902 {
0903    using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
0904    return val.compare(static_cast<ui_type>(0)) == 0;
0905 }
0906 template <class T>
0907 inline BOOST_MP_CXX14_CONSTEXPR int eval_get_sign(const T& val)
0908 {
0909    using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
0910    return val.compare(static_cast<ui_type>(0));
0911 }
0912 
0913 template <class T, class V, class U>
0914 inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp2(T& result, const V& v1, const U& v2, const std::false_type&, const std::false_type&)
0915 {
0916    using component_number_type = typename component_type<number<T> >::type;
0917 
0918    boost::multiprecision::detail::scoped_precision_options<component_number_type> sp(result);
0919    (void)sp;
0920 
0921    component_number_type x(v1), y(v2);
0922    assign_components(result, x.backend(), y.backend());
0923 }
0924 template <class T, class V, class U>
0925 inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp2(T& result, const V& v1, const U& v2, const std::true_type&, const std::false_type&)
0926 {
0927    boost::multiprecision::detail::scoped_source_precision<number<V>> scope;
0928    (void)scope;
0929    assign_components_imp2(result, number<V>(v1), v2, std::false_type(), std::false_type());
0930 }
0931 template <class T, class V, class U>
0932 inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp2(T& result, const V& v1, const U& v2, const std::true_type&, const std::true_type&)
0933 {
0934    boost::multiprecision::detail::scoped_source_precision<number<V>> scope1;
0935    boost::multiprecision::detail::scoped_source_precision<number<U>> scope2;
0936    (void)scope1;
0937    (void)scope2;
0938    assign_components_imp2(result, number<V>(v1), number<U>(v2), std::false_type(), std::false_type());
0939 }
0940 template <class T, class V, class U>
0941 inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp2(T& result, const V& v1, const U& v2, const std::false_type&, const std::true_type&)
0942 {
0943    boost::multiprecision::detail::scoped_source_precision<number<U>> scope;
0944    (void)scope;
0945    assign_components_imp2(result, v1, number<U>(v2), std::false_type(), std::false_type());
0946 }
0947 
0948 
0949 template <class T, class V, class U>
0950 inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp(T& result, const V& v1, const U& v2, const std::integral_constant<int, number_kind_rational>&)
0951 {
0952    result = v1;
0953    T t;
0954    t = v2;
0955    eval_divide(result, t);
0956 }
0957 
0958 template <class T, class V, class U, int N>
0959 inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp(T& result, const V& v1, const U& v2, const std::integral_constant<int, N>&)
0960 {
0961    assign_components_imp2(result, v1, v2, boost::multiprecision::detail::is_backend<V>(), boost::multiprecision::detail::is_backend<U>());
0962 }
0963 
0964 template <class T, class V, class U>
0965 inline BOOST_MP_CXX14_CONSTEXPR void assign_components(T& result, const V& v1, const U& v2)
0966 {
0967    return assign_components_imp(result, v1, v2, typename number_category<T>::type());
0968 }
0969 #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
0970 template <class Result, class Traits>
0971 inline void assign_from_string_view(Result& result, const std::basic_string_view<char, Traits>& view)
0972 {
0973    // since most (all?) backends require a const char* to construct from, we just
0974    // convert to that:
0975    std::string s(view);
0976    result = s.c_str();
0977 }
0978 template <class Result, class Traits>
0979 inline void assign_from_string_view(Result& result, const std::basic_string_view<char, Traits>& view_x, const std::basic_string_view<char, Traits>& view_y)
0980 {
0981    // since most (all?) backends require a const char* to construct from, we just
0982    // convert to that:
0983    std::string x(view_x), y(view_y);
0984    assign_components(result, x.c_str(), y.c_str());
0985 }
0986 #endif
0987 template <class R, int b>
0988 struct has_enough_bits
0989 {
0990    template <class T>
0991    struct type : public std::integral_constant<bool, !std::is_same<R, T>::value && (std::numeric_limits<T>::digits >= b)>
0992    {};
0993 };
0994 
0995 template <class R>
0996 struct terminal
0997 {
0998    BOOST_MP_CXX14_CONSTEXPR terminal(const R& v) : value(v) {}
0999    BOOST_MP_CXX14_CONSTEXPR terminal() {}
1000    BOOST_MP_CXX14_CONSTEXPR terminal& operator=(R val)
1001    {
1002       value = val;
1003       return *this;
1004    }
1005    R value;
1006    BOOST_MP_CXX14_CONSTEXPR operator R() const { return value; }
1007 };
1008 
1009 template <class Tuple, int i, class T, bool = (i == std::tuple_size<Tuple>::value)>
1010 struct find_index_of_type
1011 {
1012    static constexpr int value =
1013       std::is_same<T, typename std::tuple_element<static_cast<std::size_t>(i), Tuple>::type>::value
1014          ? i
1015          : find_index_of_type<Tuple, i + 1, T>::value;
1016 };
1017 template <class Tuple, int i, class T>
1018 struct find_index_of_type<Tuple, i, T, true>
1019 {
1020    static constexpr int value = -1;
1021 };
1022 
1023 
1024 template <class R, class B>
1025 struct calculate_next_larger_type
1026 {
1027    // Find which list we're looking through:
1028    using list_type = typename std::conditional<
1029        boost::multiprecision::detail::is_signed<R>::value && boost::multiprecision::detail::is_integral<R>::value,
1030        typename B::signed_types,
1031        typename std::conditional<
1032            boost::multiprecision::detail::is_unsigned<R>::value,
1033            typename B::unsigned_types,
1034            typename B::float_types>::type>::type;
1035    static constexpr int start = find_index_of_type<list_type, 0, R>::value;
1036    static constexpr int index_of_type = boost::multiprecision::detail::find_index_of_large_enough_type<list_type, start == INT_MAX ? 0 : start + 1, boost::multiprecision::detail::bits_of<R>::value> ::value;
1037    using type = typename boost::multiprecision::detail::dereference_tuple<index_of_type, list_type, terminal<R> >::type;
1038 };
1039 
1040 template <class R, class T>
1041 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<R>::value, bool>::type check_in_range(const T& t)
1042 {
1043    // Can t fit in an R?
1044    if ((t > 0) && std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::is_bounded && (t > (std::numeric_limits<R>::max)()))
1045       return true;
1046    else
1047       return false;
1048 }
1049 
1050 template <class R, class B>
1051 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<R>::value>::type eval_convert_to(R* result, const B& backend)
1052 {
1053    using next_type = typename calculate_next_larger_type<R, B>::type;
1054    next_type                                               n = next_type();
1055    eval_convert_to(&n, backend);
1056    BOOST_IF_CONSTEXPR(!boost::multiprecision::detail::is_unsigned<R>::value && std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::is_bounded)
1057    {
1058       if(n > static_cast<next_type>((std::numeric_limits<R>::max)()))
1059       {
1060          *result = (std::numeric_limits<R>::max)();
1061          return;
1062       }
1063    }
1064    BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_specialized&& std::numeric_limits<R>::is_bounded)
1065    {
1066       if (n < static_cast<next_type>((std::numeric_limits<R>::min)()))
1067       {
1068          *result = (std::numeric_limits<R>::min)();
1069          return;
1070       }
1071    }
1072    *result = static_cast<R>(n);
1073 }
1074 
1075 template <class R, class B>
1076 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !boost::multiprecision::detail::is_integral<R>::value && !std::is_enum<R>::value>::type eval_convert_to(R* result, const B& backend)
1077 {
1078    using next_type = typename calculate_next_larger_type<R, B>::type;
1079    next_type                                               n = next_type();
1080    eval_convert_to(&n, backend);
1081    BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::is_bounded)
1082    {
1083       if ((n > (next_type)(std::numeric_limits<R>::max)() || (n < (next_type) - (std::numeric_limits<R>::max)())))
1084       {
1085          *result = n > 0 ? (std::numeric_limits<R>::max)() : -(std::numeric_limits<R>::max)();
1086       }
1087       else
1088          *result = static_cast<R>(n);
1089    }
1090    else
1091       *result = static_cast<R>(n);
1092 }
1093 
1094 template <class R, class B>
1095 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_enum<R>::value>::type eval_convert_to(R* result, const B& backend)
1096 {
1097    typename std::underlying_type<R>::type t{};
1098    eval_convert_to(&t, backend);
1099    *result = static_cast<R>(t);
1100 }
1101 
1102 #ifndef BOOST_MP_STANDALONE
1103 template <class R, class B>
1104 inline void last_chance_eval_convert_to(terminal<R>* result, const B& backend, const std::integral_constant<bool, false>&)
1105 {
1106    //
1107    // We ran out of types to try for the conversion, try
1108    // a lexical_cast and hope for the best:
1109    //
1110    BOOST_IF_CONSTEXPR (std::numeric_limits<R>::is_integer && !std::numeric_limits<R>::is_signed)
1111       if (eval_get_sign(backend) < 0)
1112          BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to convert negative value to an unsigned integer results in undefined behaviour"));
1113    BOOST_MP_TRY {
1114       result->value = boost::lexical_cast<R>(backend.str(0, std::ios_base::fmtflags(0)));
1115    }
1116    BOOST_MP_CATCH (const bad_lexical_cast&)
1117    {
1118       if (eval_get_sign(backend) < 0)
1119       {
1120          BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_integer && !std::numeric_limits<R>::is_signed)
1121             *result = (std::numeric_limits<R>::max)(); // we should never get here, exception above will be raised.
1122          else BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_integer)
1123             *result = (std::numeric_limits<R>::min)();
1124          else
1125             *result = -(std::numeric_limits<R>::max)();
1126       }
1127       else
1128          *result = (std::numeric_limits<R>::max)();
1129    }
1130    BOOST_MP_CATCH_END
1131 }
1132 
1133 template <class R, class B>
1134 inline void last_chance_eval_convert_to(terminal<R>* result, const B& backend, const std::integral_constant<bool, true>&)
1135 {
1136    //
1137    // Last chance conversion to an unsigned integer.
1138    // We ran out of types to try for the conversion, try
1139    // a lexical_cast and hope for the best:
1140    //
1141    if (eval_get_sign(backend) < 0)
1142       BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to convert negative value to an unsigned integer results in undefined behaviour"));
1143    BOOST_MP_TRY {
1144       B t(backend);
1145       R mask = ~static_cast<R>(0u);
1146       eval_bitwise_and(t, mask);
1147       result->value = boost::lexical_cast<R>(t.str(0, std::ios_base::fmtflags(0)));
1148    }
1149    BOOST_MP_CATCH (const bad_lexical_cast&)
1150    {
1151       // We should never really get here...
1152       *result = (std::numeric_limits<R>::max)();
1153    }
1154    BOOST_MP_CATCH_END
1155 }
1156 #else // Using standalone mode
1157 
1158 template <class R, class B>
1159 inline void last_chance_eval_convert_to(terminal<R>*, const B&, const std::integral_constant<bool, false>&)
1160 {
1161    static_assert(sizeof(R) == 1, "This type can not be used in standalone mode. Please de-activate and file a bug at https://github.com/boostorg/multiprecision/");
1162 }
1163 
1164 template <class R, class B>
1165 inline void last_chance_eval_convert_to(terminal<R>* result, const B& backend, const std::integral_constant<bool, true>&)
1166 {
1167    static_cast<void>(result);
1168    static_cast<void>(backend);
1169 
1170    static_assert(sizeof(R) == 1, "This type can not be used in standalone mode. Please de-activate and file a bug at https://github.com/boostorg/multiprecision/");
1171 }
1172 #endif
1173 
1174 template <class R, class B>
1175 inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(terminal<R>* result, const B& backend)
1176 {
1177    using tag_type = std::integral_constant<bool, boost::multiprecision::detail::is_unsigned<R>::value && number_category<B>::value == number_kind_integer>;
1178    last_chance_eval_convert_to(result, backend, tag_type());
1179 }
1180 
1181 template <class B1, class B2, expression_template_option et>
1182 inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(terminal<number<B1, et> >* result, const B2& backend)
1183 {
1184    //
1185    // We ran out of types to try for the conversion, try
1186    // a generic conversion and hope for the best:
1187    //
1188    boost::multiprecision::detail::generic_interconvert(result->value.backend(), backend, number_category<B1>(), number_category<B2>());
1189 }
1190 
1191 template <class B>
1192 inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(std::string* result, const B& backend)
1193 {
1194    *result = backend.str(0, std::ios_base::fmtflags(0));
1195 }
1196 
1197 template <class B>
1198 inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(std::complex<float>* result, const B& backend)
1199 {
1200    using scalar_type = typename scalar_result_from_possible_complex<multiprecision::number<B> >::type;
1201    scalar_type                                                                            re, im;
1202    eval_real(re.backend(), backend);
1203    eval_imag(im.backend(), backend);
1204 
1205    *result = std::complex<float>(re.template convert_to<float>(), im.template convert_to<float>());
1206 }
1207 
1208 template <class B>
1209 inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(std::complex<double>* result, const B& backend)
1210 {
1211    using scalar_type = typename scalar_result_from_possible_complex<multiprecision::number<B> >::type;
1212    scalar_type                                                                            re, im;
1213    eval_real(re.backend(), backend);
1214    eval_imag(im.backend(), backend);
1215 
1216    *result = std::complex<double>(re.template convert_to<double>(), im.template convert_to<double>());
1217 }
1218 
1219 template <class B>
1220 inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(std::complex<long double>* result, const B& backend)
1221 {
1222    using scalar_type = typename scalar_result_from_possible_complex<multiprecision::number<B> >::type;
1223    scalar_type                                                                            re, im;
1224    eval_real(re.backend(), backend);
1225    eval_imag(im.backend(), backend);
1226 
1227    *result = std::complex<long double>(re.template convert_to<long double>(), im.template convert_to<long double>());
1228 }
1229 
1230 //
1231 // Functions:
1232 //
1233 template <class T, class U>
1234 inline BOOST_MP_CXX14_CONSTEXPR void eval_abs(T& result, const U& arg)
1235 {
1236    using type_list = typename U::signed_types            ;
1237    using front = typename std::tuple_element<0, type_list>::type;
1238    result = arg;
1239    if (arg.compare(front(0)) < 0)
1240       result.negate();
1241 }
1242 template <class T, class U>
1243 inline BOOST_MP_CXX14_CONSTEXPR void eval_fabs(T& result, const U& arg)
1244 {
1245    static_assert(number_category<T>::value == number_kind_floating_point, "The fabs function is only valid for floating point types.");
1246    using type_list = typename U::signed_types            ;
1247    using front = typename std::tuple_element<0, type_list>::type;
1248    result = arg;
1249    if (arg.compare(front(0)) < 0)
1250       result.negate();
1251 }
1252 
1253 template <class Backend>
1254 inline BOOST_MP_CXX14_CONSTEXPR int eval_fpclassify(const Backend& arg)
1255 {
1256    static_assert(number_category<Backend>::value == number_kind_floating_point, "The fpclassify function is only valid for floating point types.");
1257    return eval_is_zero(arg) ? FP_ZERO : FP_NORMAL;
1258 }
1259 
1260 template <class T>
1261 inline BOOST_MP_CXX14_CONSTEXPR void eval_fmod(T& result, const T& a, const T& b)
1262 {
1263    static_assert(number_category<T>::value == number_kind_floating_point, "The fmod function is only valid for floating point types.");
1264    if ((&result == &a) || (&result == &b))
1265    {
1266       T temp;
1267       eval_fmod(temp, a, b);
1268       result = temp;
1269       return;
1270    }
1271    switch (eval_fpclassify(a))
1272    {
1273    case FP_ZERO:
1274       result = a;
1275       return;
1276    case FP_INFINITE:
1277    case FP_NAN:
1278       result = std::numeric_limits<number<T> >::quiet_NaN().backend();
1279       errno  = EDOM;
1280       return;
1281    }
1282    switch (eval_fpclassify(b))
1283    {
1284    case FP_ZERO:
1285    case FP_NAN:
1286       result = std::numeric_limits<number<T> >::quiet_NaN().backend();
1287       errno  = EDOM;
1288       return;
1289    }
1290    T n;
1291    eval_divide(result, a, b);
1292    if (eval_get_sign(result) < 0)
1293       eval_ceil(n, result);
1294    else
1295       eval_floor(n, result);
1296    eval_multiply(n, b);
1297    eval_subtract(result, a, n);
1298    if (eval_get_sign(result) != 0)
1299    {
1300       //
1301       // Sanity check, that due to rounding errors in division, 
1302       // we haven't accidently calculated the wrong value:
1303       // See https://github.com/boostorg/multiprecision/issues/604 for an example.
1304       //
1305       if (eval_get_sign(result) == eval_get_sign(b))
1306       {
1307          if (result.compare(b) >= 0)
1308          {
1309             eval_subtract(result, b);
1310          }
1311       }
1312       else
1313       {
1314          n = b;
1315          n.negate();
1316          if (result.compare(n) >= 0)
1317          {
1318             eval_subtract(result, n);
1319          }
1320       }
1321    }
1322 }
1323 template <class T, class A>
1324 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_fmod(T& result, const T& x, const A& a)
1325 {
1326    using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type         ;
1327    using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
1328    cast_type                                                                      c;
1329    c = a;
1330    eval_fmod(result, x, c);
1331 }
1332 
1333 template <class T, class A>
1334 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_fmod(T& result, const A& x, const T& a)
1335 {
1336    using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type         ;
1337    using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
1338    cast_type                                                                      c;
1339    c = x;
1340    eval_fmod(result, c, a);
1341 }
1342 
1343 template <class T>
1344 BOOST_MP_CXX14_CONSTEXPR void eval_round(T& result, const T& a);
1345 
1346 template <class T>
1347 inline BOOST_MP_CXX14_CONSTEXPR void eval_remquo(T& result, const T& a, const T& b, int* pi)
1348 {
1349    static_assert(number_category<T>::value == number_kind_floating_point, "The remquo function is only valid for floating point types.");
1350    if ((&result == &a) || (&result == &b))
1351    {
1352       T temp;
1353       eval_remquo(temp, a, b, pi);
1354       result = temp;
1355       return;
1356    }
1357    T n;
1358    eval_divide(result, a, b);
1359    eval_round(n, result);
1360    eval_convert_to(pi, n);
1361    eval_multiply(n, b);
1362    eval_subtract(result, a, n);
1363    if (eval_is_zero(result))
1364    {
1365       if (eval_signbit(a))
1366          result.negate();
1367    }
1368 }
1369 template <class T, class A>
1370 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_remquo(T& result, const T& x, const A& a, int* pi)
1371 {
1372    using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type         ;
1373    using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
1374    cast_type                                                                      c = cast_type();
1375    c = a;
1376    eval_remquo(result, x, c, pi);
1377 }
1378 template <class T, class A>
1379 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_remquo(T& result, const A& x, const T& a, int* pi)
1380 {
1381    using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type         ;
1382    using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
1383    cast_type                                                                      c = cast_type();
1384    c = x;
1385    eval_remquo(result, c, a, pi);
1386 }
1387 template <class T, class U, class V>
1388 inline BOOST_MP_CXX14_CONSTEXPR void eval_remainder(T& result, const U& a, const V& b)
1389 {
1390    int i(0);
1391    eval_remquo(result, a, b, &i);
1392 }
1393 
1394 template <class B>
1395 BOOST_MP_CXX14_CONSTEXPR bool eval_gt(const B& a, const B& b);
1396 template <class T, class U>
1397 BOOST_MP_CXX14_CONSTEXPR bool eval_gt(const T& a, const U& b);
1398 template <class B>
1399 BOOST_MP_CXX14_CONSTEXPR bool eval_lt(const B& a, const B& b);
1400 template <class T, class U>
1401 BOOST_MP_CXX14_CONSTEXPR bool eval_lt(const T& a, const U& b);
1402 
1403 template <class T>
1404 inline BOOST_MP_CXX14_CONSTEXPR void eval_fdim(T& result, const T& a, const T& b)
1405 {
1406    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1407    const ui_type                                                                zero = 0u;
1408    switch (eval_fpclassify(b))
1409    {
1410    case FP_NAN:
1411    case FP_INFINITE:
1412       result = zero;
1413       return;
1414    }
1415    switch (eval_fpclassify(a))
1416    {
1417    case FP_NAN:
1418       result = zero;
1419       return;
1420    case FP_INFINITE:
1421       result = a;
1422       return;
1423    }
1424    if (eval_gt(a, b))
1425    {
1426       eval_subtract(result, a, b);
1427    }
1428    else
1429       result = zero;
1430 }
1431 
1432 template <class T, class A>
1433 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value>::type eval_fdim(T& result, const T& a, const A& b)
1434 {
1435    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1436    using arithmetic_type = typename boost::multiprecision::detail::canonical<A, T>::type       ;
1437    const ui_type                                                                zero        = 0u;
1438    arithmetic_type                                                              canonical_b = b;
1439    switch (BOOST_MP_FPCLASSIFY(b))
1440    {
1441    case FP_NAN:
1442    case FP_INFINITE:
1443       result = zero;
1444       return;
1445    }
1446    switch (eval_fpclassify(a))
1447    {
1448    case FP_NAN:
1449       result = zero;
1450       return;
1451    case FP_INFINITE:
1452       result = a;
1453       return;
1454    }
1455    if (eval_gt(a, canonical_b))
1456    {
1457       eval_subtract(result, a, canonical_b);
1458    }
1459    else
1460       result = zero;
1461 }
1462 
1463 template <class T, class A>
1464 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value>::type eval_fdim(T& result, const A& a, const T& b)
1465 {
1466    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1467    using arithmetic_type = typename boost::multiprecision::detail::canonical<A, T>::type       ;
1468    const ui_type                                                                zero        = 0u;
1469    arithmetic_type                                                              canonical_a = a;
1470    switch (eval_fpclassify(b))
1471    {
1472    case FP_NAN:
1473    case FP_INFINITE:
1474       result = zero;
1475       return;
1476    }
1477    switch (BOOST_MP_FPCLASSIFY(a))
1478    {
1479    case FP_NAN:
1480       result = zero;
1481       return;
1482    case FP_INFINITE:
1483       result = std::numeric_limits<number<T> >::infinity().backend();
1484       return;
1485    }
1486    if (eval_gt(canonical_a, b))
1487    {
1488       eval_subtract(result, canonical_a, b);
1489    }
1490    else
1491       result = zero;
1492 }
1493 
1494 template <class T>
1495 inline BOOST_MP_CXX14_CONSTEXPR void eval_trunc(T& result, const T& a)
1496 {
1497    static_assert(number_category<T>::value == number_kind_floating_point, "The trunc function is only valid for floating point types.");
1498    switch (eval_fpclassify(a))
1499    {
1500    case FP_NAN:
1501       errno = EDOM;
1502       // fallthrough...
1503    case FP_ZERO:
1504    case FP_INFINITE:
1505       result = a;
1506       return;
1507    }
1508    if (eval_get_sign(a) < 0)
1509       eval_ceil(result, a);
1510    else
1511       eval_floor(result, a);
1512 }
1513 
1514 template <class T>
1515 inline BOOST_MP_CXX14_CONSTEXPR void eval_modf(T& result, T const& arg, T* pipart)
1516 {
1517    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1518    int                                                                          c = eval_fpclassify(arg);
1519    if (c == static_cast<int>(FP_NAN))
1520    {
1521       if (pipart)
1522          *pipart = arg;
1523       result = arg;
1524       return;
1525    }
1526    else if (c == static_cast<int>(FP_INFINITE))
1527    {
1528       if (pipart)
1529          *pipart = arg;
1530       result = ui_type(0u);
1531       return;
1532    }
1533    if (pipart)
1534    {
1535       eval_trunc(*pipart, arg);
1536       eval_subtract(result, arg, *pipart);
1537    }
1538    else
1539    {
1540       T ipart;
1541       eval_trunc(ipart, arg);
1542       eval_subtract(result, arg, ipart);
1543    }
1544 }
1545 
1546 template <class T>
1547 inline BOOST_MP_CXX14_CONSTEXPR void eval_round(T& result, const T& a)
1548 {
1549    static_assert(number_category<T>::value == number_kind_floating_point, "The round function is only valid for floating point types.");
1550    using fp_type = typename boost::multiprecision::detail::canonical<float, T>::type;
1551    int                                                                       c = eval_fpclassify(a);
1552    if (c == static_cast<int>(FP_NAN))
1553    {
1554       result = a;
1555       errno  = EDOM;
1556       return;
1557    }
1558    if ((c == FP_ZERO) || (c == static_cast<int>(FP_INFINITE)))
1559    {
1560       result = a;
1561    }
1562    else if (eval_get_sign(a) < 0)
1563    {
1564       eval_subtract(result, a, fp_type(0.5f));
1565       eval_ceil(result, result);
1566    }
1567    else
1568    {
1569       eval_add(result, a, fp_type(0.5f));
1570       eval_floor(result, result);
1571    }
1572 }
1573 
1574 template <class B>
1575 BOOST_MP_CXX14_CONSTEXPR void eval_lcm(B& result, const B& a, const B& b);
1576 template <class B>
1577 BOOST_MP_CXX14_CONSTEXPR void eval_gcd(B& result, const B& a, const B& b);
1578 
1579 template <class T, class Arithmetic>
1580 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Arithmetic>::value >::type eval_gcd(T& result, const T& a, const Arithmetic& b)
1581 {
1582    using si_type = typename boost::multiprecision::detail::canonical<Arithmetic, T>::type;
1583    using default_ops::eval_gcd;
1584    T t;
1585    t = static_cast<si_type>(b);
1586    eval_gcd(result, a, t);
1587 }
1588 template <class T, class Arithmetic>
1589 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Arithmetic>::value >::type eval_gcd(T& result, const Arithmetic& a, const T& b)
1590 {
1591    eval_gcd(result, b, a);
1592 }
1593 template <class T, class Arithmetic>
1594 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Arithmetic>::value >::type eval_lcm(T& result, const T& a, const Arithmetic& b)
1595 {
1596    using si_type = typename boost::multiprecision::detail::canonical<Arithmetic, T>::type;
1597    using default_ops::eval_lcm;
1598    T t;
1599    t = static_cast<si_type>(b);
1600    eval_lcm(result, a, t);
1601 }
1602 template <class T, class Arithmetic>
1603 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Arithmetic>::value >::type eval_lcm(T& result, const Arithmetic& a, const T& b)
1604 {
1605    eval_lcm(result, b, a);
1606 }
1607 
1608 template <class T>
1609 inline BOOST_MP_CXX14_CONSTEXPR std::size_t eval_lsb(const T& val)
1610 {
1611    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1612    int                                                                          c = eval_get_sign(val);
1613    if (c == 0)
1614    {
1615       BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
1616    }
1617    if (c < 0)
1618    {
1619       BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
1620    }
1621    std::size_t result = 0;
1622    T        mask, t;
1623    mask = ui_type(1);
1624    do
1625    {
1626       eval_bitwise_and(t, mask, val);
1627       ++result;
1628       eval_left_shift(mask, 1);
1629    } while (eval_is_zero(t));
1630 
1631    return --result;
1632 }
1633 
1634 template <class T>
1635 inline BOOST_MP_CXX14_CONSTEXPR std::ptrdiff_t eval_msb(const T& val)
1636 {
1637    int c = eval_get_sign(val);
1638    if (c == 0)
1639    {
1640       BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
1641    }
1642    if (c < 0)
1643    {
1644       BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
1645    }
1646    //
1647    // This implementation is really really rubbish - it does
1648    // a linear scan for the most-significant-bit.  We should really
1649    // do a binary search, but as none of our backends actually needs
1650    // this implementation, we'll leave it for now.  In fact for most
1651    // backends it's likely that there will always be a more efficient
1652    // native implementation possible.
1653    //
1654    std::size_t result = 0;
1655    T        t(val);
1656    while (!eval_is_zero(t))
1657    {
1658       eval_right_shift(t, 1);
1659       ++result;
1660    }
1661    --result;
1662 
1663    return static_cast<std::ptrdiff_t>(result);
1664 }
1665 
1666 template <class T>
1667 inline BOOST_MP_CXX14_CONSTEXPR bool eval_bit_test(const T& val, std::size_t index)
1668 {
1669    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1670    T                                                                            mask, t;
1671    mask = ui_type(1);
1672    eval_left_shift(mask, index);
1673    eval_bitwise_and(t, mask, val);
1674    return !eval_is_zero(t);
1675 }
1676 
1677 template <class T>
1678 inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_set(T& val, std::size_t index)
1679 {
1680    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1681    T                                                                            mask;
1682    mask = ui_type(1);
1683    eval_left_shift(mask, index);
1684    eval_bitwise_or(val, mask);
1685 }
1686 
1687 template <class T>
1688 inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_flip(T& val, std::size_t index)
1689 {
1690    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1691    T                                                                            mask;
1692    mask = ui_type(1);
1693    eval_left_shift(mask, index);
1694    eval_bitwise_xor(val, mask);
1695 }
1696 
1697 template <class T>
1698 inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_unset(T& val, std::size_t index)
1699 {
1700    using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
1701    T                                                                            mask, t;
1702    mask = ui_type(1);
1703    eval_left_shift(mask, index);
1704    eval_bitwise_and(t, mask, val);
1705    if (!eval_is_zero(t))
1706       eval_bitwise_xor(val, mask);
1707 }
1708 
1709 template <class Backend>
1710 BOOST_MP_CXX14_CONSTEXPR void eval_qr(const Backend& x, const Backend& y, Backend& q, Backend& r);
1711 
1712 template <class Backend>
1713 BOOST_MP_CXX14_CONSTEXPR void eval_karatsuba_sqrt(Backend& result, const Backend& x, Backend& r, Backend& t, size_t bits)
1714 {
1715    using default_ops::eval_is_zero;
1716    using default_ops::eval_subtract;
1717    using default_ops::eval_right_shift;
1718    using default_ops::eval_left_shift;
1719    using default_ops::eval_bit_set;
1720    using default_ops::eval_decrement;
1721    using default_ops::eval_bitwise_and;
1722    using default_ops::eval_add;
1723    using default_ops::eval_qr;
1724 
1725    using small_uint = typename std::tuple_element<0, typename Backend::unsigned_types>::type;
1726 
1727    constexpr small_uint zero = 0u;
1728 
1729    // we can calculate it faster with std::sqrt
1730 #ifdef BOOST_HAS_INT128
1731    if (bits <= 128)
1732    {
1733       uint128_type a{}, b{}, c{};
1734       eval_convert_to(&a, x);
1735       c = boost::multiprecision::detail::karatsuba_sqrt(a, b, bits);
1736       r = number<Backend>::canonical_value(b);
1737       result = number<Backend>::canonical_value(c);
1738       return;
1739    }
1740 #else
1741    if (bits <= std::numeric_limits<std::uintmax_t>::digits)
1742    {
1743       std::uintmax_t a{ 0 }, b{ 0 }, c{ 0 };
1744       eval_convert_to(&a, x);
1745       c = boost::multiprecision::detail::karatsuba_sqrt(a, b, bits);
1746       r = number<Backend>::canonical_value(b);
1747       result = number<Backend>::canonical_value(c);
1748       return;
1749    }
1750 #endif
1751    // https://hal.inria.fr/file/index/docid/72854/filename/RR-3805.pdf
1752    std::size_t  b = bits / 4;
1753    Backend q(x);
1754    eval_right_shift(q, b * 2);
1755    Backend s;
1756    eval_karatsuba_sqrt(s, q, r, t, bits - b * 2);
1757    t = zero;
1758    eval_bit_set(t, static_cast<unsigned>(b * 2));
1759    eval_left_shift(r, b);
1760    eval_decrement(t);
1761    eval_bitwise_and(t, x);
1762    eval_right_shift(t, b);
1763    eval_add(t, r);
1764    eval_left_shift(s, 1u);
1765    eval_qr(t, s, q, r);
1766    eval_left_shift(r, b);
1767    t = zero;
1768    eval_bit_set(t, static_cast<unsigned>(b));
1769    eval_decrement(t);
1770    eval_bitwise_and(t, x);
1771    eval_add(r, t);
1772    eval_left_shift(s, b - 1);
1773    eval_add(s, q);
1774    eval_multiply(q, q);
1775    // we substract after, so it works for unsigned integers too
1776    if (r.compare(q) < 0)
1777    {
1778       t = s;
1779       eval_left_shift(t, 1u);
1780       eval_decrement(t);
1781       eval_add(r, t);
1782       eval_decrement(s);
1783    }
1784    eval_subtract(r, q);
1785    result = s;
1786 }
1787 
1788 template <class B>
1789 void BOOST_MP_CXX14_CONSTEXPR eval_integer_sqrt_bitwise(B& s, B& r, const B& x)
1790 {
1791    //
1792    // This is slow bit-by-bit integer square root, see for example
1793    // http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29
1794    // There are better methods such as http://hal.inria.fr/docs/00/07/28/54/PDF/RR-3805.pdf
1795    // and http://hal.inria.fr/docs/00/07/21/13/PDF/RR-4475.pdf which should be implemented
1796    // at some point.
1797    //
1798    using ui_type = typename boost::multiprecision::detail::canonical<unsigned char, B>::type;
1799 
1800    s = ui_type(0u);
1801    if (eval_get_sign(x) == 0)
1802    {
1803       r = ui_type(0u);
1804       return;
1805    }
1806    std::ptrdiff_t g = static_cast<std::ptrdiff_t>(eval_msb(x));
1807    if (g <= 1)
1808    {
1809       s = ui_type(1);
1810       eval_subtract(r, x, s);
1811       return;
1812    }
1813 
1814    B t;
1815    r = x;
1816    g /= 2;
1817    std::ptrdiff_t org_g = g;
1818    eval_bit_set(s, static_cast<std::size_t>(g));
1819    eval_bit_set(t, static_cast<std::size_t>(2 * g));
1820    eval_subtract(r, x, t);
1821    --g;
1822    if (eval_get_sign(r) == 0)
1823       return;
1824    std::ptrdiff_t msbr = static_cast<std::ptrdiff_t>(eval_msb(r));
1825    do
1826    {
1827       if (msbr >= org_g + g + 1)
1828       {
1829          t = s;
1830          eval_left_shift(t, static_cast<std::size_t>(g + 1));
1831          eval_bit_set(t, static_cast<std::size_t>(2 * g));
1832          if (t.compare(r) <= 0)
1833          {
1834             BOOST_MP_ASSERT(g >= 0);
1835             eval_bit_set(s, static_cast<std::size_t>(g));
1836             eval_subtract(r, t);
1837             if (eval_get_sign(r) == 0)
1838                return;
1839             msbr = static_cast<std::ptrdiff_t>(eval_msb(r));
1840          }
1841       }
1842       --g;
1843    } while (g >= 0);
1844 }
1845 
1846 template <class Backend>
1847 BOOST_MP_CXX14_CONSTEXPR void eval_integer_sqrt(Backend& result, Backend& r, const Backend& x)
1848 {
1849 #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
1850    // recursive Karatsuba sqrt can cause issues in constexpr context:
1851    if (BOOST_MP_IS_CONST_EVALUATED(result.size()))
1852       return eval_integer_sqrt_bitwise(result, r, x);
1853 #endif
1854    using small_uint = typename std::tuple_element<0, typename Backend::unsigned_types>::type;
1855 
1856    constexpr small_uint zero = 0u;
1857 
1858    if (eval_is_zero(x))
1859    {
1860       r = zero;
1861       result = zero;
1862       return;
1863    }
1864    Backend t;
1865    eval_karatsuba_sqrt(result, x, r, t, eval_msb(x) + 1);
1866 }
1867 
1868 template <class B>
1869 inline BOOST_MP_CXX14_CONSTEXPR void eval_conj(B& result, const B& val)
1870 {
1871    result = val; // assume non-complex result.
1872 }
1873 template <class B>
1874 inline BOOST_MP_CXX14_CONSTEXPR void eval_proj(B& result, const B& val)
1875 {
1876    result = val; // assume non-complex result.
1877 }
1878 
1879 //
1880 // These have to implemented by the backend, declared here so that our macro generated code compiles OK.
1881 //
1882 template <class T>
1883 typename std::enable_if<sizeof(T) == 0>::type eval_floor();
1884 template <class T>
1885 typename std::enable_if<sizeof(T) == 0>::type eval_ceil();
1886 template <class T>
1887 typename std::enable_if<sizeof(T) == 0>::type eval_trunc();
1888 template <class T>
1889 typename std::enable_if<sizeof(T) == 0>::type eval_sqrt();
1890 template <class T>
1891 typename std::enable_if<sizeof(T) == 0>::type eval_ldexp();
1892 template <class T>
1893 typename std::enable_if<sizeof(T) == 0>::type eval_frexp();
1894 // TODO implement default versions of these:
1895 template <class T>
1896 typename std::enable_if<sizeof(T) == 0>::type eval_asinh();
1897 template <class T>
1898 typename std::enable_if<sizeof(T) == 0>::type eval_acosh();
1899 template <class T>
1900 typename std::enable_if<sizeof(T) == 0>::type eval_atanh();
1901 
1902 //
1903 // eval_logb and eval_scalbn simply assume base 2 and forward to
1904 // eval_ldexp and eval_frexp:
1905 //
1906 template <class B>
1907 inline BOOST_MP_CXX14_CONSTEXPR typename B::exponent_type eval_ilogb(const B& val)
1908 {
1909    static_assert(!std::numeric_limits<number<B> >::is_specialized || (std::numeric_limits<number<B> >::radix == 2), "The default implementation of ilogb requires a base 2 number type");
1910    typename B::exponent_type e(0);
1911    switch (eval_fpclassify(val))
1912    {
1913    case FP_NAN:
1914 #ifdef FP_ILOGBNAN
1915       return FP_ILOGBNAN > 0 ? (std::numeric_limits<typename B::exponent_type>::max)() : (std::numeric_limits<typename B::exponent_type>::min)();
1916 #else
1917       return (std::numeric_limits<typename B::exponent_type>::max)();
1918 #endif
1919    case FP_INFINITE:
1920       return (std::numeric_limits<typename B::exponent_type>::max)();
1921    case FP_ZERO:
1922       return (std::numeric_limits<typename B::exponent_type>::min)();
1923    }
1924    B result;
1925    eval_frexp(result, val, &e);
1926    return e - 1;
1927 }
1928 
1929 template <class B>
1930 inline BOOST_MP_CXX14_CONSTEXPR void eval_logb(B& result, const B& val)
1931 {
1932    switch (eval_fpclassify(val))
1933    {
1934    case FP_NAN:
1935       result = val;
1936       errno  = EDOM;
1937       return;
1938    case FP_ZERO:
1939       result = std::numeric_limits<number<B> >::infinity().backend();
1940       result.negate();
1941       errno = ERANGE;
1942       return;
1943    case FP_INFINITE:
1944       result = val;
1945       if (eval_signbit(val))
1946          result.negate();
1947       return;
1948    }
1949    using max_t = typename std::conditional<std::is_same<std::intmax_t, long>::value, long long, std::intmax_t>::type;
1950    result = static_cast<max_t>(eval_ilogb(val));
1951 }
1952 template <class B, class A>
1953 inline BOOST_MP_CXX14_CONSTEXPR void eval_scalbn(B& result, const B& val, A e)
1954 {
1955    static_assert(!std::numeric_limits<number<B> >::is_specialized || (std::numeric_limits<number<B> >::radix == 2), "The default implementation of scalbn requires a base 2 number type");
1956    eval_ldexp(result, val, static_cast<typename B::exponent_type>(e));
1957 }
1958 template <class B, class A>
1959 inline BOOST_MP_CXX14_CONSTEXPR void eval_scalbln(B& result, const B& val, A e)
1960 {
1961    eval_scalbn(result, val, e);
1962 }
1963 
1964 template <class T>
1965 inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val, std::integral_constant<bool, true> const&, const std::integral_constant<bool, false>&)
1966 {
1967    return eval_fpclassify(val) == FP_NAN;
1968 }
1969 template <class T>
1970 inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val, std::integral_constant<bool, false> const&, const std::integral_constant<bool, true>&)
1971 {
1972    return BOOST_MP_ISNAN(val);
1973 }
1974 template <class T>
1975 inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T&, std::integral_constant<bool, false> const&, const std::integral_constant<bool, false>&)
1976 {
1977    return false;
1978 }
1979 
1980 template <class T>
1981 inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val)
1982 {
1983    return is_arg_nan(val, std::integral_constant<bool, boost::multiprecision::detail::is_backend<T>::value>(), std::is_floating_point<T>());
1984 }
1985 
1986 template <class T, class U, class V>
1987 inline BOOST_MP_CXX14_CONSTEXPR void eval_fmax(T& result, const U& a, const V& b)
1988 {
1989    if (is_arg_nan(a))
1990       result = number<T>::canonical_value(b);
1991    else if (is_arg_nan(b))
1992       result = number<T>::canonical_value(a);
1993    else if (eval_lt(number<T>::canonical_value(a), number<T>::canonical_value(b)))
1994       result = number<T>::canonical_value(b);
1995    else
1996       result = number<T>::canonical_value(a);
1997 }
1998 template <class T, class U, class V>
1999 inline BOOST_MP_CXX14_CONSTEXPR void eval_fmin(T& result, const U& a, const V& b)
2000 {
2001    if (is_arg_nan(a))
2002       result = number<T>::canonical_value(b);
2003    else if (is_arg_nan(b))
2004       result = number<T>::canonical_value(a);
2005    else if (eval_lt(number<T>::canonical_value(a), number<T>::canonical_value(b)))
2006       result = number<T>::canonical_value(a);
2007    else
2008       result = number<T>::canonical_value(b);
2009 }
2010 
2011 template <class R, class T, class U>
2012 inline BOOST_MP_CXX14_CONSTEXPR void eval_hypot(R& result, const T& a, const U& b)
2013 {
2014    //
2015    // Normalize x and y, so that both are positive and x >= y:
2016    //
2017    R x, y;
2018    x = number<R>::canonical_value(a);
2019    y = number<R>::canonical_value(b);
2020    if (eval_get_sign(x) < 0)
2021       x.negate();
2022    if (eval_get_sign(y) < 0)
2023       y.negate();
2024 
2025    // Special case, see C99 Annex F.
2026    // The order of the if's is important: do not change!
2027    int c1 = eval_fpclassify(x);
2028    int c2 = eval_fpclassify(y);
2029 
2030    if (c1 == FP_ZERO)
2031    {
2032       result = y;
2033       return;
2034    }
2035    if (c2 == FP_ZERO)
2036    {
2037       result = x;
2038       return;
2039    }
2040    if (c1 == FP_INFINITE)
2041    {
2042       result = x;
2043       return;
2044    }
2045    if ((c2 == FP_INFINITE) || (c2 == FP_NAN))
2046    {
2047       result = y;
2048       return;
2049    }
2050    if (c1 == FP_NAN)
2051    {
2052       result = x;
2053       return;
2054    }
2055 
2056    if (eval_gt(y, x))
2057       x.swap(y);
2058 
2059    eval_multiply(result, x, std::numeric_limits<number<R> >::epsilon().backend());
2060 
2061    if (eval_gt(result, y))
2062    {
2063       result = x;
2064       return;
2065    }
2066 
2067    R rat;
2068    eval_divide(rat, y, x);
2069    eval_multiply(result, rat, rat);
2070    eval_increment(result);
2071    eval_sqrt(rat, result);
2072    eval_multiply(result, rat, x);
2073 }
2074 
2075 template <class R, class T>
2076 inline BOOST_MP_CXX14_CONSTEXPR void eval_nearbyint(R& result, const T& a)
2077 {
2078    eval_round(result, a);
2079 }
2080 template <class R, class T>
2081 inline BOOST_MP_CXX14_CONSTEXPR void eval_rint(R& result, const T& a)
2082 {
2083    eval_nearbyint(result, a);
2084 }
2085 
2086 template <class T>
2087 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_backend<T>::value, int>::type eval_signbit(const T& val)
2088 {
2089    return eval_get_sign(val) < 0 ? 1 : 0;
2090 }
2091 
2092 //
2093 // Real and imaginary parts:
2094 //
2095 template <class To, class From>
2096 inline BOOST_MP_CXX14_CONSTEXPR void eval_real(To& to, const From& from)
2097 {
2098    to = from;
2099 }
2100 template <class To, class From>
2101 inline BOOST_MP_CXX14_CONSTEXPR void eval_imag(To& to, const From&)
2102 {
2103    using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
2104    to = ui_type(0);
2105 }
2106 
2107 } // namespace default_ops
2108 namespace default_ops_adl {
2109 
2110 template <class To, class From>
2111 inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real_imp(To& to, const From& from)
2112 {
2113    using to_component_type = typename component_type<number<To> >::type;
2114    typename to_component_type::backend_type           to_component;
2115    to_component = from;
2116    eval_set_real(to, to_component);
2117 }
2118 template <class To, class From>
2119 inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag_imp(To& to, const From& from)
2120 {
2121    using to_component_type = typename component_type<number<To> >::type;
2122    typename to_component_type::backend_type           to_component;
2123    to_component = from;
2124    eval_set_imag(to, to_component);
2125 }
2126 
2127 } // namespace default_ops_adl
2128 namespace default_ops {
2129 
2130 template <class To, class From>
2131 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<To>::value == number_kind_complex>::type eval_set_real(To& to, const From& from)
2132 {
2133    default_ops_adl::eval_set_real_imp(to, from);
2134 }
2135 template <class To, class From>
2136 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<To>::value != number_kind_complex>::type eval_set_real(To& to, const From& from)
2137 {
2138    to = from;
2139 }
2140 
2141 template <class To, class From>
2142 inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(To& to, const From& from)
2143 {
2144    default_ops_adl::eval_set_imag_imp(to, from);
2145 }
2146 
2147 template <class T>
2148 inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(T& to, const T& from)
2149 {
2150    to = from;
2151 }
2152 template <class T>
2153 void BOOST_MP_CXX14_CONSTEXPR eval_set_imag(T&, const T&)
2154 {
2155    static_assert(sizeof(T) == INT_MAX, "eval_set_imag needs to be specialised for each specific backend");
2156 }
2157 
2158 //
2159 // These functions are implemented in separate files, but expanded inline here,
2160 // DO NOT CHANGE THE ORDER OF THESE INCLUDES:
2161 //
2162 #include <boost/multiprecision/detail/functions/constants.hpp>
2163 #include <boost/multiprecision/detail/functions/pow.hpp>
2164 #include <boost/multiprecision/detail/functions/trig.hpp>
2165 
2166 } // namespace default_ops
2167 
2168 //
2169 // Default versions of floating point classification routines:
2170 //
2171 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2172 inline BOOST_MP_CXX14_CONSTEXPR int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2173 {
2174    using multiprecision::default_ops::eval_fpclassify;
2175    return eval_fpclassify(arg.backend());
2176 }
2177 template <class tag, class A1, class A2, class A3, class A4>
2178 inline BOOST_MP_CXX14_CONSTEXPR int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2179 {
2180    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2181    return fpclassify                                                                     BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
2182 }
2183 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2184 inline BOOST_MP_CXX14_CONSTEXPR bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2185 {
2186    int v = fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg);
2187    return (v != static_cast<int>(FP_INFINITE)) && (v != static_cast<int>(FP_NAN));
2188 }
2189 template <class tag, class A1, class A2, class A3, class A4>
2190 inline BOOST_MP_CXX14_CONSTEXPR bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2191 {
2192    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2193    return isfinite                                                                       BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
2194 }
2195 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2196 inline BOOST_MP_CXX14_CONSTEXPR bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2197 {
2198    return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_NAN);
2199 }
2200 template <class tag, class A1, class A2, class A3, class A4>
2201 inline BOOST_MP_CXX14_CONSTEXPR bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2202 {
2203    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2204    return isnan                                                                          BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
2205 }
2206 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2207 inline BOOST_MP_CXX14_CONSTEXPR bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2208 {
2209    return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_INFINITE);
2210 }
2211 template <class tag, class A1, class A2, class A3, class A4>
2212 inline BOOST_MP_CXX14_CONSTEXPR bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2213 {
2214    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2215    return isinf                                                                          BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
2216 }
2217 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2218 inline BOOST_MP_CXX14_CONSTEXPR bool isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2219 {
2220    return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_NORMAL);
2221 }
2222 template <class tag, class A1, class A2, class A3, class A4>
2223 inline BOOST_MP_CXX14_CONSTEXPR bool isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2224 {
2225    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2226    return isnormal                                                                       BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
2227 }
2228 
2229 // Default versions of sign manipulation functions, if individual backends can do better than this
2230 // (for example with signed zero), then they should overload these functions further:
2231 
2232 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2233 inline BOOST_MP_CXX14_CONSTEXPR int sign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2234 {
2235    return arg.sign();
2236 }
2237 template <class tag, class A1, class A2, class A3, class A4>
2238 inline BOOST_MP_CXX14_CONSTEXPR int sign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2239 {
2240    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2241    return sign                                                                           BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
2242 }
2243 
2244 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2245 inline BOOST_MP_CXX14_CONSTEXPR bool signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2246 {
2247    using default_ops::eval_signbit;
2248    return static_cast<bool>(eval_signbit(arg.backend()));
2249 }
2250 template <class tag, class A1, class A2, class A3, class A4>
2251 inline BOOST_MP_CXX14_CONSTEXPR bool signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2252 {
2253    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2254    return static_cast<bool>(signbit BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg)));
2255 }
2256 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2257 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> changesign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2258 {
2259    return -arg;
2260 }
2261 template <class tag, class A1, class A2, class A3, class A4>
2262 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type changesign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2263 {
2264    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2265    return changesign                                                                     BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
2266 }
2267 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2268 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
2269 {
2270    return (boost::multiprecision::signbit)(a) != (boost::multiprecision::signbit)(b) ? (boost::multiprecision::changesign)(a) : a;
2271 }
2272 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
2273 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::detail::expression<tag, A1, A2, A3, A4>& b)
2274 {
2275    return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
2276 }
2277 template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
2278 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
2279 {
2280    return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
2281 }
2282 template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
2283 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
2284 {
2285    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2286    return copysign                                                                       BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
2287 }
2288 //
2289 // real and imag:
2290 //
2291 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2292 inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type
2293 real(const multiprecision::number<Backend, ExpressionTemplates>& a)
2294 {
2295    using default_ops::eval_real;
2296    using result_type = typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type;
2297    boost::multiprecision::detail::scoped_default_precision<result_type>                                              precision_guard(a);
2298    result_type                                                                                                       result;
2299    eval_real(result.backend(), a.backend());
2300    return result;
2301 }
2302 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2303 inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type
2304 imag(const multiprecision::number<Backend, ExpressionTemplates>& a)
2305 {
2306    using default_ops::eval_imag;
2307    using result_type = typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type;
2308    boost::multiprecision::detail::scoped_default_precision<result_type>                                              precision_guard(a);
2309    result_type                                                                                                       result;
2310    eval_imag(result.backend(), a.backend());
2311    return result;
2312 }
2313 
2314 template <class tag, class A1, class A2, class A3, class A4>
2315 inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
2316 real(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2317 {
2318    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2319    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2320    return real(value_type(arg));
2321 }
2322 
2323 template <class tag, class A1, class A2, class A3, class A4>
2324 inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
2325 imag(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2326 {
2327    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2328    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2329    return imag(value_type(arg));
2330 }
2331 
2332 //
2333 // Complex number functions, these are overloaded at the Backend level, we just provide the
2334 // expression template versions here, plus overloads for non-complex types:
2335 //
2336 #ifdef BOOST_MP_MATH_AVAILABLE
2337 template <class T, expression_template_option ExpressionTemplates>
2338 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_complex, component_type<number<T, ExpressionTemplates>>>::type::type
2339 abs(const number<T, ExpressionTemplates>& v)
2340 {
2341    return std::move(boost::math::hypot(real(v), imag(v)));
2342 }
2343 template <class tag, class A1, class A2, class A3, class A4>
2344 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_complex, component_type<typename detail::expression<tag, A1, A2, A3, A4>::result_type>>::type::type
2345 abs(const detail::expression<tag, A1, A2, A3, A4>& v)
2346 {
2347    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2348    return std::move(abs(static_cast<number_type>(v)));
2349 }
2350 
2351 template <class T, expression_template_option ExpressionTemplates>
2352 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_complex, typename scalar_result_from_possible_complex<number<T, ExpressionTemplates> >::type>::type
2353 arg(const number<T, ExpressionTemplates>& v)
2354 {
2355    return std::move(atan2(imag(v), real(v)));
2356 }
2357 template <class T, expression_template_option ExpressionTemplates>
2358 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, typename scalar_result_from_possible_complex<number<T, ExpressionTemplates> >::type>::type
2359 arg(const number<T, ExpressionTemplates>&)
2360 {
2361    return 0;
2362 }
2363 template <class tag, class A1, class A2, class A3, class A4>
2364 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_complex || number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename scalar_result_from_possible_complex<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type>::type
2365 arg(const detail::expression<tag, A1, A2, A3, A4>& v)
2366 {
2367    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2368    return std::move(arg(static_cast<number_type>(v)));
2369 }
2370 #endif // BOOST_MP_MATH_AVAILABLE
2371 
2372 template <class T, expression_template_option ExpressionTemplates>
2373 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_complex, component_type<number<T, ExpressionTemplates>>>::type::type
2374 norm(const number<T, ExpressionTemplates>& v)
2375 {
2376    typename component_type<number<T, ExpressionTemplates> >::type a(real(v)), b(imag(v));
2377    return std::move(a * a + b * b);
2378 }
2379 template <class T, expression_template_option ExpressionTemplates>
2380 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value != number_kind_complex, typename scalar_result_from_possible_complex<number<T, ExpressionTemplates> >::type>::type
2381 norm(const number<T, ExpressionTemplates>& v)
2382 {
2383    return v * v;
2384 }
2385 template <class tag, class A1, class A2, class A3, class A4>
2386 inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
2387 norm(const detail::expression<tag, A1, A2, A3, A4>& v)
2388 {
2389    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2390    return std::move(norm(static_cast<number_type>(v)));
2391 }
2392 
2393 template <class Backend, expression_template_option ExpressionTemplates>
2394 BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type polar(number<Backend, ExpressionTemplates> const& r, number<Backend, ExpressionTemplates> const& theta)
2395 {
2396    return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
2397 }
2398 
2399 template <class tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
2400 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_same<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, ExpressionTemplates> >::value,
2401                      typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type>::type
2402 polar(detail::expression<tag, A1, A2, A3, A4> const& r, number<Backend, ExpressionTemplates> const& theta)
2403 {
2404    return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
2405 }
2406 
2407 template <class Backend, expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
2408 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_same<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, ExpressionTemplates> >::value,
2409                      typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type>::type
2410 polar(number<Backend, ExpressionTemplates> const& r, detail::expression<tag, A1, A2, A3, A4> const& theta)
2411 {
2412    return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
2413 }
2414 
2415 template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
2416 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_same<typename detail::expression<tag, A1, A2, A3, A4>::result_type, typename detail::expression<tagb, A1b, A2b, A3b, A4b>::result_type>::value,
2417                      typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type>::type
2418 polar(detail::expression<tag, A1, A2, A3, A4> const& r, detail::expression<tagb, A1b, A2b, A3b, A4b> const& theta)
2419 {
2420    using scalar_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2421    return typename complex_result_from_scalar<scalar_type>::type(scalar_type(r * cos(theta)), scalar_type(r * sin(theta)));
2422 }
2423 //
2424 // We also allow the first argument to polar to be an arithmetic type (probably a literal):
2425 //
2426 template <class Scalar, class Backend, expression_template_option ExpressionTemplates>
2427 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Scalar>::value, typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type>::type
2428 polar(Scalar const& r, number<Backend, ExpressionTemplates> const& theta)
2429 {
2430    return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
2431 }
2432 
2433 template <class tag, class A1, class A2, class A3, class A4, class Scalar>
2434 BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Scalar>::value,
2435                      typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type>::type
2436 polar(Scalar const& r, detail::expression<tag, A1, A2, A3, A4> const& theta)
2437 {
2438    using scalar_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2439    return typename complex_result_from_scalar<scalar_type>::type(scalar_type(r * cos(theta)), scalar_type(r * sin(theta)));
2440 }
2441 //
2442 // Single argument overloads:
2443 //
2444 template <class Backend, expression_template_option ExpressionTemplates>
2445 BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type polar(number<Backend, ExpressionTemplates> const& r)
2446 {
2447    return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(r);
2448 }
2449 
2450 template <class tag, class A1, class A2, class A3, class A4>
2451 BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
2452 polar(detail::expression<tag, A1, A2, A3, A4> const& r)
2453 {
2454    return typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type(r);
2455 }
2456 
2457 } // namespace multiprecision
2458 
2459 namespace math {
2460 
2461 //
2462 // Import Math functions here, so they can be found by Boost.Math:
2463 //
2464 using boost::multiprecision::changesign;
2465 using boost::multiprecision::copysign;
2466 using boost::multiprecision::fpclassify;
2467 using boost::multiprecision::isfinite;
2468 using boost::multiprecision::isinf;
2469 using boost::multiprecision::isnan;
2470 using boost::multiprecision::isnormal;
2471 using boost::multiprecision::sign;
2472 using boost::multiprecision::signbit;
2473 
2474 #ifndef BOOST_MP_MATH_AVAILABLE
2475 namespace policies {
2476 
2477 template <typename... Args>
2478 class policy {};
2479 
2480 template <typename T1, typename T2, typename T3, typename T4, typename T5>
2481 void raise_rounding_error(T1, T2, T3, T4, T5)
2482 {
2483    BOOST_MP_THROW_EXCEPTION(std::runtime_error("Rounding error"));
2484 }
2485 
2486 template <typename T1, typename T2, typename T3, typename T4, typename T5>
2487 void raise_overflow_error(T1, T2, T3, T4, T5)
2488 {
2489    BOOST_MP_THROW_EXCEPTION(std::overflow_error("Overflow error"));
2490 }
2491 
2492 template <typename T1, typename T2, typename T3, typename T4, typename T5>
2493 void raise_evaluation_error(T1, T2, T3, T4, T5)
2494 {
2495    BOOST_MP_THROW_EXCEPTION(std::runtime_error("Evaluation error"));
2496 }
2497 
2498 template <typename T1, typename T2, typename T3, typename T4, typename T5>
2499 void raise_domain_error(T1, T2, T3, T4, T5)
2500 {
2501    BOOST_MP_THROW_EXCEPTION(std::domain_error("Domain error"));
2502 }
2503 
2504 template <typename T, typename... Args>
2505 struct is_policy
2506 {
2507    static constexpr bool value = false;
2508 };
2509 
2510 template <typename... Args>
2511 struct is_policy<policy<Args...>>
2512 {
2513    static constexpr bool value = true;
2514 };
2515 
2516 } // namespace policies
2517 #endif
2518 
2519 } // namespace math
2520 
2521 namespace multiprecision {
2522 #ifdef BOOST_MP_MATH_AVAILABLE
2523 using c99_error_policy = ::boost::math::policies::policy<
2524     ::boost::math::policies::domain_error< ::boost::math::policies::errno_on_error>,
2525     ::boost::math::policies::pole_error< ::boost::math::policies::errno_on_error>,
2526     ::boost::math::policies::overflow_error< ::boost::math::policies::errno_on_error>,
2527     ::boost::math::policies::evaluation_error< ::boost::math::policies::errno_on_error>,
2528     ::boost::math::policies::rounding_error< ::boost::math::policies::errno_on_error> >;
2529 
2530 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2531 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
2532     asinh
2533     BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2534 {
2535    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2536    return boost::math::asinh(arg, c99_error_policy());
2537 }
2538 template <class tag, class A1, class A2, class A3, class A4>
2539 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex, typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
2540     asinh
2541     BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2542 {
2543    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2544    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2545    return asinh(value_type(arg));
2546 }
2547 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2548 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
2549     acosh
2550     BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2551 {
2552    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2553    return boost::math::acosh(arg, c99_error_policy());
2554 }
2555 template <class tag, class A1, class A2, class A3, class A4>
2556 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex, typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
2557     acosh
2558     BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2559 {
2560    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2561    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2562    return acosh(value_type(arg));
2563 }
2564 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2565 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
2566     atanh
2567     BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2568 {
2569    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2570    return boost::math::atanh(arg, c99_error_policy());
2571 }
2572 template <class tag, class A1, class A2, class A3, class A4>
2573 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex, typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
2574     atanh
2575     BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2576 {
2577    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2578    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2579    return atanh(value_type(arg));
2580 }
2581 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2582 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2583 {
2584    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2585    return boost::math::cbrt(arg, c99_error_policy());
2586 }
2587 template <class tag, class A1, class A2, class A3, class A4>
2588 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2589 {
2590    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2591    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2592    return cbrt(value_type(arg));
2593 }
2594 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2595 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2596 {
2597    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2598    return boost::math::erf(arg, c99_error_policy());
2599 }
2600 template <class tag, class A1, class A2, class A3, class A4>
2601 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type erf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2602 {
2603    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2604    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2605    return erf(value_type(arg));
2606 }
2607 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2608 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2609 {
2610    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2611    return boost::math::erfc(arg, c99_error_policy());
2612 }
2613 template <class tag, class A1, class A2, class A3, class A4>
2614 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2615 {
2616    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2617    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2618    return erfc(value_type(arg));
2619 }
2620 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2621 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2622 {
2623    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2624    return boost::math::expm1(arg, c99_error_policy());
2625 }
2626 template <class tag, class A1, class A2, class A3, class A4>
2627 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2628 {
2629    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2630    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2631    return expm1(value_type(arg));
2632 }
2633 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2634 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2635 {
2636    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2637    multiprecision::number<Backend, ExpressionTemplates>                                    result;
2638    result = boost::math::lgamma(arg, c99_error_policy());
2639    if ((boost::multiprecision::isnan)(result) && !(boost::multiprecision::isnan)(arg))
2640    {
2641       result = std::numeric_limits<multiprecision::number<Backend, ExpressionTemplates> >::infinity();
2642       errno  = ERANGE;
2643    }
2644    return result;
2645 }
2646 template <class tag, class A1, class A2, class A3, class A4>
2647 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2648 {
2649    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2650    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2651    return lgamma(value_type(arg));
2652 }
2653 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2654 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2655 {
2656    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2657    if ((arg == 0) && std::numeric_limits<multiprecision::number<Backend, ExpressionTemplates> >::has_infinity)
2658    {
2659       errno = ERANGE;
2660       return 1 / arg;
2661    }
2662    return boost::math::tgamma(arg, c99_error_policy());
2663 }
2664 template <class tag, class A1, class A2, class A3, class A4>
2665 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2666 {
2667    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2668    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2669    return tgamma(value_type(arg));
2670 }
2671 
2672 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2673 inline BOOST_MP_CXX14_CONSTEXPR long lrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2674 {
2675    return lround(arg);
2676 }
2677 template <class tag, class A1, class A2, class A3, class A4>
2678 inline BOOST_MP_CXX14_CONSTEXPR long lrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2679 {
2680    return lround(arg);
2681 }
2682 #ifndef BOOST_NO_LONG_LONG
2683 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2684 inline BOOST_MP_CXX14_CONSTEXPR long long llrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2685 {
2686    return llround(arg);
2687 }
2688 template <class tag, class A1, class A2, class A3, class A4>
2689 inline BOOST_MP_CXX14_CONSTEXPR long long llrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2690 {
2691    return llround(arg);
2692 }
2693 #endif
2694 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2695 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
2696 {
2697    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
2698    return boost::math::log1p(arg, c99_error_policy());
2699 }
2700 template <class tag, class A1, class A2, class A3, class A4>
2701 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
2702 {
2703    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2704    detail::scoped_default_precision<value_type>                                          precision_guard(arg);
2705    return log1p(value_type(arg));
2706 }
2707 
2708 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2709 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
2710 {
2711    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
2712    return boost::math::nextafter(a, b, c99_error_policy());
2713 }
2714 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
2715 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::detail::expression<tag, A1, A2, A3, A4>& b)
2716 {
2717    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
2718    return nextafter                                                                        BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
2719 }
2720 template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
2721 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
2722 {
2723    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
2724    return nextafter                                                                        BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
2725 }
2726 template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
2727 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
2728 {
2729    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2730    detail::scoped_default_precision<value_type>                                          precision_guard(a, b);
2731    return nextafter                                                                      BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
2732 }
2733 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
2734 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
2735 {
2736    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
2737    return boost::math::nextafter(a, b, c99_error_policy());
2738 }
2739 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
2740 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::detail::expression<tag, A1, A2, A3, A4>& b)
2741 {
2742    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
2743    return nexttoward                                                                       BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
2744 }
2745 template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
2746 inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
2747 {
2748    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
2749    return nexttoward                                                                       BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
2750 }
2751 template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
2752 inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
2753 {
2754    using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
2755    detail::scoped_default_precision<value_type>                                          precision_guard(a, b);
2756    return nexttoward                                                                     BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
2757 }
2758 #endif // BOOST_MP_MATH_AVAILABLE
2759 
2760 template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
2761 inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& add(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
2762 {
2763    static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
2764    static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
2765    using default_ops::eval_add;
2766    eval_add(result.backend(), a.backend(), b.backend());
2767    return result;
2768 }
2769 
2770 template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
2771 inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& subtract(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
2772 {
2773    static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
2774    static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
2775    using default_ops::eval_subtract;
2776    eval_subtract(result.backend(), a.backend(), b.backend());
2777    return result;
2778 }
2779 
2780 template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
2781 inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& multiply(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
2782 {
2783    static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
2784    static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
2785    using default_ops::eval_multiply;
2786    eval_multiply(result.backend(), a.backend(), b.backend());
2787    return result;
2788 }
2789 
2790 template <class B, expression_template_option ET, class I>
2791 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
2792 add(number<B, ET>& result, const I& a, const I& b)
2793 {
2794    using default_ops::eval_add;
2795    using canonical_type = typename detail::canonical<I, B>::type;
2796    eval_add(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
2797    return result;
2798 }
2799 
2800 template <class B, expression_template_option ET, class I>
2801 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
2802 subtract(number<B, ET>& result, const I& a, const I& b)
2803 {
2804    using default_ops::eval_subtract;
2805    using canonical_type = typename detail::canonical<I, B>::type;
2806    eval_subtract(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
2807    return result;
2808 }
2809 
2810 template <class B, expression_template_option ET, class I>
2811 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
2812 multiply(number<B, ET>& result, const I& a, const I& b)
2813 {
2814    using default_ops::eval_multiply;
2815    using canonical_type = typename detail::canonical<I, B>::type;
2816    eval_multiply(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
2817    return result;
2818 }
2819 
2820 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2821 inline BOOST_MP_CXX14_CONSTEXPR typename detail::expression<tag, A1, A2, A3, A4>::result_type trunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2822 {
2823    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2824    return std::move(trunc(number_type(v), pol));
2825 }
2826 
2827 template <class Backend, expression_template_option ExpressionTemplates, class Policy>
2828 inline BOOST_MP_CXX14_CONSTEXPR number<Backend, ExpressionTemplates> trunc(const number<Backend, ExpressionTemplates>& v, const Policy&)
2829 {
2830    using default_ops::eval_trunc;
2831    detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(v);
2832    number<Backend, ExpressionTemplates>                                                    result;
2833    eval_trunc(result.backend(), v.backend());
2834    return result;
2835 }
2836 
2837 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2838 inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2839 {
2840    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2841    number_type                                                           r(trunc(v, pol));
2842    if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
2843       return boost::math::policies::raise_rounding_error("boost::multiprecision::itrunc<%1%>(%1%)", nullptr, number_type(v), 0, pol);
2844    return r.template convert_to<int>();
2845 }
2846 template <class tag, class A1, class A2, class A3, class A4>
2847 inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
2848 {
2849    return itrunc(v, boost::math::policies::policy<>());
2850 }
2851 template <class Backend, expression_template_option ExpressionTemplates, class Policy>
2852 inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const number<Backend, ExpressionTemplates>& v, const Policy& pol)
2853 {
2854    number<Backend, ExpressionTemplates> r(trunc(v, pol));
2855    if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
2856       return boost::math::policies::raise_rounding_error("boost::multiprecision::itrunc<%1%>(%1%)", nullptr, v, 0, pol);
2857    return r.template convert_to<int>();
2858 }
2859 template <class Backend, expression_template_option ExpressionTemplates>
2860 inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const number<Backend, ExpressionTemplates>& v)
2861 {
2862    return itrunc(v, boost::math::policies::policy<>());
2863 }
2864 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2865 inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2866 {
2867    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2868    number_type                                                           r(trunc(v, pol));
2869    if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
2870       return boost::math::policies::raise_rounding_error("boost::multiprecision::ltrunc<%1%>(%1%)", nullptr, number_type(v), 0L, pol);
2871    return r.template convert_to<long>();
2872 }
2873 template <class tag, class A1, class A2, class A3, class A4>
2874 inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
2875 {
2876    return ltrunc(v, boost::math::policies::policy<>());
2877 }
2878 template <class T, expression_template_option ExpressionTemplates, class Policy>
2879 inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const number<T, ExpressionTemplates>& v, const Policy& pol)
2880 {
2881    number<T, ExpressionTemplates> r(trunc(v, pol));
2882    if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
2883       return boost::math::policies::raise_rounding_error("boost::multiprecision::ltrunc<%1%>(%1%)", nullptr, v, 0L, pol);
2884    return r.template convert_to<long>();
2885 }
2886 template <class T, expression_template_option ExpressionTemplates>
2887 inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const number<T, ExpressionTemplates>& v)
2888 {
2889    return ltrunc(v, boost::math::policies::policy<>());
2890 }
2891 #ifndef BOOST_NO_LONG_LONG
2892 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2893 inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2894 {
2895    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2896    number_type                                                           r(trunc(v, pol));
2897    if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
2898       return boost::math::policies::raise_rounding_error("boost::multiprecision::lltrunc<%1%>(%1%)", nullptr, number_type(v), 0LL, pol);
2899    return r.template convert_to<long long>();
2900 }
2901 template <class tag, class A1, class A2, class A3, class A4>
2902 inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
2903 {
2904    return lltrunc(v, boost::math::policies::policy<>());
2905 }
2906 template <class T, expression_template_option ExpressionTemplates, class Policy>
2907 inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const number<T, ExpressionTemplates>& v, const Policy& pol)
2908 {
2909    number<T, ExpressionTemplates> r(trunc(v, pol));
2910    if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
2911       return boost::math::policies::raise_rounding_error("boost::multiprecision::lltrunc<%1%>(%1%)", nullptr, v, 0LL, pol);
2912    return r.template convert_to<long long>();
2913 }
2914 template <class T, expression_template_option ExpressionTemplates>
2915 inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const number<T, ExpressionTemplates>& v)
2916 {
2917    return lltrunc(v, boost::math::policies::policy<>());
2918 }
2919 #endif
2920 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2921 inline BOOST_MP_CXX14_CONSTEXPR typename detail::expression<tag, A1, A2, A3, A4>::result_type round(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2922 {
2923    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2924    return std::move(round(static_cast<number_type>(v), pol));
2925 }
2926 template <class T, expression_template_option ExpressionTemplates, class Policy>
2927 inline BOOST_MP_CXX14_CONSTEXPR number<T, ExpressionTemplates> round(const number<T, ExpressionTemplates>& v, const Policy&)
2928 {
2929    using default_ops::eval_round;
2930    detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
2931    number<T, ExpressionTemplates>                                                    result;
2932    eval_round(result.backend(), v.backend());
2933    return result;
2934 }
2935 
2936 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2937 inline BOOST_MP_CXX14_CONSTEXPR int iround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2938 {
2939    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2940    number_type                                                           r(round(v, pol));
2941    if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
2942       return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, number_type(v), 0, pol);
2943    return r.template convert_to<int>();
2944 }
2945 template <class tag, class A1, class A2, class A3, class A4>
2946 inline BOOST_MP_CXX14_CONSTEXPR int iround(const detail::expression<tag, A1, A2, A3, A4>& v)
2947 {
2948    return iround(v, boost::math::policies::policy<>());
2949 }
2950 template <class T, expression_template_option ExpressionTemplates, class Policy>
2951 inline BOOST_MP_CXX14_CONSTEXPR int iround(const number<T, ExpressionTemplates>& v, const Policy& pol)
2952 {
2953    number<T, ExpressionTemplates> r(round(v, pol));
2954    if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
2955       return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, v, 0, pol);
2956    return r.template convert_to<int>();
2957 }
2958 template <class T, expression_template_option ExpressionTemplates>
2959 inline BOOST_MP_CXX14_CONSTEXPR int iround(const number<T, ExpressionTemplates>& v)
2960 {
2961    return iround(v, boost::math::policies::policy<>());
2962 }
2963 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2964 inline BOOST_MP_CXX14_CONSTEXPR long lround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2965 {
2966    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2967    number_type                                                           r(round(v, pol));
2968    if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
2969       return boost::math::policies::raise_rounding_error("boost::multiprecision::lround<%1%>(%1%)", nullptr, number_type(v), 0L, pol);
2970    return r.template convert_to<long>();
2971 }
2972 template <class tag, class A1, class A2, class A3, class A4>
2973 inline BOOST_MP_CXX14_CONSTEXPR long lround(const detail::expression<tag, A1, A2, A3, A4>& v)
2974 {
2975    return lround(v, boost::math::policies::policy<>());
2976 }
2977 template <class T, expression_template_option ExpressionTemplates, class Policy>
2978 inline BOOST_MP_CXX14_CONSTEXPR long lround(const number<T, ExpressionTemplates>& v, const Policy& pol)
2979 {
2980    number<T, ExpressionTemplates> r(round(v, pol));
2981    if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
2982       return boost::math::policies::raise_rounding_error("boost::multiprecision::lround<%1%>(%1%)", nullptr, v, 0L, pol);
2983    return r.template convert_to<long>();
2984 }
2985 template <class T, expression_template_option ExpressionTemplates>
2986 inline BOOST_MP_CXX14_CONSTEXPR long lround(const number<T, ExpressionTemplates>& v)
2987 {
2988    return lround(v, boost::math::policies::policy<>());
2989 }
2990 #ifndef BOOST_NO_LONG_LONG
2991 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2992 inline BOOST_MP_CXX14_CONSTEXPR long long llround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2993 {
2994    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
2995    number_type                                                           r(round(v, pol));
2996    if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
2997       return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, number_type(v), 0LL, pol);
2998    return r.template convert_to<long long>();
2999 }
3000 template <class tag, class A1, class A2, class A3, class A4>
3001 inline BOOST_MP_CXX14_CONSTEXPR long long llround(const detail::expression<tag, A1, A2, A3, A4>& v)
3002 {
3003    return llround(v, boost::math::policies::policy<>());
3004 }
3005 template <class T, expression_template_option ExpressionTemplates, class Policy>
3006 inline BOOST_MP_CXX14_CONSTEXPR long long llround(const number<T, ExpressionTemplates>& v, const Policy& pol)
3007 {
3008    number<T, ExpressionTemplates> r(round(v, pol));
3009    if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
3010       return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, v, 0LL, pol);
3011    return r.template convert_to<long long>();
3012 }
3013 template <class T, expression_template_option ExpressionTemplates>
3014 inline BOOST_MP_CXX14_CONSTEXPR long long llround(const number<T, ExpressionTemplates>& v)
3015 {
3016    return llround(v, boost::math::policies::policy<>());
3017 }
3018 #endif
3019 //
3020 // frexp does not return an expression template since we require the
3021 // integer argument to be evaluated even if the returned value is
3022 // not assigned to anything...
3023 //
3024 template <class T, expression_template_option ExpressionTemplates>
3025 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, short* pint)
3026 {
3027    using default_ops::eval_frexp;
3028    detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
3029    number<T, ExpressionTemplates>                                                    result;
3030    eval_frexp(result.backend(), v.backend(), pint);
3031    return result;
3032 }
3033 template <class tag, class A1, class A2, class A3, class A4>
3034 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
3035 frexp(const detail::expression<tag, A1, A2, A3, A4>& v, short* pint)
3036 {
3037    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
3038    return std::move(frexp(static_cast<number_type>(v), pint));
3039 }
3040 template <class T, expression_template_option ExpressionTemplates>
3041 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, int* pint)
3042 {
3043    using default_ops::eval_frexp;
3044    detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
3045    number<T, ExpressionTemplates>                                                    result;
3046    eval_frexp(result.backend(), v.backend(), pint);
3047    return result;
3048 }
3049 template <class tag, class A1, class A2, class A3, class A4>
3050 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
3051 frexp(const detail::expression<tag, A1, A2, A3, A4>& v, int* pint)
3052 {
3053    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
3054    return std::move(frexp(static_cast<number_type>(v), pint));
3055 }
3056 template <class T, expression_template_option ExpressionTemplates>
3057 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, long* pint)
3058 {
3059    using default_ops::eval_frexp;
3060    detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
3061    number<T, ExpressionTemplates>                                                    result;
3062    eval_frexp(result.backend(), v.backend(), pint);
3063    return result;
3064 }
3065 template <class tag, class A1, class A2, class A3, class A4>
3066 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
3067 frexp(const detail::expression<tag, A1, A2, A3, A4>& v, long* pint)
3068 {
3069    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
3070    return std::move(frexp(static_cast<number_type>(v), pint));
3071 }
3072 template <class T, expression_template_option ExpressionTemplates>
3073 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, long long* pint)
3074 {
3075    using default_ops::eval_frexp;
3076    detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
3077    number<T, ExpressionTemplates>                                                    result;
3078    eval_frexp(result.backend(), v.backend(), pint);
3079    return result;
3080 }
3081 template <class tag, class A1, class A2, class A3, class A4>
3082 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
3083 frexp(const detail::expression<tag, A1, A2, A3, A4>& v, long long* pint)
3084 {
3085    using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
3086    return std::move(frexp(static_cast<number_type>(v), pint));
3087 }
3088 //
3089 // modf does not return an expression template since we require the
3090 // second argument to be evaluated even if the returned value is
3091 // not assigned to anything...
3092 //
3093 template <class T, expression_template_option ExpressionTemplates>
3094 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type modf(const number<T, ExpressionTemplates>& v, number<T, ExpressionTemplates>* pipart)
3095 {
3096    using default_ops::eval_modf;
3097    detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
3098    number<T, ExpressionTemplates>                                                    result;
3099    eval_modf(result.backend(), v.backend(), pipart ? &pipart->backend() : nullptr);
3100    return result;
3101 }
3102 template <class T, expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
3103 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type modf(const detail::expression<tag, A1, A2, A3, A4>& v, number<T, ExpressionTemplates>* pipart)
3104 {
3105    using default_ops::eval_modf;
3106    detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
3107    number<T, ExpressionTemplates>                                                    result, arg(v);
3108    eval_modf(result.backend(), arg.backend(), pipart ? &pipart->backend() : nullptr);
3109    return result;
3110 }
3111 
3112 //
3113 // Integer square root:
3114 //
3115 template <class B, expression_template_option ExpressionTemplates>
3116 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
3117 sqrt(const number<B, ExpressionTemplates>& x)
3118 {
3119    using default_ops::eval_integer_sqrt;
3120    number<B, ExpressionTemplates> s, r;
3121    eval_integer_sqrt(s.backend(), r.backend(), x.backend());
3122    return s;
3123 }
3124 template <class tag, class A1, class A2, class A3, class A4>
3125 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_integer, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
3126          sqrt(const detail::expression<tag, A1, A2, A3, A4>& arg)
3127 {
3128    using default_ops::eval_integer_sqrt;
3129    using result_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
3130    detail::scoped_default_precision<result_type> precision_guard(arg);
3131    result_type                                   result, v(arg), r;
3132    eval_integer_sqrt(result.backend(), r.backend(), v.backend());
3133    return result;
3134 }
3135 
3136 //
3137 // fma:
3138 //
3139 
3140 namespace default_ops {
3141 
3142 struct fma_func
3143 {
3144    template <class B, class T, class U, class V>
3145    BOOST_MP_CXX14_CONSTEXPR void operator()(B& result, const T& a, const U& b, const V& c) const
3146    {
3147       eval_multiply_add(result, a, b, c);
3148    }
3149 };
3150 
3151 } // namespace default_ops
3152 
3153 template <class Backend, class U, class V>
3154 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3155         (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
3156         (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
3157         (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
3158     detail::expression<detail::function, default_ops::fma_func, number<Backend, et_on>, U, V> >::type
3159 fma(const number<Backend, et_on>& a, const U& b, const V& c)
3160 {
3161    return detail::expression<detail::function, default_ops::fma_func, number<Backend, et_on>, U, V>(
3162        default_ops::fma_func(), a, b, c);
3163 }
3164 
3165 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class U, class V>
3166 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3167         (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
3168         (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
3169         (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
3170     detail::expression<detail::function, default_ops::fma_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, V> >::type
3171 fma(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& a, const U& b, const V& c)
3172 {
3173    return detail::expression<detail::function, default_ops::fma_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, V>(
3174        default_ops::fma_func(), a, b, c);
3175 }
3176 
3177 template <class Backend, class U, class V>
3178 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3179         (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
3180         (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
3181         (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
3182     number<Backend, et_off> >::type
3183 fma(const number<Backend, et_off>& a, const U& b, const V& c)
3184 {
3185    using default_ops::eval_multiply_add;
3186    detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
3187    number<Backend, et_off>                                                    result;
3188    eval_multiply_add(result.backend(), number<Backend, et_off>::canonical_value(a), number<Backend, et_off>::canonical_value(b), number<Backend, et_off>::canonical_value(c));
3189    return result;
3190 }
3191 
3192 template <class U, class Backend, class V>
3193 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3194         (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
3195         boost::multiprecision::detail::is_arithmetic<U>::value &&
3196         (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
3197     detail::expression<detail::function, default_ops::fma_func, U, number<Backend, et_on>, V> >::type
3198 fma(const U& a, const number<Backend, et_on>& b, const V& c)
3199 {
3200    return detail::expression<detail::function, default_ops::fma_func, U, number<Backend, et_on>, V>(
3201        default_ops::fma_func(), a, b, c);
3202 }
3203 
3204 template <class U, class tag, class Arg1, class Arg2, class Arg3, class Arg4, class V>
3205 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3206         (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
3207         boost::multiprecision::detail::is_arithmetic<U>::value &&
3208         (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
3209     detail::expression<detail::function, default_ops::fma_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, V> >::type
3210 fma(const U& a, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& b, const V& c)
3211 {
3212    return detail::expression<detail::function, default_ops::fma_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, V>(
3213        default_ops::fma_func(), a, b, c);
3214 }
3215 
3216 template <class U, class Backend, class V>
3217 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3218         (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
3219         boost::multiprecision::detail::is_arithmetic<U>::value &&
3220         (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
3221     number<Backend, et_off> >::type
3222 fma(const U& a, const number<Backend, et_off>& b, const V& c)
3223 {
3224    using default_ops::eval_multiply_add;
3225    detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
3226    number<Backend, et_off>                                                    result;
3227    eval_multiply_add(result.backend(), number<Backend, et_off>::canonical_value(a), number<Backend, et_off>::canonical_value(b), number<Backend, et_off>::canonical_value(c));
3228    return result;
3229 }
3230 
3231 template <class U, class V, class Backend>
3232 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3233         (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
3234         boost::multiprecision::detail::is_arithmetic<U>::value &&
3235         boost::multiprecision::detail::is_arithmetic<V>::value,
3236     detail::expression<detail::function, default_ops::fma_func, U, V, number<Backend, et_on> > >::type
3237 fma(const U& a, const V& b, const number<Backend, et_on>& c)
3238 {
3239    return detail::expression<detail::function, default_ops::fma_func, U, V, number<Backend, et_on> >(
3240        default_ops::fma_func(), a, b, c);
3241 }
3242 
3243 template <class U, class V, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
3244 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3245         (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
3246         boost::multiprecision::detail::is_arithmetic<U>::value &&
3247         boost::multiprecision::detail::is_arithmetic<V>::value,
3248     detail::expression<detail::function, default_ops::fma_func, U, V, detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > >::type
3249 fma(const U& a, const V& b, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& c)
3250 {
3251    return detail::expression<detail::function, default_ops::fma_func, U, V, detail::expression<tag, Arg1, Arg2, Arg3, Arg4> >(
3252        default_ops::fma_func(), a, b, c);
3253 }
3254 
3255 template <class U, class V, class Backend>
3256 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3257         (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
3258         boost::multiprecision::detail::is_arithmetic<U>::value &&
3259         boost::multiprecision::detail::is_arithmetic<V>::value,
3260         number<Backend, et_off> >::type
3261 fma(const U& a, const V& b, const number<Backend, et_off>& c)
3262 {
3263    using default_ops::eval_multiply_add;
3264    detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
3265    number<Backend, et_off>                                                    result;
3266    eval_multiply_add(result.backend(), number<Backend, et_off>::canonical_value(a), number<Backend, et_off>::canonical_value(b), number<Backend, et_off>::canonical_value(c));
3267    return result;
3268 }
3269 
3270 namespace default_ops {
3271 
3272 struct remquo_func
3273 {
3274    template <class B, class T, class U>
3275    BOOST_MP_CXX14_CONSTEXPR void operator()(B& result, const T& a, const U& b, int* pi) const
3276    {
3277       eval_remquo(result, a, b, pi);
3278    }
3279 };
3280 
3281 } // namespace default_ops
3282 
3283 template <class Backend, class U>
3284 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3285     number_category<number<Backend, et_on> >::value == number_kind_floating_point,
3286     detail::expression<detail::function, default_ops::remquo_func, number<Backend, et_on>, U, int*> >::type
3287 remquo(const number<Backend, et_on>& a, const U& b, int* pi)
3288 {
3289    return detail::expression<detail::function, default_ops::remquo_func, number<Backend, et_on>, U, int*>(
3290        default_ops::remquo_func(), a, b, pi);
3291 }
3292 
3293 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class U>
3294 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3295     number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point,
3296     detail::expression<detail::function, default_ops::remquo_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, int*> >::type
3297 remquo(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& a, const U& b, int* pi)
3298 {
3299    return detail::expression<detail::function, default_ops::remquo_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, int*>(
3300        default_ops::remquo_func(), a, b, pi);
3301 }
3302 
3303 template <class U, class Backend>
3304 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3305     (number_category<number<Backend, et_on> >::value == number_kind_floating_point) && !is_number<U>::value && !is_number_expression<U>::value,
3306     detail::expression<detail::function, default_ops::remquo_func, U, number<Backend, et_on>, int*> >::type
3307 remquo(const U& a, const number<Backend, et_on>& b, int* pi)
3308 {
3309    return detail::expression<detail::function, default_ops::remquo_func, U, number<Backend, et_on>, int*>(
3310        default_ops::remquo_func(), a, b, pi);
3311 }
3312 
3313 template <class U, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
3314 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3315     (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) && !is_number<U>::value && !is_number_expression<U>::value,
3316     detail::expression<detail::function, default_ops::remquo_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, int*> >::type
3317 remquo(const U& a, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& b, int* pi)
3318 {
3319    return detail::expression<detail::function, default_ops::remquo_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, int*>(
3320        default_ops::remquo_func(), a, b, pi);
3321 }
3322 
3323 template <class Backend, class U>
3324 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3325     number_category<number<Backend, et_on> >::value == number_kind_floating_point,
3326     number<Backend, et_off> >::type
3327 remquo(const number<Backend, et_off>& a, const U& b, int* pi)
3328 {
3329    using default_ops::eval_remquo;
3330    detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b);
3331    number<Backend, et_off>                                                    result;
3332    eval_remquo(result.backend(), a.backend(), number<Backend, et_off>::canonical_value(b), pi);
3333    return result;
3334 }
3335 template <class U, class Backend>
3336 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
3337     (number_category<number<Backend, et_on> >::value == number_kind_floating_point) && !is_number<U>::value && !is_number_expression<U>::value,
3338     number<Backend, et_off> >::type
3339 remquo(const U& a, const number<Backend, et_off>& b, int* pi)
3340 {
3341    using default_ops::eval_remquo;
3342    detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b);
3343    number<Backend, et_off>                                                    result;
3344    eval_remquo(result.backend(), number<Backend, et_off>::canonical_value(a), b.backend(), pi);
3345    return result;
3346 }
3347 
3348 template <class B, expression_template_option ExpressionTemplates>
3349 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
3350 sqrt(const number<B, ExpressionTemplates>& x, number<B, ExpressionTemplates>& r)
3351 {
3352    using default_ops::eval_integer_sqrt;
3353    detail::scoped_default_precision<multiprecision::number<B, ExpressionTemplates> > precision_guard(x, r);
3354    number<B, ExpressionTemplates>                                                    s;
3355    eval_integer_sqrt(s.backend(), r.backend(), x.backend());
3356    return s;
3357 }
3358 template <class B, expression_template_option ExpressionTemplates, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
3359 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
3360 sqrt(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& arg, number<B, ExpressionTemplates>& r)
3361 {
3362    using default_ops::eval_integer_sqrt;
3363    detail::scoped_default_precision<multiprecision::number<B, ExpressionTemplates> > precision_guard(r);
3364    number<B, ExpressionTemplates>                                                    s;
3365    number<B, ExpressionTemplates>                                                    x(arg);
3366    eval_integer_sqrt(s.backend(), r.backend(), x.backend());
3367    return s;
3368 }
3369 
3370 // clang-format off
3371 //
3372 // Regrettably, when the argument to a function is an rvalue we must return by value, and not return an
3373 // expression template, otherwise we can end up with dangling references.
3374 // See https://github.com/boostorg/multiprecision/issues/175.
3375 //
3376 #define UNARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
3377    template <class Backend>                                                                                                                                                                               \
3378    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> > ::type                                                                      \
3379    func(number<Backend, et_on>&& arg)                                                                                                                                                                     \
3380    {                                                                                                                                                                                                      \
3381       detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg);                                                                                                    \
3382       number<Backend, et_on>                                                    result;                                                                                                                  \
3383       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                         \
3384       BOOST_JOIN(eval_, func)(result.backend(), arg.backend());                                                                                                                                                                  \
3385       return result;                                                                                                                                                                       \
3386    }                                                                                                                                                                                                      \
3387 
3388 #define BINARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
3389    template <class Backend>                                                                                                                                                                                                                                \
3390    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> >::type func(number<Backend, et_on>&& arg, const number<Backend, et_on>& a)                                                                                              \
3391    {                                                                                                                                                                                                                                                       \
3392       detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a);                                                                                                                                                  \
3393       number<Backend, et_on>                                                    result;                                                                                                                                                                   \
3394       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                          \
3395       BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend());                                                                                                                                                                                                      \
3396       return result;                                                                                                                                                                                                                        \
3397    }                                                                                                                                                                                                                                                       \
3398    template <class Backend>                                                                                                                                                                                                                                \
3399    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> >::type func(const number<Backend, et_on>& arg, number<Backend, et_on>&& a)                                                                                              \
3400    {                                                                                                                                                                                                                                                       \
3401       detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a);                                                                                                                                                  \
3402       number<Backend, et_on>                                                    result;                                                                                                                                                                   \
3403       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                          \
3404       BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend());                                                                                                                                                                                                      \
3405       return result;                                                                                                                                                                                                                        \
3406    }                                                                                                                                                                                                                                                       \
3407    template <class Backend>                                                                                                                                                                                                                                \
3408    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> >::type func(number<Backend, et_on>&& arg, number<Backend, et_on>&& a)                                                                                              \
3409    {                                                                                                                                                                                                                                                       \
3410       detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a);                                                                                                                                                  \
3411       number<Backend, et_on>                                                    result;                                                                                                                                                                   \
3412       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                          \
3413       BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend());                                                                                                                                                                                                      \
3414       return result;                                                                                                                                                                                                                        \
3415    }                                                                                                                                                                                                                                                       \
3416    template <class Backend, class tag, class A1, class A2, class A3, class A4>                                                                                                                                                                             \
3417    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category) && (std::is_convertible<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, et_on> >::value),                           \
3418            number<Backend, et_on> > ::type                                                                                                                                                                       \
3419    func(number<Backend, et_on>&& arg, const detail::expression<tag, A1, A2, A3, A4>& a)                                                                                    \
3420    {                                                                                                                                                                                                                                                       \
3421       return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                      \
3422              number<Backend, et_on>, detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a);                                                                                             \
3423    }                                                                                                                                                                                                                                                       \
3424    template <class tag, class A1, class A2, class A3, class A4, class Backend>                                                                                                                                                                             \
3425    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category) && (std::is_convertible<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, et_on> >::value),                           \
3426            number<Backend, et_on> > ::type                                                                                                                                                                       \
3427    func(const detail::expression<tag, A1, A2, A3, A4>& arg, number<Backend, et_on>&& a)                                                                                    \
3428    {                                                                                                                                                                                                                                                       \
3429       return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                      \
3430              detail::expression<tag, A1, A2, A3, A4>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a);                                                                                             \
3431    }                                                                                                                                                                                                                                                       \
3432    template <class Backend, class Arithmetic>                                                                                                                                                                                                              \
3433    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                                                        \
3434            is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category),                                                                                                                     \
3435            number<Backend, et_on> >::type                                                                                                                                                                                                     \
3436    func(number<Backend, et_on>&& arg, const Arithmetic& a)                                                                                                                                               \
3437    {                                                                                                                                                                                                                                                       \
3438       return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>,                                                                                                                                                      \
3439              number<Backend, et_on>, Arithmetic > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a);                                                                                                                         \
3440    }                                                                                                                                                                                                                                                       \
3441    template <class Backend, class Arithmetic>                                                                                                                                                                                                              \
3442    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                                                        \
3443            is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category),                                                                                                                     \
3444            number<Backend, et_on> > ::type                                                                                                                                                                                                    \
3445    func(const Arithmetic& arg, number<Backend, et_on>&& a)                                                                                                                                              \
3446    {                                                                                                                                                                                                                                                       \
3447       return detail::expression<                                                                                                                                                                                                                           \
3448                  detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                      \
3449              Arithmetic, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a);                                                                                                                          \
3450    }                                                                                                                                                                                                                                                       \
3451 
3452 
3453 #define UNARY_OP_FUNCTOR(func, category)                                                                                                                                                                  \
3454    namespace detail {                                                                                                                                                                                     \
3455    template <class Backend>                                                                                                                                                                               \
3456    struct BOOST_JOIN(category, BOOST_JOIN(func, _funct))                                                                                                                                                  \
3457    {                                                                                                                                                                                                      \
3458       BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const                                                                                                                                          \
3459       {                                                                                                                                                                                                   \
3460          using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                      \
3461          BOOST_JOIN(eval_, func)                                                                                                                                                                          \
3462          (result, arg);                                                                                                                                                                                   \
3463       }                                                                                                                                                                                                   \
3464       template <class U>                                                                                                                                                                                  \
3465       BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg) const                                                                                                                                                \
3466       {                                                                                                                                                                                                   \
3467          using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                      \
3468          Backend temp;                                                                                                                                                                                    \
3469          BOOST_JOIN(eval_, func)                                                                                                                                                                          \
3470          (temp, arg);                                                                                                                                                                                     \
3471          result = std::move(temp);                                                                                                                                                                                   \
3472       }                                                                                                                                                                                                   \
3473    };                                                                                                                                                                                                     \
3474    }                                                                                                                                                                                                      \
3475                                                                                                                                                                                                           \
3476    template <class tag, class A1, class A2, class A3, class A4>                                                                                                                                           \
3477    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category,                                                                     \
3478                                                         detail::expression<detail::function,                                                                                                              \
3479                                                         detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,            \
3480                                                         detail::expression<tag, A1, A2, A3, A4> > > ::type                                                                                                \
3481    func(const detail::expression<tag, A1, A2, A3, A4>& arg)                                                                    \
3482    {                                                                                                                                                                                                      \
3483       return detail::expression<                                                                                                                                                                          \
3484                  detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                               \
3485              detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type > (), arg); \
3486    }                                                                                                                                                                                                      \
3487    template <class Backend>                                                                                                                                                                               \
3488    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category,                                                                                                      \
3489           detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, number<Backend, et_on> > > ::type                                                       \
3490    func(const number<Backend, et_on>& arg)                                                                                                                                                                \
3491    {                                                                                                                                                                                                      \
3492       return detail::expression<                                                                                                                                                                          \
3493                  detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                     \
3494              number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg);                                                                                        \
3495    }                                                                                                                                                                                                      \
3496    template <class Backend>                                                                                                                                                                               \
3497    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                    \
3498        boost::multiprecision::number_category<Backend>::value == category,                                                                                                                                \
3499        number<Backend, et_off> >::type                                                                                                                                                                    \
3500    func(const number<Backend, et_off>& arg)                                                                                                                                                               \
3501    {                                                                                                                                                                                                      \
3502       detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);                                                                                                    \
3503       number<Backend, et_off>                                                    result;                                                                                                                  \
3504       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                         \
3505       BOOST_JOIN(eval_, func)(result.backend(), arg.backend());                                                                                                                                                                  \
3506       return result;                                                                                                                                                                       \
3507    }\
3508    UNARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
3509 
3510 #define BINARY_OP_FUNCTOR(func, category)                                                                                                                                                                                                                  \
3511    namespace detail {                                                                                                                                                                                                                                      \
3512    template <class Backend>                                                                                                                                                                                                                                \
3513    struct BOOST_JOIN(category, BOOST_JOIN(func, _funct))                                                                                                                                                                                                   \
3514    {                                                                                                                                                                                                                                                       \
3515       BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg, const Backend& a) const                                                                                                                                                                         \
3516       {                                                                                                                                                                                                                                                    \
3517          using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                       \
3518          BOOST_JOIN(eval_, func)                                                                                                                                                                                                                           \
3519          (result, arg, a);                                                                                                                                                                                                                                 \
3520       }                                                                                                                                                                                                                                                    \
3521       template <class Arithmetic>                                                                                                                                                                                                                          \
3522       BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg, const Arithmetic& a) const                                                                                                                                                                      \
3523       {                                                                                                                                                                                                                                                    \
3524          using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                       \
3525          BOOST_JOIN(eval_, func)                                                                                                                                                                                                                           \
3526          (result, arg, number<Backend>::canonical_value(a));                                                                                                                                                                                               \
3527       }                                                                                                                                                                                                                                                    \
3528       template <class Arithmetic>                                                                                                                                                                                                                          \
3529       BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Arithmetic& arg, const Backend& a) const                                                                                                                                                                      \
3530       {                                                                                                                                                                                                                                                    \
3531          using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                       \
3532          BOOST_JOIN(eval_, func)                                                                                                                                                                                                                           \
3533          (result, number<Backend>::canonical_value(arg), a);                                                                                                                                                                                               \
3534       }                                                                                                                                                                                                                                                    \
3535       template <class U>                                                                                                                                                                                                                                   \
3536       BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg, const Backend& a) const                                                                                                                                                                               \
3537       {                                                                                                                                                                                                                                                    \
3538          using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                       \
3539          Backend r;                                                                                                                                                                                                                                        \
3540          BOOST_JOIN(eval_, func)                                                                                                                                                                                                                           \
3541          (r, arg, a);                                                                                                                                                                                                                                      \
3542          result = std::move(r);                                                                                                                                                                                                                                       \
3543       }                                                                                                                                                                                                                                                    \
3544       template <class U, class Arithmetic>                                                                                                                                                                                                                 \
3545       BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg, const Arithmetic& a) const                                                                                                                                                                            \
3546       {                                                                                                                                                                                                                                                    \
3547          using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                       \
3548          Backend r;                                                                                                                                                                                                                                        \
3549          BOOST_JOIN(eval_, func)                                                                                                                                                                                                                           \
3550          (r, arg, number<Backend>::canonical_value(a));                                                                                                                                                                                                    \
3551          result = std::move(r);                                                                                                                                                                                                                                       \
3552       }                                                                                                                                                                                                                                                    \
3553       template <class U, class Arithmetic>                                                                                                                                                                                                                 \
3554       BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Arithmetic& arg, const Backend& a) const                                                                                                                                                                            \
3555       {                                                                                                                                                                                                                                                    \
3556          using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                       \
3557          Backend r;                                                                                                                                                                                                                                        \
3558          BOOST_JOIN(eval_, func)                                                                                                                                                                                                                           \
3559          (r, number<Backend>::canonical_value(arg), a);                                                                                                                                                                                                    \
3560          result = std::move(r);                                                                                                                                                                                                                                       \
3561       }                                                                                                                                                                                                                                                    \
3562    };                                                                                                                                                                                                                                                      \
3563    }                                                                                                                                                                                                                                                       \
3564    template <class Backend>                                                                                                                                                                                                                                \
3565    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, detail::expression<detail::function, \
3566          detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, number<Backend, et_on>, number<Backend, et_on> > > ::type                                                                                                                                                                \
3567    func(const number<Backend, et_on>& arg, const number<Backend, et_on>& a)                                                                                              \
3568    {                                                                                                                                                                                                                                                       \
3569       return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                      \
3570              number<Backend, et_on>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a);                                                                                                              \
3571    }                                                                                                                                                                                                                                                       \
3572    template <class Backend, class tag, class A1, class A2, class A3, class A4>                                                                                                                                                                             \
3573    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category) && (std::is_convertible<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, et_on> >::value),                           \
3574            detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, number<Backend, et_on>, detail::expression<tag, A1, A2, A3, A4> > > ::type                                                                                                                                                                       \
3575    func(const number<Backend, et_on>& arg, const detail::expression<tag, A1, A2, A3, A4>& a)                                                                                    \
3576    {                                                                                                                                                                                                                                                       \
3577       return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                      \
3578              number<Backend, et_on>, detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a);                                                                                             \
3579    }                                                                                                                                                                                                                                                       \
3580    template <class tag, class A1, class A2, class A3, class A4, class Backend>                                                                                                                                                                             \
3581    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category) && (std::is_convertible<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, et_on> >::value),                           \
3582            detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, detail::expression<tag, A1, A2, A3, A4>, number<Backend, et_on> > > ::type                                                                                                                                                                       \
3583    func(const detail::expression<tag, A1, A2, A3, A4>& arg, const number<Backend, et_on>& a)                                                                                    \
3584    {                                                                                                                                                                                                                                                       \
3585       return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                      \
3586              detail::expression<tag, A1, A2, A3, A4>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a);                                                                                             \
3587    }                                                                                                                                                                                                                                                       \
3588    template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>                                                                                                                                    \
3589    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category) && (number_category<detail::expression<tagb, A1b, A2b, A3b, A4b> >::value == category),                                                                          \
3590            detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                                                                                  \
3591            detail::expression<tag, A1, A2, A3, A4>, detail::expression<tagb, A1b, A2b, A3b, A4b> > > ::type                                                                                                                                                 \
3592    func(const detail::expression<tag, A1, A2, A3, A4>& arg, const detail::expression<tagb, A1b, A2b, A3b, A4b>& a)                                        \
3593    {                                                                                                                                                                                                                                                       \
3594       return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                                                                                \
3595              detail::expression<tag, A1, A2, A3, A4>, detail::expression<tagb, A1b, A2b, A3b, A4b> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg, a); \
3596    }                                                                                                                                                                                                                                                       \
3597    template <class Backend, class Arithmetic>                                                                                                                                                                                                              \
3598    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                                                        \
3599            is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category),                                                                                                                     \
3600            detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                        \
3601            number<Backend, et_on>, Arithmetic> > ::type                                                                                                                                                                                                     \
3602    func(const number<Backend, et_on>& arg, const Arithmetic& a)                                                                                                                                               \
3603    {                                                                                                                                                                                                                                                       \
3604       return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>,                                                                                                                                                      \
3605              number<Backend, et_on>, Arithmetic > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a);                                                                                                                         \
3606    }                                                                                                                                                                                                                                                       \
3607    template <class tag, class A1, class A2, class A3, class A4, class Arithmetic>                                                                                                                                                                          \
3608    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                                                        \
3609            is_compatible_arithmetic_type<Arithmetic, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value && (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category),                                              \
3610            detail::expression<                                                                                                                                                                                                                             \
3611                detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                                                                                  \
3612            detail::expression<tag, A1, A2, A3, A4>, Arithmetic> > ::type                                                                                                                                                                                    \
3613    func(const detail::expression<tag, A1, A2, A3, A4>& arg, const Arithmetic& a)                                                                                                             \
3614    {                                                                                                                                                                                                                                                       \
3615       return detail::expression<                                                                                                                                                                                                                           \
3616                  detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                                                                                \
3617              detail::expression<tag, A1, A2, A3, A4>, Arithmetic > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type > (), arg, a);                                  \
3618    }                                                                                                                                                                                                                                                       \
3619    template <class Backend, class Arithmetic>                                                                                                                                                                                                              \
3620    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                                                        \
3621            is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category),                                                                                                                     \
3622            detail::expression<                                                                                                                                                                                                                             \
3623                detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                        \
3624            Arithmetic, number<Backend, et_on> > > ::type                                                                                                                                                                                                    \
3625    func(const Arithmetic& arg, const number<Backend, et_on>& a)                                                                                                                                              \
3626    {                                                                                                                                                                                                                                                       \
3627       return detail::expression<                                                                                                                                                                                                                           \
3628                  detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                                                      \
3629              Arithmetic, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a);                                                                                                                          \
3630    }                                                                                                                                                                                                                                                       \
3631    template <class tag, class A1, class A2, class A3, class A4, class Arithmetic>                                                                                                                                                                          \
3632        inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                                                        \
3633            is_compatible_arithmetic_type<Arithmetic, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value && (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category),                                              \
3634            detail::expression<                                                                                                                                                                                                                             \
3635                detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                                                                                  \
3636            Arithmetic, detail::expression<tag, A1, A2, A3, A4> > > ::type                                                                                                                                                                                   \
3637    func(const Arithmetic& arg, const detail::expression<tag, A1, A2, A3, A4>& a)                                                                                                            \
3638    {                                                                                                                                                                                                                                                       \
3639       return detail::expression<                                                                                                                                                                                                                           \
3640                  detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                                                                                \
3641              Arithmetic, detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type > (), arg, a);                                   \
3642    }                                                                                                                                                                                                                                                       \
3643    template <class Backend>                                                                                                                                                                                                                                \
3644    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category), number<Backend, et_off> >::type                                                                                                                                                                                             \
3645    func(const number<Backend, et_off>& arg, const number<Backend, et_off>& a)                                                                                                                                                                              \
3646    {                                                                                                                                                                                                                                                       \
3647       detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg, a);                                                                                                                                                  \
3648       number<Backend, et_off>                                                    result;                                                                                                                                                                   \
3649       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                          \
3650       BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend());                                                                                                                                                                                                      \
3651       return result;                                                                                                                                                                                                                        \
3652    }                                                                                                                                                                                                                                                       \
3653    template <class Backend, class Arithmetic>                                                                                                                                                                                                              \
3654    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                                                            \
3655        is_compatible_arithmetic_type<Arithmetic, number<Backend, et_off> >::value && (number_category<Backend>::value == category),                                                                                                                        \
3656        number<Backend, et_off> >::type                                                                                                                                                                                                                     \
3657    func(const number<Backend, et_off>& arg, const Arithmetic& a)                                                                                                                                                                                           \
3658    {                                                                                                                                                                                                                                                       \
3659       detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);                                                                                                                                                     \
3660       number<Backend, et_off>                                                    result;                                                                                                                                                                   \
3661       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                          \
3662       BOOST_JOIN(eval_, func)                                                                                                                                                                                                                              \
3663       (result.backend(), arg.backend(), number<Backend, et_off>::canonical_value(a));                                                                                                                                                                      \
3664       return result;                                                                                                                                                                                                                        \
3665    }                                                                                                                                                                                                                                                       \
3666    template <class Backend, class Arithmetic>                                                                                                                                                                                                              \
3667    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                                                            \
3668        is_compatible_arithmetic_type<Arithmetic, number<Backend, et_off> >::value && (number_category<Backend>::value == category),                                                                                                                        \
3669        number<Backend, et_off> >::type                                                                                                                                                                                                                     \
3670    func(const Arithmetic& a, const number<Backend, et_off>& arg)                                                                                                                                                                                           \
3671    {                                                                                                                                                                                                                                                       \
3672       detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);                                                                                                                                                     \
3673       number<Backend, et_off>                                                    result;                                                                                                                                                                   \
3674       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                                                          \
3675       BOOST_JOIN(eval_, func)                                                                                                                                                                                                                              \
3676       (result.backend(), number<Backend, et_off>::canonical_value(a), arg.backend());                                                                                                                                                                      \
3677       return result;                                                                                                                                                                                                                        \
3678    }\
3679    BINARY_OP_FUNCTOR_CXX11_RVALUE(func, category)
3680 
3681 #define HETERO_BINARY_OP_FUNCTOR_B(func, Arg2, category)                                                                                                                                                            \
3682    template <class tag, class A1, class A2, class A3, class A4>                                                                                                                                                     \
3683        inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                 \
3684            (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category),                                                                                                                          \
3685            detail::expression<                                                                                                                                                                                      \
3686                detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                                           \
3687            detail::expression<tag, A1, A2, A3, A4>, Arg2> > ::type                                                                                                                                                   \
3688                                                            func(const detail::expression<tag, A1, A2, A3, A4>& arg, Arg2 const& a)                                                                                  \
3689    {                                                                                                                                                                                                                \
3690       return detail::expression<                                                                                                                                                                                    \
3691                  detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>,                                         \
3692              detail::expression<tag, A1, A2, A3, A4>, Arg2 > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type > (), arg, a); \
3693    }                                                                                                                                                                                                                \
3694    template <class Backend>                                                                                                                                                                                         \
3695        inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                 \
3696            (number_category<Backend>::value == category),                                                                                                                                                           \
3697            detail::expression<                                                                                                                                                                                      \
3698                detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                                 \
3699            number<Backend, et_on>, Arg2> > ::type                                                                                                                                                                    \
3700                                           func(const number<Backend, et_on>& arg, Arg2 const& a)                                                                                                                    \
3701    {                                                                                                                                                                                                                \
3702       return detail::expression<                                                                                                                                                                                    \
3703                  detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>,                                                                                                               \
3704              number<Backend, et_on>, Arg2 > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a);                                                                                        \
3705    }                                                                                                                                                                                                                \
3706    template <class Backend>                                                                                                                                                                                         \
3707    inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<                                                                                                                                                                                     \
3708        (number_category<Backend>::value == category),                                                                                                                                                               \
3709        number<Backend, et_off> >::type                                                                                                                                                                              \
3710    func(const number<Backend, et_off>& arg, Arg2 const& a)                                                                                                                                                          \
3711    {                                                                                                                                                                                                                \
3712       detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg, a);                                                                                                           \
3713       number<Backend, et_off>                                                    result;                                                                                                                            \
3714       using default_ops::BOOST_JOIN(eval_, func);                                                                                                                                                                   \
3715       BOOST_JOIN(eval_, func)                                                                                                                                                                                       \
3716       (result.backend(), arg.backend(), a);                                                                                                                                                                         \
3717       return result;                                                                                                                                                                                 \
3718    }
3719 
3720 #define HETERO_BINARY_OP_FUNCTOR(func, Arg2, category)                  \
3721    namespace detail {                                                   \
3722    template <class Backend>                                             \
3723    struct BOOST_JOIN(category, BOOST_JOIN(func, _funct))                \
3724    {                                                                    \
3725       template <class Arg>                                              \
3726       BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, Backend const& arg, Arg a) const \
3727       {                                                                 \
3728          using default_ops::BOOST_JOIN(eval_, func);                    \
3729          BOOST_JOIN(eval_, func)                                        \
3730          (result, arg, a);                                              \
3731       }                                                                 \
3732       template <class U, class Arg>                                              \
3733       BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, Backend const& arg, Arg a) const \
3734       {                                                                 \
3735          using default_ops::BOOST_JOIN(eval_, func);                    \
3736          Backend temp;                                                  \
3737          BOOST_JOIN(eval_, func)                                        \
3738          (temp, arg, a);                                                \
3739          result = std::move(temp);                                  \
3740       }                                                                 \
3741    };                                                                   \
3742    }                                                                    \
3743                                                                         \
3744    HETERO_BINARY_OP_FUNCTOR_B(func, Arg2, category)
3745 
3746 // clang-format on
3747 
3748 namespace detail {
3749 template <class Backend>
3750 struct abs_funct
3751 {
3752    BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
3753    {
3754       using default_ops::eval_abs;
3755       eval_abs(result, arg);
3756    }
3757 };
3758 template <class Backend>
3759 struct conj_funct
3760 {
3761    BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
3762    {
3763       using default_ops::eval_conj;
3764       eval_conj(result, arg);
3765    }
3766    //
3767    // To allow for mixed complex/scalar arithmetic where conj is called on the scalar type (as in Eigen)
3768    // we provide an overload that will promote the arg to the distination type:
3769    //
3770    template <class Other>
3771    BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_constructible<Other, Backend>::value>::type operator()(Other& result, const Backend& arg) const
3772    {
3773       using default_ops::eval_conj;
3774       Other t(arg);
3775       eval_conj(result, t);
3776    }
3777 };
3778 template <class Backend>
3779 struct proj_funct
3780 {
3781    BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
3782    {
3783       using default_ops::eval_proj;
3784       eval_proj(result, arg);
3785    }
3786 };
3787 
3788 } // namespace detail
3789 
3790 template <class tag, class A1, class A2, class A3, class A4>
3791 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex,
3792                                     detail::expression<
3793                                         detail::function, detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> > >::type
3794 abs(const detail::expression<tag, A1, A2, A3, A4>& arg)
3795 {
3796    return detail::expression<
3797        detail::function, detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
3798        detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
3799 }
3800 template <class Backend>
3801 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex,
3802                              detail::expression<
3803                                  detail::function, detail::abs_funct<Backend>, number<Backend, et_on> > >::type
3804 abs(const number<Backend, et_on>& arg)
3805 {
3806    return detail::expression<
3807        detail::function, detail::abs_funct<Backend>, number<Backend, et_on> >(
3808        detail::abs_funct<Backend>(), arg);
3809 }
3810 template <class Backend>
3811 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, number<Backend, et_off> >::type
3812 abs(const number<Backend, et_off>& arg)
3813 {
3814    detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
3815    number<Backend, et_off>                                                    result;
3816    using default_ops::eval_abs;
3817    eval_abs(result.backend(), arg.backend());
3818    return result;
3819 }
3820 
3821 template <class tag, class A1, class A2, class A3, class A4>
3822 inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
3823     detail::function, detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >
3824 conj(const detail::expression<tag, A1, A2, A3, A4>& arg)
3825 {
3826    return detail::expression<
3827        detail::function, detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
3828        detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
3829 }
3830 template <class Backend>
3831 inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
3832     detail::function, detail::conj_funct<Backend>, number<Backend, et_on> >
3833 conj(const number<Backend, et_on>& arg)
3834 {
3835    return detail::expression<
3836        detail::function, detail::conj_funct<Backend>, number<Backend, et_on> >(
3837        detail::conj_funct<Backend>(), arg);
3838 }
3839 template <class Backend>
3840 inline BOOST_MP_CXX14_CONSTEXPR number<Backend, et_off>
3841 conj(const number<Backend, et_off>& arg)
3842 {
3843    detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
3844    number<Backend, et_off>                                                    result;
3845    using default_ops::eval_conj;
3846    eval_conj(result.backend(), arg.backend());
3847    return result;
3848 }
3849 
3850 template <class tag, class A1, class A2, class A3, class A4>
3851 inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
3852     detail::function, detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >
3853 proj(const detail::expression<tag, A1, A2, A3, A4>& arg)
3854 {
3855    return detail::expression<
3856        detail::function, detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
3857        detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
3858 }
3859 template <class Backend>
3860 inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
3861     detail::function, detail::proj_funct<Backend>, number<Backend, et_on> >
3862 proj(const number<Backend, et_on>& arg)
3863 {
3864    return detail::expression<
3865        detail::function, detail::proj_funct<Backend>, number<Backend, et_on> >(
3866        detail::proj_funct<Backend>(), arg);
3867 }
3868 template <class Backend>
3869 inline BOOST_MP_CXX14_CONSTEXPR number<Backend, et_off>
3870 proj(const number<Backend, et_off>& arg)
3871 {
3872    detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
3873    number<Backend, et_off>                                                    result;
3874    using default_ops::eval_proj;
3875    eval_proj(result.backend(), arg.backend());
3876    return result;
3877 }
3878 
3879 UNARY_OP_FUNCTOR(fabs, number_kind_floating_point)
3880 UNARY_OP_FUNCTOR(sqrt, number_kind_floating_point)
3881 UNARY_OP_FUNCTOR(floor, number_kind_floating_point)
3882 UNARY_OP_FUNCTOR(ceil, number_kind_floating_point)
3883 UNARY_OP_FUNCTOR(trunc, number_kind_floating_point)
3884 UNARY_OP_FUNCTOR(round, number_kind_floating_point)
3885 UNARY_OP_FUNCTOR(exp, number_kind_floating_point)
3886 UNARY_OP_FUNCTOR(exp2, number_kind_floating_point)
3887 UNARY_OP_FUNCTOR(log, number_kind_floating_point)
3888 UNARY_OP_FUNCTOR(log10, number_kind_floating_point)
3889 UNARY_OP_FUNCTOR(cos, number_kind_floating_point)
3890 UNARY_OP_FUNCTOR(sin, number_kind_floating_point)
3891 UNARY_OP_FUNCTOR(tan, number_kind_floating_point)
3892 UNARY_OP_FUNCTOR(asin, number_kind_floating_point)
3893 UNARY_OP_FUNCTOR(acos, number_kind_floating_point)
3894 UNARY_OP_FUNCTOR(atan, number_kind_floating_point)
3895 UNARY_OP_FUNCTOR(cosh, number_kind_floating_point)
3896 UNARY_OP_FUNCTOR(sinh, number_kind_floating_point)
3897 UNARY_OP_FUNCTOR(tanh, number_kind_floating_point)
3898 UNARY_OP_FUNCTOR(log2, number_kind_floating_point)
3899 UNARY_OP_FUNCTOR(nearbyint, number_kind_floating_point)
3900 UNARY_OP_FUNCTOR(rint, number_kind_floating_point)
3901 
3902 HETERO_BINARY_OP_FUNCTOR(ldexp, short, number_kind_floating_point)
3903 //HETERO_BINARY_OP_FUNCTOR(frexp, short*, number_kind_floating_point)
3904 HETERO_BINARY_OP_FUNCTOR_B(ldexp, int, number_kind_floating_point)
3905 //HETERO_BINARY_OP_FUNCTOR_B(frexp, int*, number_kind_floating_point)
3906 HETERO_BINARY_OP_FUNCTOR_B(ldexp, long, number_kind_floating_point)
3907 //HETERO_BINARY_OP_FUNCTOR_B(frexp, long*, number_kind_floating_point)
3908 HETERO_BINARY_OP_FUNCTOR_B(ldexp, long long, number_kind_floating_point)
3909 //HETERO_BINARY_OP_FUNCTOR_B(frexp, long long*, number_kind_floating_point)
3910 BINARY_OP_FUNCTOR(pow, number_kind_floating_point)
3911 BINARY_OP_FUNCTOR(fmod, number_kind_floating_point)
3912 BINARY_OP_FUNCTOR(fmax, number_kind_floating_point)
3913 BINARY_OP_FUNCTOR(fmin, number_kind_floating_point)
3914 BINARY_OP_FUNCTOR(atan2, number_kind_floating_point)
3915 BINARY_OP_FUNCTOR(fdim, number_kind_floating_point)
3916 BINARY_OP_FUNCTOR(hypot, number_kind_floating_point)
3917 BINARY_OP_FUNCTOR(remainder, number_kind_floating_point)
3918 
3919 UNARY_OP_FUNCTOR(logb, number_kind_floating_point)
3920 HETERO_BINARY_OP_FUNCTOR(scalbn, short, number_kind_floating_point)
3921 HETERO_BINARY_OP_FUNCTOR(scalbln, short, number_kind_floating_point)
3922 HETERO_BINARY_OP_FUNCTOR_B(scalbn, int, number_kind_floating_point)
3923 HETERO_BINARY_OP_FUNCTOR_B(scalbln, int, number_kind_floating_point)
3924 HETERO_BINARY_OP_FUNCTOR_B(scalbn, long, number_kind_floating_point)
3925 HETERO_BINARY_OP_FUNCTOR_B(scalbln, long, number_kind_floating_point)
3926 HETERO_BINARY_OP_FUNCTOR_B(scalbn, long long, number_kind_floating_point)
3927 HETERO_BINARY_OP_FUNCTOR_B(scalbln, long long, number_kind_floating_point)
3928 
3929 //
3930 // Complex functions:
3931 //
3932 UNARY_OP_FUNCTOR(exp, number_kind_complex)
3933 UNARY_OP_FUNCTOR(log, number_kind_complex)
3934 UNARY_OP_FUNCTOR(log10, number_kind_complex)
3935 BINARY_OP_FUNCTOR(pow, number_kind_complex)
3936 UNARY_OP_FUNCTOR(sqrt, number_kind_complex)
3937 UNARY_OP_FUNCTOR(sin, number_kind_complex)
3938 UNARY_OP_FUNCTOR(cos, number_kind_complex)
3939 UNARY_OP_FUNCTOR(tan, number_kind_complex)
3940 UNARY_OP_FUNCTOR(asin, number_kind_complex)
3941 UNARY_OP_FUNCTOR(acos, number_kind_complex)
3942 UNARY_OP_FUNCTOR(atan, number_kind_complex)
3943 UNARY_OP_FUNCTOR(sinh, number_kind_complex)
3944 UNARY_OP_FUNCTOR(cosh, number_kind_complex)
3945 UNARY_OP_FUNCTOR(tanh, number_kind_complex)
3946 UNARY_OP_FUNCTOR(asinh, number_kind_complex)
3947 UNARY_OP_FUNCTOR(acosh, number_kind_complex)
3948 UNARY_OP_FUNCTOR(atanh, number_kind_complex)
3949 
3950 //
3951 // Integer functions:
3952 //
3953 BINARY_OP_FUNCTOR(gcd, number_kind_integer)
3954 BINARY_OP_FUNCTOR(lcm, number_kind_integer)
3955 HETERO_BINARY_OP_FUNCTOR(pow, unsigned, number_kind_integer)
3956 
3957 #undef BINARY_OP_FUNCTOR
3958 #undef UNARY_OP_FUNCTOR
3959 
3960 //
3961 // ilogb:
3962 //
3963 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
3964 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_floating_point, typename Backend::exponent_type>::type
3965 ilogb(const multiprecision::number<Backend, ExpressionTemplates>& val)
3966 {
3967    using default_ops::eval_ilogb;
3968    return eval_ilogb(val.backend());
3969 }
3970 
3971 template <class tag, class A1, class A2, class A3, class A4>
3972 inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<detail::expression<tag, A1, A2, A3, A4> >::value == number_kind_floating_point, typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type::backend_type::exponent_type>::type
3973 ilogb(const detail::expression<tag, A1, A2, A3, A4>& val)
3974 {
3975    using default_ops::eval_ilogb;
3976    typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type arg(val);
3977    return eval_ilogb(arg.backend());
3978 }
3979 
3980 } //namespace multiprecision
3981 
3982 namespace math {
3983 
3984 
3985 //
3986 // Overload of Boost.Math functions that find the wrong overload when used with number:
3987 //
3988 namespace detail {
3989 
3990 template <class T>
3991 T sinc_pi_imp(T);
3992 template <class T, class Policy>
3993 T sinhc_pi_imp(T, const Policy&);
3994 
3995 } // namespace detail
3996 
3997 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
3998 inline multiprecision::number<Backend, ExpressionTemplates> sinc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x)
3999 {
4000    boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
4001    return detail::sinc_pi_imp(x);
4002 }
4003 
4004 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class Policy>
4005 inline multiprecision::number<Backend, ExpressionTemplates> sinc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x, const Policy&)
4006 {
4007    boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
4008    return detail::sinc_pi_imp(x);
4009 }
4010 
4011 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
4012 inline multiprecision::number<Backend, ExpressionTemplates> sinhc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x)
4013 {
4014    boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
4015    return detail::sinhc_pi_imp(x, boost::math::policies::policy<>());
4016 }
4017 
4018 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class Policy>
4019 inline multiprecision::number<Backend, ExpressionTemplates> sinhc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x, const Policy& pol)
4020 {
4021    boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x, pol);
4022    return detail::sinhc_pi_imp(x, pol);
4023 }
4024 
4025 using boost::multiprecision::gcd;
4026 using boost::multiprecision::lcm;
4027 
4028 #ifdef BOOST_MSVC
4029 #pragma warning(pop)
4030 #endif
4031 } // namespace math
4032 
4033 namespace integer {
4034 
4035 using boost::multiprecision::gcd;
4036 using boost::multiprecision::lcm;
4037 
4038 } // namespace integer
4039 
4040 } // namespace boost
4041 
4042 //
4043 // This has to come last of all:
4044 //
4045 #include <boost/multiprecision/detail/no_et_ops.hpp>
4046 #include <boost/multiprecision/detail/et_ops.hpp>
4047 //
4048 // min/max overloads:
4049 //
4050 #include <boost/multiprecision/detail/min_max.hpp>
4051 
4052 #endif