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_DEF_OPS
7 #define BOOST_MATH_BIG_NUM_DEF_OPS
9 #include <boost/math/policies/error_handling.hpp>
10 #include <boost/multiprecision/detail/number_base.hpp>
11 #include <boost/math/special_functions/fpclassify.hpp>
12 #include <boost/math/special_functions/next.hpp>
13 #include <boost/utility/enable_if.hpp>
14 #include <boost/mpl/front.hpp>
15 #include <boost/mpl/fold.hpp>
16 #include <boost/cstdint.hpp>
17 #include <boost/type_traits/make_unsigned.hpp>
19 #ifndef INSTRUMENT_BACKEND
20 #ifndef BOOST_MP_INSTRUMENT
21 #define INSTRUMENT_BACKEND(x)
23 #define INSTRUMENT_BACKEND(x)\
24 std::cout << BOOST_STRINGIZE(x) << " = " << x.str(0, std::ios_base::scientific) << std::endl;
29 namespace boost{ namespace multiprecision{
36 template <class To, class From>
37 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/);
38 template <class To, class From>
39 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_integer>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/);
40 template <class To, class From>
41 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_floating_point>& /*from_type*/);
42 template <class To, class From>
43 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_rational>& /*to_type*/, const mpl::int_<number_kind_rational>& /*from_type*/);
44 template <class To, class From>
45 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_rational>& /*to_type*/, const mpl::int_<number_kind_integer>& /*from_type*/);
49 namespace default_ops{
52 // warning C4127: conditional expression is constant
54 #pragma warning(disable:4127)
57 // Default versions of mixed arithmetic, these just construct a temporary
58 // from the arithmetic value and then do the arithmetic on that, two versions
59 // of each depending on whether the backend can be directly constructed from type V.
61 // Note that we have to provide *all* the template parameters to class number when used in
62 // enable_if as MSVC-10 won't compile the code if we rely on a computed-default parameter.
63 // Since the result of the test doesn't depend on whether expression templates are on or off
64 // we just use et_on everywhere. We could use a BOOST_WORKAROUND but that just obfuscates the
67 template <class T, class V>
68 inline typename disable_if_c<is_convertible<V, T>::value >::type
69 eval_add(T& result, V const& v)
75 template <class T, class V>
76 inline typename enable_if_c<is_convertible<V, T>::value >::type
77 eval_add(T& result, V const& v)
82 template <class T, class V>
83 inline typename disable_if_c<is_convertible<V, T>::value>::type
84 eval_subtract(T& result, V const& v)
88 eval_subtract(result, t);
90 template <class T, class V>
91 inline typename enable_if_c<is_convertible<V, T>::value>::type
92 eval_subtract(T& result, V const& v)
95 eval_subtract(result, t);
97 template <class T, class V>
98 inline typename disable_if_c<is_convertible<V, T>::value>::type
99 eval_multiply(T& result, V const& v)
103 eval_multiply(result, t);
105 template <class T, class V>
106 inline typename enable_if_c<is_convertible<V, T>::value>::type
107 eval_multiply(T& result, V const& v)
110 eval_multiply(result, t);
113 template <class T, class U, class V>
114 void eval_multiply(T& t, const U& u, const V& v);
116 template <class T, class U, class V>
117 inline typename disable_if_c<!is_same<T, U>::value && is_same<T, V>::value>::type eval_multiply_add(T& t, const U& u, const V& v)
120 eval_multiply(z, u, v);
123 template <class T, class U, class V>
124 inline typename enable_if_c<!is_same<T, U>::value && is_same<T, V>::value>::type eval_multiply_add(T& t, const U& u, const V& v)
126 eval_multiply_add(t, v, u);
128 template <class T, class U, class V>
129 inline typename disable_if_c<!is_same<T, U>::value && is_same<T, V>::value>::type eval_multiply_subtract(T& t, const U& u, const V& v)
132 eval_multiply(z, u, v);
135 template <class T, class U, class V>
136 inline typename enable_if_c<!is_same<T, U>::value && is_same<T, V>::value>::type eval_multiply_subtract(T& t, const U& u, const V& v)
138 eval_multiply_subtract(t, v, u);
140 template <class T, class V>
141 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && !is_convertible<V, T>::value>::type
142 eval_divide(T& result, V const& v)
146 eval_divide(result, t);
148 template <class T, class V>
149 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && is_convertible<V, T>::value>::type
150 eval_divide(T& result, V const& v)
153 eval_divide(result, t);
155 template <class T, class V>
156 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && !is_convertible<V, T>::value>::type
157 eval_modulus(T& result, V const& v)
161 eval_modulus(result, t);
163 template <class T, class V>
164 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value&& is_convertible<V, T>::value>::type
165 eval_modulus(T& result, V const& v)
168 eval_modulus(result, t);
170 template <class T, class V>
171 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && !is_convertible<V, T>::value>::type
172 eval_bitwise_and(T& result, V const& v)
176 eval_bitwise_and(result, t);
178 template <class T, class V>
179 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && is_convertible<V, T>::value>::type
180 eval_bitwise_and(T& result, V const& v)
183 eval_bitwise_and(result, t);
185 template <class T, class V>
186 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && !is_convertible<V, T>::value>::type
187 eval_bitwise_or(T& result, V const& v)
191 eval_bitwise_or(result, t);
193 template <class T, class V>
194 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && is_convertible<V, T>::value>::type
195 eval_bitwise_or(T& result, V const& v)
198 eval_bitwise_or(result, t);
200 template <class T, class V>
201 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && !is_convertible<V, T>::value>::type
202 eval_bitwise_xor(T& result, V const& v)
206 eval_bitwise_xor(result, t);
208 template <class T, class V>
209 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && is_convertible<V, T>::value>::type
210 eval_bitwise_xor(T& result, V const& v)
213 eval_bitwise_xor(result, t);
216 template <class T, class V>
217 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && !is_convertible<V, T>::value>::type
218 eval_complement(T& result, V const& v)
222 eval_complement(result, t);
224 template <class T, class V>
225 inline typename enable_if_c<is_convertible<V, number<T, et_on> >::value && is_convertible<V, T>::value>::type
226 eval_complement(T& result, V const& v)
229 eval_complement(result, t);
233 // Default versions of 3-arg arithmetic functions, these mostly just forward to the 2 arg versions:
235 template <class T, class U, class V>
236 void eval_add(T& t, const U& u, const V& v);
239 inline void eval_add_default(T& t, const T& u, const T& v)
255 template <class T, class U>
256 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_add_default(T& t, const T& u, const U& v)
262 template <class T, class U>
263 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_add_default(T& t, const T& u, const U& v)
268 template <class T, class U>
269 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value>::type eval_add_default(T& t, const U& u, const T& v)
273 template <class T, class U, class V>
274 inline void eval_add_default(T& t, const U& u, const V& v)
276 if(is_same<T, V>::value && ((void*)&t == (void*)&v))
286 template <class T, class U, class V>
287 inline void eval_add(T& t, const U& u, const V& v)
289 eval_add_default(t, u, v);
292 template <class T, class U, class V>
293 void eval_subtract(T& t, const U& u, const V& v);
296 inline void eval_subtract_default(T& t, const T& u, const T& v)
298 if((&t == &v) && is_signed_number<T>::value)
313 template <class T, class U>
314 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_subtract_default(T& t, const T& u, const U& v)
318 eval_subtract(t, u, vv);
320 template <class T, class U>
321 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_subtract_default(T& t, const T& u, const U& v)
324 eval_subtract(t, u, vv);
326 template <class T, class U>
327 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_signed_number<T>::value>::type eval_subtract_default(T& t, const U& u, const T& v)
329 eval_subtract(t, v, u);
332 template <class T, class U>
333 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value && is_unsigned_number<T>::value>::type eval_subtract_default(T& t, const U& u, const T& v)
337 eval_subtract(t, temp, v);
339 template <class T, class U>
340 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value && is_unsigned_number<T>::value>::type eval_subtract_default(T& t, const U& u, const T& v)
343 eval_subtract(t, temp, v);
345 template <class T, class U, class V>
346 inline void eval_subtract_default(T& t, const U& u, const V& v)
348 if(is_same<T, V>::value && ((void*)&t == (void*)&v))
359 template <class T, class U, class V>
360 inline void eval_subtract(T& t, const U& u, const V& v)
362 eval_subtract_default(t, u, v);
366 inline void eval_multiply_default(T& t, const T& u, const T& v)
382 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
383 template <class T, class U>
384 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_multiply_default(T& t, const T& u, const U& v)
388 eval_multiply(t, u, vv);
390 template <class T, class U>
391 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_multiply_default(T& t, const T& u, const U& v)
394 eval_multiply(t, u, vv);
396 template <class T, class U>
397 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value>::type eval_multiply_default(T& t, const U& u, const T& v)
399 eval_multiply(t, v, u);
402 template <class T, class U, class V>
403 inline void eval_multiply_default(T& t, const U& u, const V& v)
405 if(is_same<T, V>::value && ((void*)&t == (void*)&v))
411 t = number<T>::canonical_value(u);
415 template <class T, class U, class V>
416 inline void eval_multiply(T& t, const U& u, const V& v)
418 eval_multiply_default(t, u, v);
422 inline void eval_multiply_add(T& t, const T& u, const T& v, const T& x)
424 if((void*)&x == (void*)&t)
427 z = number<T>::canonical_value(x);
428 eval_multiply_add(t, u, v, z);
432 eval_multiply(t, u, v);
437 template <class T, class U>
438 inline typename boost::disable_if_c<boost::is_same<T, U>::value, T>::type make_T(const U& u)
441 t = number<T>::canonical_value(u);
442 return BOOST_MP_MOVE(t);
445 inline const T& make_T(const T& t)
450 template <class T, class U, class V, class X>
451 inline typename disable_if_c<!is_same<T, U>::value && is_same<T, V>::value>::type eval_multiply_add(T& t, const U& u, const V& v, const X& x)
453 eval_multiply_add(t, make_T<T>(u), make_T<T>(v), make_T<T>(x));
455 template <class T, class U, class V, class X>
456 inline typename enable_if_c<!is_same<T, U>::value && is_same<T, V>::value>::type eval_multiply_add(T& t, const U& u, const V& v, const X& x)
458 eval_multiply_add(t, v, u, x);
460 template <class T, class U, class V, class X>
461 inline typename disable_if_c<!is_same<T, U>::value && is_same<T, V>::value>::type eval_multiply_subtract(T& t, const U& u, const V& v, const X& x)
463 if((void*)&x == (void*)&t)
467 eval_multiply_subtract(t, u, v, z);
471 eval_multiply(t, u, v);
475 template <class T, class U, class V, class X>
476 inline typename enable_if_c<!is_same<T, U>::value && is_same<T, V>::value>::type eval_multiply_subtract(T& t, const U& u, const V& v, const X& x)
478 eval_multiply_subtract(t, v, u, x);
481 template <class T, class U, class V>
482 void eval_divide(T& t, const U& u, const V& v);
485 inline void eval_divide_default(T& t, const T& u, const T& v)
492 eval_divide(temp, u, v);
501 #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
502 template <class T, class U>
503 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_divide_default(T& t, const T& u, const U& v)
507 eval_divide(t, u, vv);
509 template <class T, class U>
510 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_divide_default(T& t, const T& u, const U& v)
513 eval_divide(t, u, vv);
515 template <class T, class U>
516 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_divide_default(T& t, const U& u, const T& v)
520 eval_divide(t, uu, v);
522 template <class T, class U>
523 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_divide_default(T& t, const U& u, const T& v)
526 eval_divide(t, uu, v);
529 template <class T, class U, class V>
530 inline void eval_divide_default(T& t, const U& u, const V& v)
532 if(is_same<T, V>::value && ((void*)&t == (void*)&v))
536 eval_divide(temp, v);
545 template <class T, class U, class V>
546 inline void eval_divide(T& t, const U& u, const V& v)
548 eval_divide_default(t, u, v);
551 template <class T, class U, class V>
552 void eval_modulus(T& t, const U& u, const V& v);
555 inline void eval_modulus_default(T& t, const T& u, const T& v)
562 eval_modulus(temp, u, v);
571 template <class T, class U>
572 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_modulus_default(T& t, const T& u, const U& v)
576 eval_modulus(t, u, vv);
578 template <class T, class U>
579 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_modulus_default(T& t, const T& u, const U& v)
582 eval_modulus(t, u, vv);
584 template <class T, class U>
585 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_modulus_default(T& t, const U& u, const T& v)
589 eval_modulus(t, uu, v);
591 template <class T, class U>
592 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_modulus_default(T& t, const U& u, const T& v)
595 eval_modulus(t, uu, v);
597 template <class T, class U, class V>
598 inline void eval_modulus_default(T& t, const U& u, const V& v)
600 if(is_same<T, V>::value && ((void*)&t == (void*)&v))
603 eval_modulus(temp, v);
612 template <class T, class U, class V>
613 inline void eval_modulus(T& t, const U& u, const V& v)
615 eval_modulus_default(t, u, v);
618 template <class T, class U, class V>
619 void eval_bitwise_and(T& t, const U& u, const V& v);
622 inline void eval_bitwise_and_default(T& t, const T& u, const T& v)
626 eval_bitwise_and(t, u);
630 eval_bitwise_and(t, v);
635 eval_bitwise_and(t, v);
638 template <class T, class U>
639 inline typename disable_if_c<is_convertible<U, T>::value>::type eval_bitwise_and_default(T& t, const T& u, const U& v)
643 eval_bitwise_and(t, u, vv);
645 template <class T, class U>
646 inline typename enable_if_c<is_convertible<U, T>::value>::type eval_bitwise_and_default(T& t, const T& u, const U& v)
649 eval_bitwise_and(t, u, vv);
651 template <class T, class U>
652 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_and_default(T& t, const U& u, const T& v)
654 eval_bitwise_and(t, v, u);
656 template <class T, class U, class V>
657 inline typename disable_if_c<is_same<T, U>::value || is_same<T, V>::value>::type eval_bitwise_and_default(T& t, const U& u, const V& v)
660 eval_bitwise_and(t, v);
662 template <class T, class U, class V>
663 inline void eval_bitwise_and(T& t, const U& u, const V& v)
665 eval_bitwise_and_default(t, u, v);
668 template <class T, class U, class V>
669 void eval_bitwise_or(T& t, const U& u, const V& v);
672 inline void eval_bitwise_or_default(T& t, const T& u, const T& v)
676 eval_bitwise_or(t, u);
680 eval_bitwise_or(t, v);
685 eval_bitwise_or(t, v);
688 template <class T, class U>
689 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_bitwise_or_default(T& t, const T& u, const U& v)
693 eval_bitwise_or(t, u, vv);
695 template <class T, class U>
696 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_bitwise_or_default(T& t, const T& u, const U& v)
699 eval_bitwise_or(t, u, vv);
701 template <class T, class U>
702 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_or_default(T& t, const U& u, const T& v)
704 eval_bitwise_or(t, v, u);
706 template <class T, class U, class V>
707 inline void eval_bitwise_or_default(T& t, const U& u, const V& v)
709 if(is_same<T, V>::value && ((void*)&t == (void*)&v))
711 eval_bitwise_or(t, u);
716 eval_bitwise_or(t, v);
719 template <class T, class U, class V>
720 inline void eval_bitwise_or(T& t, const U& u, const V& v)
722 eval_bitwise_or_default(t, u, v);
725 template <class T, class U, class V>
726 void eval_bitwise_xor(T& t, const U& u, const V& v);
729 inline void eval_bitwise_xor_default(T& t, const T& u, const T& v)
733 eval_bitwise_xor(t, u);
737 eval_bitwise_xor(t, v);
742 eval_bitwise_xor(t, v);
745 template <class T, class U>
746 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && !is_convertible<U, T>::value>::type eval_bitwise_xor_default(T& t, const T& u, const U& v)
750 eval_bitwise_xor(t, u, vv);
752 template <class T, class U>
753 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value && is_convertible<U, T>::value>::type eval_bitwise_xor_default(T& t, const T& u, const U& v)
756 eval_bitwise_xor(t, u, vv);
758 template <class T, class U>
759 inline typename enable_if_c<is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_xor_default(T& t, const U& u, const T& v)
761 eval_bitwise_xor(t, v, u);
763 template <class T, class U, class V>
764 inline void eval_bitwise_xor_default(T& t, const U& u, const V& v)
766 if(is_same<T, V>::value && ((void*)&t == (void*)&v))
768 eval_bitwise_xor(t, u);
773 eval_bitwise_xor(t, v);
776 template <class T, class U, class V>
777 inline void eval_bitwise_xor(T& t, const U& u, const V& v)
779 eval_bitwise_xor_default(t, u, v);
783 inline void eval_increment(T& val)
785 typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
786 eval_add(val, static_cast<ui_type>(1u));
789 inline void eval_decrement(T& val)
791 typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
792 eval_subtract(val, static_cast<ui_type>(1u));
795 template <class T, class V>
796 inline void eval_left_shift(T& result, const T& arg, const V val)
799 eval_left_shift(result, val);
802 template <class T, class V>
803 inline void eval_right_shift(T& result, const T& arg, const V val)
806 eval_right_shift(result, val);
810 inline bool eval_is_zero(const T& val)
812 typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
813 return val.compare(static_cast<ui_type>(0)) == 0;
816 inline int eval_get_sign(const T& val)
818 typedef typename mpl::front<typename T::unsigned_types>::type ui_type;
819 return val.compare(static_cast<ui_type>(0));
822 template <class T, class V>
823 inline void assign_components_imp(T& result, const V& v1, const V& v2, const mpl::int_<number_kind_rational>&)
828 eval_divide(result, t);
831 template <class T, class V>
832 inline void assign_components(T& result, const V& v1, const V& v2)
834 return assign_components_imp(result, v1, v2, typename number_category<T>::type());
837 template <class R, int b>
838 struct has_enough_bits
841 struct type : public mpl::and_<mpl::not_<is_same<R, T> >, mpl::bool_<std::numeric_limits<T>::digits >= b> >{};
847 terminal(const R& v) : value(v){}
849 terminal& operator = (R val) { value = val; return *this; }
851 operator R()const { return value; }
854 template<class R, class B>
855 struct calculate_next_larger_type
857 // Find which list we're looking through:
858 typedef typename mpl::if_<
860 typename B::signed_types,
863 typename B::unsigned_types,
864 typename B::float_types
867 // A predicate to find a type with enough bits:
868 typedef typename has_enough_bits<R, std::numeric_limits<R>::digits>::template type<mpl::_> pred_type;
869 // See if the last type is in the list, if so we have to start after this:
870 typedef typename mpl::find_if<
874 // Where we're starting from, either the start of the sequence or the last type found:
875 typedef typename mpl::if_<is_same<start_last, typename mpl::end<list_type>::type>, typename mpl::begin<list_type>::type, start_last>::type start_seq;
876 // The range we're searching:
877 typedef mpl::iterator_range<start_seq, typename mpl::end<list_type>::type> range;
878 // Find the next type:
879 typedef typename mpl::find_if<
883 // Either the next type, or a "terminal" to indicate we've run out of types to search:
884 typedef typename mpl::eval_if<
885 is_same<typename mpl::end<list_type>::type, iter_type>,
886 mpl::identity<terminal<R> >,
887 mpl::deref<iter_type>
891 template <class R, class T>
892 inline bool check_in_range(const T& t)
894 // Can t fit in an R?
895 if(std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::is_bounded && (t > (std::numeric_limits<R>::max)()))
900 template <class R, class T>
901 inline bool check_in_range(const terminal<T>&)
906 template <class R, class B>
907 inline void eval_convert_to(R* result, const B& backend)
909 typedef typename calculate_next_larger_type<R, B>::type next_type;
911 eval_convert_to(&n, backend);
912 if(check_in_range<R>(n))
914 *result = (std::numeric_limits<R>::max)();
917 *result = static_cast<R>(n);
920 template <class R, class B>
921 inline void eval_convert_to(terminal<R>* result, const B& backend)
924 // We ran out of types to try for the conversion, try
925 // a lexical_cast and hope for the best:
927 result->value = boost::lexical_cast<R>(backend.str(0, std::ios_base::fmtflags(0)));
930 template <class B1, class B2, expression_template_option et>
931 inline void eval_convert_to(terminal<number<B1, et> >* result, const B2& backend)
934 // We ran out of types to try for the conversion, try
935 // a generic conversion and hope for the best:
937 boost::multiprecision::detail::generic_interconvert(result->value.backend(), backend, number_category<B1>(), number_category<B2>());
941 inline void eval_convert_to(std::string* result, const B& backend)
943 *result = backend.str(0, std::ios_base::fmtflags(0));
949 void eval_abs(T& result, const T& arg)
951 typedef typename T::signed_types type_list;
952 typedef typename mpl::front<type_list>::type front;
954 if(arg.compare(front(0)) < 0)
958 void eval_fabs(T& result, const T& arg)
960 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The fabs function is only valid for floating point types.");
961 typedef typename T::signed_types type_list;
962 typedef typename mpl::front<type_list>::type front;
964 if(arg.compare(front(0)) < 0)
968 template <class Backend>
969 inline int eval_fpclassify(const Backend& arg)
971 BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_floating_point, "The fpclassify function is only valid for floating point types.");
972 return eval_is_zero(arg) ? FP_ZERO : FP_NORMAL;
976 inline void eval_fmod(T& result, const T& a, const T& b)
978 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The fmod function is only valid for floating point types.");
979 if((&result == &a) || (&result == &b))
982 eval_fmod(temp, a, b);
987 eval_divide(result, a, b);
988 if(eval_get_sign(result) < 0)
989 eval_ceil(n, result);
991 eval_floor(n, result);
993 eval_subtract(result, a, n);
995 template<class T, class A>
996 inline typename enable_if<is_arithmetic<A>, void>::type eval_fmod(T& result, const T& x, const A& a)
998 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
999 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
1002 eval_fmod(result, x, c);
1005 template<class T, class A>
1006 inline typename enable_if<is_arithmetic<A>, void>::type eval_fmod(T& result, const A& x, const T& a)
1008 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
1009 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
1012 eval_fmod(result, c, a);
1016 void eval_round(T& result, const T& a);
1019 inline void eval_remquo(T& result, const T& a, const T& b, int* pi)
1021 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The remquo function is only valid for floating point types.");
1022 if((&result == &a) || (&result == &b))
1025 eval_remquo(temp, a, b, pi);
1030 eval_divide(result, a, b);
1031 eval_round(n, result);
1032 eval_convert_to(pi, n);
1033 eval_multiply(n, b);
1034 eval_subtract(result, a, n);
1036 template<class T, class A>
1037 inline typename enable_if<is_arithmetic<A>, void>::type eval_remquo(T& result, const T& x, const A& a, int* pi)
1039 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
1040 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
1043 eval_remquo(result, x, c, pi);
1045 template<class T, class A>
1046 inline typename enable_if<is_arithmetic<A>, void>::type eval_remquo(T& result, const A& x, const T& a, int* pi)
1048 typedef typename boost::multiprecision::detail::canonical<A, T>::type canonical_type;
1049 typedef typename mpl::if_<is_same<A, canonical_type>, T, canonical_type>::type cast_type;
1052 eval_remquo(result, c, a, pi);
1054 template <class T, class U, class V>
1055 inline void eval_remainder(T& result, const U& a, const V& b)
1058 eval_remquo(result, a, b, &i);
1062 bool eval_gt(const B& a, const B& b);
1063 template <class T, class U>
1064 bool eval_gt(const T& a, const U& b);
1066 bool eval_lt(const B& a, const B& b);
1067 template <class T, class U>
1068 bool eval_lt(const T& a, const U& b);
1071 inline void eval_fdim(T& result, const T& a, const T& b)
1073 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1074 static const ui_type zero = 0u;
1075 switch(eval_fpclassify(b))
1082 switch(eval_fpclassify(a))
1093 eval_subtract(result, a, b);
1099 template<class T, class A>
1100 inline typename boost::enable_if_c<boost::is_arithmetic<A>::value>::type eval_fdim(T& result, const T& a, const A& b)
1102 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1103 typedef typename boost::multiprecision::detail::canonical<A, T>::type arithmetic_type;
1104 static const ui_type zero = 0u;
1105 arithmetic_type canonical_b = b;
1106 switch((::boost::math::fpclassify)(b))
1113 switch(eval_fpclassify(a))
1122 if(eval_gt(a, canonical_b))
1124 eval_subtract(result, a, canonical_b);
1130 template<class T, class A>
1131 inline typename boost::enable_if_c<boost::is_arithmetic<A>::value>::type eval_fdim(T& result, const A& a, const T& b)
1133 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1134 typedef typename boost::multiprecision::detail::canonical<A, T>::type arithmetic_type;
1135 static const ui_type zero = 0u;
1136 arithmetic_type canonical_a = a;
1137 switch(eval_fpclassify(b))
1144 switch((::boost::math::fpclassify)(a))
1150 result = std::numeric_limits<number<T> >::infinity().backend();
1153 if(eval_gt(canonical_a, b))
1155 eval_subtract(result, canonical_a, b);
1162 inline void eval_trunc(T& result, const T& a)
1164 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The trunc function is only valid for floating point types.");
1165 int c = eval_fpclassify(a);
1166 if(c == (int)FP_NAN || c == (int)FP_INFINITE)
1168 result = boost::math::policies::raise_rounding_error("boost::multiprecision::trunc<%1%>(%1%)", 0, number<T>(a), number<T>(a), boost::math::policies::policy<>()).backend();
1171 if(eval_get_sign(a) < 0)
1172 eval_ceil(result, a);
1174 eval_floor(result, a);
1178 inline void eval_modf(T& result, T const& arg, T* pipart)
1180 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1181 int c = eval_fpclassify(arg);
1182 if(c == (int)FP_NAN)
1189 else if(c == (int)FP_INFINITE)
1193 result = ui_type(0u);
1198 eval_trunc(*pipart, arg);
1199 eval_subtract(result, arg, *pipart);
1204 eval_trunc(ipart, arg);
1205 eval_subtract(result, arg, ipart);
1210 inline void eval_round(T& result, const T& a)
1212 BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The round function is only valid for floating point types.");
1213 typedef typename boost::multiprecision::detail::canonical<float, T>::type fp_type;
1214 int c = eval_fpclassify(a);
1215 if((c == (int)FP_NAN) || (c == (int)FP_INFINITE))
1217 result = boost::math::policies::raise_rounding_error("boost::multiprecision::round<%1%>(%1%)", 0, number<T>(a), number<T>(a), boost::math::policies::policy<>()).backend();
1220 if(eval_get_sign(a) < 0)
1222 eval_subtract(result, a, fp_type(0.5f));
1223 eval_ceil(result, result);
1227 eval_add(result, a, fp_type(0.5f));
1228 eval_floor(result, result);
1233 void eval_lcm(B& result, const B& a, const B& b);
1235 void eval_gcd(B& result, const B& a, const B& b);
1237 template <class T, class Arithmetic>
1238 inline typename enable_if<is_integral<Arithmetic> >::type eval_gcd(T& result, const T& a, const Arithmetic& b)
1240 typedef typename boost::multiprecision::detail::canonical<Arithmetic, T>::type si_type;
1241 using default_ops::eval_gcd;
1243 t = static_cast<si_type>(b);
1244 eval_gcd(result, a, t);
1246 template <class T, class Arithmetic>
1247 inline typename enable_if<is_integral<Arithmetic> >::type eval_gcd(T& result, const Arithmetic& a, const T& b)
1249 eval_gcd(result, b, a);
1251 template <class T, class Arithmetic>
1252 inline typename enable_if<is_integral<Arithmetic> >::type eval_lcm(T& result, const T& a, const Arithmetic& b)
1254 typedef typename boost::multiprecision::detail::canonical<Arithmetic, T>::type si_type;
1255 using default_ops::eval_lcm;
1257 t = static_cast<si_type>(b);
1258 eval_lcm(result, a, t);
1260 template <class T, class Arithmetic>
1261 inline typename enable_if<is_integral<Arithmetic> >::type eval_lcm(T& result, const Arithmetic& a, const T& b)
1263 eval_lcm(result, b, a);
1267 inline unsigned eval_lsb(const T& val)
1269 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1270 int c = eval_get_sign(val);
1273 BOOST_THROW_EXCEPTION(std::range_error("No bits were set in the operand."));
1277 BOOST_THROW_EXCEPTION(std::range_error("Testing individual bits in negative values is not supported - results are undefined."));
1279 unsigned result = 0;
1284 eval_bitwise_and(t, mask, val);
1286 eval_left_shift(mask, 1);
1288 while(eval_is_zero(t));
1294 inline int eval_msb(const T& val)
1296 int c = eval_get_sign(val);
1299 BOOST_THROW_EXCEPTION(std::range_error("No bits were set in the operand."));
1303 BOOST_THROW_EXCEPTION(std::range_error("Testing individual bits in negative values is not supported - results are undefined."));
1306 // This implementation is really really rubbish - it does
1307 // a linear scan for the most-significant-bit. We should really
1308 // do a binary search, but as none of our backends actually needs
1309 // this implementation, we'll leave it for now. In fact for most
1310 // backends it's likely that there will always be a more efficient
1311 // native implementation possible.
1313 unsigned result = 0;
1315 while(!eval_is_zero(t))
1317 eval_right_shift(t, 1);
1324 inline bool eval_bit_test(const T& val, unsigned index)
1326 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1329 eval_left_shift(mask, index);
1330 eval_bitwise_and(t, mask, val);
1331 return !eval_is_zero(t);
1335 inline void eval_bit_set(T& val, unsigned index)
1337 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1340 eval_left_shift(mask, index);
1341 eval_bitwise_or(val, mask);
1345 inline void eval_bit_flip(T& val, unsigned index)
1347 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1350 eval_left_shift(mask, index);
1351 eval_bitwise_xor(val, mask);
1355 inline void eval_bit_unset(T& val, unsigned index)
1357 typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type;
1360 eval_left_shift(mask, index);
1361 eval_bitwise_and(t, mask, val);
1362 if(!eval_is_zero(t))
1363 eval_bitwise_xor(val, mask);
1367 void eval_integer_sqrt(B& s, B& r, const B& x)
1370 // This is slow bit-by-bit integer square root, see for example
1371 // http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29
1372 // There are better methods such as http://hal.inria.fr/docs/00/07/28/54/PDF/RR-3805.pdf
1373 // and http://hal.inria.fr/docs/00/07/21/13/PDF/RR-4475.pdf which should be implemented
1376 typedef typename boost::multiprecision::detail::canonical<unsigned char, B>::type ui_type;
1379 if(eval_get_sign(x) == 0)
1384 int g = eval_msb(x);
1396 eval_bit_set(t, 2 * g);
1397 eval_subtract(r, x, t);
1399 if(eval_get_sign(r) == 0)
1401 int msbr = eval_msb(r);
1404 if(msbr >= org_g + g + 1)
1407 eval_left_shift(t, g + 1);
1408 eval_bit_set(t, 2 * g);
1409 if(t.compare(r) <= 0)
1412 eval_subtract(r, t);
1413 if(eval_get_sign(r) == 0)
1424 // These have to implemented by the backend, declared here so that our macro generated code compiles OK.
1427 typename enable_if_c<sizeof(T) == 0>::type eval_floor();
1429 typename enable_if_c<sizeof(T) == 0>::type eval_ceil();
1431 typename enable_if_c<sizeof(T) == 0>::type eval_trunc();
1433 typename enable_if_c<sizeof(T) == 0>::type eval_sqrt();
1435 typename enable_if_c<sizeof(T) == 0>::type eval_ldexp();
1437 typename enable_if_c<sizeof(T) == 0>::type eval_frexp();
1440 // eval_logb and eval_scalbn simply assume base 2 and forward to
1441 // eval_ldexp and eval_frexp:
1444 inline typename B::exponent_type eval_ilogb(const B& val)
1446 BOOST_STATIC_ASSERT_MSG(!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");
1447 typename B::exponent_type e;
1448 switch(eval_fpclassify(val))
1451 return (std::numeric_limits<typename B::exponent_type>::min)();
1453 return (std::numeric_limits<typename B::exponent_type>::max)();
1455 return (std::numeric_limits<typename B::exponent_type>::min)();
1458 eval_frexp(result, val, &e);
1462 inline void eval_logb(B& result, const B& val)
1464 typedef typename boost::mpl::if_c<boost::is_same<boost::intmax_t, long>::value, boost::long_long_type, boost::intmax_t>::type max_t;
1465 result = static_cast<max_t>(eval_ilogb(val));
1467 template <class B, class A>
1468 inline void eval_scalbn(B& result, const B& val, A e)
1470 BOOST_STATIC_ASSERT_MSG(!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");
1471 eval_ldexp(result, val, static_cast<typename B::exponent_type>(e));
1473 template <class B, class A>
1474 inline void eval_scalbln(B& result, const B& val, A e)
1476 eval_scalbn(result, val, e);
1480 inline bool is_arg_nan(const T& val, mpl::true_ const&, const mpl::false_&)
1482 return eval_fpclassify(val) == FP_NAN;
1485 inline bool is_arg_nan(const T& val, mpl::false_ const&, const mpl::true_&)
1487 return (boost::math::isnan)(val);
1490 inline bool is_arg_nan(const T&, mpl::false_ const&, const mpl::false_&)
1496 inline bool is_arg_nan(const T& val)
1498 return is_arg_nan(val, mpl::bool_<boost::multiprecision::detail::is_backend<T>::value>(), is_floating_point<T>());
1501 template <class T, class U, class V>
1502 inline void eval_fmax(T& result, const U& a, const V& b)
1505 result = number<T>::canonical_value(b);
1506 else if(is_arg_nan(b))
1507 result = number<T>::canonical_value(a);
1508 else if(eval_lt(number<T>::canonical_value(a), number<T>::canonical_value(b)))
1509 result = number<T>::canonical_value(b);
1511 result = number<T>::canonical_value(a);
1513 template <class T, class U, class V>
1514 inline void eval_fmin(T& result, const U& a, const V& b)
1517 result = number<T>::canonical_value(b);
1518 else if(is_arg_nan(b))
1519 result = number<T>::canonical_value(a);
1520 else if(eval_lt(number<T>::canonical_value(a), number<T>::canonical_value(b)))
1521 result = number<T>::canonical_value(a);
1523 result = number<T>::canonical_value(b);
1526 template <class R, class T, class U>
1527 inline void eval_hypot(R& result, const T& a, const U& b)
1530 // Normalize x and y, so that both are positive and x >= y:
1533 x = number<R>::canonical_value(a);
1534 y = number<R>::canonical_value(b);
1535 if(eval_get_sign(x) < 0)
1537 if(eval_get_sign(y) < 0)
1540 // Special case, see C99 Annex F.
1541 // The order of the if's is important: do not change!
1542 int c1 = eval_fpclassify(x);
1543 int c2 = eval_fpclassify(y);
1555 if(c1 == FP_INFINITE)
1560 if((c2 == FP_INFINITE) || (c2 == FP_NAN))
1574 eval_multiply(result, x, std::numeric_limits<number<R> >::epsilon().backend());
1576 if(eval_gt(result, y))
1583 eval_divide(rat, y, x);
1584 eval_multiply(result, rat, rat);
1585 eval_increment(result);
1586 eval_sqrt(rat, result);
1587 eval_multiply(result, rat, x);
1590 template <class R, class T>
1591 inline void eval_nearbyint(R& result, const T& a)
1593 eval_round(result, a);
1595 template <class R, class T>
1596 inline void eval_rint(R& result, const T& a)
1598 eval_nearbyint(result, a);
1602 // These functions are implemented in separate files, but expanded inline here,
1603 // DO NOT CHANGE THE ORDER OF THESE INCLUDES:
1605 #include <boost/multiprecision/detail/functions/constants.hpp>
1606 #include <boost/multiprecision/detail/functions/pow.hpp>
1607 #include <boost/multiprecision/detail/functions/trig.hpp>
1612 // Default versions of floating point classification routines:
1614 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1615 inline int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1617 using multiprecision::default_ops::eval_fpclassify;
1618 return eval_fpclassify(arg.backend());
1620 template <class tag, class A1, class A2, class A3, class A4>
1621 inline int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1623 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1624 return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
1626 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1627 inline bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1629 int v = fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg);
1630 return (v != (int)FP_INFINITE) && (v != (int)FP_NAN);
1632 template <class tag, class A1, class A2, class A3, class A4>
1633 inline bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1635 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1636 return isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
1638 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1639 inline bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1641 return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == (int)FP_NAN;
1643 template <class tag, class A1, class A2, class A3, class A4>
1644 inline bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1646 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1647 return isnan BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
1649 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1650 inline bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1652 return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == (int)FP_INFINITE;
1654 template <class tag, class A1, class A2, class A3, class A4>
1655 inline bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1657 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1658 return isinf BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
1660 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1661 inline bool isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1663 return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == (int)FP_NORMAL;
1665 template <class tag, class A1, class A2, class A3, class A4>
1666 inline bool isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1668 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1669 return isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
1672 // Default versions of sign manipulation functions, if individual backends can do better than this
1673 // (for example with signed zero), then they should overload these functions further:
1675 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1676 inline int sign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1680 template <class tag, class A1, class A2, class A3, class A4>
1681 inline int sign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1683 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1684 return sign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
1687 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1688 inline int signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1690 return arg.sign() < 0;
1692 template <class tag, class A1, class A2, class A3, class A4>
1693 inline int signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1695 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1696 return signbit BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
1698 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1699 inline multiprecision::number<Backend, ExpressionTemplates> changesign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1703 template <class tag, class A1, class A2, class A3, class A4>
1704 inline 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)
1706 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1707 return changesign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
1709 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1710 inline multiprecision::number<Backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
1712 return (boost::multiprecision::signbit)(a) != (boost::multiprecision::signbit)(b) ? (boost::multiprecision::changesign)(a) : a;
1714 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
1715 inline 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)
1717 return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
1719 template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
1720 inline 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)
1722 return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
1724 template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
1725 inline 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)
1727 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1728 return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
1731 } // namespace multiprecision
1736 // Import Math functions here, so they can be found by Boost.Math:
1738 using boost::multiprecision::signbit;
1739 using boost::multiprecision::sign;
1740 using boost::multiprecision::copysign;
1741 using boost::multiprecision::changesign;
1742 using boost::multiprecision::fpclassify;
1743 using boost::multiprecision::isinf;
1744 using boost::multiprecision::isnan;
1745 using boost::multiprecision::isnormal;
1746 using boost::multiprecision::isfinite;
1750 namespace multiprecision{
1752 typedef ::boost::math::policies::policy<
1753 ::boost::math::policies::domain_error< ::boost::math::policies::errno_on_error>,
1754 ::boost::math::policies::pole_error< ::boost::math::policies::errno_on_error>,
1755 ::boost::math::policies::overflow_error< ::boost::math::policies::errno_on_error>,
1756 ::boost::math::policies::evaluation_error< ::boost::math::policies::errno_on_error>,
1757 ::boost::math::policies::rounding_error< ::boost::math::policies::errno_on_error>
1760 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1761 inline multiprecision::number<Backend, ExpressionTemplates> asinh BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1763 return boost::math::asinh(arg, c99_error_policy());
1765 template <class tag, class A1, class A2, class A3, class A4>
1766 inline typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type asinh BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1768 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1769 return asinh(value_type(arg));
1771 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1772 inline multiprecision::number<Backend, ExpressionTemplates> acosh BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1774 return boost::math::acosh(arg, c99_error_policy());
1776 template <class tag, class A1, class A2, class A3, class A4>
1777 inline typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type acosh BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1779 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1780 return acosh(value_type(arg));
1782 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1783 inline multiprecision::number<Backend, ExpressionTemplates> atanh BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1785 return boost::math::atanh(arg, c99_error_policy());
1787 template <class tag, class A1, class A2, class A3, class A4>
1788 inline typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type atanh BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1790 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1791 return atanh(value_type(arg));
1793 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1794 inline multiprecision::number<Backend, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1796 return boost::math::cbrt(arg, c99_error_policy());
1798 template <class tag, class A1, class A2, class A3, class A4>
1799 inline 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)
1801 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1802 return cbrt(value_type(arg));
1804 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1805 inline multiprecision::number<Backend, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1807 return boost::math::erf(arg, c99_error_policy());
1809 template <class tag, class A1, class A2, class A3, class A4>
1810 inline 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)
1812 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1813 return erf(value_type(arg));
1815 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1816 inline multiprecision::number<Backend, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1818 return boost::math::erfc(arg, c99_error_policy());
1820 template <class tag, class A1, class A2, class A3, class A4>
1821 inline 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)
1823 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1824 return erfc(value_type(arg));
1826 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1827 inline multiprecision::number<Backend, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1829 return boost::math::expm1(arg, c99_error_policy());
1831 template <class tag, class A1, class A2, class A3, class A4>
1832 inline 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)
1834 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1835 return expm1(value_type(arg));
1837 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1838 inline multiprecision::number<Backend, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1840 return boost::math::lgamma(arg, c99_error_policy());
1842 template <class tag, class A1, class A2, class A3, class A4>
1843 inline 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)
1845 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1846 return lgamma(value_type(arg));
1848 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1849 inline multiprecision::number<Backend, ExpressionTemplates> tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1851 return boost::math::tgamma(arg, c99_error_policy());
1853 template <class tag, class A1, class A2, class A3, class A4>
1854 inline 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)
1856 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1857 return tgamma(value_type(arg));
1860 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1861 inline long lrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1865 template <class tag, class A1, class A2, class A3, class A4>
1866 inline long lrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1870 #ifndef BOOST_NO_LONG_LONG
1871 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1872 inline boost::long_long_type llrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1874 return llround(arg);
1876 template <class tag, class A1, class A2, class A3, class A4>
1877 inline boost::long_long_type llrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
1879 return llround(arg);
1882 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1883 inline multiprecision::number<Backend, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
1885 return boost::math::log1p(arg, c99_error_policy());
1887 template <class tag, class A1, class A2, class A3, class A4>
1888 inline 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)
1890 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1891 return log1p(value_type(arg));
1894 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1895 inline multiprecision::number<Backend, ExpressionTemplates> nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
1897 return boost::math::nextafter(a, b, c99_error_policy());
1899 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
1900 inline 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)
1902 return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
1904 template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
1905 inline 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)
1907 return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
1909 template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
1910 inline 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)
1912 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1913 return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
1915 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
1916 inline multiprecision::number<Backend, ExpressionTemplates> nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
1918 return boost::math::nextafter(a, b, c99_error_policy());
1920 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
1921 inline 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)
1923 return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
1925 template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
1926 inline 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)
1928 return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
1930 template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
1931 inline 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)
1933 typedef typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type value_type;
1934 return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
1937 template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
1938 inline number<B1, ET1>& add(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
1940 BOOST_STATIC_ASSERT_MSG((is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
1941 BOOST_STATIC_ASSERT_MSG((is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
1942 using default_ops::eval_add;
1943 eval_add(result.backend(), a.backend(), b.backend());
1947 template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
1948 inline number<B1, ET1>& subtract(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
1950 BOOST_STATIC_ASSERT_MSG((is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
1951 BOOST_STATIC_ASSERT_MSG((is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
1952 using default_ops::eval_subtract;
1953 eval_subtract(result.backend(), a.backend(), b.backend());
1957 template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
1958 inline number<B1, ET1>& multiply(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
1960 BOOST_STATIC_ASSERT_MSG((is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
1961 BOOST_STATIC_ASSERT_MSG((is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
1962 using default_ops::eval_multiply;
1963 eval_multiply(result.backend(), a.backend(), b.backend());
1967 template <class B, expression_template_option ET, class I>
1968 inline typename enable_if_c<is_integral<I>::value, number<B, ET>&>::type
1969 add(number<B, ET>& result, const I& a, const I& b)
1971 using default_ops::eval_add;
1972 typedef typename detail::canonical<I, B>::type canonical_type;
1973 eval_add(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
1977 template <class B, expression_template_option ET, class I>
1978 inline typename enable_if_c<is_integral<I>::value, number<B, ET>&>::type
1979 subtract(number<B, ET>& result, const I& a, const I& b)
1981 using default_ops::eval_subtract;
1982 typedef typename detail::canonical<I, B>::type canonical_type;
1983 eval_subtract(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
1987 template <class B, expression_template_option ET, class I>
1988 inline typename enable_if_c<is_integral<I>::value, number<B, ET>&>::type
1989 multiply(number<B, ET>& result, const I& a, const I& b)
1991 using default_ops::eval_multiply;
1992 typedef typename detail::canonical<I, B>::type canonical_type;
1993 eval_multiply(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
1997 template <class tag, class A1, class A2, class A3, class A4, class Policy>
1998 inline typename detail::expression<tag, A1, A2, A3, A4>::result_type trunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2000 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2001 return BOOST_MP_MOVE(trunc(number_type(v), pol));
2004 template <class Backend, expression_template_option ExpressionTemplates, class Policy>
2005 inline number<Backend, ExpressionTemplates> trunc(const number<Backend, ExpressionTemplates>& v, const Policy&)
2007 using default_ops::eval_trunc;
2008 number<Backend, ExpressionTemplates> result;
2009 eval_trunc(result.backend(), v.backend());
2010 return BOOST_MP_MOVE(result);
2013 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2014 inline int itrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2016 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2017 number_type r = trunc(v, pol);
2018 if((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !(boost::math::isfinite)(v))
2019 return boost::math::policies::raise_rounding_error("boost::multiprecision::itrunc<%1%>(%1%)", 0, number_type(v), 0, pol);
2020 return r.template convert_to<int>();
2022 template <class tag, class A1, class A2, class A3, class A4>
2023 inline int itrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
2025 return itrunc(v, boost::math::policies::policy<>());
2027 template <class Backend, expression_template_option ExpressionTemplates, class Policy>
2028 inline int itrunc(const number<Backend, ExpressionTemplates>& v, const Policy& pol)
2030 number<Backend, ExpressionTemplates> r = trunc(v, pol);
2031 if((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !(boost::math::isfinite)(v))
2032 return boost::math::policies::raise_rounding_error("boost::multiprecision::itrunc<%1%>(%1%)", 0, v, 0, pol);
2033 return r.template convert_to<int>();
2035 template <class Backend, expression_template_option ExpressionTemplates>
2036 inline int itrunc(const number<Backend, ExpressionTemplates>& v)
2038 return itrunc(v, boost::math::policies::policy<>());
2040 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2041 inline long ltrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2043 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2044 number_type r = trunc(v, pol);
2045 if((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !(boost::math::isfinite)(v))
2046 return boost::math::policies::raise_rounding_error("boost::multiprecision::ltrunc<%1%>(%1%)", 0, number_type(v), 0L, pol);
2047 return r.template convert_to<long>();
2049 template <class tag, class A1, class A2, class A3, class A4>
2050 inline long ltrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
2052 return ltrunc(v, boost::math::policies::policy<>());
2054 template <class T, expression_template_option ExpressionTemplates, class Policy>
2055 inline long ltrunc(const number<T, ExpressionTemplates>& v, const Policy& pol)
2057 number<T, ExpressionTemplates> r = trunc(v, pol);
2058 if((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !(boost::math::isfinite)(v))
2059 return boost::math::policies::raise_rounding_error("boost::multiprecision::ltrunc<%1%>(%1%)", 0, v, 0L, pol);
2060 return r.template convert_to<long>();
2062 template <class T, expression_template_option ExpressionTemplates>
2063 inline long ltrunc(const number<T, ExpressionTemplates>& v)
2065 return ltrunc(v, boost::math::policies::policy<>());
2067 #ifndef BOOST_NO_LONG_LONG
2068 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2069 inline boost::long_long_type lltrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2071 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2072 number_type r = trunc(v, pol);
2073 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || r < (std::numeric_limits<boost::long_long_type>::min)() || !(boost::math::isfinite)(v))
2074 return boost::math::policies::raise_rounding_error("boost::multiprecision::lltrunc<%1%>(%1%)", 0, number_type(v), 0LL, pol);
2075 return r.template convert_to<boost::long_long_type>();
2077 template <class tag, class A1, class A2, class A3, class A4>
2078 inline boost::long_long_type lltrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
2080 return lltrunc(v, boost::math::policies::policy<>());
2082 template <class T, expression_template_option ExpressionTemplates, class Policy>
2083 inline boost::long_long_type lltrunc(const number<T, ExpressionTemplates>& v, const Policy& pol)
2085 number<T, ExpressionTemplates> r = trunc(v, pol);
2086 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || r < (std::numeric_limits<boost::long_long_type>::min)() || !(boost::math::isfinite)(v))
2087 return boost::math::policies::raise_rounding_error("boost::multiprecision::lltrunc<%1%>(%1%)", 0, v, 0LL, pol);
2088 return r.template convert_to<boost::long_long_type>();
2090 template <class T, expression_template_option ExpressionTemplates>
2091 inline boost::long_long_type lltrunc(const number<T, ExpressionTemplates>& v)
2093 return lltrunc(v, boost::math::policies::policy<>());
2096 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2097 inline typename detail::expression<tag, A1, A2, A3, A4>::result_type round(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2099 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2100 return BOOST_MP_MOVE(round(static_cast<number_type>(v), pol));
2102 template <class T, expression_template_option ExpressionTemplates, class Policy>
2103 inline number<T, ExpressionTemplates> round(const number<T, ExpressionTemplates>& v, const Policy&)
2105 using default_ops::eval_round;
2106 number<T, ExpressionTemplates> result;
2107 eval_round(result.backend(), v.backend());
2108 return BOOST_MP_MOVE(result);
2111 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2112 inline int iround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2114 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2115 number_type r = round(v, pol);
2116 if((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !(boost::math::isfinite)(v))
2117 return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", 0, number_type(v), 0, pol);
2118 return r.template convert_to<int>();
2120 template <class tag, class A1, class A2, class A3, class A4>
2121 inline int iround(const detail::expression<tag, A1, A2, A3, A4>& v)
2123 return iround(v, boost::math::policies::policy<>());
2125 template <class T, expression_template_option ExpressionTemplates, class Policy>
2126 inline int iround(const number<T, ExpressionTemplates>& v, const Policy& pol)
2128 number<T, ExpressionTemplates> r = round(v, pol);
2129 if((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !(boost::math::isfinite)(v))
2130 return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", 0, v, 0, pol);
2131 return r.template convert_to<int>();
2133 template <class T, expression_template_option ExpressionTemplates>
2134 inline int iround(const number<T, ExpressionTemplates>& v)
2136 return iround(v, boost::math::policies::policy<>());
2138 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2139 inline long lround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2141 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2142 number_type r = round(v, pol);
2143 if((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !(boost::math::isfinite)(v))
2144 return boost::math::policies::raise_rounding_error("boost::multiprecision::lround<%1%>(%1%)", 0, number_type(v), 0L, pol);
2145 return r.template convert_to<long>();
2147 template <class tag, class A1, class A2, class A3, class A4>
2148 inline long lround(const detail::expression<tag, A1, A2, A3, A4>& v)
2150 return lround(v, boost::math::policies::policy<>());
2152 template <class T, expression_template_option ExpressionTemplates, class Policy>
2153 inline long lround(const number<T, ExpressionTemplates>& v, const Policy& pol)
2155 number<T, ExpressionTemplates> r = round(v, pol);
2156 if((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !(boost::math::isfinite)(v))
2157 return boost::math::policies::raise_rounding_error("boost::multiprecision::lround<%1%>(%1%)", 0, v, 0L, pol);
2158 return r.template convert_to<long>();
2160 template <class T, expression_template_option ExpressionTemplates>
2161 inline long lround(const number<T, ExpressionTemplates>& v)
2163 return lround(v, boost::math::policies::policy<>());
2165 #ifndef BOOST_NO_LONG_LONG
2166 template <class tag, class A1, class A2, class A3, class A4, class Policy>
2167 inline boost::long_long_type llround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
2169 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2170 number_type r = round(v, pol);
2171 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || r < (std::numeric_limits<boost::long_long_type>::min)() || !(boost::math::isfinite)(v))
2172 return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", 0, number_type(v), 0LL, pol);
2173 return r.template convert_to<boost::long_long_type>();
2175 template <class tag, class A1, class A2, class A3, class A4>
2176 inline boost::long_long_type llround(const detail::expression<tag, A1, A2, A3, A4>& v)
2178 return llround(v, boost::math::policies::policy<>());
2180 template <class T, expression_template_option ExpressionTemplates, class Policy>
2181 inline boost::long_long_type llround(const number<T, ExpressionTemplates>& v, const Policy& pol)
2183 number<T, ExpressionTemplates> r = round(v, pol);
2184 if((r > (std::numeric_limits<boost::long_long_type>::max)()) || r < (std::numeric_limits<boost::long_long_type>::min)() || !(boost::math::isfinite)(v))
2185 return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", 0, v, 0LL, pol);
2186 return r.template convert_to<boost::long_long_type>();
2188 template <class T, expression_template_option ExpressionTemplates>
2189 inline boost::long_long_type llround(const number<T, ExpressionTemplates>& v)
2191 return llround(v, boost::math::policies::policy<>());
2195 // frexp does not return an expression template since we require the
2196 // integer argument to be evaluated even if the returned value is
2197 // not assigned to anything...
2199 template <class T, expression_template_option ExpressionTemplates>
2200 inline typename enable_if_c<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, short* pint)
2202 using default_ops::eval_frexp;
2203 number<T, ExpressionTemplates> result;
2204 eval_frexp(result.backend(), v.backend(), pint);
2205 return BOOST_MP_MOVE(result);
2207 template <class tag, class A1, class A2, class A3, class A4>
2208 inline typename enable_if_c<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
2209 frexp(const detail::expression<tag, A1, A2, A3, A4>& v, short* pint)
2211 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2212 return BOOST_MP_MOVE(frexp(static_cast<number_type>(v), pint));
2214 template <class T, expression_template_option ExpressionTemplates>
2215 inline typename enable_if_c<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, int* pint)
2217 using default_ops::eval_frexp;
2218 number<T, ExpressionTemplates> result;
2219 eval_frexp(result.backend(), v.backend(), pint);
2220 return BOOST_MP_MOVE(result);
2222 template <class tag, class A1, class A2, class A3, class A4>
2223 inline typename enable_if_c<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
2224 frexp(const detail::expression<tag, A1, A2, A3, A4>& v, int* pint)
2226 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2227 return BOOST_MP_MOVE(frexp(static_cast<number_type>(v), pint));
2229 template <class T, expression_template_option ExpressionTemplates>
2230 inline typename enable_if_c<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, long* pint)
2232 using default_ops::eval_frexp;
2233 number<T, ExpressionTemplates> result;
2234 eval_frexp(result.backend(), v.backend(), pint);
2235 return BOOST_MP_MOVE(result);
2237 template <class tag, class A1, class A2, class A3, class A4>
2238 inline typename enable_if_c<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
2239 frexp(const detail::expression<tag, A1, A2, A3, A4>& v, long* pint)
2241 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2242 return BOOST_MP_MOVE(frexp(static_cast<number_type>(v), pint));
2244 template <class T, expression_template_option ExpressionTemplates>
2245 inline typename enable_if_c<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, boost::long_long_type* pint)
2247 using default_ops::eval_frexp;
2248 number<T, ExpressionTemplates> result;
2249 eval_frexp(result.backend(), v.backend(), pint);
2250 return BOOST_MP_MOVE(result);
2252 template <class tag, class A1, class A2, class A3, class A4>
2253 inline typename enable_if_c<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
2254 frexp(const detail::expression<tag, A1, A2, A3, A4>& v, boost::long_long_type* pint)
2256 typedef typename detail::expression<tag, A1, A2, A3, A4>::result_type number_type;
2257 return BOOST_MP_MOVE(frexp(static_cast<number_type>(v), pint));
2260 // modf does not return an expression template since we require the
2261 // second argument to be evaluated even if the returned value is
2262 // not assigned to anything...
2264 template <class T, expression_template_option ExpressionTemplates>
2265 inline typename enable_if_c<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type modf(const number<T, ExpressionTemplates>& v, number<T, ExpressionTemplates>* pipart)
2267 using default_ops::eval_modf;
2268 number<T, ExpressionTemplates> result;
2269 eval_modf(result.backend(), v.backend(), pipart ? &pipart->backend() : 0);
2270 return BOOST_MP_MOVE(result);
2272 template <class T, expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
2273 inline typename enable_if_c<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)
2275 using default_ops::eval_modf;
2276 number<T, ExpressionTemplates> result, arg(v);
2277 eval_modf(result.backend(), arg.backend(), pipart ? &pipart->backend() : 0);
2278 return BOOST_MP_MOVE(result);
2282 // Integer square root:
2284 template <class B, expression_template_option ExpressionTemplates>
2285 inline typename enable_if_c<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
2286 sqrt(const number<B, ExpressionTemplates>& x)
2288 using default_ops::eval_integer_sqrt;
2289 number<B, ExpressionTemplates> s, r;
2290 eval_integer_sqrt(s.backend(), r.backend(), x.backend());
2297 namespace default_ops {
2301 template <class B, class T, class U, class V>
2302 void operator()(B& result, const T& a, const U& b, const V& c)const
2304 eval_multiply_add(result, a, b, c);
2311 template <class Backend, class U, class V>
2312 inline typename enable_if<
2314 mpl::bool_<number_category<number<Backend, et_on> >::value == number_kind_floating_point>,
2317 is_number_expression<U>,
2322 is_number_expression<V>,
2326 detail::expression<detail::function, default_ops::fma_func, number<Backend, et_on>, U, V>
2328 fma(const number<Backend, et_on>& a, const U& b, const V& c)
2330 return detail::expression<detail::function, default_ops::fma_func, number<Backend, et_on>, U, V>(
2331 default_ops::fma_func(), a, b, c);
2334 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class U, class V>
2335 inline typename enable_if<
2337 mpl::bool_<number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type >::value == number_kind_floating_point>,
2340 is_number_expression<U>,
2345 is_number_expression<V>,
2349 detail::expression<detail::function, default_ops::fma_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, V>
2351 fma(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& a, const U& b, const V& c)
2353 return detail::expression<detail::function, default_ops::fma_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, V>(
2354 default_ops::fma_func(), a, b, c);
2357 template <class Backend, class U, class V>
2358 inline typename enable_if<
2360 mpl::bool_<number_category<number<Backend, et_off> >::value == number_kind_floating_point>,
2363 is_number_expression<U>,
2368 is_number_expression<V>,
2372 number<Backend, et_off>
2374 fma(const number<Backend, et_off>& a, const U& b, const V& c)
2376 using default_ops::eval_multiply_add;
2377 number<Backend, et_off> result;
2378 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));
2379 return BOOST_MP_MOVE(result);
2382 template <class U, class Backend, class V>
2383 inline typename enable_if<
2385 mpl::bool_<number_category<number<Backend, et_on> >::value == number_kind_floating_point>,
2389 is_number_expression<V>,
2393 detail::expression<detail::function, default_ops::fma_func, U, number<Backend, et_on>, V>
2395 fma(const U& a, const number<Backend, et_on>& b, const V& c)
2397 return detail::expression<detail::function, default_ops::fma_func, U, number<Backend, et_on>, V>(
2398 default_ops::fma_func(), a, b, c);
2401 template <class U, class tag, class Arg1, class Arg2, class Arg3, class Arg4, class V>
2402 inline typename enable_if<
2404 mpl::bool_<number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type >::value == number_kind_floating_point>,
2408 is_number_expression<V>,
2412 detail::expression<detail::function, default_ops::fma_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, V>
2414 fma(const U& a, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& b, const V& c)
2416 return detail::expression<detail::function, default_ops::fma_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, V>(
2417 default_ops::fma_func(), a, b, c);
2420 template <class U, class Backend, class V>
2421 inline typename enable_if<
2423 mpl::bool_<number_category<number<Backend, et_off> >::value == number_kind_floating_point>,
2427 is_number_expression<V>,
2431 number<Backend, et_off>
2433 fma(const U& a, const number<Backend, et_off>& b, const V& c)
2435 using default_ops::eval_multiply_add;
2436 number<Backend, et_off> result;
2437 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));
2438 return BOOST_MP_MOVE(result);
2441 template <class U, class V, class Backend>
2442 inline typename enable_if<
2444 mpl::bool_<number_category<number<Backend, et_on> >::value == number_kind_floating_point>,
2448 detail::expression<detail::function, default_ops::fma_func, U, V, number<Backend, et_on> >
2450 fma(const U& a, const V& b, const number<Backend, et_on>& c)
2452 return detail::expression<detail::function, default_ops::fma_func, U, V, number<Backend, et_on> >(
2453 default_ops::fma_func(), a, b, c);
2456 template <class U, class V, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
2457 inline typename enable_if<
2459 mpl::bool_<number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type >::value == number_kind_floating_point>,
2463 detail::expression<detail::function, default_ops::fma_func, U, V, detail::expression<tag, Arg1, Arg2, Arg3, Arg4> >
2465 fma(const U& a, const V& b, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& c)
2467 return detail::expression<detail::function, default_ops::fma_func, U, V, detail::expression<tag, Arg1, Arg2, Arg3, Arg4> >(
2468 default_ops::fma_func(), a, b, c);
2471 template <class U, class V, class Backend>
2472 inline typename enable_if<
2474 mpl::bool_<number_category<number<Backend, et_off> >::value == number_kind_floating_point>,
2478 number<Backend, et_off>
2480 fma(const U& a, const V& b, const number<Backend, et_off>& c)
2482 using default_ops::eval_multiply_add;
2483 number<Backend, et_off> result;
2484 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));
2485 return BOOST_MP_MOVE(result);
2488 namespace default_ops {
2492 template <class B, class T, class U>
2493 void operator()(B& result, const T& a, const U& b, int* pi)const
2495 eval_remquo(result, a, b, pi);
2501 template <class Backend, class U>
2502 inline typename enable_if_c<
2503 number_category<number<Backend, et_on> >::value == number_kind_floating_point,
2504 detail::expression<detail::function, default_ops::remquo_func, number<Backend, et_on>, U, int*>
2506 remquo(const number<Backend, et_on>& a, const U& b, int* pi)
2508 return detail::expression<detail::function, default_ops::remquo_func, number<Backend, et_on>, U, int*>(
2509 default_ops::remquo_func(), a, b, pi);
2512 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class U>
2513 inline typename enable_if_c<
2514 number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type >::value == number_kind_floating_point,
2515 detail::expression<detail::function, default_ops::remquo_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, int*>
2517 remquo(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& a, const U& b, int* pi)
2519 return detail::expression<detail::function, default_ops::remquo_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, int*>(
2520 default_ops::remquo_func(), a, b, pi);
2523 template <class U, class Backend>
2524 inline typename enable_if_c<
2525 (number_category<number<Backend, et_on> >::value == number_kind_floating_point)
2526 && !is_number<U>::value && !is_number_expression<U>::value,
2527 detail::expression<detail::function, default_ops::remquo_func, U, number<Backend, et_on>, int*>
2529 remquo(const U& a, const number<Backend, et_on>& b, int* pi)
2531 return detail::expression<detail::function, default_ops::remquo_func, U, number<Backend, et_on>, int*>(
2532 default_ops::remquo_func(), a, b, pi);
2535 template <class U, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
2536 inline typename enable_if_c<
2537 (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type >::value == number_kind_floating_point)
2538 && !is_number<U>::value && !is_number_expression<U>::value,
2539 detail::expression<detail::function, default_ops::remquo_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, int*>
2541 remquo(const U& a, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& b, int* pi)
2543 return detail::expression<detail::function, default_ops::remquo_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, int*>(
2544 default_ops::remquo_func(), a, b, pi);
2547 template <class Backend, class U>
2548 inline typename enable_if_c<
2549 number_category<number<Backend, et_on> >::value == number_kind_floating_point,
2550 number<Backend, et_off>
2552 remquo(const number<Backend, et_off>& a, const U& b, int* pi)
2554 using default_ops::eval_remquo;
2555 number<Backend, et_off> result;
2556 eval_remquo(result.backend(), a.backend(), number<Backend, et_off>::canonical_value(b), pi);
2557 return BOOST_MP_MOVE(result);
2559 template <class U, class Backend>
2560 inline typename enable_if_c<
2561 (number_category<number<Backend, et_on> >::value == number_kind_floating_point)
2562 && !is_number<U>::value && !is_number_expression<U>::value,
2563 number<Backend, et_off>
2565 remquo(const U& a, const number<Backend, et_off>& b, int* pi)
2567 using default_ops::eval_remquo;
2568 number<Backend, et_off> result;
2569 eval_remquo(result.backend(), number<Backend, et_off>::canonical_value(a), b.backend(), pi);
2570 return BOOST_MP_MOVE(result);
2574 template <class B, expression_template_option ExpressionTemplates>
2575 inline typename enable_if_c<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
2576 sqrt(const number<B, ExpressionTemplates>& x, number<B, ExpressionTemplates>& r)
2578 using default_ops::eval_integer_sqrt;
2579 number<B, ExpressionTemplates> s;
2580 eval_integer_sqrt(s.backend(), r.backend(), x.backend());
2584 #define UNARY_OP_FUNCTOR(func, category)\
2586 template <class Backend> \
2587 struct BOOST_JOIN(func, _funct)\
2589 void operator()(Backend& result, const Backend& arg)const\
2591 using default_ops::BOOST_JOIN(eval_,func);\
2592 BOOST_JOIN(eval_,func)(result, arg);\
2598 template <class tag, class A1, class A2, class A3, class A4> \
2599 inline typename enable_if_c<number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category,\
2600 detail::expression<\
2602 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2603 , detail::expression<tag, A1, A2, A3, A4> > \
2605 func(const detail::expression<tag, A1, A2, A3, A4>& arg)\
2607 return detail::expression<\
2609 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2610 , detail::expression<tag, A1, A2, A3, A4> \
2612 detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>() \
2616 template <class Backend> \
2617 inline typename enable_if_c<number_category<Backend>::value == category,\
2618 detail::expression<\
2620 , detail::BOOST_JOIN(func, _funct)<Backend> \
2621 , number<Backend, et_on> > \
2623 func(const number<Backend, et_on>& arg)\
2625 return detail::expression<\
2627 , detail::BOOST_JOIN(func, _funct)<Backend> \
2628 , number<Backend, et_on> \
2630 detail::BOOST_JOIN(func, _funct)<Backend>() \
2634 template <class Backend> \
2635 inline typename boost::enable_if_c<\
2636 boost::multiprecision::number_category<Backend>::value == category,\
2637 number<Backend, et_off> >::type \
2638 func(const number<Backend, et_off>& arg)\
2640 number<Backend, et_off> result;\
2641 using default_ops::BOOST_JOIN(eval_,func);\
2642 BOOST_JOIN(eval_,func)(result.backend(), arg.backend());\
2643 return BOOST_MP_MOVE(result);\
2646 #define BINARY_OP_FUNCTOR(func, category)\
2648 template <class Backend> \
2649 struct BOOST_JOIN(func, _funct)\
2651 void operator()(Backend& result, const Backend& arg, const Backend& a)const\
2653 using default_ops:: BOOST_JOIN(eval_,func);\
2654 BOOST_JOIN(eval_,func)(result, arg, a);\
2656 template <class Arithmetic> \
2657 void operator()(Backend& result, const Backend& arg, const Arithmetic& a)const\
2659 using default_ops:: BOOST_JOIN(eval_,func);\
2660 BOOST_JOIN(eval_,func)(result, arg, a);\
2662 template <class Arithmetic> \
2663 void operator()(Backend& result, const Arithmetic& arg, const Backend& a)const\
2665 using default_ops:: BOOST_JOIN(eval_,func);\
2666 BOOST_JOIN(eval_,func)(result, arg, a);\
2671 template <class Backend> \
2672 inline typename enable_if_c<number_category<Backend>::value == category,\
2673 detail::expression<\
2675 , detail::BOOST_JOIN(func, _funct)<Backend> \
2676 , number<Backend, et_on> \
2677 , number<Backend, et_on> > \
2679 func(const number<Backend, et_on>& arg, const number<Backend, et_on>& a)\
2681 return detail::expression<\
2683 , detail::BOOST_JOIN(func, _funct)<Backend> \
2684 , number<Backend, et_on> \
2685 , number<Backend, et_on> \
2687 detail::BOOST_JOIN(func, _funct)<Backend>() \
2692 template <class Backend, class tag, class A1, class A2, class A3, class A4> \
2693 inline typename enable_if_c<\
2694 (number_category<Backend>::value == category) && (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category),\
2695 detail::expression<\
2697 , detail::BOOST_JOIN(func, _funct)<Backend> \
2698 , number<Backend, et_on> \
2699 , detail::expression<tag, A1, A2, A3, A4> > \
2701 func(const number<Backend, et_on>& arg, const detail::expression<tag, A1, A2, A3, A4>& a)\
2703 return detail::expression<\
2705 , detail::BOOST_JOIN(func, _funct)<Backend> \
2706 , number<Backend, et_on> \
2707 , detail::expression<tag, A1, A2, A3, A4> \
2709 detail::BOOST_JOIN(func, _funct)<Backend>() \
2714 template <class tag, class A1, class A2, class A3, class A4, class Backend> \
2715 inline typename enable_if_c<\
2716 (number_category<Backend>::value == category) && (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category),\
2717 detail::expression<\
2719 , detail::BOOST_JOIN(func, _funct)<Backend> \
2720 , detail::expression<tag, A1, A2, A3, A4> \
2721 , number<Backend, et_on> > \
2723 func(const detail::expression<tag, A1, A2, A3, A4>& arg, const number<Backend, et_on>& a)\
2725 return detail::expression<\
2727 , detail::BOOST_JOIN(func, _funct)<Backend> \
2728 , detail::expression<tag, A1, A2, A3, A4> \
2729 , number<Backend, et_on> \
2731 detail::BOOST_JOIN(func, _funct)<Backend>() \
2736 template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b> \
2737 inline typename enable_if_c<\
2738 (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category) && (number_category<detail::expression<tagb, A1b, A2b, A3b, A4b> >::value == category),\
2739 detail::expression<\
2741 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2742 , detail::expression<tag, A1, A2, A3, A4> \
2743 , detail::expression<tagb, A1b, A2b, A3b, A4b> > \
2745 func(const detail::expression<tag, A1, A2, A3, A4>& arg, const detail::expression<tagb, A1b, A2b, A3b, A4b>& a)\
2747 return detail::expression<\
2749 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2750 , detail::expression<tag, A1, A2, A3, A4> \
2751 , detail::expression<tagb, A1b, A2b, A3b, A4b> \
2753 detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>() \
2758 template <class Backend, class Arithmetic> \
2759 inline typename enable_if_c<\
2760 is_arithmetic<Arithmetic>::value && (number_category<Backend>::value == category),\
2761 detail::expression<\
2763 , detail::BOOST_JOIN(func, _funct)<Backend> \
2764 , number<Backend, et_on> \
2768 func(const number<Backend, et_on>& arg, const Arithmetic& a)\
2770 return detail::expression<\
2772 , detail::BOOST_JOIN(func, _funct)<Backend> \
2773 , number<Backend, et_on> \
2776 detail::BOOST_JOIN(func, _funct)<Backend>() \
2781 template <class tag, class A1, class A2, class A3, class A4, class Arithmetic> \
2782 inline typename enable_if_c<\
2783 is_arithmetic<Arithmetic>::value && (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category),\
2784 detail::expression<\
2786 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2787 , detail::expression<tag, A1, A2, A3, A4> \
2791 func(const detail::expression<tag, A1, A2, A3, A4>& arg, const Arithmetic& a)\
2793 return detail::expression<\
2795 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2796 , detail::expression<tag, A1, A2, A3, A4> \
2799 detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>() \
2804 template <class Backend, class Arithmetic> \
2805 inline typename enable_if_c<\
2806 is_arithmetic<Arithmetic>::value && (number_category<Backend>::value == category),\
2807 detail::expression<\
2809 , detail::BOOST_JOIN(func, _funct)<Backend> \
2811 , number<Backend, et_on> \
2814 func(const Arithmetic& arg, const number<Backend, et_on>& a)\
2816 return detail::expression<\
2818 , detail::BOOST_JOIN(func, _funct)<Backend> \
2820 , number<Backend, et_on> \
2822 detail::BOOST_JOIN(func, _funct)<Backend>() \
2827 template <class tag, class A1, class A2, class A3, class A4, class Arithmetic> \
2828 inline typename enable_if_c<\
2829 is_arithmetic<Arithmetic>::value && (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category),\
2830 detail::expression<\
2832 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2834 , detail::expression<tag, A1, A2, A3, A4> \
2837 func(const Arithmetic& arg, const detail::expression<tag, A1, A2, A3, A4>& a)\
2839 return detail::expression<\
2841 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2843 , detail::expression<tag, A1, A2, A3, A4> \
2845 detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>() \
2850 template <class Backend> \
2851 inline typename enable_if_c<(number_category<Backend>::value == category),\
2852 number<Backend, et_off> >::type \
2853 func(const number<Backend, et_off>& arg, const number<Backend, et_off>& a)\
2855 number<Backend, et_off> result;\
2856 using default_ops:: BOOST_JOIN(eval_,func);\
2857 BOOST_JOIN(eval_,func)(result.backend(), arg.backend(), a.backend());\
2858 return BOOST_MP_MOVE(result);\
2860 template <class Backend, class Arithmetic> \
2861 inline typename enable_if_c<\
2862 is_arithmetic<Arithmetic>::value && (number_category<Backend>::value == category),\
2863 number<Backend, et_off> \
2865 func(const number<Backend, et_off>& arg, const Arithmetic& a)\
2867 typedef typename detail::canonical<Arithmetic, Backend>::type canonical_type;\
2868 number<Backend, et_off> result;\
2869 using default_ops:: BOOST_JOIN(eval_,func);\
2870 BOOST_JOIN(eval_,func)(result.backend(), arg.backend(), static_cast<canonical_type>(a));\
2871 return BOOST_MP_MOVE(result);\
2873 template <class Backend, class Arithmetic> \
2874 inline typename enable_if_c<\
2875 is_arithmetic<Arithmetic>::value && (number_category<Backend>::value == category),\
2876 number<Backend, et_off> \
2878 func(const Arithmetic& a, const number<Backend, et_off>& arg)\
2880 typedef typename detail::canonical<Arithmetic, Backend>::type canonical_type;\
2881 number<Backend, et_off> result;\
2882 using default_ops:: BOOST_JOIN(eval_,func);\
2883 BOOST_JOIN(eval_,func)(result.backend(), static_cast<canonical_type>(a), arg.backend());\
2884 return BOOST_MP_MOVE(result);\
2888 #define HETERO_BINARY_OP_FUNCTOR_B(func, Arg2, category)\
2889 template <class tag, class A1, class A2, class A3, class A4> \
2890 inline typename enable_if_c<\
2891 (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category),\
2892 detail::expression<\
2894 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2895 , detail::expression<tag, A1, A2, A3, A4> \
2898 func(const detail::expression<tag, A1, A2, A3, A4>& arg, Arg2 const& a)\
2900 return detail::expression<\
2902 , detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type> \
2903 , detail::expression<tag, A1, A2, A3, A4> \
2906 detail::BOOST_JOIN(func, _funct)<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>() \
2910 template <class Backend> \
2911 inline typename enable_if_c<\
2912 (number_category<Backend>::value == category),\
2913 detail::expression<\
2915 , detail::BOOST_JOIN(func, _funct)<Backend> \
2916 , number<Backend, et_on> \
2919 func(const number<Backend, et_on>& arg, Arg2 const& a)\
2921 return detail::expression<\
2923 , detail::BOOST_JOIN(func, _funct)<Backend> \
2924 , number<Backend, et_on> \
2927 detail::BOOST_JOIN(func, _funct)<Backend>() \
2932 template <class Backend> \
2933 inline typename enable_if_c<\
2934 (number_category<Backend>::value == category),\
2935 number<Backend, et_off> >::type \
2936 func(const number<Backend, et_off>& arg, Arg2 const& a)\
2938 number<Backend, et_off> result;\
2939 using default_ops:: BOOST_JOIN(eval_,func);\
2940 BOOST_JOIN(eval_,func)(result.backend(), arg.backend(), a);\
2941 return BOOST_MP_MOVE(result);\
2944 #define HETERO_BINARY_OP_FUNCTOR(func, Arg2, category)\
2946 template <class Backend> \
2947 struct BOOST_JOIN(func, _funct)\
2949 template <class Arg>\
2950 void operator()(Backend& result, Backend const& arg, Arg a)const\
2952 using default_ops:: BOOST_JOIN(eval_,func);\
2953 BOOST_JOIN(eval_,func)(result, arg, a);\
2959 HETERO_BINARY_OP_FUNCTOR_B(func, Arg2, category)
2962 template <class Backend>
2965 void operator()(Backend& result, const Backend& arg)const
2967 using default_ops::eval_abs;
2968 eval_abs(result, arg);
2974 template <class tag, class A1, class A2, class A3, class A4>
2975 inline detail::expression<
2977 , detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>
2978 , detail::expression<tag, A1, A2, A3, A4> >
2979 abs(const detail::expression<tag, A1, A2, A3, A4>& arg)
2981 return detail::expression<
2983 , detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>
2984 , detail::expression<tag, A1, A2, A3, A4>
2986 detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>()
2990 template <class Backend>
2991 inline detail::expression<
2993 , detail::abs_funct<Backend>
2994 , number<Backend, et_on> >
2995 abs(const number<Backend, et_on>& arg)
2997 return detail::expression<
2999 , detail::abs_funct<Backend>
3000 , number<Backend, et_on>
3002 detail::abs_funct<Backend>()
3006 template <class Backend>
3007 inline number<Backend, et_off>
3008 abs(const number<Backend, et_off>& arg)
3010 number<Backend, et_off> result;
3011 using default_ops::eval_abs;
3012 eval_abs(result.backend(), arg.backend());
3013 return BOOST_MP_MOVE(result);
3016 UNARY_OP_FUNCTOR(fabs, number_kind_floating_point)
3017 UNARY_OP_FUNCTOR(sqrt, number_kind_floating_point)
3018 UNARY_OP_FUNCTOR(floor, number_kind_floating_point)
3019 UNARY_OP_FUNCTOR(ceil, number_kind_floating_point)
3020 UNARY_OP_FUNCTOR(trunc, number_kind_floating_point)
3021 UNARY_OP_FUNCTOR(round, number_kind_floating_point)
3022 UNARY_OP_FUNCTOR(exp, number_kind_floating_point)
3023 UNARY_OP_FUNCTOR(exp2, number_kind_floating_point)
3024 UNARY_OP_FUNCTOR(log, number_kind_floating_point)
3025 UNARY_OP_FUNCTOR(log10, number_kind_floating_point)
3026 UNARY_OP_FUNCTOR(cos, number_kind_floating_point)
3027 UNARY_OP_FUNCTOR(sin, number_kind_floating_point)
3028 UNARY_OP_FUNCTOR(tan, number_kind_floating_point)
3029 UNARY_OP_FUNCTOR(asin, number_kind_floating_point)
3030 UNARY_OP_FUNCTOR(acos, number_kind_floating_point)
3031 UNARY_OP_FUNCTOR(atan, number_kind_floating_point)
3032 UNARY_OP_FUNCTOR(cosh, number_kind_floating_point)
3033 UNARY_OP_FUNCTOR(sinh, number_kind_floating_point)
3034 UNARY_OP_FUNCTOR(tanh, number_kind_floating_point)
3035 UNARY_OP_FUNCTOR(log2, number_kind_floating_point)
3036 UNARY_OP_FUNCTOR(nearbyint, number_kind_floating_point)
3037 UNARY_OP_FUNCTOR(rint, number_kind_floating_point)
3039 HETERO_BINARY_OP_FUNCTOR(ldexp, short, number_kind_floating_point)
3040 //HETERO_BINARY_OP_FUNCTOR(frexp, short*, number_kind_floating_point)
3041 HETERO_BINARY_OP_FUNCTOR_B(ldexp, int, number_kind_floating_point)
3042 //HETERO_BINARY_OP_FUNCTOR_B(frexp, int*, number_kind_floating_point)
3043 HETERO_BINARY_OP_FUNCTOR_B(ldexp, long, number_kind_floating_point)
3044 //HETERO_BINARY_OP_FUNCTOR_B(frexp, long*, number_kind_floating_point)
3045 HETERO_BINARY_OP_FUNCTOR_B(ldexp, boost::long_long_type, number_kind_floating_point)
3046 //HETERO_BINARY_OP_FUNCTOR_B(frexp, boost::long_long_type*, number_kind_floating_point)
3047 BINARY_OP_FUNCTOR(pow, number_kind_floating_point)
3048 BINARY_OP_FUNCTOR(fmod, number_kind_floating_point)
3049 BINARY_OP_FUNCTOR(fmax, number_kind_floating_point)
3050 BINARY_OP_FUNCTOR(fmin, number_kind_floating_point)
3051 BINARY_OP_FUNCTOR(atan2, number_kind_floating_point)
3052 BINARY_OP_FUNCTOR(fdim, number_kind_floating_point)
3053 BINARY_OP_FUNCTOR(hypot, number_kind_floating_point)
3054 BINARY_OP_FUNCTOR(remainder, number_kind_floating_point)
3056 UNARY_OP_FUNCTOR(logb, number_kind_floating_point)
3057 HETERO_BINARY_OP_FUNCTOR(scalbn, short, number_kind_floating_point)
3058 HETERO_BINARY_OP_FUNCTOR(scalbln, short, number_kind_floating_point)
3059 HETERO_BINARY_OP_FUNCTOR_B(scalbn, int, number_kind_floating_point)
3060 HETERO_BINARY_OP_FUNCTOR_B(scalbln, int, number_kind_floating_point)
3061 HETERO_BINARY_OP_FUNCTOR_B(scalbn, long, number_kind_floating_point)
3062 HETERO_BINARY_OP_FUNCTOR_B(scalbln, long, number_kind_floating_point)
3063 HETERO_BINARY_OP_FUNCTOR_B(scalbn, boost::long_long_type, number_kind_floating_point)
3064 HETERO_BINARY_OP_FUNCTOR_B(scalbln, boost::long_long_type, number_kind_floating_point)
3067 // Integer functions:
3069 BINARY_OP_FUNCTOR(gcd, number_kind_integer)
3070 BINARY_OP_FUNCTOR(lcm, number_kind_integer)
3071 HETERO_BINARY_OP_FUNCTOR_B(pow, unsigned, number_kind_integer)
3073 #undef BINARY_OP_FUNCTOR
3074 #undef UNARY_OP_FUNCTOR
3079 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
3080 inline typename enable_if_c<number_category<Backend>::value == number_kind_floating_point, typename Backend::exponent_type>::type
3081 ilogb(const multiprecision::number<Backend, ExpressionTemplates>& val)
3083 using default_ops::eval_ilogb;
3084 return eval_ilogb(val.backend());
3087 template <class tag, class A1, class A2, class A3, class A4>
3088 inline typename enable_if_c<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
3089 ilogb(const detail::expression<tag, A1, A2, A3, A4>& val)
3091 using default_ops::eval_ilogb;
3092 typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type arg(val);
3093 return eval_ilogb(arg.backend());
3096 } //namespace multiprecision
3100 // Overload of Boost.Math functions that find the wrong overload when used with number:
3103 template <class T> T sinc_pi_imp(T);
3104 template <class T> T sinhc_pi_imp(T);
3106 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
3107 inline multiprecision::number<Backend, ExpressionTemplates> sinc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x)
3109 return BOOST_MP_MOVE(detail::sinc_pi_imp(x));
3112 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class Policy>
3113 inline multiprecision::number<Backend, ExpressionTemplates> sinc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x, const Policy&)
3115 return BOOST_MP_MOVE(detail::sinc_pi_imp(x));
3118 template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
3119 inline multiprecision::number<Backend, ExpressionTemplates> sinhc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x)
3121 return BOOST_MP_MOVE(detail::sinhc_pi_imp(x));
3124 template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class Policy>
3125 inline multiprecision::number<Backend, ExpressionTemplates> sinhc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x, const Policy&)
3127 return BOOST_MP_MOVE(boost::math::sinhc_pi(x));
3131 #pragma warning(pop)
3134 } // namespace boost
3137 // This has to come last of all:
3139 #include <boost/multiprecision/detail/no_et_ops.hpp>
3140 #include <boost/multiprecision/detail/et_ops.hpp>
3142 // min/max overloads:
3144 #include <boost/multiprecision/detail/min_max.hpp>