number_base.hpp 24 KB

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