/////////////////////////////////////////////////////////////////////////////// // Copyright 2011 John Maddock. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_MATH_BIG_NUM_BASE_HPP #define BOOST_MATH_BIG_NUM_BASE_HPP #include #include #include #include #ifdef BOOST_MSVC # pragma warning(push) # pragma warning(disable:4307) #endif #include #ifdef BOOST_MSVC # pragma warning(pop) #endif #if defined(NDEBUG) && !defined(_DEBUG) # define BOOST_MP_FORCEINLINE BOOST_FORCEINLINE #else # define BOOST_MP_FORCEINLINE inline #endif namespace boost{ namespace multiprecision{ enum expression_template_option { et_off = 0, et_on = 1 }; template struct expression_template_default { static const expression_template_option value = et_on; }; template ::value> class number; template struct is_number : public mpl::false_ {}; template struct is_number > : public mpl::true_ {}; namespace detail{ // Forward-declare an expression wrapper template struct expression; } // namespace detail template struct is_number_expression : public mpl::false_ {}; template struct is_number_expression > : public mpl::true_ {}; template struct is_compatible_arithmetic_type : public mpl::bool_< is_convertible::value && !is_same::value && !is_number_expression::value> {}; namespace detail{ // // Workaround for missing abs(long long) and abs(__int128) on some compilers: // template BOOST_CONSTEXPR typename enable_if_c<(is_signed::value || is_floating_point::value), T>::type abs(T t) BOOST_NOEXCEPT { // This strange expression avoids a hardware trap in the corner case // that val is the most negative value permitted in long long. // See https://svn.boost.org/trac/boost/ticket/9740. return t < 0 ? T(1u) + T(-(t + 1)) : t; } template BOOST_CONSTEXPR typename enable_if_c<(is_unsigned::value), T>::type abs(T t) BOOST_NOEXCEPT { return t; } #define BOOST_MP_USING_ABS using boost::multiprecision::detail::abs; template BOOST_CONSTEXPR typename enable_if_c<(is_signed::value || is_floating_point::value), typename make_unsigned::type>::type unsigned_abs(T t) BOOST_NOEXCEPT { // This strange expression avoids a hardware trap in the corner case // that val is the most negative value permitted in long long. // See https://svn.boost.org/trac/boost/ticket/9740. return t < 0 ? static_cast::type>(1u) + static_cast::type>(-(t + 1)) : static_cast::type>(t); } template BOOST_CONSTEXPR typename enable_if_c<(is_unsigned::value), T>::type unsigned_abs(T t) BOOST_NOEXCEPT { return t; } // // Move support: // #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES # define BOOST_MP_MOVE(x) std::move(x) #else # define BOOST_MP_MOVE(x) x #endif template struct bits_of { BOOST_STATIC_ASSERT(is_integral::value || is_enum::value || std::numeric_limits::is_specialized); static const unsigned value = std::numeric_limits::is_specialized ? std::numeric_limits::digits : sizeof(T) * CHAR_BIT - (is_signed::value ? 1 : 0); }; template struct has_enough_bits { template struct type : public mpl::bool_::value>= b>{}; }; template struct canonical_imp { typedef typename remove_cv::type>::type type; }; template struct canonical_imp, Backend, Tag> { typedef B type; }; template struct canonical_imp, Backend, Tag> { typedef B type; }; template struct canonical_imp > { typedef typename has_enough_bits::value>::template type pred_type; typedef typename mpl::find_if< typename Backend::signed_types, pred_type >::type iter_type; typedef typename mpl::deref::type type; }; template struct canonical_imp > { typedef typename has_enough_bits::value>::template type pred_type; typedef typename mpl::find_if< typename Backend::unsigned_types, pred_type >::type iter_type; typedef typename mpl::deref::type type; }; template struct canonical_imp > { typedef typename has_enough_bits::value>::template type pred_type; typedef typename mpl::find_if< typename Backend::float_types, pred_type >::type iter_type; typedef typename mpl::deref::type type; }; template struct canonical_imp > { typedef const char* type; }; template struct canonical { typedef typename mpl::if_< is_signed, mpl::int_<0>, typename mpl::if_< is_unsigned, mpl::int_<1>, typename mpl::if_< is_floating_point, mpl::int_<2>, typename mpl::if_< mpl::or_< is_convertible, is_same >, mpl::int_<3>, mpl::int_<4> >::type >::type >::type >::type tag_type; typedef typename canonical_imp::type type; }; struct terminal{}; struct negate{}; struct plus{}; struct minus{}; struct multiplies{}; struct divides{}; struct modulus{}; struct shift_left{}; struct shift_right{}; struct bitwise_and{}; struct bitwise_or{}; struct bitwise_xor{}; struct bitwise_complement{}; struct add_immediates{}; struct subtract_immediates{}; struct multiply_immediates{}; struct divide_immediates{}; struct modulus_immediates{}; struct bitwise_and_immediates{}; struct bitwise_or_immediates{}; struct bitwise_xor_immediates{}; struct complement_immediates{}; struct function{}; struct multiply_add{}; struct multiply_subtract{}; template struct backend_type; template struct backend_type > { typedef T type; }; template struct backend_type > { typedef typename backend_type::result_type>::type type; }; template struct combine_expression { #ifdef BOOST_NO_CXX11_DECLTYPE typedef typename mpl::if_c<(sizeof(T1() + T2()) == sizeof(T1)), T1, T2>::type type; #else typedef decltype(T1() + T2()) type; #endif }; template struct combine_expression, T2> { typedef number type; }; template struct combine_expression > { typedef number type; }; template struct combine_expression, number > { typedef number type; }; template struct combine_expression, number > { typedef typename mpl::if_c< is_convertible, number >::value, number, number >::type type; }; template struct arg_type { typedef expression type; }; template struct arg_type > { typedef expression type; }; struct unmentionable { unmentionable* proc(){ return 0; } }; typedef unmentionable* (unmentionable::*unmentionable_type)(); template struct expression_storage { typedef const T& type; }; template struct expression_storage { typedef T* type; }; template struct expression_storage { typedef const T* type; }; template struct expression_storage > { typedef expression type; }; template struct expression { typedef mpl::int_<1> arity; typedef typename arg_type::type left_type; typedef typename left_type::result_type left_result_type; typedef typename left_type::result_type result_type; typedef tag tag_type; explicit expression(const Arg1& a) : arg(a) {} left_type left()const { return left_type(arg); } const Arg1& left_ref()const BOOST_NOEXCEPT { return arg; } static const unsigned depth = left_type::depth + 1; #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS explicit operator bool()const { result_type r(*this); return static_cast(r); } #else operator unmentionable_type()const { result_type r(*this); return r ? &unmentionable::proc : 0; } #endif private: typename expression_storage::type arg; expression& operator=(const expression&); }; template struct expression { typedef mpl::int_<0> arity; typedef Arg1 result_type; typedef terminal tag_type; explicit expression(const Arg1& a) : arg(a) {} const Arg1& value()const BOOST_NOEXCEPT { return arg; } static const unsigned depth = 0; #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS explicit operator bool()const { return static_cast(arg); } #else operator unmentionable_type()const { return arg ? &unmentionable::proc : 0; } #endif private: typename expression_storage::type arg; expression& operator=(const expression&); }; template struct expression { typedef mpl::int_<2> arity; typedef typename arg_type::type left_type; typedef typename arg_type::type right_type; typedef typename left_type::result_type left_result_type; typedef typename right_type::result_type right_result_type; typedef typename combine_expression::type result_type; typedef tag tag_type; expression(const Arg1& a1, const Arg2& a2) : arg1(a1), arg2(a2) {} left_type left()const { return left_type(arg1); } right_type right()const { return right_type(arg2); } const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; } const Arg2& right_ref()const BOOST_NOEXCEPT { return arg2; } #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS explicit operator bool()const { result_type r(*this); return static_cast(r); } #else operator unmentionable_type()const { result_type r(*this); return r ? &unmentionable::proc : 0; } #endif static const unsigned left_depth = left_type::depth + 1; static const unsigned right_depth = right_type::depth + 1; static const unsigned depth = left_depth > right_depth ? left_depth : right_depth; private: typename expression_storage::type arg1; typename expression_storage::type arg2; expression& operator=(const expression&); }; template struct expression { typedef mpl::int_<3> arity; typedef typename arg_type::type left_type; typedef typename arg_type::type middle_type; typedef typename arg_type::type right_type; typedef typename left_type::result_type left_result_type; typedef typename middle_type::result_type middle_result_type; typedef typename right_type::result_type right_result_type; typedef typename combine_expression< left_result_type, typename combine_expression::type >::type result_type; typedef tag tag_type; expression(const Arg1& a1, const Arg2& a2, const Arg3& a3) : arg1(a1), arg2(a2), arg3(a3) {} left_type left()const { return left_type(arg1); } middle_type middle()const { return middle_type(arg2); } right_type right()const { return right_type(arg3); } const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; } const Arg2& middle_ref()const BOOST_NOEXCEPT { return arg2; } const Arg3& right_ref()const BOOST_NOEXCEPT { return arg3; } #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS explicit operator bool()const { result_type r(*this); return static_cast(r); } #else operator unmentionable_type()const { result_type r(*this); return r ? &unmentionable::proc : 0; } #endif static const unsigned left_depth = left_type::depth + 1; static const unsigned middle_depth = middle_type::depth + 1; static const unsigned right_depth = right_type::depth + 1; static const unsigned depth = left_depth > right_depth ? (left_depth > middle_depth ? left_depth : middle_depth) : (right_depth > middle_depth ? right_depth : middle_depth); private: typename expression_storage::type arg1; typename expression_storage::type arg2; typename expression_storage::type arg3; expression& operator=(const expression&); }; template struct expression { typedef mpl::int_<4> arity; typedef typename arg_type::type left_type; typedef typename arg_type::type left_middle_type; typedef typename arg_type::type right_middle_type; typedef typename arg_type::type right_type; typedef typename left_type::result_type left_result_type; typedef typename left_middle_type::result_type left_middle_result_type; typedef typename right_middle_type::result_type right_middle_result_type; typedef typename right_type::result_type right_result_type; typedef typename combine_expression< typename combine_expression< typename combine_expression::type, right_middle_result_type >::type, right_result_type >::type result_type; typedef tag tag_type; expression(const Arg1& a1, const Arg2& a2, const Arg3& a3, const Arg4& a4) : arg1(a1), arg2(a2), arg3(a3), arg4(a4) {} left_type left()const { return left_type(arg1); } left_middle_type left_middle()const { return left_middle_type(arg2); } right_middle_type right_middle()const { return right_middle_type(arg3); } right_type right()const { return right_type(arg4); } const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; } const Arg2& left_middle_ref()const BOOST_NOEXCEPT { return arg2; } const Arg3& right_middle_ref()const BOOST_NOEXCEPT { return arg3; } const Arg4& right_ref()const BOOST_NOEXCEPT { return arg4; } #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS explicit operator bool()const { result_type r(*this); return static_cast(r); } #else operator unmentionable_type()const { result_type r(*this); return r ? &unmentionable::proc : 0; } #endif static const unsigned left_depth = left_type::depth + 1; static const unsigned left_middle_depth = left_middle_type::depth + 1; static const unsigned right_middle_depth = right_middle_type::depth + 1; static const unsigned right_depth = right_type::depth + 1; static const unsigned left_max_depth = left_depth > left_middle_depth ? left_depth : left_middle_depth; static const unsigned right_max_depth = right_depth > right_middle_depth ? right_depth : right_middle_depth; static const unsigned depth = left_max_depth > right_max_depth ? left_max_depth : right_max_depth; private: typename expression_storage::type arg1; typename expression_storage::type arg2; typename expression_storage::type arg3; typename expression_storage::type arg4; expression& operator=(const expression&); }; template struct digits2 { BOOST_STATIC_ASSERT(std::numeric_limits::is_specialized); BOOST_STATIC_ASSERT((std::numeric_limits::radix == 2) || (std::numeric_limits::radix == 10)); // If we really have so many digits that this fails, then we're probably going to hit other problems anyway: BOOST_STATIC_ASSERT(LONG_MAX / 1000 > (std::numeric_limits::digits + 1)); static const long value = std::numeric_limits::radix == 10 ? (((std::numeric_limits::digits + 1) * 1000L) / 301L) : std::numeric_limits::digits; }; #ifndef BOOST_MP_MIN_EXPONENT_DIGITS #ifdef _MSC_VER # define BOOST_MP_MIN_EXPONENT_DIGITS 2 #else # define BOOST_MP_MIN_EXPONENT_DIGITS 2 #endif #endif template void format_float_string(S& str, boost::intmax_t my_exp, boost::intmax_t digits, std::ios_base::fmtflags f, bool iszero) { typedef typename S::size_type size_type; bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific; bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed; bool showpoint = (f & std::ios_base::showpoint) == std::ios_base::showpoint; bool showpos = (f & std::ios_base::showpos) == std::ios_base::showpos; bool neg = str.size() && (str[0] == '-'); if(neg) str.erase(0, 1); if(digits == 0) { digits = (std::max)(str.size(), size_type(16)); } if(iszero || str.empty() || (str.find_first_not_of('0') == S::npos)) { // We will be printing zero, even though the value might not // actually be zero (it just may have been rounded to zero). str = "0"; if(scientific || fixed) { str.append(1, '.'); str.append(size_type(digits), '0'); if(scientific) str.append("e+00"); } else { if(showpoint) { str.append(1, '.'); if(digits > 1) str.append(size_type(digits - 1), '0'); } } if(neg) str.insert(0, 1, '-'); else if(showpos) str.insert(0, 1, '+'); return; } if(!fixed && !scientific && !showpoint) { // // Suppress trailing zeros: // std::string::iterator pos = str.end(); while(pos != str.begin() && *--pos == '0'){} if(pos != str.end()) ++pos; str.erase(pos, str.end()); if(str.empty()) str = '0'; } else if(!fixed || (my_exp >= 0)) { // // Pad out the end with zero's if we need to: // boost::intmax_t chars = str.size(); chars = digits - chars; if(scientific) ++chars; if(chars > 0) { str.append(static_cast(chars), '0'); } } if(fixed || (!scientific && (my_exp >= -4) && (my_exp < digits))) { if(1 + my_exp > static_cast(str.size())) { // Just pad out the end with zeros: str.append(static_cast(1 + my_exp - str.size()), '0'); if(showpoint || fixed) str.append("."); } else if(my_exp + 1 < static_cast(str.size())) { if(my_exp < 0) { str.insert(0, static_cast(-1 - my_exp), '0'); str.insert(0, "0."); } else { // Insert the decimal point: str.insert(static_cast(my_exp + 1), 1, '.'); } } else if(showpoint || fixed) // we have exactly the digits we require to left of the point str += "."; if(fixed) { // We may need to add trailing zeros: boost::intmax_t l = str.find('.') + 1; l = digits - (str.size() - l); if(l > 0) str.append(size_type(l), '0'); } } else { BOOST_MP_USING_ABS // Scientific format: if(showpoint || (str.size() > 1)) str.insert(1, 1, '.'); str.append(1, 'e'); S e = boost::lexical_cast(abs(my_exp)); if(e.size() < BOOST_MP_MIN_EXPONENT_DIGITS) e.insert(0, BOOST_MP_MIN_EXPONENT_DIGITS-e.size(), '0'); if(my_exp < 0) e.insert(0, 1, '-'); else e.insert(0, 1, '+'); str.append(e); } if(neg) str.insert(0, 1, '-'); else if(showpos) str.insert(0, 1, '+'); } template void check_shift_range(V val, const mpl::true_&, const mpl::true_&) { if(val > (std::numeric_limits::max)()) BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits::max().")); if(val < 0) BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value.")); } template void check_shift_range(V val, const mpl::false_&, const mpl::true_&) { if(val < 0) BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value.")); } template void check_shift_range(V val, const mpl::true_&, const mpl::false_&) { if(val > (std::numeric_limits::max)()) BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits::max().")); } template void check_shift_range(V, const mpl::false_&, const mpl::false_&) BOOST_NOEXCEPT{} } // namespace detail // // Traits class, lets us know what kind of number we have, defaults to a floating point type: // enum number_category_type { number_kind_unknown = -1, number_kind_integer = 0, number_kind_floating_point = 1, number_kind_rational = 2, number_kind_fixed_point = 3 }; template struct number_category : public mpl::int_::is_integer ? number_kind_integer : (std::numeric_limits::max_exponent ? number_kind_floating_point : number_kind_unknown)> {}; template struct number_category > : public number_category{}; template struct number_category > : public number_category::result_type>{}; template struct component_type; template struct component_type > : public component_type{}; template struct component_type > : public component_type::result_type>{}; template struct is_unsigned_number : public mpl::false_{}; template struct is_unsigned_number > : public is_unsigned_number {}; template struct is_signed_number : public mpl::bool_::value> {}; template struct is_interval_number : public mpl::false_ {}; template struct is_interval_number > : public is_interval_number{}; }} // namespaces namespace boost{ namespace math{ namespace tools{ template struct promote_arg; template struct promote_arg > { typedef typename boost::multiprecision::detail::expression::result_type type; }; template inline R real_cast(const boost::multiprecision::number& val) { return val.template convert_to(); } template inline R real_cast(const boost::multiprecision::detail::expression& val) { typedef typename boost::multiprecision::detail::expression::result_type val_type; return val_type(val).template convert_to(); } } namespace constants{ template struct is_explicitly_convertible_from_string; template struct is_explicitly_convertible_from_string > { static const bool value = true; }; } }} #endif // BOOST_MATH_BIG_NUM_BASE_HPP