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)
5
6 #ifndef BOOST_MATH_BIG_NUM_BASE_HPP
7 #define BOOST_MATH_BIG_NUM_BASE_HPP
8
9 #include <limits>
10 #include <boost/utility/enable_if.hpp>
11 #include <boost/type_traits/is_convertible.hpp>
12 #include <boost/type_traits/decay.hpp>
13 #ifdef BOOST_MSVC
14 # pragma warning(push)
15 # pragma warning(disable:4307)
16 #endif
17 #include <boost/lexical_cast.hpp>
18 #ifdef BOOST_MSVC
19 # pragma warning(pop)
20 #endif
21
22 #if defined(NDEBUG) && !defined(_DEBUG)
23 # define BOOST_MP_FORCEINLINE BOOST_FORCEINLINE
24 #else
25 # define BOOST_MP_FORCEINLINE inline
26 #endif
27
28 #if defined(BOOST_GCC) && (BOOST_GCC <= 40700)
29 # define BOOST_MP_NOEXCEPT_IF(x)
30 #else
31 # define BOOST_MP_NOEXCEPT_IF(x) BOOST_NOEXCEPT_IF(x)
32 #endif
33
34 #ifdef BOOST_MSVC
35 # pragma warning(push)
36 # pragma warning(disable:6326)
37 #endif
38
39 namespace boost{
40 namespace multiprecision{
41
42 enum expression_template_option
43 {
44 et_off = 0,
45 et_on = 1
46 };
47
48 template <class Backend>
49 struct expression_template_default
50 {
51 static const expression_template_option value = et_on;
52 };
53
54 template <class Backend, expression_template_option ExpressionTemplates = expression_template_default<Backend>::value>
55 class number;
56
57 template <class T>
58 struct is_number : public mpl::false_ {};
59
60 template <class Backend, expression_template_option ExpressionTemplates>
61 struct is_number<number<Backend, ExpressionTemplates> > : public mpl::true_ {};
62
63 namespace detail{
64
65 // Forward-declare an expression wrapper
66 template<class tag, class Arg1 = void, class Arg2 = void, class Arg3 = void, class Arg4 = void>
67 struct expression;
68
69 } // namespace detail
70
71 template <class T>
72 struct is_number_expression : public mpl::false_ {};
73
74 template<class tag, class Arg1, class Arg2, class Arg3, class Arg4>
75 struct is_number_expression<detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > : public mpl::true_ {};
76
77 template <class T, class Num>
78 struct is_compatible_arithmetic_type
79 : public mpl::bool_<
80 is_convertible<T, Num>::value
81 && !is_same<T, Num>::value
82 && !is_number_expression<T>::value>
83 {};
84
85 namespace detail{
86 //
87 // Workaround for missing abs(boost::long_long_type) and abs(__int128) on some compilers:
88 //
89 template <class T>
abs(T t)90 BOOST_CONSTEXPR typename enable_if_c<(is_signed<T>::value || is_floating_point<T>::value), T>::type abs(T t) BOOST_NOEXCEPT
91 {
92 // This strange expression avoids a hardware trap in the corner case
93 // that val is the most negative value permitted in boost::long_long_type.
94 // See https://svn.boost.org/trac/boost/ticket/9740.
95 return t < 0 ? T(1u) + T(-(t + 1)) : t;
96 }
97 template <class T>
abs(T t)98 BOOST_CONSTEXPR typename enable_if_c<(is_unsigned<T>::value), T>::type abs(T t) BOOST_NOEXCEPT
99 {
100 return t;
101 }
102
103 #define BOOST_MP_USING_ABS using boost::multiprecision::detail::abs;
104
105 template <class T>
unsigned_abs(T t)106 BOOST_CONSTEXPR typename enable_if_c<(is_signed<T>::value || is_floating_point<T>::value), typename make_unsigned<T>::type>::type unsigned_abs(T t) BOOST_NOEXCEPT
107 {
108 // This strange expression avoids a hardware trap in the corner case
109 // that val is the most negative value permitted in boost::long_long_type.
110 // See https://svn.boost.org/trac/boost/ticket/9740.
111 return t < 0 ? static_cast<typename make_unsigned<T>::type>(1u) + static_cast<typename make_unsigned<T>::type>(-(t + 1)) : static_cast<typename make_unsigned<T>::type>(t);
112 }
113 template <class T>
unsigned_abs(T t)114 BOOST_CONSTEXPR typename enable_if_c<(is_unsigned<T>::value), T>::type unsigned_abs(T t) BOOST_NOEXCEPT
115 {
116 return t;
117 }
118
119 //
120 // Move support:
121 //
122 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
123 # define BOOST_MP_MOVE(x) std::move(x)
124 #else
125 # define BOOST_MP_MOVE(x) x
126 #endif
127
128 template <class T>
129 struct bits_of
130 {
131 BOOST_STATIC_ASSERT(is_integral<T>::value || is_enum<T>::value || std::numeric_limits<T>::is_specialized);
132 static const unsigned value =
133 std::numeric_limits<T>::is_specialized ?
134 std::numeric_limits<T>::digits
135 : sizeof(T) * CHAR_BIT - (is_signed<T>::value ? 1 : 0);
136 };
137
138 #if defined(_GLIBCXX_USE_FLOAT128) && defined(BOOST_GCC) && !defined(__STRICT_ANSI__)
139 template<> struct bits_of<__float128> { static const unsigned value = 113; };
140 #endif
141
142 template <int b>
143 struct has_enough_bits
144 {
145 template <class T>
146 struct type : public mpl::bool_<bits_of<T>::value>= b>{};
147 };
148
149 template <class Val, class Backend, class Tag>
150 struct canonical_imp
151 {
152 typedef typename remove_cv<typename decay<const Val>::type>::type type;
153 };
154 template <class B, class Backend, class Tag>
155 struct canonical_imp<number<B, et_on>, Backend, Tag>
156 {
157 typedef B type;
158 };
159 template <class B, class Backend, class Tag>
160 struct canonical_imp<number<B, et_off>, Backend, Tag>
161 {
162 typedef B type;
163 };
164 #ifdef __SUNPRO_CC
165 template <class B, class Backend>
166 struct canonical_imp<number<B, et_on>, Backend, mpl::int_<3> >
167 {
168 typedef B type;
169 };
170 template <class B, class Backend>
171 struct canonical_imp<number<B, et_off>, Backend, mpl::int_<3> >
172 {
173 typedef B type;
174 };
175 #endif
176 template <class Val, class Backend>
177 struct canonical_imp<Val, Backend, mpl::int_<0> >
178 {
179 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
180 typedef typename mpl::find_if<
181 typename Backend::signed_types,
182 pred_type
183 >::type iter_type;
184 typedef typename mpl::end<typename Backend::signed_types>::type end_type;
185 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
186 };
187 template <class Val, class Backend>
188 struct canonical_imp<Val, Backend, mpl::int_<1> >
189 {
190 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
191 typedef typename mpl::find_if<
192 typename Backend::unsigned_types,
193 pred_type
194 >::type iter_type;
195 typedef typename mpl::end<typename Backend::unsigned_types>::type end_type;
196 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
197 };
198 template <class Val, class Backend>
199 struct canonical_imp<Val, Backend, mpl::int_<2> >
200 {
201 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
202 typedef typename mpl::find_if<
203 typename Backend::float_types,
204 pred_type
205 >::type iter_type;
206 typedef typename mpl::end<typename Backend::float_types>::type end_type;
207 typedef typename mpl::eval_if<boost::is_same<iter_type, end_type>, mpl::identity<Val>, mpl::deref<iter_type> >::type type;
208 };
209 template <class Val, class Backend>
210 struct canonical_imp<Val, Backend, mpl::int_<3> >
211 {
212 typedef const char* type;
213 };
214
215 template <class Val, class Backend>
216 struct canonical
217 {
218 typedef typename mpl::if_<
219 is_signed<Val>,
220 mpl::int_<0>,
221 typename mpl::if_<
222 is_unsigned<Val>,
223 mpl::int_<1>,
224 typename mpl::if_<
225 is_floating_point<Val>,
226 mpl::int_<2>,
227 typename mpl::if_<
228 mpl::or_<
229 is_convertible<Val, const char*>,
230 is_same<Val, std::string>
231 >,
232 mpl::int_<3>,
233 mpl::int_<4>
234 >::type
235 >::type
236 >::type
237 >::type tag_type;
238
239 typedef typename canonical_imp<Val, Backend, tag_type>::type type;
240 };
241
242 struct terminal{};
243 struct negate{};
244 struct plus{};
245 struct minus{};
246 struct multiplies{};
247 struct divides{};
248 struct modulus{};
249 struct shift_left{};
250 struct shift_right{};
251 struct bitwise_and{};
252 struct bitwise_or{};
253 struct bitwise_xor{};
254 struct bitwise_complement{};
255 struct add_immediates{};
256 struct subtract_immediates{};
257 struct multiply_immediates{};
258 struct divide_immediates{};
259 struct modulus_immediates{};
260 struct bitwise_and_immediates{};
261 struct bitwise_or_immediates{};
262 struct bitwise_xor_immediates{};
263 struct complement_immediates{};
264 struct function{};
265 struct multiply_add{};
266 struct multiply_subtract{};
267
268 template <class T>
269 struct backend_type;
270
271 template <class T, expression_template_option ExpressionTemplates>
272 struct backend_type<number<T, ExpressionTemplates> >
273 {
274 typedef T type;
275 };
276
277 template <class tag, class A1, class A2, class A3, class A4>
278 struct backend_type<expression<tag, A1, A2, A3, A4> >
279 {
280 typedef typename backend_type<typename expression<tag, A1, A2, A3, A4>::result_type>::type type;
281 };
282
283
284 template <class T1, class T2>
285 struct combine_expression
286 {
287 #ifdef BOOST_NO_CXX11_DECLTYPE
288 typedef typename mpl::if_c<(sizeof(T1() + T2()) == sizeof(T1)), T1, T2>::type type;
289 #else
290 typedef decltype(T1() + T2()) type;
291 #endif
292 };
293
294 template <class T1, expression_template_option ExpressionTemplates, class T2>
295 struct combine_expression<number<T1, ExpressionTemplates>, T2>
296 {
297 typedef number<T1, ExpressionTemplates> type;
298 };
299
300 template <class T1, class T2, expression_template_option ExpressionTemplates>
301 struct combine_expression<T1, number<T2, ExpressionTemplates> >
302 {
303 typedef number<T2, ExpressionTemplates> type;
304 };
305
306 template <class T, expression_template_option ExpressionTemplates>
307 struct combine_expression<number<T, ExpressionTemplates>, number<T, ExpressionTemplates> >
308 {
309 typedef number<T, ExpressionTemplates> type;
310 };
311
312 template <class T1, expression_template_option ExpressionTemplates1, class T2, expression_template_option ExpressionTemplates2>
313 struct combine_expression<number<T1, ExpressionTemplates1>, number<T2, ExpressionTemplates2> >
314 {
315 typedef typename mpl::if_c<
316 is_convertible<number<T2, ExpressionTemplates2>, number<T1, ExpressionTemplates2> >::value,
317 number<T1, ExpressionTemplates1>,
318 number<T2, ExpressionTemplates2>
319 >::type type;
320 };
321
322 template <class T>
323 struct arg_type
324 {
325 typedef expression<terminal, T> type;
326 };
327
328 template <class Tag, class Arg1, class Arg2, class Arg3, class Arg4>
329 struct arg_type<expression<Tag, Arg1, Arg2, Arg3, Arg4> >
330 {
331 typedef expression<Tag, Arg1, Arg2, Arg3, Arg4> type;
332 };
333
334 struct unmentionable
335 {
procboost::multiprecision::detail::unmentionable336 unmentionable* proc(){ return 0; }
337 };
338
339 typedef unmentionable* (unmentionable::*unmentionable_type)();
340
341 template <class T>
342 struct expression_storage
343 {
344 typedef const T& type;
345 };
346
347 template <class T>
348 struct expression_storage<T*>
349 {
350 typedef T* type;
351 };
352
353 template <class T>
354 struct expression_storage<const T*>
355 {
356 typedef const T* type;
357 };
358
359 template <class tag, class A1, class A2, class A3, class A4>
360 struct expression_storage<expression<tag, A1, A2, A3, A4> >
361 {
362 typedef expression<tag, A1, A2, A3, A4> type;
363 };
364
365 template<class tag, class Arg1>
366 struct expression<tag, Arg1, void, void, void>
367 {
368 typedef mpl::int_<1> arity;
369 typedef typename arg_type<Arg1>::type left_type;
370 typedef typename left_type::result_type left_result_type;
371 typedef typename left_type::result_type result_type;
372 typedef tag tag_type;
373
expressionboost::multiprecision::detail::expression374 explicit expression(const Arg1& a) : arg(a) {}
375
leftboost::multiprecision::detail::expression376 left_type left()const { return left_type(arg); }
377
left_refboost::multiprecision::detail::expression378 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg; }
379
380 static const unsigned depth = left_type::depth + 1;
381 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
382 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
383 //
384 // Horrible workaround for gcc-4.6.x which always prefers the template
385 // operator bool() rather than the non-template operator when converting to
386 // an arithmetic type:
387 //
388 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
operator Tboost::multiprecision::detail::expression389 explicit operator T ()const
390 {
391 result_type r(*this);
392 return static_cast<bool>(r);
393 }
394 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression395 explicit operator T ()const
396 {
397 return static_cast<T>(static_cast<result_type>(*this));
398 }
399 # else
400 template <class T, typename boost::disable_if_c<is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression401 explicit operator T()const
402 {
403 return static_cast<T>(static_cast<result_type>(*this));
404 }
operator boolboost::multiprecision::detail::expression405 BOOST_MP_FORCEINLINE explicit operator bool()const
406 {
407 result_type r(*this);
408 return static_cast<bool>(r);
409 }
operator voidboost::multiprecision::detail::expression410 explicit operator void()const {}
411 # endif
412 #else
operator unmentionable_typeboost::multiprecision::detail::expression413 operator unmentionable_type()const
414 {
415 result_type r(*this);
416 return r ? &unmentionable::proc : 0;
417 }
418 #endif
419
420 template <class T>
convert_toboost::multiprecision::detail::expression421 T convert_to()
422 {
423 result_type r(*this);
424 return r.template convert_to<T>();
425 }
426
427 private:
428 typename expression_storage<Arg1>::type arg;
429 expression& operator=(const expression&);
430 };
431
432 template<class Arg1>
433 struct expression<terminal, Arg1, void, void, void>
434 {
435 typedef mpl::int_<0> arity;
436 typedef Arg1 result_type;
437 typedef terminal tag_type;
438
expressionboost::multiprecision::detail::expression439 explicit expression(const Arg1& a) : arg(a) {}
440
valueboost::multiprecision::detail::expression441 const Arg1& value()const BOOST_NOEXCEPT { return arg; }
442
443 static const unsigned depth = 0;
444
445 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
446 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
447 //
448 // Horrible workaround for gcc-4.6.x which always prefers the template
449 // operator bool() rather than the non-template operator when converting to
450 // an arithmetic type:
451 //
452 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
operator Tboost::multiprecision::detail::expression453 explicit operator T ()const
454 {
455 result_type r(*this);
456 return static_cast<bool>(r);
457 }
458 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression459 explicit operator T ()const
460 {
461 return static_cast<T>(static_cast<result_type>(*this));
462 }
463 # else
464 template <class T, typename boost::disable_if_c<is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression465 explicit operator T()const
466 {
467 return static_cast<T>(static_cast<result_type>(*this));
468 }
operator boolboost::multiprecision::detail::expression469 BOOST_MP_FORCEINLINE explicit operator bool()const
470 {
471 result_type r(*this);
472 return static_cast<bool>(r);
473 }
operator voidboost::multiprecision::detail::expression474 explicit operator void()const {}
475 # endif
476 #else
operator unmentionable_typeboost::multiprecision::detail::expression477 operator unmentionable_type()const
478 {
479 return arg ? &unmentionable::proc : 0;
480 }
481 #endif
482
483 template <class T>
convert_toboost::multiprecision::detail::expression484 T convert_to()
485 {
486 result_type r(*this);
487 return r.template convert_to<T>();
488 }
489
490 private:
491 typename expression_storage<Arg1>::type arg;
492 expression& operator=(const expression&);
493 };
494
495 template <class tag, class Arg1, class Arg2>
496 struct expression<tag, Arg1, Arg2, void, void>
497 {
498 typedef mpl::int_<2> arity;
499 typedef typename arg_type<Arg1>::type left_type;
500 typedef typename arg_type<Arg2>::type right_type;
501 typedef typename left_type::result_type left_result_type;
502 typedef typename right_type::result_type right_result_type;
503 typedef typename combine_expression<left_result_type, right_result_type>::type result_type;
504 typedef tag tag_type;
505
expressionboost::multiprecision::detail::expression506 expression(const Arg1& a1, const Arg2& a2) : arg1(a1), arg2(a2) {}
507
leftboost::multiprecision::detail::expression508 left_type left()const { return left_type(arg1); }
rightboost::multiprecision::detail::expression509 right_type right()const { return right_type(arg2); }
left_refboost::multiprecision::detail::expression510 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
right_refboost::multiprecision::detail::expression511 const Arg2& right_ref()const BOOST_NOEXCEPT { return arg2; }
512
513 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
514 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
515 //
516 // Horrible workaround for gcc-4.6.x which always prefers the template
517 // operator bool() rather than the non-template operator when converting to
518 // an arithmetic type:
519 //
520 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
operator Tboost::multiprecision::detail::expression521 explicit operator T ()const
522 {
523 result_type r(*this);
524 return static_cast<bool>(r);
525 }
526 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression527 explicit operator T ()const
528 {
529 return static_cast<T>(static_cast<result_type>(*this));
530 }
531 # else
532 template <class T, typename boost::disable_if_c<is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression533 explicit operator T()const
534 {
535 return static_cast<T>(static_cast<result_type>(*this));
536 }
operator boolboost::multiprecision::detail::expression537 BOOST_MP_FORCEINLINE explicit operator bool()const
538 {
539 result_type r(*this);
540 return static_cast<bool>(r);
541 }
operator voidboost::multiprecision::detail::expression542 explicit operator void()const {}
543 # endif
544 #else
operator unmentionable_typeboost::multiprecision::detail::expression545 operator unmentionable_type()const
546 {
547 result_type r(*this);
548 return r ? &unmentionable::proc : 0;
549 }
550 #endif
551 template <class T>
convert_toboost::multiprecision::detail::expression552 T convert_to()
553 {
554 result_type r(*this);
555 return r.template convert_to<T>();
556 }
557
558 static const unsigned left_depth = left_type::depth + 1;
559 static const unsigned right_depth = right_type::depth + 1;
560 static const unsigned depth = left_depth > right_depth ? left_depth : right_depth;
561 private:
562 typename expression_storage<Arg1>::type arg1;
563 typename expression_storage<Arg2>::type arg2;
564 expression& operator=(const expression&);
565 };
566
567 template <class tag, class Arg1, class Arg2, class Arg3>
568 struct expression<tag, Arg1, Arg2, Arg3, void>
569 {
570 typedef mpl::int_<3> arity;
571 typedef typename arg_type<Arg1>::type left_type;
572 typedef typename arg_type<Arg2>::type middle_type;
573 typedef typename arg_type<Arg3>::type right_type;
574 typedef typename left_type::result_type left_result_type;
575 typedef typename middle_type::result_type middle_result_type;
576 typedef typename right_type::result_type right_result_type;
577 typedef typename combine_expression<
578 left_result_type,
579 typename combine_expression<right_result_type, middle_result_type>::type
580 >::type result_type;
581 typedef tag tag_type;
582
expressionboost::multiprecision::detail::expression583 expression(const Arg1& a1, const Arg2& a2, const Arg3& a3) : arg1(a1), arg2(a2), arg3(a3) {}
584
leftboost::multiprecision::detail::expression585 left_type left()const { return left_type(arg1); }
middleboost::multiprecision::detail::expression586 middle_type middle()const { return middle_type(arg2); }
rightboost::multiprecision::detail::expression587 right_type right()const { return right_type(arg3); }
left_refboost::multiprecision::detail::expression588 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
middle_refboost::multiprecision::detail::expression589 const Arg2& middle_ref()const BOOST_NOEXCEPT { return arg2; }
right_refboost::multiprecision::detail::expression590 const Arg3& right_ref()const BOOST_NOEXCEPT { return arg3; }
591
592 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
593 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
594 //
595 // Horrible workaround for gcc-4.6.x which always prefers the template
596 // operator bool() rather than the non-template operator when converting to
597 // an arithmetic type:
598 //
599 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
operator Tboost::multiprecision::detail::expression600 explicit operator T ()const
601 {
602 result_type r(*this);
603 return static_cast<bool>(r);
604 }
605 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression606 explicit operator T ()const
607 {
608 return static_cast<T>(static_cast<result_type>(*this));
609 }
610 # else
611 template <class T, typename boost::disable_if_c<is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression612 explicit operator T()const
613 {
614 return static_cast<T>(static_cast<result_type>(*this));
615 }
operator boolboost::multiprecision::detail::expression616 BOOST_MP_FORCEINLINE explicit operator bool()const
617 {
618 result_type r(*this);
619 return static_cast<bool>(r);
620 }
operator voidboost::multiprecision::detail::expression621 explicit operator void()const {}
622 # endif
623 #else
operator unmentionable_typeboost::multiprecision::detail::expression624 operator unmentionable_type()const
625 {
626 result_type r(*this);
627 return r ? &unmentionable::proc : 0;
628 }
629 #endif
630 template <class T>
convert_toboost::multiprecision::detail::expression631 T convert_to()
632 {
633 result_type r(*this);
634 return r.template convert_to<T>();
635 }
636
637 static const unsigned left_depth = left_type::depth + 1;
638 static const unsigned middle_depth = middle_type::depth + 1;
639 static const unsigned right_depth = right_type::depth + 1;
640 static const unsigned depth = left_depth > right_depth ? (left_depth > middle_depth ? left_depth : middle_depth) : (right_depth > middle_depth ? right_depth : middle_depth);
641 private:
642 typename expression_storage<Arg1>::type arg1;
643 typename expression_storage<Arg2>::type arg2;
644 typename expression_storage<Arg3>::type arg3;
645 expression& operator=(const expression&);
646 };
647
648 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
649 struct expression
650 {
651 typedef mpl::int_<4> arity;
652 typedef typename arg_type<Arg1>::type left_type;
653 typedef typename arg_type<Arg2>::type left_middle_type;
654 typedef typename arg_type<Arg3>::type right_middle_type;
655 typedef typename arg_type<Arg4>::type right_type;
656 typedef typename left_type::result_type left_result_type;
657 typedef typename left_middle_type::result_type left_middle_result_type;
658 typedef typename right_middle_type::result_type right_middle_result_type;
659 typedef typename right_type::result_type right_result_type;
660 typedef typename combine_expression<
661 typename combine_expression<
662 typename combine_expression<left_result_type, left_middle_result_type>::type,
663 right_middle_result_type
664 >::type,
665 right_result_type
666 >::type result_type;
667 typedef tag tag_type;
668
expressionboost::multiprecision::detail::expression669 expression(const Arg1& a1, const Arg2& a2, const Arg3& a3, const Arg4& a4) : arg1(a1), arg2(a2), arg3(a3), arg4(a4) {}
670
leftboost::multiprecision::detail::expression671 left_type left()const { return left_type(arg1); }
left_middleboost::multiprecision::detail::expression672 left_middle_type left_middle()const { return left_middle_type(arg2); }
right_middleboost::multiprecision::detail::expression673 right_middle_type right_middle()const { return right_middle_type(arg3); }
rightboost::multiprecision::detail::expression674 right_type right()const { return right_type(arg4); }
left_refboost::multiprecision::detail::expression675 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
left_middle_refboost::multiprecision::detail::expression676 const Arg2& left_middle_ref()const BOOST_NOEXCEPT { return arg2; }
right_middle_refboost::multiprecision::detail::expression677 const Arg3& right_middle_ref()const BOOST_NOEXCEPT { return arg3; }
right_refboost::multiprecision::detail::expression678 const Arg4& right_ref()const BOOST_NOEXCEPT { return arg4; }
679
680 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
681 # if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
682 //
683 // Horrible workaround for gcc-4.6.x which always prefers the template
684 // operator bool() rather than the non-template operator when converting to
685 // an arithmetic type:
686 //
687 template <class T, typename boost::enable_if<is_same<T, bool>, int>::type = 0>
operator Tboost::multiprecision::detail::expression688 explicit operator T ()const
689 {
690 result_type r(*this);
691 return static_cast<bool>(r);
692 }
693 template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value || is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression694 explicit operator T ()const
695 {
696 return static_cast<T>(static_cast<result_type>(*this));
697 }
698 # else
699 template <class T, typename boost::disable_if_c<is_number<T>::value, int>::type = 0>
operator Tboost::multiprecision::detail::expression700 explicit operator T()const
701 {
702 return static_cast<T>(static_cast<result_type>(*this));
703 }
operator boolboost::multiprecision::detail::expression704 BOOST_MP_FORCEINLINE explicit operator bool()const
705 {
706 result_type r(*this);
707 return static_cast<bool>(r);
708 }
operator voidboost::multiprecision::detail::expression709 explicit operator void()const {}
710 # endif
711 #else
operator unmentionable_typeboost::multiprecision::detail::expression712 operator unmentionable_type()const
713 {
714 result_type r(*this);
715 return r ? &unmentionable::proc : 0;
716 }
717 #endif
718 template <class T>
convert_toboost::multiprecision::detail::expression719 T convert_to()
720 {
721 result_type r(*this);
722 return r.template convert_to<T>();
723 }
724
725 static const unsigned left_depth = left_type::depth + 1;
726 static const unsigned left_middle_depth = left_middle_type::depth + 1;
727 static const unsigned right_middle_depth = right_middle_type::depth + 1;
728 static const unsigned right_depth = right_type::depth + 1;
729
730 static const unsigned left_max_depth = left_depth > left_middle_depth ? left_depth : left_middle_depth;
731 static const unsigned right_max_depth = right_depth > right_middle_depth ? right_depth : right_middle_depth;
732
733 static const unsigned depth = left_max_depth > right_max_depth ? left_max_depth : right_max_depth;
734 private:
735 typename expression_storage<Arg1>::type arg1;
736 typename expression_storage<Arg2>::type arg2;
737 typename expression_storage<Arg3>::type arg3;
738 typename expression_storage<Arg4>::type arg4;
739 expression& operator=(const expression&);
740 };
741
742 template <class T>
743 struct digits2
744 {
745 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
746 BOOST_STATIC_ASSERT((std::numeric_limits<T>::radix == 2) || (std::numeric_limits<T>::radix == 10));
747 // If we really have so many digits that this fails, then we're probably going to hit other problems anyway:
748 BOOST_STATIC_ASSERT(LONG_MAX / 1000 > (std::numeric_limits<T>::digits + 1));
749 static const long value = std::numeric_limits<T>::radix == 10 ? (((std::numeric_limits<T>::digits + 1) * 1000L) / 301L) : std::numeric_limits<T>::digits;
750 };
751
752 #ifndef BOOST_MP_MIN_EXPONENT_DIGITS
753 #ifdef _MSC_VER
754 # define BOOST_MP_MIN_EXPONENT_DIGITS 2
755 #else
756 # define BOOST_MP_MIN_EXPONENT_DIGITS 2
757 #endif
758 #endif
759
760 template <class S>
format_float_string(S & str,boost::intmax_t my_exp,boost::intmax_t digits,std::ios_base::fmtflags f,bool iszero)761 void format_float_string(S& str, boost::intmax_t my_exp, boost::intmax_t digits, std::ios_base::fmtflags f, bool iszero)
762 {
763 typedef typename S::size_type size_type;
764 bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
765 bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
766 bool showpoint = (f & std::ios_base::showpoint) == std::ios_base::showpoint;
767 bool showpos = (f & std::ios_base::showpos) == std::ios_base::showpos;
768
769 bool neg = str.size() && (str[0] == '-');
770
771 if(neg)
772 str.erase(0, 1);
773
774 if(digits == 0)
775 {
776 digits = (std::max)(str.size(), size_type(16));
777 }
778
779 if(iszero || str.empty() || (str.find_first_not_of('0') == S::npos))
780 {
781 // We will be printing zero, even though the value might not
782 // actually be zero (it just may have been rounded to zero).
783 str = "0";
784 if(scientific || fixed)
785 {
786 str.append(1, '.');
787 str.append(size_type(digits), '0');
788 if(scientific)
789 str.append("e+00");
790 }
791 else
792 {
793 if(showpoint)
794 {
795 str.append(1, '.');
796 if(digits > 1)
797 str.append(size_type(digits - 1), '0');
798 }
799 }
800 if(neg)
801 str.insert(static_cast<std::string::size_type>(0), 1, '-');
802 else if(showpos)
803 str.insert(static_cast<std::string::size_type>(0), 1, '+');
804 return;
805 }
806
807 if(!fixed && !scientific && !showpoint)
808 {
809 //
810 // Suppress trailing zeros:
811 //
812 std::string::iterator pos = str.end();
813 while(pos != str.begin() && *--pos == '0'){}
814 if(pos != str.end())
815 ++pos;
816 str.erase(pos, str.end());
817 if(str.empty())
818 str = '0';
819 }
820 else if(!fixed || (my_exp >= 0))
821 {
822 //
823 // Pad out the end with zero's if we need to:
824 //
825 boost::intmax_t chars = str.size();
826 chars = digits - chars;
827 if(scientific)
828 ++chars;
829 if(chars > 0)
830 {
831 str.append(static_cast<std::string::size_type>(chars), '0');
832 }
833 }
834
835 if(fixed || (!scientific && (my_exp >= -4) && (my_exp < digits)))
836 {
837 if(1 + my_exp > static_cast<boost::intmax_t>(str.size()))
838 {
839 // Just pad out the end with zeros:
840 str.append(static_cast<std::string::size_type>(1 + my_exp - str.size()), '0');
841 if(showpoint || fixed)
842 str.append(".");
843 }
844 else if(my_exp + 1 < static_cast<boost::intmax_t>(str.size()))
845 {
846 if(my_exp < 0)
847 {
848 str.insert(static_cast<std::string::size_type>(0), static_cast<std::string::size_type>(-1 - my_exp), '0');
849 str.insert(static_cast<std::string::size_type>(0), "0.");
850 }
851 else
852 {
853 // Insert the decimal point:
854 str.insert(static_cast<std::string::size_type>(my_exp + 1), 1, '.');
855 }
856 }
857 else if(showpoint || fixed) // we have exactly the digits we require to left of the point
858 str += ".";
859
860 if(fixed)
861 {
862 // We may need to add trailing zeros:
863 boost::intmax_t l = str.find('.') + 1;
864 l = digits - (str.size() - l);
865 if(l > 0)
866 str.append(size_type(l), '0');
867 }
868 }
869 else
870 {
871 BOOST_MP_USING_ABS
872 // Scientific format:
873 if(showpoint || (str.size() > 1))
874 str.insert(static_cast<std::string::size_type>(1u), 1, '.');
875 str.append(static_cast<std::string::size_type>(1u), 'e');
876 S e = boost::lexical_cast<S>(abs(my_exp));
877 if(e.size() < BOOST_MP_MIN_EXPONENT_DIGITS)
878 e.insert(static_cast<std::string::size_type>(0), BOOST_MP_MIN_EXPONENT_DIGITS - e.size(), '0');
879 if(my_exp < 0)
880 e.insert(static_cast<std::string::size_type>(0), 1, '-');
881 else
882 e.insert(static_cast<std::string::size_type>(0), 1, '+');
883 str.append(e);
884 }
885 if(neg)
886 str.insert(static_cast<std::string::size_type>(0), 1, '-');
887 else if(showpos)
888 str.insert(static_cast<std::string::size_type>(0), 1, '+');
889 }
890
891 template <class V>
check_shift_range(V val,const mpl::true_ &,const mpl::true_ &)892 void check_shift_range(V val, const mpl::true_&, const mpl::true_&)
893 {
894 if(val > (std::numeric_limits<std::size_t>::max)())
895 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
896 if(val < 0)
897 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
898 }
899 template <class V>
check_shift_range(V val,const mpl::false_ &,const mpl::true_ &)900 void check_shift_range(V val, const mpl::false_&, const mpl::true_&)
901 {
902 if(val < 0)
903 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
904 }
905 template <class V>
check_shift_range(V val,const mpl::true_ &,const mpl::false_ &)906 void check_shift_range(V val, const mpl::true_&, const mpl::false_&)
907 {
908 if(val > (std::numeric_limits<std::size_t>::max)())
909 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
910 }
911 template <class V>
check_shift_range(V,const mpl::false_ &,const mpl::false_ &)912 void check_shift_range(V, const mpl::false_&, const mpl::false_&) BOOST_NOEXCEPT{}
913
914 } // namespace detail
915
916 //
917 // Traits class, lets us know what kind of number we have, defaults to a floating point type:
918 //
919 enum number_category_type
920 {
921 number_kind_unknown = -1,
922 number_kind_integer = 0,
923 number_kind_floating_point = 1,
924 number_kind_rational = 2,
925 number_kind_fixed_point = 3
926 };
927
928 template <class Num>
929 struct number_category : public mpl::int_<std::numeric_limits<Num>::is_integer ? number_kind_integer : (std::numeric_limits<Num>::max_exponent ? number_kind_floating_point : number_kind_unknown)> {};
930 template <class Backend, expression_template_option ExpressionTemplates>
931 struct number_category<number<Backend, ExpressionTemplates> > : public number_category<Backend>{};
932 template <class tag, class A1, class A2, class A3, class A4>
933 struct number_category<detail::expression<tag, A1, A2, A3, A4> > : public number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>{};
934
935 template <class T>
936 struct component_type;
937 template <class T, expression_template_option ExpressionTemplates>
938 struct component_type<number<T, ExpressionTemplates> > : public component_type<T>{};
939 template <class tag, class A1, class A2, class A3, class A4>
940 struct component_type<detail::expression<tag, A1, A2, A3, A4> > : public component_type<typename detail::expression<tag, A1, A2, A3, A4>::result_type>{};
941
942 template <class T>
943 struct is_unsigned_number : public mpl::false_{};
944 template <class Backend, expression_template_option ExpressionTemplates>
945 struct is_unsigned_number<number<Backend, ExpressionTemplates> > : public is_unsigned_number<Backend> {};
946 template <class T>
947 struct is_signed_number : public mpl::bool_<!is_unsigned_number<T>::value> {};
948 template <class T>
949 struct is_interval_number : public mpl::false_ {};
950 template <class Backend, expression_template_option ExpressionTemplates>
951 struct is_interval_number<number<Backend, ExpressionTemplates> > : public is_interval_number<Backend>{};
952
953 }} // namespaces
954
955 namespace boost{ namespace math{ namespace tools{
956
957 template <class T>
958 struct promote_arg;
959
960 template <class tag, class A1, class A2, class A3, class A4>
961 struct promote_arg<boost::multiprecision::detail::expression<tag, A1, A2, A3, A4> >
962 {
963 typedef typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type type;
964 };
965
966 template <class R, class B, boost::multiprecision::expression_template_option ET>
real_cast(const boost::multiprecision::number<B,ET> & val)967 inline R real_cast(const boost::multiprecision::number<B, ET>& val)
968 {
969 return val.template convert_to<R>();
970 }
971
972 template <class R, class tag, class A1, class A2, class A3, class A4>
real_cast(const boost::multiprecision::detail::expression<tag,A1,A2,A3,A4> & val)973 inline R real_cast(const boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>& val)
974 {
975 typedef typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type val_type;
976 return val_type(val).template convert_to<R>();
977 }
978
979
980 }
981
982 namespace constants{
983
984 template <class T>
985 struct is_explicitly_convertible_from_string;
986
987 template <class B, boost::multiprecision::expression_template_option ET>
988 struct is_explicitly_convertible_from_string<boost::multiprecision::number<B, ET> >
989 {
990 static const bool value = true;
991 };
992
993 }
994
995 }}
996
997 #ifdef BOOST_MSVC
998 # pragma warning(pop)
999 #endif
1000
1001 #endif // BOOST_MATH_BIG_NUM_BASE_HPP
1002
1003
1004