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:
42 #if !defined(BOOST_NO_CXX11_THREAD_LOCAL) && !defined(BOOST_INTEL)
43 # define BOOST_MP_THREAD_LOCAL thread_local
45 # define BOOST_MP_THREAD_LOCAL
49 # pragma warning(push)
50 # pragma warning(disable:6326)
54 namespace multiprecision{
56 enum expression_template_option
62 template <class Backend>
63 struct expression_template_default
65 static const expression_template_option value = et_on;
68 template <class Backend, expression_template_option ExpressionTemplates = expression_template_default<Backend>::value>
72 struct is_number : public mpl::false_ {};
74 template <class Backend, expression_template_option ExpressionTemplates>
75 struct is_number<number<Backend, ExpressionTemplates> > : public mpl::true_ {};
78 struct is_et_number : public mpl::false_ {};
80 template <class Backend>
81 struct is_et_number<number<Backend, et_on> > : public mpl::true_ {};
84 struct is_no_et_number : public mpl::false_ {};
86 template <class Backend>
87 struct is_no_et_number<number<Backend, et_off> > : public mpl::true_ {};
91 // Forward-declare an expression wrapper
92 template<class tag, class Arg1 = void, class Arg2 = void, class Arg3 = void, class Arg4 = void>
98 struct is_number_expression : public mpl::false_ {};
100 template<class tag, class Arg1, class Arg2, class Arg3, class Arg4>
101 struct is_number_expression<detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > : public mpl::true_ {};
103 template <class T, class Num>
104 struct is_compatible_arithmetic_type
106 is_convertible<T, Num>::value
107 && !is_same<T, Num>::value
108 && !is_number_expression<T>::value>
113 // Workaround for missing abs(boost::long_long_type) and abs(__int128) on some compilers:
116 BOOST_CONSTEXPR typename enable_if_c<(is_signed<T>::value || is_floating_point<T>::value), T>::type abs(T t) BOOST_NOEXCEPT
118 // This strange expression avoids a hardware trap in the corner case
119 // that val is the most negative value permitted in boost::long_long_type.
120 // See https://svn.boost.org/trac/boost/ticket/9740.
121 return t < 0 ? T(1u) + T(-(t + 1)) : t;
124 BOOST_CONSTEXPR typename enable_if_c<(is_unsigned<T>::value), T>::type abs(T t) BOOST_NOEXCEPT
129 #define BOOST_MP_USING_ABS using boost::multiprecision::detail::abs;
132 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
134 // This strange expression avoids a hardware trap in the corner case
135 // that val is the most negative value permitted in boost::long_long_type.
136 // See https://svn.boost.org/trac/boost/ticket/9740.
137 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);
140 BOOST_CONSTEXPR typename enable_if_c<(is_unsigned<T>::value), T>::type unsigned_abs(T t) BOOST_NOEXCEPT
148 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
149 # define BOOST_MP_MOVE(x) std::move(x)
151 # define BOOST_MP_MOVE(x) x
157 BOOST_STATIC_ASSERT(is_integral<T>::value || is_enum<T>::value || std::numeric_limits<T>::is_specialized);
158 static const unsigned value =
159 std::numeric_limits<T>::is_specialized ?
160 std::numeric_limits<T>::digits
161 : sizeof(T) * CHAR_BIT - (is_signed<T>::value ? 1 : 0);
164 #if defined(_GLIBCXX_USE_FLOAT128) && defined(BOOST_GCC) && !defined(__STRICT_ANSI__)
165 template<> struct bits_of<__float128> { static const unsigned value = 113; };
169 struct has_enough_bits
172 struct type : public mpl::bool_<bits_of<T>::value>= b>{};
175 template <class Val, class Backend, class Tag>
178 typedef typename remove_cv<typename decay<const Val>::type>::type type;
180 template <class B, class Backend, class Tag>
181 struct canonical_imp<number<B, et_on>, Backend, Tag>
185 template <class B, class Backend, class Tag>
186 struct canonical_imp<number<B, et_off>, Backend, Tag>
191 template <class B, class Backend>
192 struct canonical_imp<number<B, et_on>, Backend, mpl::int_<3> >
196 template <class B, class Backend>
197 struct canonical_imp<number<B, et_off>, Backend, mpl::int_<3> >
202 template <class Val, class Backend>
203 struct canonical_imp<Val, Backend, mpl::int_<0> >
205 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
206 typedef typename mpl::find_if<
207 typename Backend::signed_types,
210 typedef typename mpl::end<typename Backend::signed_types>::type end_type;
211 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
213 template <class Val, class Backend>
214 struct canonical_imp<Val, Backend, mpl::int_<1> >
216 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
217 typedef typename mpl::find_if<
218 typename Backend::unsigned_types,
221 typedef typename mpl::end<typename Backend::unsigned_types>::type end_type;
222 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
224 template <class Val, class Backend>
225 struct canonical_imp<Val, Backend, mpl::int_<2> >
227 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
228 typedef typename mpl::find_if<
229 typename Backend::float_types,
232 typedef typename mpl::end<typename Backend::float_types>::type end_type;
233 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
235 template <class Val, class Backend>
236 struct canonical_imp<Val, Backend, mpl::int_<3> >
238 typedef const char* type;
241 template <class Val, class Backend>
244 typedef typename mpl::if_<
251 is_floating_point<Val>,
255 is_convertible<Val, const char*>,
256 is_same<Val, std::string>
265 typedef typename canonical_imp<Val, Backend, tag_type>::type type;
276 struct shift_right{};
277 struct bitwise_and{};
279 struct bitwise_xor{};
280 struct bitwise_complement{};
281 struct add_immediates{};
282 struct subtract_immediates{};
283 struct multiply_immediates{};
284 struct divide_immediates{};
285 struct modulus_immediates{};
286 struct bitwise_and_immediates{};
287 struct bitwise_or_immediates{};
288 struct bitwise_xor_immediates{};
289 struct complement_immediates{};
291 struct multiply_add{};
292 struct multiply_subtract{};
297 template <class T, expression_template_option ExpressionTemplates>
298 struct backend_type<number<T, ExpressionTemplates> >
303 template <class tag, class A1, class A2, class A3, class A4>
304 struct backend_type<expression<tag, A1, A2, A3, A4> >
306 typedef typename backend_type<typename expression<tag, A1, A2, A3, A4>::result_type>::type type;
310 template <class T1, class T2>
311 struct combine_expression
313 #ifdef BOOST_NO_CXX11_DECLTYPE
314 typedef typename mpl::if_c<(sizeof(T1() + T2()) == sizeof(T1)), T1, T2>::type type;
316 typedef decltype(T1() + T2()) type;
320 template <class T1, expression_template_option ExpressionTemplates, class T2>
321 struct combine_expression<number<T1, ExpressionTemplates>, T2>
323 typedef number<T1, ExpressionTemplates> type;
326 template <class T1, class T2, expression_template_option ExpressionTemplates>
327 struct combine_expression<T1, number<T2, ExpressionTemplates> >
329 typedef number<T2, ExpressionTemplates> type;
332 template <class T, expression_template_option ExpressionTemplates>
333 struct combine_expression<number<T, ExpressionTemplates>, number<T, ExpressionTemplates> >
335 typedef number<T, ExpressionTemplates> type;
338 template <class T1, expression_template_option ExpressionTemplates1, class T2, expression_template_option ExpressionTemplates2>
339 struct combine_expression<number<T1, ExpressionTemplates1>, number<T2, ExpressionTemplates2> >
341 typedef typename mpl::if_c<
342 is_convertible<number<T2, ExpressionTemplates2>, number<T1, ExpressionTemplates2> >::value,
343 number<T1, ExpressionTemplates1>,
344 number<T2, ExpressionTemplates2>
351 typedef expression<terminal, T> type;
354 template <class Tag, class Arg1, class Arg2, class Arg3, class Arg4>
355 struct arg_type<expression<Tag, Arg1, Arg2, Arg3, Arg4> >
357 typedef expression<Tag, Arg1, Arg2, Arg3, Arg4> type;
362 unmentionable* proc(){ return 0; }
365 typedef unmentionable* (unmentionable::*unmentionable_type)();
368 struct expression_storage
370 typedef const T& type;
374 struct expression_storage<T*>
380 struct expression_storage<const T*>
382 typedef const T* type;
385 template <class tag, class A1, class A2, class A3, class A4>
386 struct expression_storage<expression<tag, A1, A2, A3, A4> >
388 typedef expression<tag, A1, A2, A3, A4> type;
391 template<class tag, class Arg1>
392 struct expression<tag, Arg1, void, void, void>
394 typedef mpl::int_<1> arity;
395 typedef typename arg_type<Arg1>::type left_type;
396 typedef typename left_type::result_type left_result_type;
397 typedef typename left_type::result_type result_type;
398 typedef tag tag_type;
400 explicit expression(const Arg1& a) : arg(a) {}
402 left_type left()const { return left_type(arg); }
404 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg; }
406 static const unsigned depth = left_type::depth + 1;
407 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
408 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
410 // Horrible workaround for gcc-4.6.x which always prefers the template
411 // operator bool() rather than the non-template operator when converting to
412 // an arithmetic type:
414 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
415 explicit operator T ()const
417 result_type r(*this);
418 return static_cast<bool>(r);
420 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
421 explicit operator T ()const
423 return static_cast<T>(static_cast<result_type>(*this));
428 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
431 explicit operator T()const
433 return static_cast<T>(static_cast<result_type>(*this));
435 BOOST_MP_FORCEINLINE explicit operator bool()const
437 result_type r(*this);
438 return static_cast<bool>(r);
440 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
441 BOOST_MP_FORCEINLINE explicit operator void()const {}
445 operator unmentionable_type()const
447 result_type r(*this);
448 return r ? &unmentionable::proc : 0;
455 result_type r(*this);
456 return r.template convert_to<T>();
460 typename expression_storage<Arg1>::type arg;
461 expression& operator=(const expression&);
465 struct expression<terminal, Arg1, void, void, void>
467 typedef mpl::int_<0> arity;
468 typedef Arg1 result_type;
469 typedef terminal tag_type;
471 explicit expression(const Arg1& a) : arg(a) {}
473 const Arg1& value()const BOOST_NOEXCEPT { return arg; }
475 static const unsigned depth = 0;
477 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
478 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
480 // Horrible workaround for gcc-4.6.x which always prefers the template
481 // operator bool() rather than the non-template operator when converting to
482 // an arithmetic type:
484 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
485 explicit operator T ()const
487 result_type r(*this);
488 return static_cast<bool>(r);
490 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
491 explicit operator T ()const
493 return static_cast<T>(static_cast<result_type>(*this));
498 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
501 explicit operator T()const
503 return static_cast<T>(static_cast<result_type>(*this));
505 BOOST_MP_FORCEINLINE explicit operator bool()const
507 result_type r(*this);
508 return static_cast<bool>(r);
510 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
511 BOOST_MP_FORCEINLINE explicit operator void()const {}
515 operator unmentionable_type()const
517 return arg ? &unmentionable::proc : 0;
524 result_type r(*this);
525 return r.template convert_to<T>();
529 typename expression_storage<Arg1>::type arg;
530 expression& operator=(const expression&);
533 template <class tag, class Arg1, class Arg2>
534 struct expression<tag, Arg1, Arg2, void, void>
536 typedef mpl::int_<2> arity;
537 typedef typename arg_type<Arg1>::type left_type;
538 typedef typename arg_type<Arg2>::type right_type;
539 typedef typename left_type::result_type left_result_type;
540 typedef typename right_type::result_type right_result_type;
541 typedef typename combine_expression<left_result_type, right_result_type>::type result_type;
542 typedef tag tag_type;
544 expression(const Arg1& a1, const Arg2& a2) : arg1(a1), arg2(a2) {}
546 left_type left()const { return left_type(arg1); }
547 right_type right()const { return right_type(arg2); }
548 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
549 const Arg2& right_ref()const BOOST_NOEXCEPT { return arg2; }
551 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
552 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
554 // Horrible workaround for gcc-4.6.x which always prefers the template
555 // operator bool() rather than the non-template operator when converting to
556 // an arithmetic type:
558 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
559 explicit operator T ()const
561 result_type r(*this);
562 return static_cast<bool>(r);
564 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
565 explicit operator T ()const
567 return static_cast<T>(static_cast<result_type>(*this));
572 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
575 explicit operator T()const
577 return static_cast<T>(static_cast<result_type>(*this));
579 BOOST_MP_FORCEINLINE explicit operator bool()const
581 result_type r(*this);
582 return static_cast<bool>(r);
584 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
585 BOOST_MP_FORCEINLINE explicit operator void()const {}
589 operator unmentionable_type()const
591 result_type r(*this);
592 return r ? &unmentionable::proc : 0;
598 result_type r(*this);
599 return r.template convert_to<T>();
602 static const unsigned left_depth = left_type::depth + 1;
603 static const unsigned right_depth = right_type::depth + 1;
604 static const unsigned depth = left_depth > right_depth ? left_depth : right_depth;
606 typename expression_storage<Arg1>::type arg1;
607 typename expression_storage<Arg2>::type arg2;
608 expression& operator=(const expression&);
611 template <class tag, class Arg1, class Arg2, class Arg3>
612 struct expression<tag, Arg1, Arg2, Arg3, void>
614 typedef mpl::int_<3> arity;
615 typedef typename arg_type<Arg1>::type left_type;
616 typedef typename arg_type<Arg2>::type middle_type;
617 typedef typename arg_type<Arg3>::type right_type;
618 typedef typename left_type::result_type left_result_type;
619 typedef typename middle_type::result_type middle_result_type;
620 typedef typename right_type::result_type right_result_type;
621 typedef typename combine_expression<
623 typename combine_expression<right_result_type, middle_result_type>::type
625 typedef tag tag_type;
627 expression(const Arg1& a1, const Arg2& a2, const Arg3& a3) : arg1(a1), arg2(a2), arg3(a3) {}
629 left_type left()const { return left_type(arg1); }
630 middle_type middle()const { return middle_type(arg2); }
631 right_type right()const { return right_type(arg3); }
632 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
633 const Arg2& middle_ref()const BOOST_NOEXCEPT { return arg2; }
634 const Arg3& right_ref()const BOOST_NOEXCEPT { return arg3; }
636 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
637 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
639 // Horrible workaround for gcc-4.6.x which always prefers the template
640 // operator bool() rather than the non-template operator when converting to
641 // an arithmetic type:
643 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
644 explicit operator T ()const
646 result_type r(*this);
647 return static_cast<bool>(r);
649 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
650 explicit operator T ()const
652 return static_cast<T>(static_cast<result_type>(*this));
657 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
660 explicit operator T()const
662 return static_cast<T>(static_cast<result_type>(*this));
664 BOOST_MP_FORCEINLINE explicit operator bool()const
666 result_type r(*this);
667 return static_cast<bool>(r);
669 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
670 BOOST_MP_FORCEINLINE explicit operator void()const {}
674 operator unmentionable_type()const
676 result_type r(*this);
677 return r ? &unmentionable::proc : 0;
683 result_type r(*this);
684 return r.template convert_to<T>();
687 static const unsigned left_depth = left_type::depth + 1;
688 static const unsigned middle_depth = middle_type::depth + 1;
689 static const unsigned right_depth = right_type::depth + 1;
690 static const unsigned depth = left_depth > right_depth ? (left_depth > middle_depth ? left_depth : middle_depth) : (right_depth > middle_depth ? right_depth : middle_depth);
692 typename expression_storage<Arg1>::type arg1;
693 typename expression_storage<Arg2>::type arg2;
694 typename expression_storage<Arg3>::type arg3;
695 expression& operator=(const expression&);
698 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
701 typedef mpl::int_<4> arity;
702 typedef typename arg_type<Arg1>::type left_type;
703 typedef typename arg_type<Arg2>::type left_middle_type;
704 typedef typename arg_type<Arg3>::type right_middle_type;
705 typedef typename arg_type<Arg4>::type right_type;
706 typedef typename left_type::result_type left_result_type;
707 typedef typename left_middle_type::result_type left_middle_result_type;
708 typedef typename right_middle_type::result_type right_middle_result_type;
709 typedef typename right_type::result_type right_result_type;
710 typedef typename combine_expression<
712 typename combine_expression<
713 left_middle_result_type,
714 typename combine_expression<right_middle_result_type, right_result_type>::type
717 typedef tag tag_type;
719 expression(const Arg1& a1, const Arg2& a2, const Arg3& a3, const Arg4& a4) : arg1(a1), arg2(a2), arg3(a3), arg4(a4) {}
721 left_type left()const { return left_type(arg1); }
722 left_middle_type left_middle()const { return left_middle_type(arg2); }
723 right_middle_type right_middle()const { return right_middle_type(arg3); }
724 right_type right()const { return right_type(arg4); }
725 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
726 const Arg2& left_middle_ref()const BOOST_NOEXCEPT { return arg2; }
727 const Arg3& right_middle_ref()const BOOST_NOEXCEPT { return arg3; }
728 const Arg4& right_ref()const BOOST_NOEXCEPT { return arg4; }
730 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
731 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
733 // Horrible workaround for gcc-4.6.x which always prefers the template
734 // operator bool() rather than the non-template operator when converting to
735 // an arithmetic type:
737 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
738 explicit operator T ()const
740 result_type r(*this);
741 return static_cast<bool>(r);
743 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
744 explicit operator T ()const
746 return static_cast<T>(static_cast<result_type>(*this));
751 , typename boost::disable_if_c<is_number<T>::value || is_constructible<T const&, result_type>::value, int>::type = 0
754 explicit operator T()const
756 return static_cast<T>(static_cast<result_type>(*this));
758 BOOST_MP_FORCEINLINE explicit operator bool()const
760 result_type r(*this);
761 return static_cast<bool>(r);
763 #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
764 BOOST_MP_FORCEINLINE explicit operator void()const {}
768 operator unmentionable_type()const
770 result_type r(*this);
771 return r ? &unmentionable::proc : 0;
777 result_type r(*this);
778 return r.template convert_to<T>();
781 static const unsigned left_depth = left_type::depth + 1;
782 static const unsigned left_middle_depth = left_middle_type::depth + 1;
783 static const unsigned right_middle_depth = right_middle_type::depth + 1;
784 static const unsigned right_depth = right_type::depth + 1;
786 static const unsigned left_max_depth = left_depth > left_middle_depth ? left_depth : left_middle_depth;
787 static const unsigned right_max_depth = right_depth > right_middle_depth ? right_depth : right_middle_depth;
789 static const unsigned depth = left_max_depth > right_max_depth ? left_max_depth : right_max_depth;
791 typename expression_storage<Arg1>::type arg1;
792 typename expression_storage<Arg2>::type arg2;
793 typename expression_storage<Arg3>::type arg3;
794 typename expression_storage<Arg4>::type arg4;
795 expression& operator=(const expression&);
801 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
802 BOOST_STATIC_ASSERT((std::numeric_limits<T>::radix == 2) || (std::numeric_limits<T>::radix == 10));
803 // If we really have so many digits that this fails, then we're probably going to hit other problems anyway:
804 BOOST_STATIC_ASSERT(LONG_MAX / 1000 > (std::numeric_limits<T>::digits + 1));
805 static const long m_value = std::numeric_limits<T>::radix == 10 ? (((std::numeric_limits<T>::digits + 1) * 1000L) / 301L) : std::numeric_limits<T>::digits;
806 static inline BOOST_CONSTEXPR long value()BOOST_NOEXCEPT { return m_value; }
809 #ifndef BOOST_MP_MIN_EXPONENT_DIGITS
811 # define BOOST_MP_MIN_EXPONENT_DIGITS 2
813 # define BOOST_MP_MIN_EXPONENT_DIGITS 2
818 void format_float_string(S& str, boost::intmax_t my_exp, boost::intmax_t digits, std::ios_base::fmtflags f, bool iszero)
820 typedef typename S::size_type size_type;
821 bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
822 bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
823 bool showpoint = (f & std::ios_base::showpoint) == std::ios_base::showpoint;
824 bool showpos = (f & std::ios_base::showpos) == std::ios_base::showpos;
826 bool neg = str.size() && (str[0] == '-');
833 digits = (std::max)(str.size(), size_type(16));
836 if(iszero || str.empty() || (str.find_first_not_of('0') == S::npos))
838 // We will be printing zero, even though the value might not
839 // actually be zero (it just may have been rounded to zero).
841 if(scientific || fixed)
844 str.append(size_type(digits), '0');
854 str.append(size_type(digits - 1), '0');
858 str.insert(static_cast<std::string::size_type>(0), 1, '-');
860 str.insert(static_cast<std::string::size_type>(0), 1, '+');
864 if(!fixed && !scientific && !showpoint)
867 // Suppress trailing zeros:
869 std::string::iterator pos = str.end();
870 while(pos != str.begin() && *--pos == '0'){}
873 str.erase(pos, str.end());
877 else if(!fixed || (my_exp >= 0))
880 // Pad out the end with zero's if we need to:
882 boost::intmax_t chars = str.size();
883 chars = digits - chars;
888 str.append(static_cast<std::string::size_type>(chars), '0');
892 if(fixed || (!scientific && (my_exp >= -4) && (my_exp < digits)))
894 if(1 + my_exp > static_cast<boost::intmax_t>(str.size()))
896 // Just pad out the end with zeros:
897 str.append(static_cast<std::string::size_type>(1 + my_exp - str.size()), '0');
898 if(showpoint || fixed)
901 else if(my_exp + 1 < static_cast<boost::intmax_t>(str.size()))
905 str.insert(static_cast<std::string::size_type>(0), static_cast<std::string::size_type>(-1 - my_exp), '0');
906 str.insert(static_cast<std::string::size_type>(0), "0.");
910 // Insert the decimal point:
911 str.insert(static_cast<std::string::size_type>(my_exp + 1), 1, '.');
914 else if(showpoint || fixed) // we have exactly the digits we require to left of the point
919 // We may need to add trailing zeros:
920 boost::intmax_t l = str.find('.') + 1;
921 l = digits - (str.size() - l);
923 str.append(size_type(l), '0');
929 // Scientific format:
930 if(showpoint || (str.size() > 1))
931 str.insert(static_cast<std::string::size_type>(1u), 1, '.');
932 str.append(static_cast<std::string::size_type>(1u), 'e');
933 S e = boost::lexical_cast<S>(abs(my_exp));
934 if(e.size() < BOOST_MP_MIN_EXPONENT_DIGITS)
935 e.insert(static_cast<std::string::size_type>(0), BOOST_MP_MIN_EXPONENT_DIGITS - e.size(), '0');
937 e.insert(static_cast<std::string::size_type>(0), 1, '-');
939 e.insert(static_cast<std::string::size_type>(0), 1, '+');
943 str.insert(static_cast<std::string::size_type>(0), 1, '-');
945 str.insert(static_cast<std::string::size_type>(0), 1, '+');
949 void check_shift_range(V val, const mpl::true_&, const mpl::true_&)
951 if(val > (std::numeric_limits<std::size_t>::max)())
952 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
954 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
957 void check_shift_range(V val, const mpl::false_&, const mpl::true_&)
960 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
963 void check_shift_range(V val, const mpl::true_&, const mpl::false_&)
965 if(val > (std::numeric_limits<std::size_t>::max)())
966 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
969 void check_shift_range(V, const mpl::false_&, const mpl::false_&) BOOST_NOEXCEPT{}
971 } // namespace detail
974 // Traits class, lets us know what kind of number we have, defaults to a floating point type:
976 enum number_category_type
978 number_kind_unknown = -1,
979 number_kind_integer = 0,
980 number_kind_floating_point = 1,
981 number_kind_rational = 2,
982 number_kind_fixed_point = 3
986 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)> {};
987 template <class Backend, expression_template_option ExpressionTemplates>
988 struct number_category<number<Backend, ExpressionTemplates> > : public number_category<Backend>{};
989 template <class tag, class A1, class A2, class A3, class A4>
990 struct number_category<detail::expression<tag, A1, A2, A3, A4> > : public number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>{};
993 struct component_type;
994 template <class T, expression_template_option ExpressionTemplates>
995 struct component_type<number<T, ExpressionTemplates> > : public component_type<T>{};
996 template <class tag, class A1, class A2, class A3, class A4>
997 struct component_type<detail::expression<tag, A1, A2, A3, A4> > : public component_type<typename detail::expression<tag, A1, A2, A3, A4>::result_type>{};
1000 struct is_unsigned_number : public mpl::false_{};
1001 template <class Backend, expression_template_option ExpressionTemplates>
1002 struct is_unsigned_number<number<Backend, ExpressionTemplates> > : public is_unsigned_number<Backend> {};
1004 struct is_signed_number : public mpl::bool_<!is_unsigned_number<T>::value> {};
1006 struct is_interval_number : public mpl::false_ {};
1007 template <class Backend, expression_template_option ExpressionTemplates>
1008 struct is_interval_number<number<Backend, ExpressionTemplates> > : public is_interval_number<Backend>{};
1012 namespace boost{ namespace math{ namespace tools{
1017 template <class tag, class A1, class A2, class A3, class A4>
1018 struct promote_arg<boost::multiprecision::detail::expression<tag, A1, A2, A3, A4> >
1020 typedef typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type type;
1023 template <class R, class B, boost::multiprecision::expression_template_option ET>
1024 inline R real_cast(const boost::multiprecision::number<B, ET>& val)
1026 return val.template convert_to<R>();
1029 template <class R, class tag, class A1, class A2, class A3, class A4>
1030 inline R real_cast(const boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>& val)
1032 typedef typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type val_type;
1033 return val_type(val).template convert_to<R>();
1039 namespace constants{
1042 struct is_explicitly_convertible_from_string;
1044 template <class B, boost::multiprecision::expression_template_option ET>
1045 struct is_explicitly_convertible_from_string<boost::multiprecision::number<B, ET> >
1047 static const bool value = true;
1055 # pragma warning(pop)
1058 #endif // BOOST_MATH_BIG_NUM_BASE_HPP