comparison DEPENDENCIES/generic/include/boost/multiprecision/detail/number_base.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
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 namespace boost{ namespace multiprecision{
29
30 enum expression_template_option
31 {
32 et_off = 0,
33 et_on = 1
34 };
35
36 template <class Backend>
37 struct expression_template_default
38 {
39 static const expression_template_option value = et_on;
40 };
41
42 template <class Backend, expression_template_option ExpressionTemplates = expression_template_default<Backend>::value>
43 class number;
44
45 template <class T>
46 struct is_number : public mpl::false_ {};
47
48 template <class Backend, expression_template_option ExpressionTemplates>
49 struct is_number<number<Backend, ExpressionTemplates> > : public mpl::true_ {};
50
51 namespace detail{
52
53 // Forward-declare an expression wrapper
54 template<class tag, class Arg1 = void, class Arg2 = void, class Arg3 = void, class Arg4 = void>
55 struct expression;
56
57 } // namespace detail
58
59 template <class T>
60 struct is_number_expression : public mpl::false_ {};
61
62 template<class tag, class Arg1, class Arg2, class Arg3, class Arg4>
63 struct is_number_expression<detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > : public mpl::true_ {};
64
65 template <class T, class Num>
66 struct is_compatible_arithmetic_type
67 : public mpl::bool_<
68 is_convertible<T, Num>::value
69 && !is_same<T, Num>::value
70 && !is_number_expression<T>::value>
71 {};
72
73 namespace detail{
74 //
75 // Workaround for missing abs(long long) and abs(__int128) on some compilers:
76 //
77 template <class T>
78 typename enable_if_c<(is_signed<T>::value || is_floating_point<T>::value), T>::type abs(T t) BOOST_NOEXCEPT
79 {
80 return t < 0 ? -t : t;
81 }
82 template <class T>
83 typename enable_if_c<(is_unsigned<T>::value), T>::type abs(T t) BOOST_NOEXCEPT
84 {
85 return t;
86 }
87
88 #define BOOST_MP_USING_ABS using boost::multiprecision::detail::abs;
89
90 //
91 // Move support:
92 //
93 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
94 # define BOOST_MP_MOVE(x) std::move(x)
95 #else
96 # define BOOST_MP_MOVE(x) x
97 #endif
98
99 template <class T>
100 struct bits_of
101 {
102 BOOST_STATIC_ASSERT(is_integral<T>::value || is_enum<T>::value || std::numeric_limits<T>::is_specialized);
103 static const unsigned value =
104 std::numeric_limits<T>::is_specialized ?
105 std::numeric_limits<T>::digits
106 : sizeof(T) * CHAR_BIT - (is_signed<T>::value ? 1 : 0);
107 };
108
109 template <int b>
110 struct has_enough_bits
111 {
112 template <class T>
113 struct type : public mpl::bool_<bits_of<T>::value>= b>{};
114 };
115
116 template <class Val, class Backend, class Tag>
117 struct canonical_imp
118 {
119 typedef typename remove_cv<typename decay<const Val>::type>::type type;
120 };
121 template <class B, class Backend, class Tag>
122 struct canonical_imp<number<B, et_on>, Backend, Tag>
123 {
124 typedef B type;
125 };
126 template <class B, class Backend, class Tag>
127 struct canonical_imp<number<B, et_off>, Backend, Tag>
128 {
129 typedef B type;
130 };
131 template <class Val, class Backend>
132 struct canonical_imp<Val, Backend, mpl::int_<0> >
133 {
134 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
135 typedef typename mpl::find_if<
136 typename Backend::signed_types,
137 pred_type
138 >::type iter_type;
139 typedef typename mpl::deref<iter_type>::type type;
140 };
141 template <class Val, class Backend>
142 struct canonical_imp<Val, Backend, mpl::int_<1> >
143 {
144 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
145 typedef typename mpl::find_if<
146 typename Backend::unsigned_types,
147 pred_type
148 >::type iter_type;
149 typedef typename mpl::deref<iter_type>::type type;
150 };
151 template <class Val, class Backend>
152 struct canonical_imp<Val, Backend, mpl::int_<2> >
153 {
154 typedef typename has_enough_bits<bits_of<Val>::value>::template type<mpl::_> pred_type;
155 typedef typename mpl::find_if<
156 typename Backend::float_types,
157 pred_type
158 >::type iter_type;
159 typedef typename mpl::deref<iter_type>::type type;
160 };
161 template <class Val, class Backend>
162 struct canonical_imp<Val, Backend, mpl::int_<3> >
163 {
164 typedef const char* type;
165 };
166
167 template <class Val, class Backend>
168 struct canonical
169 {
170 typedef typename mpl::if_<
171 is_signed<Val>,
172 mpl::int_<0>,
173 typename mpl::if_<
174 is_unsigned<Val>,
175 mpl::int_<1>,
176 typename mpl::if_<
177 is_floating_point<Val>,
178 mpl::int_<2>,
179 typename mpl::if_<
180 mpl::or_<
181 is_convertible<Val, const char*>,
182 is_same<Val, std::string>
183 >,
184 mpl::int_<3>,
185 mpl::int_<4>
186 >::type
187 >::type
188 >::type
189 >::type tag_type;
190
191 typedef typename canonical_imp<Val, Backend, tag_type>::type type;
192 };
193
194 struct terminal{};
195 struct negate{};
196 struct plus{};
197 struct minus{};
198 struct multiplies{};
199 struct divides{};
200 struct modulus{};
201 struct shift_left{};
202 struct shift_right{};
203 struct bitwise_and{};
204 struct bitwise_or{};
205 struct bitwise_xor{};
206 struct bitwise_complement{};
207 struct add_immediates{};
208 struct subtract_immediates{};
209 struct multiply_immediates{};
210 struct divide_immediates{};
211 struct modulus_immediates{};
212 struct bitwise_and_immediates{};
213 struct bitwise_or_immediates{};
214 struct bitwise_xor_immediates{};
215 struct complement_immediates{};
216 struct function{};
217 struct multiply_add{};
218 struct multiply_subtract{};
219
220 template <class T>
221 struct backend_type;
222
223 template <class T, expression_template_option ExpressionTemplates>
224 struct backend_type<number<T, ExpressionTemplates> >
225 {
226 typedef T type;
227 };
228
229 template <class tag, class A1, class A2, class A3, class A4>
230 struct backend_type<expression<tag, A1, A2, A3, A4> >
231 {
232 typedef typename backend_type<typename expression<tag, A1, A2, A3, A4>::result_type>::type type;
233 };
234
235
236 template <class T1, class T2>
237 struct combine_expression
238 {
239 #ifdef BOOST_NO_CXX11_DECLTYPE
240 typedef typename mpl::if_c<(sizeof(T1() + T2()) == sizeof(T1)), T1, T2>::type type;
241 #else
242 typedef decltype(T1() + T2()) type;
243 #endif
244 };
245
246 template <class T1, expression_template_option ExpressionTemplates, class T2>
247 struct combine_expression<number<T1, ExpressionTemplates>, T2>
248 {
249 typedef number<T1, ExpressionTemplates> type;
250 };
251
252 template <class T1, class T2, expression_template_option ExpressionTemplates>
253 struct combine_expression<T1, number<T2, ExpressionTemplates> >
254 {
255 typedef number<T2, ExpressionTemplates> type;
256 };
257
258 template <class T, expression_template_option ExpressionTemplates>
259 struct combine_expression<number<T, ExpressionTemplates>, number<T, ExpressionTemplates> >
260 {
261 typedef number<T, ExpressionTemplates> type;
262 };
263
264 template <class T1, expression_template_option ExpressionTemplates1, class T2, expression_template_option ExpressionTemplates2>
265 struct combine_expression<number<T1, ExpressionTemplates1>, number<T2, ExpressionTemplates2> >
266 {
267 typedef typename mpl::if_c<
268 is_convertible<number<T2, ExpressionTemplates2>, number<T1, ExpressionTemplates2> >::value,
269 number<T1, ExpressionTemplates1>,
270 number<T2, ExpressionTemplates2>
271 >::type type;
272 };
273
274 template <class T>
275 struct arg_type
276 {
277 typedef expression<terminal, T> type;
278 };
279
280 template <class Tag, class Arg1, class Arg2, class Arg3, class Arg4>
281 struct arg_type<expression<Tag, Arg1, Arg2, Arg3, Arg4> >
282 {
283 typedef expression<Tag, Arg1, Arg2, Arg3, Arg4> type;
284 };
285
286 struct unmentionable
287 {
288 unmentionable* proc(){ return 0; }
289 };
290
291 typedef unmentionable* (unmentionable::*unmentionable_type)();
292
293 template <class T>
294 struct expression_storage
295 {
296 typedef const T& type;
297 };
298
299 template <class T>
300 struct expression_storage<T*>
301 {
302 typedef T* type;
303 };
304
305 template <class T>
306 struct expression_storage<const T*>
307 {
308 typedef const T* type;
309 };
310
311 template <class tag, class A1, class A2, class A3, class A4>
312 struct expression_storage<expression<tag, A1, A2, A3, A4> >
313 {
314 typedef expression<tag, A1, A2, A3, A4> type;
315 };
316
317 template<class tag, class Arg1>
318 struct expression<tag, Arg1, void, void, void>
319 {
320 typedef mpl::int_<1> arity;
321 typedef typename arg_type<Arg1>::type left_type;
322 typedef typename left_type::result_type left_result_type;
323 typedef typename left_type::result_type result_type;
324 typedef tag tag_type;
325
326 explicit expression(const Arg1& a) : arg(a) {}
327
328 left_type left()const { return left_type(arg); }
329
330 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg; }
331
332 static const unsigned depth = left_type::depth + 1;
333 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
334 explicit operator bool()const
335 {
336 result_type r(*this);
337 return static_cast<bool>(r);
338 }
339 #else
340 operator unmentionable_type()const
341 {
342 result_type r(*this);
343 return r ? &unmentionable::proc : 0;
344 }
345 #endif
346
347 private:
348 typename expression_storage<Arg1>::type arg;
349 expression& operator=(const expression&);
350 };
351
352 template<class Arg1>
353 struct expression<terminal, Arg1, void, void, void>
354 {
355 typedef mpl::int_<0> arity;
356 typedef Arg1 result_type;
357 typedef terminal tag_type;
358
359 explicit expression(const Arg1& a) : arg(a) {}
360
361 const Arg1& value()const BOOST_NOEXCEPT { return arg; }
362
363 static const unsigned depth = 0;
364
365 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
366 explicit operator bool()const
367 {
368 return static_cast<bool>(arg);
369 }
370 #else
371 operator unmentionable_type()const
372 {
373 return arg ? &unmentionable::proc : 0;
374 }
375 #endif
376
377 private:
378 typename expression_storage<Arg1>::type arg;
379 expression& operator=(const expression&);
380 };
381
382 template <class tag, class Arg1, class Arg2>
383 struct expression<tag, Arg1, Arg2, void, void>
384 {
385 typedef mpl::int_<2> arity;
386 typedef typename arg_type<Arg1>::type left_type;
387 typedef typename arg_type<Arg2>::type right_type;
388 typedef typename left_type::result_type left_result_type;
389 typedef typename right_type::result_type right_result_type;
390 typedef typename combine_expression<left_result_type, right_result_type>::type result_type;
391 typedef tag tag_type;
392
393 expression(const Arg1& a1, const Arg2& a2) : arg1(a1), arg2(a2) {}
394
395 left_type left()const { return left_type(arg1); }
396 right_type right()const { return right_type(arg2); }
397 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
398 const Arg2& right_ref()const BOOST_NOEXCEPT { return arg2; }
399
400 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
401 explicit operator bool()const
402 {
403 result_type r(*this);
404 return static_cast<bool>(r);
405 }
406 #else
407 operator unmentionable_type()const
408 {
409 result_type r(*this);
410 return r ? &unmentionable::proc : 0;
411 }
412 #endif
413 static const unsigned left_depth = left_type::depth + 1;
414 static const unsigned right_depth = right_type::depth + 1;
415 static const unsigned depth = left_depth > right_depth ? left_depth : right_depth;
416 private:
417 typename expression_storage<Arg1>::type arg1;
418 typename expression_storage<Arg2>::type arg2;
419 expression& operator=(const expression&);
420 };
421
422 template <class tag, class Arg1, class Arg2, class Arg3>
423 struct expression<tag, Arg1, Arg2, Arg3, void>
424 {
425 typedef mpl::int_<3> arity;
426 typedef typename arg_type<Arg1>::type left_type;
427 typedef typename arg_type<Arg2>::type middle_type;
428 typedef typename arg_type<Arg3>::type right_type;
429 typedef typename left_type::result_type left_result_type;
430 typedef typename middle_type::result_type middle_result_type;
431 typedef typename right_type::result_type right_result_type;
432 typedef typename combine_expression<
433 left_result_type,
434 typename combine_expression<right_result_type, middle_result_type>::type
435 >::type result_type;
436 typedef tag tag_type;
437
438 expression(const Arg1& a1, const Arg2& a2, const Arg3& a3) : arg1(a1), arg2(a2), arg3(a3) {}
439
440 left_type left()const { return left_type(arg1); }
441 middle_type middle()const { return middle_type(arg2); }
442 right_type right()const { return right_type(arg3); }
443 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
444 const Arg2& middle_ref()const BOOST_NOEXCEPT { return arg2; }
445 const Arg3& right_ref()const BOOST_NOEXCEPT { return arg3; }
446
447 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
448 explicit operator bool()const
449 {
450 result_type r(*this);
451 return static_cast<bool>(r);
452 }
453 #else
454 operator unmentionable_type()const
455 {
456 result_type r(*this);
457 return r ? &unmentionable::proc : 0;
458 }
459 #endif
460 static const unsigned left_depth = left_type::depth + 1;
461 static const unsigned middle_depth = middle_type::depth + 1;
462 static const unsigned right_depth = right_type::depth + 1;
463 static const unsigned depth = left_depth > right_depth ? (left_depth > middle_depth ? left_depth : middle_depth) : (right_depth > middle_depth ? right_depth : middle_depth);
464 private:
465 typename expression_storage<Arg1>::type arg1;
466 typename expression_storage<Arg2>::type arg2;
467 typename expression_storage<Arg3>::type arg3;
468 expression& operator=(const expression&);
469 };
470
471 template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
472 struct expression
473 {
474 typedef mpl::int_<4> arity;
475 typedef typename arg_type<Arg1>::type left_type;
476 typedef typename arg_type<Arg2>::type left_middle_type;
477 typedef typename arg_type<Arg3>::type right_middle_type;
478 typedef typename arg_type<Arg4>::type right_type;
479 typedef typename left_type::result_type left_result_type;
480 typedef typename left_middle_type::result_type left_middle_result_type;
481 typedef typename right_middle_type::result_type right_middle_result_type;
482 typedef typename right_type::result_type right_result_type;
483 typedef typename combine_expression<
484 typename combine_expression<
485 typename combine_expression<left_result_type, left_middle_result_type>::type,
486 right_middle_result_type
487 >::type,
488 right_result_type
489 >::type result_type;
490 typedef tag tag_type;
491
492 expression(const Arg1& a1, const Arg2& a2, const Arg3& a3, const Arg4& a4) : arg1(a1), arg2(a2), arg3(a3), arg4(a4) {}
493
494 left_type left()const { return left_type(arg1); }
495 left_middle_type left_middle()const { return left_middle_type(arg2); }
496 right_middle_type right_middle()const { return right_middle_type(arg3); }
497 right_type right()const { return right_type(arg4); }
498 const Arg1& left_ref()const BOOST_NOEXCEPT { return arg1; }
499 const Arg2& left_middle_ref()const BOOST_NOEXCEPT { return arg2; }
500 const Arg3& right_middle_ref()const BOOST_NOEXCEPT { return arg3; }
501 const Arg4& right_ref()const BOOST_NOEXCEPT { return arg4; }
502
503 #ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
504 explicit operator bool()const
505 {
506 result_type r(*this);
507 return static_cast<bool>(r);
508 }
509 #else
510 operator unmentionable_type()const
511 {
512 result_type r(*this);
513 return r ? &unmentionable::proc : 0;
514 }
515 #endif
516 static const unsigned left_depth = left_type::depth + 1;
517 static const unsigned left_middle_depth = left_middle_type::depth + 1;
518 static const unsigned right_middle_depth = right_middle_type::depth + 1;
519 static const unsigned right_depth = right_type::depth + 1;
520
521 static const unsigned left_max_depth = left_depth > left_middle_depth ? left_depth : left_middle_depth;
522 static const unsigned right_max_depth = right_depth > right_middle_depth ? right_depth : right_middle_depth;
523
524 static const unsigned depth = left_max_depth > right_max_depth ? left_max_depth : right_max_depth;
525 private:
526 typename expression_storage<Arg1>::type arg1;
527 typename expression_storage<Arg2>::type arg2;
528 typename expression_storage<Arg3>::type arg3;
529 typename expression_storage<Arg4>::type arg4;
530 expression& operator=(const expression&);
531 };
532
533 template <class T>
534 struct digits2
535 {
536 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
537 BOOST_STATIC_ASSERT((std::numeric_limits<T>::radix == 2) || (std::numeric_limits<T>::radix == 10));
538 // If we really have so many digits that this fails, then we're probably going to hit other problems anyway:
539 BOOST_STATIC_ASSERT(LONG_MAX / 1000 > (std::numeric_limits<T>::digits + 1));
540 static const long value = std::numeric_limits<T>::radix == 10 ? (((std::numeric_limits<T>::digits + 1) * 1000L) / 301L) : std::numeric_limits<T>::digits;
541 };
542
543 #ifndef BOOST_MP_MIN_EXPONENT_DIGITS
544 #ifdef _MSC_VER
545 # define BOOST_MP_MIN_EXPONENT_DIGITS 2
546 #else
547 # define BOOST_MP_MIN_EXPONENT_DIGITS 2
548 #endif
549 #endif
550
551 template <class S>
552 void format_float_string(S& str, boost::intmax_t my_exp, boost::intmax_t digits, std::ios_base::fmtflags f, bool iszero)
553 {
554 typedef typename S::size_type size_type;
555 bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
556 bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
557 bool showpoint = (f & std::ios_base::showpoint) == std::ios_base::showpoint;
558 bool showpos = (f & std::ios_base::showpos) == std::ios_base::showpos;
559
560 bool neg = str.size() && (str[0] == '-');
561
562 if(neg)
563 str.erase(0, 1);
564
565 if(digits == 0)
566 {
567 digits = (std::max)(str.size(), size_type(16));
568 }
569
570 if(iszero || str.empty() || (str.find_first_not_of('0') == S::npos))
571 {
572 // We will be printing zero, even though the value might not
573 // actually be zero (it just may have been rounded to zero).
574 str = "0";
575 if(scientific || fixed)
576 {
577 str.append(1, '.');
578 str.append(size_type(digits), '0');
579 if(scientific)
580 str.append("e+00");
581 }
582 else
583 {
584 if(showpoint)
585 {
586 str.append(1, '.');
587 if(digits > 1)
588 str.append(size_type(digits - 1), '0');
589 }
590 }
591 if(neg)
592 str.insert(0, 1, '-');
593 else if(showpos)
594 str.insert(0, 1, '+');
595 return;
596 }
597
598 if(!fixed && !scientific && !showpoint)
599 {
600 //
601 // Suppress trailing zeros:
602 //
603 std::string::iterator pos = str.end();
604 while(pos != str.begin() && *--pos == '0'){}
605 if(pos != str.end())
606 ++pos;
607 str.erase(pos, str.end());
608 if(str.empty())
609 str = '0';
610 }
611 else if(!fixed || (my_exp >= 0))
612 {
613 //
614 // Pad out the end with zero's if we need to:
615 //
616 boost::intmax_t chars = str.size();
617 chars = digits - chars;
618 if(scientific)
619 ++chars;
620 if(chars > 0)
621 {
622 str.append(static_cast<std::string::size_type>(chars), '0');
623 }
624 }
625
626 if(fixed || (!scientific && (my_exp >= -4) && (my_exp < digits)))
627 {
628 if(1 + my_exp > static_cast<boost::intmax_t>(str.size()))
629 {
630 // Just pad out the end with zeros:
631 str.append(static_cast<std::string::size_type>(1 + my_exp - str.size()), '0');
632 if(showpoint || fixed)
633 str.append(".");
634 }
635 else if(my_exp + 1 < static_cast<boost::intmax_t>(str.size()))
636 {
637 if(my_exp < 0)
638 {
639 str.insert(0, static_cast<std::string::size_type>(-1 - my_exp), '0');
640 str.insert(0, "0.");
641 }
642 else
643 {
644 // Insert the decimal point:
645 str.insert(static_cast<std::string::size_type>(my_exp + 1), 1, '.');
646 }
647 }
648 else if(showpoint || fixed) // we have exactly the digits we require to left of the point
649 str += ".";
650
651 if(fixed)
652 {
653 // We may need to add trailing zeros:
654 boost::intmax_t l = str.find('.') + 1;
655 l = digits - (str.size() - l);
656 if(l > 0)
657 str.append(size_type(l), '0');
658 }
659 }
660 else
661 {
662 BOOST_MP_USING_ABS
663 // Scientific format:
664 if(showpoint || (str.size() > 1))
665 str.insert(1, 1, '.');
666 str.append(1, 'e');
667 S e = boost::lexical_cast<S>(abs(my_exp));
668 if(e.size() < BOOST_MP_MIN_EXPONENT_DIGITS)
669 e.insert(0, BOOST_MP_MIN_EXPONENT_DIGITS-e.size(), '0');
670 if(my_exp < 0)
671 e.insert(0, 1, '-');
672 else
673 e.insert(0, 1, '+');
674 str.append(e);
675 }
676 if(neg)
677 str.insert(0, 1, '-');
678 else if(showpos)
679 str.insert(0, 1, '+');
680 }
681
682 template <class V>
683 void check_shift_range(V val, const mpl::true_&, const mpl::true_&)
684 {
685 if(val > (std::numeric_limits<std::size_t>::max)())
686 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
687 if(val < 0)
688 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
689 }
690 template <class V>
691 void check_shift_range(V val, const mpl::false_&, const mpl::true_&)
692 {
693 if(val < 0)
694 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
695 }
696 template <class V>
697 void check_shift_range(V val, const mpl::true_&, const mpl::false_&)
698 {
699 if(val > (std::numeric_limits<std::size_t>::max)())
700 BOOST_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
701 }
702 template <class V>
703 void check_shift_range(V, const mpl::false_&, const mpl::false_&) BOOST_NOEXCEPT{}
704
705 } // namespace detail
706
707 //
708 // Traits class, lets us know what kind of number we have, defaults to a floating point type:
709 //
710 enum number_category_type
711 {
712 number_kind_unknown = -1,
713 number_kind_integer = 0,
714 number_kind_floating_point = 1,
715 number_kind_rational = 2,
716 number_kind_fixed_point = 3
717 };
718
719 template <class Num>
720 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)> {};
721 template <class Backend, expression_template_option ExpressionTemplates>
722 struct number_category<number<Backend, ExpressionTemplates> > : public number_category<Backend>{};
723 template <class tag, class A1, class A2, class A3, class A4>
724 struct number_category<detail::expression<tag, A1, A2, A3, A4> > : public number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>{};
725
726 template <class T>
727 struct component_type;
728 template <class T, expression_template_option ExpressionTemplates>
729 struct component_type<number<T, ExpressionTemplates> > : public component_type<T>{};
730 template <class tag, class A1, class A2, class A3, class A4>
731 struct component_type<detail::expression<tag, A1, A2, A3, A4> > : public component_type<typename detail::expression<tag, A1, A2, A3, A4>::result_type>{};
732
733 template <class T>
734 struct is_unsigned_number : public mpl::false_{};
735 template <class Backend, expression_template_option ExpressionTemplates>
736 struct is_unsigned_number<number<Backend, ExpressionTemplates> > : public is_unsigned_number<Backend> {};
737 template <class T>
738 struct is_signed_number : public mpl::bool_<!is_unsigned_number<T>::value> {};
739 template <class T>
740 struct is_interval_number : public mpl::false_ {};
741 template <class Backend, expression_template_option ExpressionTemplates>
742 struct is_interval_number<number<Backend, ExpressionTemplates> > : public is_interval_number<Backend>{};
743
744 }} // namespaces
745
746 namespace boost{ namespace math{ namespace tools{
747
748 template <class T>
749 struct promote_arg;
750
751 template <class tag, class A1, class A2, class A3, class A4>
752 struct promote_arg<boost::multiprecision::detail::expression<tag, A1, A2, A3, A4> >
753 {
754 typedef typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type type;
755 };
756
757 template <class R, class B, boost::multiprecision::expression_template_option ET>
758 inline R real_cast(const boost::multiprecision::number<B, ET>& val)
759 {
760 return val.template convert_to<R>();
761 }
762
763 template <class R, class tag, class A1, class A2, class A3, class A4>
764 inline R real_cast(const boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>& val)
765 {
766 typedef typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type val_type;
767 return val_type(val).template convert_to<R>();
768 }
769
770
771 }}}
772
773 #endif // BOOST_MATH_BIG_NUM_BASE_HPP
774
775