1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright 2011 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_BIG_NUM_BASE_HPP
7 #define BOOST_MATH_BIG_NUM_BASE_HPP
10 #include <boost/utility/enable_if.hpp>
11 #include <boost/type_traits/is_convertible.hpp>
12 #include <boost/type_traits/is_constructible.hpp>
13 #include <boost/type_traits/decay.hpp>
15 # pragma warning(push)
16 # pragma warning(disable:4307)
18 #include <boost/lexical_cast.hpp>
23 #if defined(NDEBUG) && !defined(_DEBUG)
24 # define BOOST_MP_FORCEINLINE BOOST_FORCEINLINE
26 # define BOOST_MP_FORCEINLINE inline
29 #if (defined(BOOST_GCC) && (BOOST_GCC <= 40700)) || BOOST_WORKAROUND(__SUNPRO_CC, < 0x5140)
30 # define BOOST_MP_NOEXCEPT_IF(x)
32 # define BOOST_MP_NOEXCEPT_IF(x) BOOST_NOEXCEPT_IF(x)
35 #if defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) || BOOST_WORKAROUND(__SUNPRO_CC, < 0x5140)
36 #define BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
40 // Thread local storage:
41 // Note fails on Mingw, see https://sourceforge.net/p/mingw-w64/bugs/527/
43 #if !defined(BOOST_NO_CXX11_THREAD_LOCAL) && !defined(BOOST_INTEL) && !defined(__MINGW32__)
44 # define BOOST_MP_THREAD_LOCAL thread_local
46 # define BOOST_MP_THREAD_LOCAL
50 # pragma warning(push)
51 # pragma warning(disable:6326)
55 namespace multiprecision{
57 enum expression_template_option
63 template <class Backend>
64 struct expression_template_default
66 static const expression_template_option value = et_on;
69 template <class Backend, expression_template_option ExpressionTemplates = expression_template_default<Backend>::value>
73 struct is_number : public mpl::false_ {};
75 template <class Backend, expression_template_option ExpressionTemplates>
76 struct is_number<number<Backend, ExpressionTemplates> > : public mpl::true_ {};
79 struct is_et_number : public mpl::false_ {};
81 template <class Backend>
82 struct is_et_number<number<Backend, et_on> > : public mpl::true_ {};
85 struct is_no_et_number : public mpl::false_ {};
87 template <class Backend>
88 struct is_no_et_number<number<Backend, et_off> > : public mpl::true_ {};
92 // Forward-declare an expression wrapper
93 template<class tag, class Arg1 = void, class Arg2 = void, class Arg3 = void, class Arg4 = void>
99 struct is_number_expression : public mpl::false_ {};
101 template<class tag, class Arg1, class Arg2, class Arg3, class Arg4>
102 struct is_number_expression<detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > : public mpl::true_ {};
104 template <class T, class Num>
105 struct is_compatible_arithmetic_type
107 is_convertible<T, Num>::value
108 && !is_same<T, Num>::value
109 && !is_number_expression<T>::value>
114 // Workaround for missing abs(boost::long_long_type) and abs(__int128) on some compilers:
117 BOOST_CONSTEXPR typename enable_if_c<(is_signed<T>::value || is_floating_point<T>::value), T>::type abs(T t) BOOST_NOEXCEPT
119 // This strange expression avoids a hardware trap in the corner case
120 // that val is the most negative value permitted in boost::long_long_type.
121 // See https://svn.boost.org/trac/boost/ticket/9740.
122 return t < 0 ? T(1u) + T(-(t + 1)) : t;
125 BOOST_CONSTEXPR typename enable_if_c<(is_unsigned<T>::value), T>::type abs(T t) BOOST_NOEXCEPT
130 #define BOOST_MP_USING_ABS using boost::multiprecision::detail::abs;
133 BOOST_CONSTEXPR typename enable_if_c<(is_signed<T>::value || is_floating_point<T>::value), typename make_unsigned<T>::type>::type unsigned_abs(T t) BOOST_NOEXCEPT
135 // This strange expression avoids a hardware trap in the corner case
136 // that val is the most negative value permitted in boost::long_long_type.
137 // See https://svn.boost.org/trac/boost/ticket/9740.
138 return t < 0 ? static_cast<typename make_unsigned<T>::type>(1u) + static_cast<typename make_unsigned<T>::type>(-(t + 1)) : static_cast<typename make_unsigned<T>::type>(t);
141 BOOST_CONSTEXPR typename enable_if_c<(is_unsigned<T>::value), T>::type unsigned_abs(T t) BOOST_NOEXCEPT
149 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
150 # define BOOST_MP_MOVE(x) std::move(x)
152 # define BOOST_MP_MOVE(x) x
158 BOOST_STATIC_ASSERT(is_integral<T>::value || is_enum<T>::value || std::numeric_limits<T>::is_specialized);
159 static const unsigned value =
160 std::numeric_limits<T>::is_specialized ?
161 std::numeric_limits<T>::digits
162 : sizeof(T) * CHAR_BIT - (is_signed<T>::value ? 1 : 0);
165 #if defined(_GLIBCXX_USE_FLOAT128) && defined(BOOST_GCC) && !defined(__STRICT_ANSI__)
166 template<> struct bits_of<__float128> { static const unsigned value = 113; };
170 struct has_enough_bits
173 struct type : public mpl::bool_<bits_of<T>::value>= b>{};
176 template <class Val, class Backend, class Tag>
179 typedef typename remove_cv<typename decay<const Val>::type>::type type;
181 template <class B, class Backend, class Tag>
182 struct canonical_imp<number<B, et_on>, Backend, Tag>
186 template <class B, class Backend, class Tag>
187 struct canonical_imp<number<B, et_off>, Backend, Tag>
192 template <class B, class Backend>
193 struct canonical_imp<number<B, et_on>, Backend, mpl::int_<3> >
197 template <class B, class Backend>
198 struct canonical_imp<number<B, et_off>, Backend, mpl::int_<3> >
203 template <class Val, class Backend>
204 struct canonical_imp<Val, Backend, mpl::int_<0> >
206 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
207 typedef typename mpl::find_if<
208 typename Backend::signed_types,
211 typedef typename mpl::end<typename Backend::signed_types>::type end_type;
212 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
214 template <class Val, class Backend>
215 struct canonical_imp<Val, Backend, mpl::int_<1> >
217 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
218 typedef typename mpl::find_if<
219 typename Backend::unsigned_types,
222 typedef typename mpl::end<typename Backend::unsigned_types>::type end_type;
223 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
225 template <class Val, class Backend>
226 struct canonical_imp<Val, Backend, mpl::int_<2> >
228 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
229 typedef typename mpl::find_if<
230 typename Backend::float_types,
233 typedef typename mpl::end<typename Backend::float_types>::type end_type;
234 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
236 template <class Val, class Backend>
237 struct canonical_imp<Val, Backend, mpl::int_<3> >
239 typedef const char* type;
242 template <class Val, class Backend>
245 typedef typename mpl::if_<
252 is_floating_point<Val>,
256 is_convertible<Val, const char*>,
257 is_same<Val, std::string>
266 typedef typename canonical_imp<Val, Backend, tag_type>::type type;
277 struct shift_right{};
278 struct bitwise_and{};
280 struct bitwise_xor{};
281 struct bitwise_complement{};
282 struct add_immediates{};
283 struct subtract_immediates{};
284 struct multiply_immediates{};
285 struct divide_immediates{};
286 struct modulus_immediates{};
287 struct bitwise_and_immediates{};
288 struct bitwise_or_immediates{};
289 struct bitwise_xor_immediates{};
290 struct complement_immediates{};
292 struct multiply_add{};
293 struct multiply_subtract{};
298 template <class T, expression_template_option ExpressionTemplates>
299 struct backend_type<number<T, ExpressionTemplates> >
304 template <class tag, class A1, class A2, class A3, class A4>
305 struct backend_type<expression<tag, A1, A2, A3, A4> >
307 typedef typename backend_type<typename expression<tag, A1, A2, A3, A4>::result_type>::type type;
311 template <class T1, class T2>
312 struct combine_expression
314 #ifdef BOOST_NO_CXX11_DECLTYPE
315 typedef typename mpl::if_c<(sizeof(T1() + T2()) == sizeof(T1)), T1, T2>::type type;
317 typedef decltype(T1() + T2()) type;
321 template <class T1, expression_template_option ExpressionTemplates, class T2>
322 struct combine_expression<number<T1, ExpressionTemplates>, T2>
324 typedef number<T1, ExpressionTemplates> type;
327 template <class T1, class T2, expression_template_option ExpressionTemplates>
328 struct combine_expression<T1, number<T2, ExpressionTemplates> >
330 typedef number<T2, ExpressionTemplates> type;
333 template <class T, expression_template_option ExpressionTemplates>
334 struct combine_expression<number<T, ExpressionTemplates>, number<T, ExpressionTemplates> >
336 typedef number<T, ExpressionTemplates> type;
339 template <class T1, expression_template_option ExpressionTemplates1, class T2, expression_template_option ExpressionTemplates2>
340 struct combine_expression<number<T1, ExpressionTemplates1>, number<T2, ExpressionTemplates2> >
342 typedef typename mpl::if_c<
343 is_convertible<number<T2, ExpressionTemplates2>, number<T1, ExpressionTemplates2> >::value,
344 number<T1, ExpressionTemplates1>,
345 number<T2, ExpressionTemplates2>
352 typedef expression<terminal, T> type;
355 template <class Tag, class Arg1, class Arg2, class Arg3, class Arg4>
356 struct arg_type<expression<Tag, Arg1, Arg2, Arg3, Arg4> >
358 typedef expression<Tag, Arg1, Arg2, Arg3, Arg4> type;
363 unmentionable* proc(){ return 0; }
366 typedef unmentionable* (unmentionable::*unmentionable_type)();
368 template <class T, bool b>
369 struct expression_storage_base
371 typedef const T& type;
375 struct expression_storage_base<T, true>
381 struct expression_storage : public expression_storage_base<T, boost::is_arithmetic<T>::value> {};
384 struct expression_storage<T*>
390 struct expression_storage<const T*>
392 typedef const T* type;
395 template <class tag, class A1, class A2, class A3, class A4>
396 struct expression_storage<expression<tag, A1, A2, A3, A4> >
398 typedef expression<tag, A1, A2, A3, A4> type;
401 template<class tag, class Arg1>
402 struct expression<tag, Arg1, void, void, void>
404 typedef mpl::int_<1> arity;
405 typedef typename arg_type<Arg1>::type left_type;
406 typedef typename left_type::result_type left_result_type;
407 typedef typename left_type::result_type result_type;
408 typedef tag tag_type;
410 explicit expression(const Arg1& a) : arg(a) {}
412 left_type left()const { return left_type(arg); }
414 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg; }
416 static const unsigned depth = left_type::depth + 1;
417 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
418 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
420 // Horrible workaround for gcc-4.6.x which always prefers the template
421 // operator bool() rather than the non-template operator when converting to
422 // an arithmetic type:
424 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
425 explicit operator T ()const
427 result_type r(*this);
428 return static_cast<bool>(r);
430 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
431 explicit operator T ()const
433 return static_cast<T>(static_cast<result_type>(*this));
438 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
441 explicit operator T()const
443 return static_cast<T>(static_cast<result_type>(*this));
445 BOOST_MP_FORCEINLINE explicit operator bool()const
447 result_type r(*this);
448 return static_cast<bool>(r);
450 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
451 BOOST_MP_FORCEINLINE explicit operator void()const {}
455 operator unmentionable_type()const
457 result_type r(*this);
458 return r ? &unmentionable::proc : 0;
465 result_type r(*this);
466 return r.template convert_to<T>();
470 typename expression_storage<Arg1>::type arg;
471 expression& operator=(const expression&);
475 struct expression<terminal, Arg1, void, void, void>
477 typedef mpl::int_<0> arity;
478 typedef Arg1 result_type;
479 typedef terminal tag_type;
481 explicit expression(const Arg1& a) : arg(a) {}
483 const Arg1& value()const BOOST_NOEXCEPT { return arg; }
485 static const unsigned depth = 0;
487 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
488 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
490 // Horrible workaround for gcc-4.6.x which always prefers the template
491 // operator bool() rather than the non-template operator when converting to
492 // an arithmetic type:
494 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
495 explicit operator T ()const
497 result_type r(*this);
498 return static_cast<bool>(r);
500 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
501 explicit operator T ()const
503 return static_cast<T>(static_cast<result_type>(*this));
508 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
511 explicit operator T()const
513 return static_cast<T>(static_cast<result_type>(*this));
515 BOOST_MP_FORCEINLINE explicit operator bool()const
517 result_type r(*this);
518 return static_cast<bool>(r);
520 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
521 BOOST_MP_FORCEINLINE explicit operator void()const {}
525 operator unmentionable_type()const
527 return arg ? &unmentionable::proc : 0;
534 result_type r(*this);
535 return r.template convert_to<T>();
539 typename expression_storage<Arg1>::type arg;
540 expression& operator=(const expression&);
543 template <class tag, class Arg1, class Arg2>
544 struct expression<tag, Arg1, Arg2, void, void>
546 typedef mpl::int_<2> arity;
547 typedef typename arg_type<Arg1>::type left_type;
548 typedef typename arg_type<Arg2>::type right_type;
549 typedef typename left_type::result_type left_result_type;
550 typedef typename right_type::result_type right_result_type;
551 typedef typename combine_expression<left_result_type, right_result_type>::type result_type;
552 typedef tag tag_type;
554 expression(const Arg1& a1, const Arg2& a2) : arg1(a1), arg2(a2) {}
556 left_type left()const { return left_type(arg1); }
557 right_type right()const { return right_type(arg2); }
558 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
559 const Arg2& right_ref()const BOOST_NOEXCEPT { return arg2; }
561 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
562 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
564 // Horrible workaround for gcc-4.6.x which always prefers the template
565 // operator bool() rather than the non-template operator when converting to
566 // an arithmetic type:
568 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
569 explicit operator T ()const
571 result_type r(*this);
572 return static_cast<bool>(r);
574 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
575 explicit operator T ()const
577 return static_cast<T>(static_cast<result_type>(*this));
582 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
585 explicit operator T()const
587 return static_cast<T>(static_cast<result_type>(*this));
589 BOOST_MP_FORCEINLINE explicit operator bool()const
591 result_type r(*this);
592 return static_cast<bool>(r);
594 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
595 BOOST_MP_FORCEINLINE explicit operator void()const {}
599 operator unmentionable_type()const
601 result_type r(*this);
602 return r ? &unmentionable::proc : 0;
608 result_type r(*this);
609 return r.template convert_to<T>();
612 static const unsigned left_depth = left_type::depth + 1;
613 static const unsigned right_depth = right_type::depth + 1;
614 static const unsigned depth = left_depth > right_depth ? left_depth : right_depth;
616 typename expression_storage<Arg1>::type arg1;
617 typename expression_storage<Arg2>::type arg2;
618 expression& operator=(const expression&);
621 template <class tag, class Arg1, class Arg2, class Arg3>
622 struct expression<tag, Arg1, Arg2, Arg3, void>
624 typedef mpl::int_<3> arity;
625 typedef typename arg_type<Arg1>::type left_type;
626 typedef typename arg_type<Arg2>::type middle_type;
627 typedef typename arg_type<Arg3>::type right_type;
628 typedef typename left_type::result_type left_result_type;
629 typedef typename middle_type::result_type middle_result_type;
630 typedef typename right_type::result_type right_result_type;
631 typedef typename combine_expression<
633 typename combine_expression<right_result_type, middle_result_type>::type
635 typedef tag tag_type;
637 expression(const Arg1& a1, const Arg2& a2, const Arg3& a3) : arg1(a1), arg2(a2), arg3(a3) {}
639 left_type left()const { return left_type(arg1); }
640 middle_type middle()const { return middle_type(arg2); }
641 right_type right()const { return right_type(arg3); }
642 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
643 const Arg2& middle_ref()const BOOST_NOEXCEPT { return arg2; }
644 const Arg3& right_ref()const BOOST_NOEXCEPT { return arg3; }
646 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
647 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
649 // Horrible workaround for gcc-4.6.x which always prefers the template
650 // operator bool() rather than the non-template operator when converting to
651 // an arithmetic type:
653 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
654 explicit operator T ()const
656 result_type r(*this);
657 return static_cast<bool>(r);
659 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
660 explicit operator T ()const
662 return static_cast<T>(static_cast<result_type>(*this));
667 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
670 explicit operator T()const
672 return static_cast<T>(static_cast<result_type>(*this));
674 BOOST_MP_FORCEINLINE explicit operator bool()const
676 result_type r(*this);
677 return static_cast<bool>(r);
679 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
680 BOOST_MP_FORCEINLINE explicit operator void()const {}
684 operator unmentionable_type()const
686 result_type r(*this);
687 return r ? &unmentionable::proc : 0;
693 result_type r(*this);
694 return r.template convert_to<T>();
697 static const unsigned left_depth = left_type::depth + 1;
698 static const unsigned middle_depth = middle_type::depth + 1;
699 static const unsigned right_depth = right_type::depth + 1;
700 static const unsigned depth = left_depth > right_depth ? (left_depth > middle_depth ? left_depth : middle_depth) : (right_depth > middle_depth ? right_depth : middle_depth);
702 typename expression_storage<Arg1>::type arg1;
703 typename expression_storage<Arg2>::type arg2;
704 typename expression_storage<Arg3>::type arg3;
705 expression& operator=(const expression&);
708 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
711 typedef mpl::int_<4> arity;
712 typedef typename arg_type<Arg1>::type left_type;
713 typedef typename arg_type<Arg2>::type left_middle_type;
714 typedef typename arg_type<Arg3>::type right_middle_type;
715 typedef typename arg_type<Arg4>::type right_type;
716 typedef typename left_type::result_type left_result_type;
717 typedef typename left_middle_type::result_type left_middle_result_type;
718 typedef typename right_middle_type::result_type right_middle_result_type;
719 typedef typename right_type::result_type right_result_type;
720 typedef typename combine_expression<
722 typename combine_expression<
723 left_middle_result_type,
724 typename combine_expression<right_middle_result_type, right_result_type>::type
727 typedef tag tag_type;
729 expression(const Arg1& a1, const Arg2& a2, const Arg3& a3, const Arg4& a4) : arg1(a1), arg2(a2), arg3(a3), arg4(a4) {}
731 left_type left()const { return left_type(arg1); }
732 left_middle_type left_middle()const { return left_middle_type(arg2); }
733 right_middle_type right_middle()const { return right_middle_type(arg3); }
734 right_type right()const { return right_type(arg4); }
735 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
736 const Arg2& left_middle_ref()const BOOST_NOEXCEPT { return arg2; }
737 const Arg3& right_middle_ref()const BOOST_NOEXCEPT { return arg3; }
738 const Arg4& right_ref()const BOOST_NOEXCEPT { return arg4; }
740 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
741 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
743 // Horrible workaround for gcc-4.6.x which always prefers the template
744 // operator bool() rather than the non-template operator when converting to
745 // an arithmetic type:
747 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
748 explicit operator T ()const
750 result_type r(*this);
751 return static_cast<bool>(r);
753 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
754 explicit operator T ()const
756 return static_cast<T>(static_cast<result_type>(*this));
761 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
764 explicit operator T()const
766 return static_cast<T>(static_cast<result_type>(*this));
768 BOOST_MP_FORCEINLINE explicit operator bool()const
770 result_type r(*this);
771 return static_cast<bool>(r);
773 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
774 BOOST_MP_FORCEINLINE explicit operator void()const {}
778 operator unmentionable_type()const
780 result_type r(*this);
781 return r ? &unmentionable::proc : 0;
787 result_type r(*this);
788 return r.template convert_to<T>();
791 static const unsigned left_depth = left_type::depth + 1;
792 static const unsigned left_middle_depth = left_middle_type::depth + 1;
793 static const unsigned right_middle_depth = right_middle_type::depth + 1;
794 static const unsigned right_depth = right_type::depth + 1;
796 static const unsigned left_max_depth = left_depth > left_middle_depth ? left_depth : left_middle_depth;
797 static const unsigned right_max_depth = right_depth > right_middle_depth ? right_depth : right_middle_depth;
799 static const unsigned depth = left_max_depth > right_max_depth ? left_max_depth : right_max_depth;
801 typename expression_storage<Arg1>::type arg1;
802 typename expression_storage<Arg2>::type arg2;
803 typename expression_storage<Arg3>::type arg3;
804 typename expression_storage<Arg4>::type arg4;
805 expression& operator=(const expression&);
811 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
812 BOOST_STATIC_ASSERT((std::numeric_limits<T>::radix == 2) || (std::numeric_limits<T>::radix == 10));
813 // If we really have so many digits that this fails, then we're probably going to hit other problems anyway:
814 BOOST_STATIC_ASSERT(LONG_MAX / 1000 > (std::numeric_limits<T>::digits + 1));
815 static const long m_value = std::numeric_limits<T>::radix == 10 ? (((std::numeric_limits<T>::digits + 1) * 1000L) / 301L) : std::numeric_limits<T>::digits;
816 static inline BOOST_CONSTEXPR long value()BOOST_NOEXCEPT { return m_value; }
819 #ifndef BOOST_MP_MIN_EXPONENT_DIGITS
821 # define BOOST_MP_MIN_EXPONENT_DIGITS 2
823 # define BOOST_MP_MIN_EXPONENT_DIGITS 2
828 void format_float_string(S& str, boost::intmax_t my_exp, boost::intmax_t digits, std::ios_base::fmtflags f, bool iszero)
830 typedef typename S::size_type size_type;
831 bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
832 bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
833 bool showpoint = (f & std::ios_base::showpoint) == std::ios_base::showpoint;
834 bool showpos = (f & std::ios_base::showpos) == std::ios_base::showpos;
836 bool neg = str.size() && (str[0] == '-');
843 digits = (std::max)(str.size(), size_type(16));
846 if(iszero || str.empty() || (str.find_first_not_of('0') == S::npos))
848 // We will be printing zero, even though the value might not
849 // actually be zero (it just may have been rounded to zero).
851 if(scientific || fixed)
854 str.append(size_type(digits), '0');
864 str.append(size_type(digits - 1), '0');
868 str.insert(static_cast<std::string::size_type>(0), 1, '-');
870 str.insert(static_cast<std::string::size_type>(0), 1, '+');
874 if(!fixed && !scientific && !showpoint)
877 // Suppress trailing zeros:
879 std::string::iterator pos = str.end();
880 while(pos != str.begin() && *--pos == '0'){}
883 str.erase(pos, str.end());
887 else if(!fixed || (my_exp >= 0))
890 // Pad out the end with zero's if we need to:
892 boost::intmax_t chars = str.size();
893 chars = digits - chars;
898 str.append(static_cast<std::string::size_type>(chars), '0');
902 if(fixed || (!scientific && (my_exp >= -4) && (my_exp < digits)))
904 if(1 + my_exp > static_cast<boost::intmax_t>(str.size()))
906 // Just pad out the end with zeros:
907 str.append(static_cast<std::string::size_type>(1 + my_exp - str.size()), '0');
908 if(showpoint || fixed)
911 else if(my_exp + 1 < static_cast<boost::intmax_t>(str.size()))
915 str.insert(static_cast<std::string::size_type>(0), static_cast<std::string::size_type>(-1 - my_exp), '0');
916 str.insert(static_cast<std::string::size_type>(0), "0.");
920 // Insert the decimal point:
921 str.insert(static_cast<std::string::size_type>(my_exp + 1), 1, '.');
924 else if(showpoint || fixed) // we have exactly the digits we require to left of the point
929 // We may need to add trailing zeros:
930 boost::intmax_t l = str.find('.') + 1;
931 l = digits - (str.size() - l);
933 str.append(size_type(l), '0');
939 // Scientific format:
940 if(showpoint || (str.size() > 1))
941 str.insert(static_cast<std::string::size_type>(1u), 1, '.');
942 str.append(static_cast<std::string::size_type>(1u), 'e');
943 S e = boost::lexical_cast<S>(abs(my_exp));
944 if(e.size() < BOOST_MP_MIN_EXPONENT_DIGITS)
945 e.insert(static_cast<std::string::size_type>(0), BOOST_MP_MIN_EXPONENT_DIGITS - e.size(), '0');
947 e.insert(static_cast<std::string::size_type>(0), 1, '-');
949 e.insert(static_cast<std::string::size_type>(0), 1, '+');
953 str.insert(static_cast<std::string::size_type>(0), 1, '-');
955 str.insert(static_cast<std::string::size_type>(0), 1, '+');
959 void check_shift_range(V val, const mpl::true_&, const mpl::true_&)
961 if(val > (std::numeric_limits<std::size_t>::max)())
962 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
964 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
967 void check_shift_range(V val, const mpl::false_&, const mpl::true_&)
970 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
973 void check_shift_range(V val, const mpl::true_&, const mpl::false_&)
975 if(val > (std::numeric_limits<std::size_t>::max)())
976 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
979 void check_shift_range(V, const mpl::false_&, const mpl::false_&) BOOST_NOEXCEPT{}
981 } // namespace detail
984 // Traits class, lets us know what kind of number we have, defaults to a floating point type:
986 enum number_category_type
988 number_kind_unknown = -1,
989 number_kind_integer = 0,
990 number_kind_floating_point = 1,
991 number_kind_rational = 2,
992 number_kind_fixed_point = 3
996 struct number_category : public mpl::int_<std::numeric_limits<Num>::is_integer ? number_kind_integer : (std::numeric_limits<Num>::max_exponent ? number_kind_floating_point : number_kind_unknown)> {};
997 template <class Backend, expression_template_option ExpressionTemplates>
998 struct number_category<number<Backend, ExpressionTemplates> > : public number_category<Backend>{};
999 template <class tag, class A1, class A2, class A3, class A4>
1000 struct number_category<detail::expression<tag, A1, A2, A3, A4> > : public number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>{};
1002 // Specializations for types which do not always have numberic_limits specializations:
1004 #ifdef BOOST_HAS_INT128
1006 struct number_category<__int128> : public mpl::int_<number_kind_integer> {};
1008 struct number_category<unsigned __int128> : public mpl::int_<number_kind_integer> {};
1010 #ifdef BOOST_HAS_FLOAT128
1012 struct number_category<__float128> : public mpl::int_<number_kind_floating_point> {};
1016 struct component_type;
1017 template <class T, expression_template_option ExpressionTemplates>
1018 struct component_type<number<T, ExpressionTemplates> > : public component_type<T>{};
1019 template <class tag, class A1, class A2, class A3, class A4>
1020 struct component_type<detail::expression<tag, A1, A2, A3, A4> > : public component_type<typename detail::expression<tag, A1, A2, A3, A4>::result_type>{};
1023 struct is_unsigned_number : public mpl::false_{};
1024 template <class Backend, expression_template_option ExpressionTemplates>
1025 struct is_unsigned_number<number<Backend, ExpressionTemplates> > : public is_unsigned_number<Backend> {};
1027 struct is_signed_number : public mpl::bool_<!is_unsigned_number<T>::value> {};
1029 struct is_interval_number : public mpl::false_ {};
1030 template <class Backend, expression_template_option ExpressionTemplates>
1031 struct is_interval_number<number<Backend, ExpressionTemplates> > : public is_interval_number<Backend>{};
1035 namespace boost{ namespace math{ namespace tools{
1040 template <class tag, class A1, class A2, class A3, class A4>
1041 struct promote_arg<boost::multiprecision::detail::expression<tag, A1, A2, A3, A4> >
1043 typedef typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type type;
1046 template <class R, class B, boost::multiprecision::expression_template_option ET>
1047 inline R real_cast(const boost::multiprecision::number<B, ET>& val)
1049 return val.template convert_to<R>();
1052 template <class R, class tag, class A1, class A2, class A3, class A4>
1053 inline R real_cast(const boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>& val)
1055 typedef typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type val_type;
1056 return val_type(val).template convert_to<R>();
1062 namespace constants{
1065 struct is_explicitly_convertible_from_string;
1067 template <class B, boost::multiprecision::expression_template_option ET>
1068 struct is_explicitly_convertible_from_string<boost::multiprecision::number<B, ET> >
1070 static const bool value = true;
1078 # pragma warning(pop)
1081 #endif // BOOST_MATH_BIG_NUM_BASE_HPP