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