number_base.hpp 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  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_MP_NUMBER_BASE_HPP
  6. #define BOOST_MP_NUMBER_BASE_HPP
  7. #include <climits>
  8. #include <ios>
  9. #include <string>
  10. #include <limits>
  11. #include <type_traits>
  12. #include <stdexcept>
  13. #include <tuple>
  14. #include <boost/multiprecision/detail/standalone_config.hpp>
  15. #include <boost/multiprecision/fwd.hpp>
  16. #include <boost/multiprecision/traits/transcendental_reduction_type.hpp>
  17. #include <boost/multiprecision/traits/std_integer_traits.hpp>
  18. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  19. #ifdef BOOST_MSVC
  20. #pragma warning(push)
  21. #pragma warning(disable : 4307)
  22. #pragma warning(pop)
  23. #endif
  24. #ifndef BOOST_MP_STANDALONE
  25. #include <boost/lexical_cast.hpp>
  26. #include <boost/core/nvp.hpp>
  27. #endif
  28. #ifdef BOOST_MP_MATH_AVAILABLE
  29. #include <boost/math/tools/complex.hpp>
  30. #endif
  31. // We now require C++11.
  32. #include <boost/multiprecision/detail/check_cpp11_config.hpp>
  33. #if defined(NDEBUG) && !defined(_DEBUG)
  34. #define BOOST_MP_FORCEINLINE BOOST_FORCEINLINE
  35. #else
  36. #define BOOST_MP_FORCEINLINE inline
  37. #endif
  38. //
  39. // Thread local storage:
  40. // Note fails on Mingw, see https://sourceforge.net/p/mingw-w64/bugs/527/
  41. //
  42. #if defined(BOOST_NO_CXX11_THREAD_LOCAL)
  43. #define BOOST_MP_THREAD_LOCAL
  44. #elif !(defined(__MINGW32__) && (defined(__GNUC__) && (__GNUC__ < 9)) && !defined(__clang__))
  45. #define BOOST_MP_THREAD_LOCAL thread_local
  46. #define BOOST_MP_USING_THREAD_LOCAL
  47. #else
  48. #pragma GCC warning "thread_local on mingw is broken, please use MSys mingw gcc-9 or later, see https://sourceforge.net/p/mingw-w64/bugs/527/"
  49. #define BOOST_MP_THREAD_LOCAL
  50. #endif
  51. #ifdef __has_include
  52. # if __has_include(<version>)
  53. # include <version>
  54. # ifdef __cpp_lib_is_constant_evaluated
  55. # include <type_traits>
  56. # define BOOST_MP_HAS_IS_CONSTANT_EVALUATED
  57. # endif
  58. # endif
  59. #endif
  60. #ifdef __has_builtin
  61. #if __has_builtin(__builtin_is_constant_evaluated) && !defined(BOOST_NO_CXX14_CONSTEXPR) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  62. #define BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED
  63. #endif
  64. #endif
  65. //
  66. // MSVC also supports __builtin_is_constant_evaluated if it's recent enough:
  67. //
  68. #if defined(_MSC_FULL_VER) && (_MSC_FULL_VER >= 192528326)
  69. # define BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED
  70. #endif
  71. //
  72. // As does GCC-9:
  73. //
  74. #if defined(BOOST_GCC) && !defined(BOOST_NO_CXX14_CONSTEXPR) && (__GNUC__ >= 9) && !defined(BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED)
  75. # define BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED
  76. #endif
  77. #if defined(BOOST_MP_HAS_IS_CONSTANT_EVALUATED) && !defined(BOOST_NO_CXX14_CONSTEXPR)
  78. # define BOOST_MP_IS_CONST_EVALUATED(x) std::is_constant_evaluated()
  79. #elif defined(BOOST_MP_HAS_BUILTIN_IS_CONSTANT_EVALUATED)
  80. # define BOOST_MP_IS_CONST_EVALUATED(x) __builtin_is_constant_evaluated()
  81. #elif !defined(BOOST_NO_CXX14_CONSTEXPR) && defined(BOOST_GCC) && (__GNUC__ >= 6)
  82. # define BOOST_MP_IS_CONST_EVALUATED(x) __builtin_constant_p(x)
  83. #else
  84. # define BOOST_MP_NO_CONSTEXPR_DETECTION
  85. #endif
  86. #ifdef BOOST_MP_NO_CONSTEXPR_DETECTION
  87. # define BOOST_CXX14_CONSTEXPR_IF_DETECTION
  88. #else
  89. # define BOOST_CXX14_CONSTEXPR_IF_DETECTION constexpr
  90. #endif
  91. #ifdef BOOST_MSVC
  92. #pragma warning(push)
  93. #pragma warning(disable : 6326)
  94. #endif
  95. namespace boost {
  96. namespace multiprecision {
  97. enum struct variable_precision_options : signed char
  98. {
  99. assume_uniform_precision = -1,
  100. preserve_target_precision = 0,
  101. preserve_source_precision = 1,
  102. preserve_component_precision = 2,
  103. preserve_related_precision = 3,
  104. preserve_all_precision = 4,
  105. };
  106. inline constexpr bool operator==(variable_precision_options a, variable_precision_options b)
  107. {
  108. return static_cast<unsigned>(a) == static_cast<unsigned>(b);
  109. }
  110. template <class T>
  111. struct is_et_number : public std::integral_constant<bool, false>
  112. {};
  113. template <class Backend>
  114. struct is_et_number<number<Backend, et_on> > : public std::integral_constant<bool, true>
  115. {};
  116. template <class T>
  117. struct is_no_et_number : public std::integral_constant<bool, false>
  118. {};
  119. template <class Backend>
  120. struct is_no_et_number<number<Backend, et_off> > : public std::integral_constant<bool, true>
  121. {};
  122. template <class T>
  123. struct is_number_expression : public std::integral_constant<bool, false>
  124. {};
  125. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  126. struct is_number_expression<detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > : public std::integral_constant<bool, true>
  127. {};
  128. namespace detail {
  129. template <class Val, class Backend>
  130. struct canonical;
  131. }
  132. template <class T, class Num>
  133. struct is_compatible_arithmetic_type
  134. : public std::integral_constant<bool,
  135. std::is_convertible<T, Num>::value && !std::is_same<T, Num>::value && !is_number_expression<T>::value
  136. && (std::is_constructible<typename Num::backend_type, typename detail::canonical<T, typename Num::backend_type>::type>::value
  137. || std::is_assignable<typename Num::backend_type, typename detail::canonical<T, typename Num::backend_type>::type>::value || is_number<T>::value || is_number_expression<T>::value)>
  138. {};
  139. namespace detail {
  140. //
  141. // Workaround for missing abs(long long) and abs(__int128) on some compilers:
  142. //
  143. template <class T>
  144. constexpr typename std::enable_if<(boost::multiprecision::detail::is_signed<T>::value || std::is_floating_point<T>::value), T>::type abs(T t) noexcept
  145. {
  146. // This strange expression avoids a hardware trap in the corner case
  147. // that val is the most negative value permitted in long long.
  148. // See https://svn.boost.org/trac/boost/ticket/9740.
  149. return t < 0 ? T(1u) + T(-(t + 1)) : t;
  150. }
  151. template <class T>
  152. constexpr typename std::enable_if<boost::multiprecision::detail::is_unsigned<T>::value, T>::type abs(T t) noexcept
  153. {
  154. return t;
  155. }
  156. #define BOOST_MP_USING_ABS using boost::multiprecision::detail::abs;
  157. template <class T>
  158. constexpr typename std::enable_if<(boost::multiprecision::detail::is_signed<T>::value || std::is_floating_point<T>::value), typename boost::multiprecision::detail::make_unsigned<T>::type>::type unsigned_abs(T t) noexcept
  159. {
  160. // This strange expression avoids a hardware trap in the corner case
  161. // that val is the most negative value permitted in long long.
  162. // See https://svn.boost.org/trac/boost/ticket/9740.
  163. return t < 0 ? static_cast<typename boost::multiprecision::detail::make_unsigned<T>::type>(1u) + static_cast<typename boost::multiprecision::detail::make_unsigned<T>::type>(-(t + 1)) : static_cast<typename boost::multiprecision::detail::make_unsigned<T>::type>(t);
  164. }
  165. template <class T>
  166. constexpr typename std::enable_if<boost::multiprecision::detail::is_unsigned<T>::value, T>::type unsigned_abs(T t) noexcept
  167. {
  168. return t;
  169. }
  170. template <class T>
  171. struct bits_of
  172. {
  173. static_assert(boost::multiprecision::detail::is_integral<T>::value || std::is_enum<T>::value || std::numeric_limits<T>::is_specialized, "Failed integer size check");
  174. static constexpr unsigned value =
  175. std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits
  176. : sizeof(T) * CHAR_BIT - (boost::multiprecision::detail::is_signed<T>::value ? 1 : 0);
  177. };
  178. #if defined(_GLIBCXX_USE_FLOAT128) && defined(BOOST_GCC) && !defined(__STRICT_ANSI__) && !defined(__PGI)
  179. #define BOOST_MP_BITS_OF_FLOAT128_DEFINED
  180. template <>
  181. struct bits_of<float128_type>
  182. {
  183. static constexpr unsigned value = 113;
  184. };
  185. #endif
  186. template <int b>
  187. struct has_enough_bits
  188. {
  189. template <class T>
  190. struct type : public std::integral_constant<bool, bits_of<T>::value >= b>
  191. {};
  192. };
  193. template <class Tuple, int i, int digits, bool = (i >= std::tuple_size<Tuple>::value)>
  194. struct find_index_of_large_enough_type
  195. {
  196. static constexpr int value = bits_of<typename std::tuple_element<static_cast<std::size_t>(i), Tuple>::type>::value >= digits ? i : find_index_of_large_enough_type<Tuple, i + 1, digits>::value;
  197. };
  198. template <class Tuple, int i, int digits>
  199. struct find_index_of_large_enough_type<Tuple, i, digits, true>
  200. {
  201. static constexpr int value = INT_MAX;
  202. };
  203. template <int index, class Tuple, class Fallback, bool = (std::tuple_size<Tuple>::value <= index)>
  204. struct dereference_tuple
  205. {
  206. using type = typename std::tuple_element<static_cast<std::size_t>(index), Tuple>::type;
  207. };
  208. template <int index, class Tuple, class Fallback>
  209. struct dereference_tuple<index, Tuple, Fallback, true>
  210. {
  211. using type = Fallback;
  212. };
  213. template <class Val, class Backend, class Tag>
  214. struct canonical_imp
  215. {
  216. using type = typename std::remove_cv<typename std::decay<const Val>::type>::type;
  217. };
  218. template <class B, class Backend, class Tag>
  219. struct canonical_imp<number<B, et_on>, Backend, Tag>
  220. {
  221. using type = B;
  222. };
  223. template <class B, class Backend, class Tag>
  224. struct canonical_imp<number<B, et_off>, Backend, Tag>
  225. {
  226. using type = B;
  227. };
  228. #ifdef __SUNPRO_CC
  229. template <class B, class Backend>
  230. struct canonical_imp<number<B, et_on>, Backend, std::integral_constant<int, 3> >
  231. {
  232. using type = B;
  233. };
  234. template <class B, class Backend>
  235. struct canonical_imp<number<B, et_off>, Backend, std::integral_constant<int, 3> >
  236. {
  237. using type = B;
  238. };
  239. #endif
  240. template <class Val, class Backend>
  241. struct canonical_imp<Val, Backend, std::integral_constant<int, 0> >
  242. {
  243. static constexpr int index = find_index_of_large_enough_type<typename Backend::signed_types, 0, bits_of<Val>::value>::value;
  244. using type = typename dereference_tuple<index, typename Backend::signed_types, Val>::type;
  245. };
  246. template <class Val, class Backend>
  247. struct canonical_imp<Val, Backend, std::integral_constant<int, 1> >
  248. {
  249. static constexpr int index = find_index_of_large_enough_type<typename Backend::unsigned_types, 0, bits_of<Val>::value>::value;
  250. using type = typename dereference_tuple<index, typename Backend::unsigned_types, Val>::type;
  251. };
  252. template <class Val, class Backend>
  253. struct canonical_imp<Val, Backend, std::integral_constant<int, 2> >
  254. {
  255. static constexpr int index = find_index_of_large_enough_type<typename Backend::float_types, 0, bits_of<Val>::value>::value;
  256. using type = typename dereference_tuple<index, typename Backend::float_types, Val>::type;
  257. };
  258. template <class Val, class Backend>
  259. struct canonical_imp<Val, Backend, std::integral_constant<int, 3> >
  260. {
  261. using type = const char*;
  262. };
  263. template <class Val, class Backend>
  264. struct canonical_imp<Val, Backend, std::integral_constant<int, 4> >
  265. {
  266. using underlying = typename std::underlying_type<Val>::type;
  267. using tag = typename std::conditional<boost::multiprecision::detail::is_signed<Val>::value, std::integral_constant<int, 0>, std::integral_constant<int, 1>>::type;
  268. using type = typename canonical_imp<underlying, Backend, tag>::type;
  269. };
  270. template <class Val, class Backend>
  271. struct canonical
  272. {
  273. using tag_type = typename std::conditional<
  274. boost::multiprecision::detail::is_signed<Val>::value && boost::multiprecision::detail::is_integral<Val>::value,
  275. std::integral_constant<int, 0>,
  276. typename std::conditional<
  277. boost::multiprecision::detail::is_unsigned<Val>::value,
  278. std::integral_constant<int, 1>,
  279. typename std::conditional<
  280. std::is_floating_point<Val>::value,
  281. std::integral_constant<int, 2>,
  282. typename std::conditional<
  283. (std::is_convertible<Val, const char*>::value || std::is_same<Val, std::string>::value),
  284. std::integral_constant<int, 3>,
  285. typename std::conditional<
  286. std::is_enum<Val>::value,
  287. std::integral_constant<int, 4>,
  288. std::integral_constant<int, 5> >::type>::type>::type>::type>::type;
  289. using type = typename canonical_imp<Val, Backend, tag_type>::type;
  290. };
  291. struct terminal
  292. {};
  293. struct negate
  294. {};
  295. struct plus
  296. {};
  297. struct minus
  298. {};
  299. struct multiplies
  300. {};
  301. struct divides
  302. {};
  303. struct modulus
  304. {};
  305. struct shift_left
  306. {};
  307. struct shift_right
  308. {};
  309. struct bitwise_and
  310. {};
  311. struct bitwise_or
  312. {};
  313. struct bitwise_xor
  314. {};
  315. struct bitwise_complement
  316. {};
  317. struct add_immediates
  318. {};
  319. struct subtract_immediates
  320. {};
  321. struct multiply_immediates
  322. {};
  323. struct divide_immediates
  324. {};
  325. struct modulus_immediates
  326. {};
  327. struct bitwise_and_immediates
  328. {};
  329. struct bitwise_or_immediates
  330. {};
  331. struct bitwise_xor_immediates
  332. {};
  333. struct complement_immediates
  334. {};
  335. struct function
  336. {};
  337. struct multiply_add
  338. {};
  339. struct multiply_subtract
  340. {};
  341. template <class T>
  342. struct backend_type;
  343. template <class T, expression_template_option ExpressionTemplates>
  344. struct backend_type<number<T, ExpressionTemplates> >
  345. {
  346. using type = T;
  347. };
  348. template <class tag, class A1, class A2, class A3, class A4>
  349. struct backend_type<expression<tag, A1, A2, A3, A4> >
  350. {
  351. using type = typename backend_type<typename expression<tag, A1, A2, A3, A4>::result_type>::type;
  352. };
  353. template <class T1, class T2>
  354. struct combine_expression
  355. {
  356. using type = decltype(T1() + T2());
  357. };
  358. template <class T1, expression_template_option ExpressionTemplates, class T2>
  359. struct combine_expression<number<T1, ExpressionTemplates>, T2>
  360. {
  361. using type = number<T1, ExpressionTemplates>;
  362. };
  363. template <class T1, class T2, expression_template_option ExpressionTemplates>
  364. struct combine_expression<T1, number<T2, ExpressionTemplates> >
  365. {
  366. using type = number<T2, ExpressionTemplates>;
  367. };
  368. template <class T, expression_template_option ExpressionTemplates>
  369. struct combine_expression<number<T, ExpressionTemplates>, number<T, ExpressionTemplates> >
  370. {
  371. using type = number<T, ExpressionTemplates>;
  372. };
  373. template <class T1, expression_template_option ExpressionTemplates1, class T2, expression_template_option ExpressionTemplates2>
  374. struct combine_expression<number<T1, ExpressionTemplates1>, number<T2, ExpressionTemplates2> >
  375. {
  376. using type = typename std::conditional<
  377. std::is_convertible<number<T2, ExpressionTemplates2>, number<T1, ExpressionTemplates2> >::value,
  378. number<T1, ExpressionTemplates1>,
  379. number<T2, ExpressionTemplates2> >::type;
  380. };
  381. template <class T>
  382. struct arg_type
  383. {
  384. using type = expression<terminal, T>;
  385. };
  386. template <class Tag, class Arg1, class Arg2, class Arg3, class Arg4>
  387. struct arg_type<expression<Tag, Arg1, Arg2, Arg3, Arg4> >
  388. {
  389. using type = expression<Tag, Arg1, Arg2, Arg3, Arg4>;
  390. };
  391. struct unmentionable
  392. {
  393. unmentionable* proc() { return nullptr; }
  394. };
  395. typedef unmentionable* (unmentionable::*unmentionable_type)();
  396. template <class T, bool b>
  397. struct expression_storage_base
  398. {
  399. using type = const T&;
  400. };
  401. template <class T>
  402. struct expression_storage_base<T, true>
  403. {
  404. using type = T;
  405. };
  406. template <class T>
  407. struct expression_storage : public expression_storage_base<T, boost::multiprecision::detail::is_arithmetic<T>::value>
  408. {};
  409. template <class T>
  410. struct expression_storage<T*>
  411. {
  412. using type = T*;
  413. };
  414. template <class T>
  415. struct expression_storage<const T*>
  416. {
  417. using type = const T*;
  418. };
  419. template <class tag, class A1, class A2, class A3, class A4>
  420. struct expression_storage<expression<tag, A1, A2, A3, A4> >
  421. {
  422. using type = expression<tag, A1, A2, A3, A4>;
  423. };
  424. template <class tag, class Arg1>
  425. struct expression<tag, Arg1, void, void, void>
  426. {
  427. using arity = std::integral_constant<int, 1> ;
  428. using left_type = typename arg_type<Arg1>::type ;
  429. using left_result_type = typename left_type::result_type;
  430. using result_type = typename left_type::result_type;
  431. using tag_type = tag ;
  432. explicit BOOST_MP_CXX14_CONSTEXPR expression(const Arg1& a) : arg(a) {}
  433. BOOST_MP_CXX14_CONSTEXPR expression(const expression& e) : arg(e.arg) {}
  434. //
  435. // If we have static_assert we can give a more useful error message
  436. // than if we simply have no operator defined at all:
  437. //
  438. template <class Other>
  439. BOOST_MP_CXX14_CONSTEXPR expression& operator=(const Other&)
  440. {
  441. // This should always fail:
  442. static_assert(sizeof(Other) == INT_MAX, "You can not assign to a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  443. return *this;
  444. }
  445. BOOST_MP_CXX14_CONSTEXPR expression& operator++()
  446. {
  447. // This should always fail:
  448. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  449. return *this;
  450. }
  451. BOOST_MP_CXX14_CONSTEXPR expression& operator++(int)
  452. {
  453. // This should always fail:
  454. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  455. return *this;
  456. }
  457. BOOST_MP_CXX14_CONSTEXPR expression& operator--()
  458. {
  459. // This should always fail:
  460. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  461. return *this;
  462. }
  463. BOOST_MP_CXX14_CONSTEXPR expression& operator--(int)
  464. {
  465. // This should always fail:
  466. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  467. return *this;
  468. }
  469. template <class Other>
  470. BOOST_MP_CXX14_CONSTEXPR expression& operator+=(const Other&)
  471. {
  472. // This should always fail:
  473. static_assert(sizeof(Other) == INT_MAX, "You can not use operator+= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  474. return *this;
  475. }
  476. template <class Other>
  477. BOOST_MP_CXX14_CONSTEXPR expression& operator-=(const Other&)
  478. {
  479. // This should always fail:
  480. static_assert(sizeof(Other) == INT_MAX, "You can not use operator-= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  481. return *this;
  482. }
  483. template <class Other>
  484. BOOST_MP_CXX14_CONSTEXPR expression& operator*=(const Other&)
  485. {
  486. // This should always fail:
  487. static_assert(sizeof(Other) == INT_MAX, "You can not use operator*= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  488. return *this;
  489. }
  490. template <class Other>
  491. BOOST_MP_CXX14_CONSTEXPR expression& operator/=(const Other&)
  492. {
  493. // This should always fail:
  494. static_assert(sizeof(Other) == INT_MAX, "You can not use operator/= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  495. return *this;
  496. }
  497. template <class Other>
  498. BOOST_MP_CXX14_CONSTEXPR expression& operator%=(const Other&)
  499. {
  500. // This should always fail:
  501. static_assert(sizeof(Other) == INT_MAX, "You can not use operator%= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  502. return *this;
  503. }
  504. template <class Other>
  505. BOOST_MP_CXX14_CONSTEXPR expression& operator|=(const Other&)
  506. {
  507. // This should always fail:
  508. static_assert(sizeof(Other) == INT_MAX, "You can not use operator|= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  509. return *this;
  510. }
  511. template <class Other>
  512. BOOST_MP_CXX14_CONSTEXPR expression& operator&=(const Other&)
  513. {
  514. // This should always fail:
  515. static_assert(sizeof(Other) == INT_MAX, "You can not use operator&= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  516. return *this;
  517. }
  518. template <class Other>
  519. BOOST_MP_CXX14_CONSTEXPR expression& operator^=(const Other&)
  520. {
  521. // This should always fail:
  522. static_assert(sizeof(Other) == INT_MAX, "You can not use operator^= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  523. return *this;
  524. }
  525. template <class Other>
  526. BOOST_MP_CXX14_CONSTEXPR expression& operator<<=(const Other&)
  527. {
  528. // This should always fail:
  529. static_assert(sizeof(Other) == INT_MAX, "You can not use operator<<= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  530. return *this;
  531. }
  532. template <class Other>
  533. BOOST_MP_CXX14_CONSTEXPR expression& operator>>=(const Other&)
  534. {
  535. // This should always fail:
  536. static_assert(sizeof(Other) == INT_MAX, "You can not use operator>>= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  537. return *this;
  538. }
  539. BOOST_MP_CXX14_CONSTEXPR left_type left() const
  540. {
  541. return left_type(arg);
  542. }
  543. BOOST_MP_CXX14_CONSTEXPR const Arg1& left_ref() const noexcept { return arg; }
  544. static constexpr unsigned depth = left_type::depth + 1;
  545. template <class T
  546. #ifndef __SUNPRO_CC
  547. ,
  548. typename std::enable_if<!is_number<T>::value && !std::is_convertible<result_type, T const&>::value && std::is_constructible<T, result_type>::value, int>::type = 0
  549. #endif
  550. >
  551. explicit BOOST_MP_CXX14_CONSTEXPR operator T() const
  552. {
  553. return static_cast<T>(static_cast<result_type>(*this));
  554. }
  555. BOOST_MP_FORCEINLINE explicit BOOST_MP_CXX14_CONSTEXPR operator bool() const
  556. {
  557. result_type r(*this);
  558. return static_cast<bool>(r);
  559. }
  560. template <class T>
  561. BOOST_MP_CXX14_CONSTEXPR T convert_to()
  562. {
  563. result_type r(*this);
  564. return r.template convert_to<T>();
  565. }
  566. private:
  567. typename expression_storage<Arg1>::type arg;
  568. expression& operator=(const expression&);
  569. };
  570. template <class Arg1>
  571. struct expression<terminal, Arg1, void, void, void>
  572. {
  573. using arity = std::integral_constant<int, 0>;
  574. using result_type = Arg1 ;
  575. using tag_type = terminal ;
  576. explicit BOOST_MP_CXX14_CONSTEXPR expression(const Arg1& a) : arg(a) {}
  577. BOOST_MP_CXX14_CONSTEXPR expression(const expression& e) : arg(e.arg) {}
  578. //
  579. // If we have static_assert we can give a more useful error message
  580. // than if we simply have no operator defined at all:
  581. //
  582. template <class Other>
  583. BOOST_MP_CXX14_CONSTEXPR expression& operator=(const Other&)
  584. {
  585. // This should always fail:
  586. static_assert(sizeof(Other) == INT_MAX, "You can not assign to a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  587. return *this;
  588. }
  589. BOOST_MP_CXX14_CONSTEXPR expression& operator++()
  590. {
  591. // This should always fail:
  592. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  593. return *this;
  594. }
  595. BOOST_MP_CXX14_CONSTEXPR expression& operator++(int)
  596. {
  597. // This should always fail:
  598. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  599. return *this;
  600. }
  601. BOOST_MP_CXX14_CONSTEXPR expression& operator--()
  602. {
  603. // This should always fail:
  604. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  605. return *this;
  606. }
  607. BOOST_MP_CXX14_CONSTEXPR expression& operator--(int)
  608. {
  609. // This should always fail:
  610. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  611. return *this;
  612. }
  613. template <class Other>
  614. BOOST_MP_CXX14_CONSTEXPR expression& operator+=(const Other&)
  615. {
  616. // This should always fail:
  617. static_assert(sizeof(Other) == INT_MAX, "You can not use operator+= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  618. return *this;
  619. }
  620. template <class Other>
  621. BOOST_MP_CXX14_CONSTEXPR expression& operator-=(const Other&)
  622. {
  623. // This should always fail:
  624. static_assert(sizeof(Other) == INT_MAX, "You can not use operator-= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  625. return *this;
  626. }
  627. template <class Other>
  628. BOOST_MP_CXX14_CONSTEXPR expression& operator*=(const Other&)
  629. {
  630. // This should always fail:
  631. static_assert(sizeof(Other) == INT_MAX, "You can not use operator*= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  632. return *this;
  633. }
  634. template <class Other>
  635. BOOST_MP_CXX14_CONSTEXPR expression& operator/=(const Other&)
  636. {
  637. // This should always fail:
  638. static_assert(sizeof(Other) == INT_MAX, "You can not use operator/= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  639. return *this;
  640. }
  641. template <class Other>
  642. BOOST_MP_CXX14_CONSTEXPR expression& operator%=(const Other&)
  643. {
  644. // This should always fail:
  645. static_assert(sizeof(Other) == INT_MAX, "You can not use operator%= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  646. return *this;
  647. }
  648. template <class Other>
  649. BOOST_MP_CXX14_CONSTEXPR expression& operator|=(const Other&)
  650. {
  651. // This should always fail:
  652. static_assert(sizeof(Other) == INT_MAX, "You can not use operator|= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  653. return *this;
  654. }
  655. template <class Other>
  656. BOOST_MP_CXX14_CONSTEXPR expression& operator&=(const Other&)
  657. {
  658. // This should always fail:
  659. static_assert(sizeof(Other) == INT_MAX, "You can not use operator&= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  660. return *this;
  661. }
  662. template <class Other>
  663. BOOST_MP_CXX14_CONSTEXPR expression& operator^=(const Other&)
  664. {
  665. // This should always fail:
  666. static_assert(sizeof(Other) == INT_MAX, "You can not use operator^= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  667. return *this;
  668. }
  669. template <class Other>
  670. BOOST_MP_CXX14_CONSTEXPR expression& operator<<=(const Other&)
  671. {
  672. // This should always fail:
  673. static_assert(sizeof(Other) == INT_MAX, "You can not use operator<<= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  674. return *this;
  675. }
  676. template <class Other>
  677. BOOST_MP_CXX14_CONSTEXPR expression& operator>>=(const Other&)
  678. {
  679. // This should always fail:
  680. static_assert(sizeof(Other) == INT_MAX, "You can not use operator>>= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  681. return *this;
  682. }
  683. BOOST_MP_CXX14_CONSTEXPR const Arg1& value() const noexcept
  684. {
  685. return arg;
  686. }
  687. static constexpr unsigned depth = 0;
  688. template <class T
  689. #ifndef __SUNPRO_CC
  690. ,
  691. typename std::enable_if<!is_number<T>::value && !std::is_convertible<result_type, T const&>::value && std::is_constructible<T, result_type>::value, int>::type = 0
  692. #endif
  693. >
  694. explicit BOOST_MP_CXX14_CONSTEXPR operator T() const
  695. {
  696. return static_cast<T>(static_cast<result_type>(*this));
  697. }
  698. BOOST_MP_FORCEINLINE explicit BOOST_MP_CXX14_CONSTEXPR operator bool() const
  699. {
  700. result_type r(*this);
  701. return static_cast<bool>(r);
  702. }
  703. template <class T>
  704. BOOST_MP_CXX14_CONSTEXPR T convert_to()
  705. {
  706. result_type r(*this);
  707. return r.template convert_to<T>();
  708. }
  709. private:
  710. typename expression_storage<Arg1>::type arg;
  711. expression& operator=(const expression&);
  712. };
  713. template <class tag, class Arg1, class Arg2>
  714. struct expression<tag, Arg1, Arg2, void, void>
  715. {
  716. using arity = std::integral_constant<int, 2> ;
  717. using left_type = typename arg_type<Arg1>::type ;
  718. using right_type = typename arg_type<Arg2>::type ;
  719. using left_result_type = typename left_type::result_type ;
  720. using right_result_type = typename right_type::result_type ;
  721. using result_type = typename combine_expression<left_result_type, right_result_type>::type;
  722. using tag_type = tag ;
  723. BOOST_MP_CXX14_CONSTEXPR expression(const Arg1& a1, const Arg2& a2) : arg1(a1), arg2(a2) {}
  724. BOOST_MP_CXX14_CONSTEXPR expression(const expression& e) : arg1(e.arg1), arg2(e.arg2) {}
  725. //
  726. // If we have static_assert we can give a more useful error message
  727. // than if we simply have no operator defined at all:
  728. //
  729. template <class Other>
  730. BOOST_MP_CXX14_CONSTEXPR expression& operator=(const Other&)
  731. {
  732. // This should always fail:
  733. static_assert(sizeof(Other) == INT_MAX, "You can not assign to a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  734. return *this;
  735. }
  736. BOOST_MP_CXX14_CONSTEXPR expression& operator++()
  737. {
  738. // This should always fail:
  739. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  740. return *this;
  741. }
  742. BOOST_MP_CXX14_CONSTEXPR expression& operator++(int)
  743. {
  744. // This should always fail:
  745. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  746. return *this;
  747. }
  748. BOOST_MP_CXX14_CONSTEXPR expression& operator--()
  749. {
  750. // This should always fail:
  751. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  752. return *this;
  753. }
  754. BOOST_MP_CXX14_CONSTEXPR expression& operator--(int)
  755. {
  756. // This should always fail:
  757. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  758. return *this;
  759. }
  760. template <class Other>
  761. BOOST_MP_CXX14_CONSTEXPR expression& operator+=(const Other&)
  762. {
  763. // This should always fail:
  764. static_assert(sizeof(Other) == INT_MAX, "You can not use operator+= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  765. return *this;
  766. }
  767. template <class Other>
  768. BOOST_MP_CXX14_CONSTEXPR expression& operator-=(const Other&)
  769. {
  770. // This should always fail:
  771. static_assert(sizeof(Other) == INT_MAX, "You can not use operator-= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  772. return *this;
  773. }
  774. template <class Other>
  775. BOOST_MP_CXX14_CONSTEXPR expression& operator*=(const Other&)
  776. {
  777. // This should always fail:
  778. static_assert(sizeof(Other) == INT_MAX, "You can not use operator*= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  779. return *this;
  780. }
  781. template <class Other>
  782. BOOST_MP_CXX14_CONSTEXPR expression& operator/=(const Other&)
  783. {
  784. // This should always fail:
  785. static_assert(sizeof(Other) == INT_MAX, "You can not use operator/= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  786. return *this;
  787. }
  788. template <class Other>
  789. BOOST_MP_CXX14_CONSTEXPR expression& operator%=(const Other&)
  790. {
  791. // This should always fail:
  792. static_assert(sizeof(Other) == INT_MAX, "You can not use operator%= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  793. return *this;
  794. }
  795. template <class Other>
  796. BOOST_MP_CXX14_CONSTEXPR expression& operator|=(const Other&)
  797. {
  798. // This should always fail:
  799. static_assert(sizeof(Other) == INT_MAX, "You can not use operator|= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  800. return *this;
  801. }
  802. template <class Other>
  803. BOOST_MP_CXX14_CONSTEXPR expression& operator&=(const Other&)
  804. {
  805. // This should always fail:
  806. static_assert(sizeof(Other) == INT_MAX, "You can not use operator&= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  807. return *this;
  808. }
  809. template <class Other>
  810. BOOST_MP_CXX14_CONSTEXPR expression& operator^=(const Other&)
  811. {
  812. // This should always fail:
  813. static_assert(sizeof(Other) == INT_MAX, "You can not use operator^= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  814. return *this;
  815. }
  816. template <class Other>
  817. BOOST_MP_CXX14_CONSTEXPR expression& operator<<=(const Other&)
  818. {
  819. // This should always fail:
  820. static_assert(sizeof(Other) == INT_MAX, "You can not use operator<<= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  821. return *this;
  822. }
  823. template <class Other>
  824. BOOST_MP_CXX14_CONSTEXPR expression& operator>>=(const Other&)
  825. {
  826. // This should always fail:
  827. static_assert(sizeof(Other) == INT_MAX, "You can not use operator>>= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  828. return *this;
  829. }
  830. BOOST_MP_CXX14_CONSTEXPR left_type left() const
  831. {
  832. return left_type(arg1);
  833. }
  834. BOOST_MP_CXX14_CONSTEXPR right_type right() const { return right_type(arg2); }
  835. BOOST_MP_CXX14_CONSTEXPR const Arg1& left_ref() const noexcept { return arg1; }
  836. BOOST_MP_CXX14_CONSTEXPR const Arg2& right_ref() const noexcept { return arg2; }
  837. template <class T
  838. #ifndef __SUNPRO_CC
  839. ,
  840. typename std::enable_if<!is_number<T>::value && !std::is_convertible<result_type, T const&>::value && std::is_constructible<T, result_type>::value, int>::type = 0
  841. #endif
  842. >
  843. explicit BOOST_MP_CXX14_CONSTEXPR operator T() const
  844. {
  845. return static_cast<T>(static_cast<result_type>(*this));
  846. }
  847. BOOST_MP_FORCEINLINE explicit BOOST_MP_CXX14_CONSTEXPR operator bool() const
  848. {
  849. result_type r(*this);
  850. return static_cast<bool>(r);
  851. }
  852. template <class T>
  853. BOOST_MP_CXX14_CONSTEXPR T convert_to()
  854. {
  855. result_type r(*this);
  856. return r.template convert_to<T>();
  857. }
  858. static const constexpr unsigned left_depth = left_type::depth + 1;
  859. static const constexpr unsigned right_depth = right_type::depth + 1;
  860. static const constexpr unsigned depth = left_depth > right_depth ? left_depth : right_depth;
  861. private:
  862. typename expression_storage<Arg1>::type arg1;
  863. typename expression_storage<Arg2>::type arg2;
  864. expression& operator=(const expression&);
  865. };
  866. template <class tag, class Arg1, class Arg2, class Arg3>
  867. struct expression<tag, Arg1, Arg2, Arg3, void>
  868. {
  869. using arity = std::integral_constant<int, 3> ;
  870. using left_type = typename arg_type<Arg1>::type ;
  871. using middle_type = typename arg_type<Arg2>::type ;
  872. using right_type = typename arg_type<Arg3>::type ;
  873. using left_result_type = typename left_type::result_type ;
  874. using middle_result_type = typename middle_type::result_type;
  875. using right_result_type = typename right_type::result_type ;
  876. using result_type = typename combine_expression<
  877. left_result_type,
  878. typename combine_expression<right_result_type, middle_result_type>::type>::type;
  879. using tag_type = tag ;
  880. BOOST_MP_CXX14_CONSTEXPR expression(const Arg1& a1, const Arg2& a2, const Arg3& a3) : arg1(a1), arg2(a2), arg3(a3) {}
  881. BOOST_MP_CXX14_CONSTEXPR expression(const expression& e) : arg1(e.arg1), arg2(e.arg2), arg3(e.arg3) {}
  882. //
  883. // If we have static_assert we can give a more useful error message
  884. // than if we simply have no operator defined at all:
  885. //
  886. template <class Other>
  887. BOOST_MP_CXX14_CONSTEXPR expression& operator=(const Other&)
  888. {
  889. // This should always fail:
  890. static_assert(sizeof(Other) == INT_MAX, "You can not assign to a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  891. return *this;
  892. }
  893. BOOST_MP_CXX14_CONSTEXPR expression& operator++()
  894. {
  895. // This should always fail:
  896. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  897. return *this;
  898. }
  899. BOOST_MP_CXX14_CONSTEXPR expression& operator++(int)
  900. {
  901. // This should always fail:
  902. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  903. return *this;
  904. }
  905. BOOST_MP_CXX14_CONSTEXPR expression& operator--()
  906. {
  907. // This should always fail:
  908. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  909. return *this;
  910. }
  911. BOOST_MP_CXX14_CONSTEXPR expression& operator--(int)
  912. {
  913. // This should always fail:
  914. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  915. return *this;
  916. }
  917. template <class Other>
  918. BOOST_MP_CXX14_CONSTEXPR expression& operator+=(const Other&)
  919. {
  920. // This should always fail:
  921. static_assert(sizeof(Other) == INT_MAX, "You can not use operator+= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  922. return *this;
  923. }
  924. template <class Other>
  925. BOOST_MP_CXX14_CONSTEXPR expression& operator-=(const Other&)
  926. {
  927. // This should always fail:
  928. static_assert(sizeof(Other) == INT_MAX, "You can not use operator-= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  929. return *this;
  930. }
  931. template <class Other>
  932. BOOST_MP_CXX14_CONSTEXPR expression& operator*=(const Other&)
  933. {
  934. // This should always fail:
  935. static_assert(sizeof(Other) == INT_MAX, "You can not use operator*= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  936. return *this;
  937. }
  938. template <class Other>
  939. BOOST_MP_CXX14_CONSTEXPR expression& operator/=(const Other&)
  940. {
  941. // This should always fail:
  942. static_assert(sizeof(Other) == INT_MAX, "You can not use operator/= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  943. return *this;
  944. }
  945. template <class Other>
  946. BOOST_MP_CXX14_CONSTEXPR expression& operator%=(const Other&)
  947. {
  948. // This should always fail:
  949. static_assert(sizeof(Other) == INT_MAX, "You can not use operator%= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  950. return *this;
  951. }
  952. template <class Other>
  953. BOOST_MP_CXX14_CONSTEXPR expression& operator|=(const Other&)
  954. {
  955. // This should always fail:
  956. static_assert(sizeof(Other) == INT_MAX, "You can not use operator|= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  957. return *this;
  958. }
  959. template <class Other>
  960. BOOST_MP_CXX14_CONSTEXPR expression& operator&=(const Other&)
  961. {
  962. // This should always fail:
  963. static_assert(sizeof(Other) == INT_MAX, "You can not use operator&= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  964. return *this;
  965. }
  966. template <class Other>
  967. BOOST_MP_CXX14_CONSTEXPR expression& operator^=(const Other&)
  968. {
  969. // This should always fail:
  970. static_assert(sizeof(Other) == INT_MAX, "You can not use operator^= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  971. return *this;
  972. }
  973. template <class Other>
  974. BOOST_MP_CXX14_CONSTEXPR expression& operator<<=(const Other&)
  975. {
  976. // This should always fail:
  977. static_assert(sizeof(Other) == INT_MAX, "You can not use operator<<= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  978. return *this;
  979. }
  980. template <class Other>
  981. BOOST_MP_CXX14_CONSTEXPR expression& operator>>=(const Other&)
  982. {
  983. // This should always fail:
  984. static_assert(sizeof(Other) == INT_MAX, "You can not use operator>>= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  985. return *this;
  986. }
  987. BOOST_MP_CXX14_CONSTEXPR left_type left() const
  988. {
  989. return left_type(arg1);
  990. }
  991. BOOST_MP_CXX14_CONSTEXPR middle_type middle() const { return middle_type(arg2); }
  992. BOOST_MP_CXX14_CONSTEXPR right_type right() const { return right_type(arg3); }
  993. BOOST_MP_CXX14_CONSTEXPR const Arg1& left_ref() const noexcept { return arg1; }
  994. BOOST_MP_CXX14_CONSTEXPR const Arg2& middle_ref() const noexcept { return arg2; }
  995. BOOST_MP_CXX14_CONSTEXPR const Arg3& right_ref() const noexcept { return arg3; }
  996. template <class T
  997. #ifndef __SUNPRO_CC
  998. ,
  999. typename std::enable_if<!is_number<T>::value && !std::is_convertible<result_type, T const&>::value && std::is_constructible<T, result_type>::value, int>::type = 0
  1000. #endif
  1001. >
  1002. explicit BOOST_MP_CXX14_CONSTEXPR operator T() const
  1003. {
  1004. return static_cast<T>(static_cast<result_type>(*this));
  1005. }
  1006. BOOST_MP_FORCEINLINE explicit BOOST_MP_CXX14_CONSTEXPR operator bool() const
  1007. {
  1008. result_type r(*this);
  1009. return static_cast<bool>(r);
  1010. }
  1011. template <class T>
  1012. BOOST_MP_CXX14_CONSTEXPR T convert_to()
  1013. {
  1014. result_type r(*this);
  1015. return r.template convert_to<T>();
  1016. }
  1017. static constexpr unsigned left_depth = left_type::depth + 1;
  1018. static constexpr unsigned middle_depth = middle_type::depth + 1;
  1019. static constexpr unsigned right_depth = right_type::depth + 1;
  1020. static constexpr unsigned depth = left_depth > right_depth ? (left_depth > middle_depth ? left_depth : middle_depth) : (right_depth > middle_depth ? right_depth : middle_depth);
  1021. private:
  1022. typename expression_storage<Arg1>::type arg1;
  1023. typename expression_storage<Arg2>::type arg2;
  1024. typename expression_storage<Arg3>::type arg3;
  1025. expression& operator=(const expression&);
  1026. };
  1027. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  1028. struct expression
  1029. {
  1030. using arity = std::integral_constant<int, 4> ;
  1031. using left_type = typename arg_type<Arg1>::type ;
  1032. using left_middle_type = typename arg_type<Arg2>::type ;
  1033. using right_middle_type = typename arg_type<Arg3>::type ;
  1034. using right_type = typename arg_type<Arg4>::type ;
  1035. using left_result_type = typename left_type::result_type ;
  1036. using left_middle_result_type = typename left_middle_type::result_type ;
  1037. using right_middle_result_type = typename right_middle_type::result_type;
  1038. using right_result_type = typename right_type::result_type ;
  1039. using result_type = typename combine_expression<
  1040. left_result_type,
  1041. typename combine_expression<
  1042. left_middle_result_type,
  1043. typename combine_expression<right_middle_result_type, right_result_type>::type>::type>::type;
  1044. using tag_type = tag ;
  1045. BOOST_MP_CXX14_CONSTEXPR expression(const Arg1& a1, const Arg2& a2, const Arg3& a3, const Arg4& a4) : arg1(a1), arg2(a2), arg3(a3), arg4(a4) {}
  1046. BOOST_MP_CXX14_CONSTEXPR expression(const expression& e) : arg1(e.arg1), arg2(e.arg2), arg3(e.arg3), arg4(e.arg4) {}
  1047. //
  1048. // If we have static_assert we can give a more useful error message
  1049. // than if we simply have no operator defined at all:
  1050. //
  1051. template <class Other>
  1052. BOOST_MP_CXX14_CONSTEXPR expression& operator=(const Other&)
  1053. {
  1054. // This should always fail:
  1055. static_assert(sizeof(Other) == INT_MAX, "You can not assign to a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1056. return *this;
  1057. }
  1058. BOOST_MP_CXX14_CONSTEXPR expression& operator++()
  1059. {
  1060. // This should always fail:
  1061. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1062. return *this;
  1063. }
  1064. BOOST_MP_CXX14_CONSTEXPR expression& operator++(int)
  1065. {
  1066. // This should always fail:
  1067. static_assert(sizeof(*this) == INT_MAX, "You can not increment a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1068. return *this;
  1069. }
  1070. BOOST_MP_CXX14_CONSTEXPR expression& operator--()
  1071. {
  1072. // This should always fail:
  1073. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1074. return *this;
  1075. }
  1076. BOOST_MP_CXX14_CONSTEXPR expression& operator--(int)
  1077. {
  1078. // This should always fail:
  1079. static_assert(sizeof(*this) == INT_MAX, "You can not decrement a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1080. return *this;
  1081. }
  1082. template <class Other>
  1083. BOOST_MP_CXX14_CONSTEXPR expression& operator+=(const Other&)
  1084. {
  1085. // This should always fail:
  1086. static_assert(sizeof(Other) == INT_MAX, "You can not use operator+= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1087. return *this;
  1088. }
  1089. template <class Other>
  1090. BOOST_MP_CXX14_CONSTEXPR expression& operator-=(const Other&)
  1091. {
  1092. // This should always fail:
  1093. static_assert(sizeof(Other) == INT_MAX, "You can not use operator-= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1094. return *this;
  1095. }
  1096. template <class Other>
  1097. BOOST_MP_CXX14_CONSTEXPR expression& operator*=(const Other&)
  1098. {
  1099. // This should always fail:
  1100. static_assert(sizeof(Other) == INT_MAX, "You can not use operator*= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1101. return *this;
  1102. }
  1103. template <class Other>
  1104. BOOST_MP_CXX14_CONSTEXPR expression& operator/=(const Other&)
  1105. {
  1106. // This should always fail:
  1107. static_assert(sizeof(Other) == INT_MAX, "You can not use operator/= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1108. return *this;
  1109. }
  1110. template <class Other>
  1111. BOOST_MP_CXX14_CONSTEXPR expression& operator%=(const Other&)
  1112. {
  1113. // This should always fail:
  1114. static_assert(sizeof(Other) == INT_MAX, "You can not use operator%= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1115. return *this;
  1116. }
  1117. template <class Other>
  1118. BOOST_MP_CXX14_CONSTEXPR expression& operator|=(const Other&)
  1119. {
  1120. // This should always fail:
  1121. static_assert(sizeof(Other) == INT_MAX, "You can not use operator|= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1122. return *this;
  1123. }
  1124. template <class Other>
  1125. BOOST_MP_CXX14_CONSTEXPR expression& operator&=(const Other&)
  1126. {
  1127. // This should always fail:
  1128. static_assert(sizeof(Other) == INT_MAX, "You can not use operator&= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1129. return *this;
  1130. }
  1131. template <class Other>
  1132. BOOST_MP_CXX14_CONSTEXPR expression& operator^=(const Other&)
  1133. {
  1134. // This should always fail:
  1135. static_assert(sizeof(Other) == INT_MAX, "You can not use operator^= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1136. return *this;
  1137. }
  1138. template <class Other>
  1139. BOOST_MP_CXX14_CONSTEXPR expression& operator<<=(const Other&)
  1140. {
  1141. // This should always fail:
  1142. static_assert(sizeof(Other) == INT_MAX, "You can not use operator<<= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1143. return *this;
  1144. }
  1145. template <class Other>
  1146. BOOST_MP_CXX14_CONSTEXPR expression& operator>>=(const Other&)
  1147. {
  1148. // This should always fail:
  1149. static_assert(sizeof(Other) == INT_MAX, "You can not use operator>>= on a Boost.Multiprecision expression template: did you inadvertantly store an expression template in a \"auto\" variable? Or pass an expression to a template function with deduced temnplate arguments?");
  1150. return *this;
  1151. }
  1152. BOOST_MP_CXX14_CONSTEXPR left_type left() const
  1153. {
  1154. return left_type(arg1);
  1155. }
  1156. BOOST_MP_CXX14_CONSTEXPR left_middle_type left_middle() const { return left_middle_type(arg2); }
  1157. BOOST_MP_CXX14_CONSTEXPR right_middle_type right_middle() const { return right_middle_type(arg3); }
  1158. BOOST_MP_CXX14_CONSTEXPR right_type right() const { return right_type(arg4); }
  1159. BOOST_MP_CXX14_CONSTEXPR const Arg1& left_ref() const noexcept { return arg1; }
  1160. BOOST_MP_CXX14_CONSTEXPR const Arg2& left_middle_ref() const noexcept { return arg2; }
  1161. BOOST_MP_CXX14_CONSTEXPR const Arg3& right_middle_ref() const noexcept { return arg3; }
  1162. BOOST_MP_CXX14_CONSTEXPR const Arg4& right_ref() const noexcept { return arg4; }
  1163. template <class T
  1164. #ifndef __SUNPRO_CC
  1165. ,
  1166. typename std::enable_if<!is_number<T>::value && !std::is_convertible<result_type, T const&>::value && std::is_constructible<T, result_type>::value, int>::type = 0
  1167. #endif
  1168. >
  1169. explicit BOOST_MP_CXX14_CONSTEXPR operator T() const
  1170. {
  1171. return static_cast<T>(static_cast<result_type>(*this));
  1172. }
  1173. BOOST_MP_FORCEINLINE explicit BOOST_MP_CXX14_CONSTEXPR operator bool() const
  1174. {
  1175. result_type r(*this);
  1176. return static_cast<bool>(r);
  1177. }
  1178. template <class T>
  1179. BOOST_MP_CXX14_CONSTEXPR T convert_to()
  1180. {
  1181. result_type r(*this);
  1182. return r.template convert_to<T>();
  1183. }
  1184. static constexpr unsigned left_depth = left_type::depth + 1;
  1185. static constexpr unsigned left_middle_depth = left_middle_type::depth + 1;
  1186. static constexpr unsigned right_middle_depth = right_middle_type::depth + 1;
  1187. static constexpr unsigned right_depth = right_type::depth + 1;
  1188. static constexpr unsigned left_max_depth = left_depth > left_middle_depth ? left_depth : left_middle_depth;
  1189. static constexpr unsigned right_max_depth = right_depth > right_middle_depth ? right_depth : right_middle_depth;
  1190. static constexpr unsigned depth = left_max_depth > right_max_depth ? left_max_depth : right_max_depth;
  1191. private:
  1192. typename expression_storage<Arg1>::type arg1;
  1193. typename expression_storage<Arg2>::type arg2;
  1194. typename expression_storage<Arg3>::type arg3;
  1195. typename expression_storage<Arg4>::type arg4;
  1196. expression& operator=(const expression&);
  1197. };
  1198. template <class T>
  1199. struct digits2
  1200. {
  1201. static_assert(std::numeric_limits<T>::is_specialized, "numeric_limits must be specialized here");
  1202. static_assert((std::numeric_limits<T>::radix == 2) || (std::numeric_limits<T>::radix == 10), "Failed radix check");
  1203. // If we really have so many digits that this fails, then we're probably going to hit other problems anyway:
  1204. static_assert(LONG_MAX / 1000 > (std::numeric_limits<T>::digits + 1), "Too many digits to cope with here");
  1205. static constexpr long m_value = std::numeric_limits<T>::radix == 10 ? (((std::numeric_limits<T>::digits + 1) * 1000L) / 301L) : std::numeric_limits<T>::digits;
  1206. static inline constexpr long value() noexcept { return m_value; }
  1207. };
  1208. #ifndef BOOST_MP_MIN_EXPONENT_DIGITS
  1209. #ifdef _MSC_VER
  1210. #define BOOST_MP_MIN_EXPONENT_DIGITS 2
  1211. #else
  1212. #define BOOST_MP_MIN_EXPONENT_DIGITS 2
  1213. #endif
  1214. #endif
  1215. template <class S>
  1216. void format_float_string(S& str, std::intmax_t my_exp, std::intmax_t digits, std::ios_base::fmtflags f, bool iszero)
  1217. {
  1218. using size_type = typename S::size_type;
  1219. bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
  1220. bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
  1221. bool showpoint = (f & std::ios_base::showpoint) == std::ios_base::showpoint;
  1222. bool showpos = (f & std::ios_base::showpos) == std::ios_base::showpos;
  1223. bool neg = str.size() && (str[0] == '-');
  1224. if (neg)
  1225. str.erase(0, 1);
  1226. if (digits == 0 && !fixed)
  1227. {
  1228. digits = static_cast<std::intmax_t>((std::max)(str.size(), size_type(16)));
  1229. }
  1230. if (iszero || str.empty() || (str.find_first_not_of('0') == S::npos))
  1231. {
  1232. // We will be printing zero, even though the value might not
  1233. // actually be zero (it just may have been rounded to zero).
  1234. str = "0";
  1235. if (scientific || fixed)
  1236. {
  1237. if (showpoint || digits > 0) {
  1238. str.append(1, '.');
  1239. if (digits > 0)
  1240. str.append(size_type(digits), '0');
  1241. }
  1242. if (scientific)
  1243. str.append("e+00");
  1244. }
  1245. else
  1246. {
  1247. if (showpoint)
  1248. {
  1249. str.append(1, '.');
  1250. if (digits > 1)
  1251. str.append(size_type(digits - 1), '0');
  1252. }
  1253. }
  1254. if (neg)
  1255. str.insert(static_cast<std::string::size_type>(0), 1, '-');
  1256. else if (showpos)
  1257. str.insert(static_cast<std::string::size_type>(0), 1, '+');
  1258. return;
  1259. }
  1260. if (!fixed && !scientific && !showpoint)
  1261. {
  1262. //
  1263. // Suppress trailing zeros:
  1264. //
  1265. std::string::iterator pos = str.end();
  1266. while (pos != str.begin() && *--pos == '0')
  1267. {
  1268. }
  1269. if (pos != str.end())
  1270. ++pos;
  1271. str.erase(pos, str.end());
  1272. if (str.empty())
  1273. str = '0';
  1274. }
  1275. else if (!fixed || (my_exp >= 0))
  1276. {
  1277. //
  1278. // Pad out the end with zero's if we need to:
  1279. //
  1280. std::intmax_t chars = static_cast<std::intmax_t>(str.size());
  1281. chars = digits - chars;
  1282. if (scientific)
  1283. ++chars;
  1284. if (chars > 0)
  1285. {
  1286. str.append(static_cast<std::string::size_type>(chars), '0');
  1287. }
  1288. }
  1289. if (fixed || (!scientific && (my_exp >= -4) && (my_exp < digits)))
  1290. {
  1291. if (1 + my_exp > static_cast<std::intmax_t>(str.size()))
  1292. {
  1293. // Just pad out the end with zeros:
  1294. str.append(static_cast<std::string::size_type>(1 + my_exp - static_cast<std::intmax_t>(str.size())), '0');
  1295. if (showpoint || (fixed && digits > 0))
  1296. str.append(".");
  1297. }
  1298. else if (my_exp + 1 < static_cast<std::intmax_t>(str.size()))
  1299. {
  1300. if (my_exp < 0)
  1301. {
  1302. str.insert(static_cast<std::string::size_type>(0), static_cast<std::string::size_type>(-1 - my_exp), '0');
  1303. str.insert(static_cast<std::string::size_type>(0), "0.");
  1304. }
  1305. else
  1306. {
  1307. // Insert the decimal point:
  1308. str.insert(static_cast<std::string::size_type>(my_exp + 1), 1, '.');
  1309. }
  1310. }
  1311. else if (showpoint || (fixed && digits > 0)) // we have exactly the digits we require to left of the point
  1312. str += ".";
  1313. if (fixed)
  1314. {
  1315. // We may need to add trailing zeros:
  1316. auto pos = str.find('.');
  1317. if (pos != str.npos) { // this test is probably redundant, but just to be safe and for clarity
  1318. std::intmax_t l = static_cast<std::intmax_t>(pos + 1);
  1319. l = static_cast<std::intmax_t>(digits - (static_cast<std::intmax_t>(str.size()) - l));
  1320. if (l > 0)
  1321. str.append(size_type(l), '0');
  1322. }
  1323. }
  1324. }
  1325. else
  1326. {
  1327. BOOST_MP_USING_ABS
  1328. // Scientific format:
  1329. if (showpoint || (str.size() > 1))
  1330. str.insert(static_cast<std::string::size_type>(1u), 1, '.');
  1331. str.append(static_cast<std::string::size_type>(1u), 'e');
  1332. S e;
  1333. #ifndef BOOST_MP_STANDALONE
  1334. e = boost::lexical_cast<S>(abs(my_exp));
  1335. #else
  1336. BOOST_IF_CONSTEXPR(std::is_same<S, std::string>::value)
  1337. {
  1338. e = std::to_string(abs(my_exp));
  1339. }
  1340. else
  1341. {
  1342. const std::string str_local_exp = std::to_string(abs(my_exp));
  1343. e = S(str_local_exp.cbegin(), str_local_exp.cend());
  1344. }
  1345. #endif
  1346. if (e.size() < BOOST_MP_MIN_EXPONENT_DIGITS)
  1347. e.insert(static_cast<std::string::size_type>(0), BOOST_MP_MIN_EXPONENT_DIGITS - e.size(), '0');
  1348. if (my_exp < 0)
  1349. e.insert(static_cast<std::string::size_type>(0), 1, '-');
  1350. else
  1351. e.insert(static_cast<std::string::size_type>(0), 1, '+');
  1352. str.append(e);
  1353. }
  1354. if (neg)
  1355. str.insert(static_cast<std::string::size_type>(0), 1, '-');
  1356. else if (showpos)
  1357. str.insert(static_cast<std::string::size_type>(0), 1, '+');
  1358. }
  1359. template <class V>
  1360. BOOST_MP_CXX14_CONSTEXPR void check_shift_range(V val, const std::integral_constant<bool, true>&, const std::integral_constant<bool, true>&)
  1361. {
  1362. if (val > (std::numeric_limits<std::size_t>::max)())
  1363. BOOST_MP_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
  1364. if (val < 0)
  1365. BOOST_MP_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
  1366. }
  1367. template <class V>
  1368. BOOST_MP_CXX14_CONSTEXPR void check_shift_range(V val, const std::integral_constant<bool, false>&, const std::integral_constant<bool, true>&)
  1369. {
  1370. if (val < 0)
  1371. BOOST_MP_THROW_EXCEPTION(std::out_of_range("Can not shift by a negative value."));
  1372. }
  1373. template <class V>
  1374. BOOST_MP_CXX14_CONSTEXPR void check_shift_range(V val, const std::integral_constant<bool, true>&, const std::integral_constant<bool, false>&)
  1375. {
  1376. if (val > (std::numeric_limits<std::size_t>::max)())
  1377. BOOST_MP_THROW_EXCEPTION(std::out_of_range("Can not shift by a value greater than std::numeric_limits<std::size_t>::max()."));
  1378. }
  1379. template <class V>
  1380. BOOST_MP_CXX14_CONSTEXPR void check_shift_range(V, const std::integral_constant<bool, false>&, const std::integral_constant<bool, false>&) noexcept {}
  1381. template <class T>
  1382. BOOST_MP_CXX14_CONSTEXPR const T& evaluate_if_expression(const T& val) { return val; }
  1383. template <class T>
  1384. BOOST_MP_CXX14_CONSTEXPR T&& evaluate_if_expression(T&& val) { return static_cast<T&&>(val); }
  1385. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  1386. BOOST_MP_CXX14_CONSTEXPR typename expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type evaluate_if_expression(const expression<tag, Arg1, Arg2, Arg3, Arg4>& val) { return val; }
  1387. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  1388. BOOST_MP_CXX14_CONSTEXPR typename expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type evaluate_if_expression(expression<tag, Arg1, Arg2, Arg3, Arg4>&& val) { return val; }
  1389. template <class T>
  1390. struct convertible_to
  1391. {
  1392. operator T () const;
  1393. };
  1394. } // namespace detail
  1395. //
  1396. // Traits class, lets us know what kind of number we have, defaults to a floating point type:
  1397. //
  1398. enum number_category_type
  1399. {
  1400. number_kind_unknown = -1,
  1401. number_kind_integer = 0,
  1402. number_kind_floating_point = 1,
  1403. number_kind_rational = 2,
  1404. number_kind_fixed_point = 3,
  1405. number_kind_complex = 4
  1406. };
  1407. template <class Num, bool, bool>
  1408. struct number_category_base : public std::integral_constant<int, number_kind_unknown>
  1409. {};
  1410. template <class Num>
  1411. struct number_category_base<Num, true, false> : public std::integral_constant<int, std::numeric_limits<Num>::is_integer ? number_kind_integer : (std::numeric_limits<Num>::max_exponent ? number_kind_floating_point : number_kind_unknown)>
  1412. {};
  1413. template <class Num>
  1414. struct number_category : public number_category_base<Num, std::is_class<Num>::value || boost::multiprecision::detail::is_arithmetic<Num>::value, std::is_abstract<Num>::value>
  1415. {};
  1416. template <class Backend, expression_template_option ExpressionTemplates>
  1417. struct number_category<number<Backend, ExpressionTemplates> > : public number_category<Backend>
  1418. {};
  1419. template <class tag, class A1, class A2, class A3, class A4>
  1420. struct number_category<detail::expression<tag, A1, A2, A3, A4> > : public number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>
  1421. {};
  1422. #if defined(__SIZEOF_FLOAT128__) || defined(BOOST_HAS_FLOAT128)
  1423. template <>
  1424. struct number_category<__float128> : public std::integral_constant<int, number_kind_floating_point> {};
  1425. #endif
  1426. //
  1427. // Specializations for types which do not always have numberic_limits specializations:
  1428. //
  1429. #ifdef BOOST_HAS_INT128
  1430. template <>
  1431. struct number_category<boost::multiprecision::int128_type> : public std::integral_constant<int, number_kind_integer>
  1432. {};
  1433. template <>
  1434. struct number_category<boost::multiprecision::uint128_type> : public std::integral_constant<int, number_kind_integer>
  1435. {};
  1436. #endif
  1437. template <class T>
  1438. struct component_type
  1439. {
  1440. using type = T;
  1441. };
  1442. template <class tag, class A1, class A2, class A3, class A4>
  1443. struct component_type<detail::expression<tag, A1, A2, A3, A4> > : public component_type<typename detail::expression<tag, A1, A2, A3, A4>::result_type>
  1444. {};
  1445. template <class T>
  1446. struct scalar_result_from_possible_complex
  1447. {
  1448. using type = typename std::conditional<number_category<T>::value == number_kind_complex, typename component_type<T>::type, T>::type;
  1449. };
  1450. template <class T>
  1451. struct complex_result_from_scalar; // individual backends must specialize this trait.
  1452. template <class T>
  1453. struct is_unsigned_number : public std::integral_constant<bool, false>
  1454. {};
  1455. template <class Backend, expression_template_option ExpressionTemplates>
  1456. struct is_unsigned_number<number<Backend, ExpressionTemplates> > : public is_unsigned_number<Backend>
  1457. {};
  1458. template <class T>
  1459. struct is_signed_number : public std::integral_constant<bool, !is_unsigned_number<T>::value>
  1460. {};
  1461. template <class T>
  1462. struct is_interval_number : public std::integral_constant<bool, false>
  1463. {};
  1464. template <class Backend, expression_template_option ExpressionTemplates>
  1465. struct is_interval_number<number<Backend, ExpressionTemplates> > : public is_interval_number<Backend>
  1466. {};
  1467. template <class T, class U>
  1468. struct is_equivalent_number_type : public std::is_same<T, U>
  1469. {};
  1470. template <class Backend, expression_template_option ExpressionTemplates, class T2>
  1471. struct is_equivalent_number_type<number<Backend, ExpressionTemplates>, T2> : public is_equivalent_number_type<Backend, T2>
  1472. {};
  1473. template <class T1, class Backend, expression_template_option ExpressionTemplates>
  1474. struct is_equivalent_number_type<T1, number<Backend, ExpressionTemplates> > : public is_equivalent_number_type<Backend, T1>
  1475. {};
  1476. template <class Backend, expression_template_option ExpressionTemplates, class Backend2, expression_template_option ExpressionTemplates2>
  1477. struct is_equivalent_number_type<number<Backend, ExpressionTemplates>, number<Backend2, ExpressionTemplates2> > : public is_equivalent_number_type<Backend, Backend2>
  1478. {};
  1479. }
  1480. } // namespace boost
  1481. #ifdef BOOST_MP_MATH_AVAILABLE
  1482. namespace boost { namespace math {
  1483. namespace tools {
  1484. template <class T>
  1485. struct promote_arg;
  1486. template <class tag, class A1, class A2, class A3, class A4>
  1487. struct promote_arg<boost::multiprecision::detail::expression<tag, A1, A2, A3, A4> >
  1488. {
  1489. using type = typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  1490. };
  1491. template <class R, class B, boost::multiprecision::expression_template_option ET>
  1492. inline R real_cast(const boost::multiprecision::number<B, ET>& val)
  1493. {
  1494. return val.template convert_to<R>();
  1495. }
  1496. template <class R, class tag, class A1, class A2, class A3, class A4>
  1497. inline R real_cast(const boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>& val)
  1498. {
  1499. using val_type = typename boost::multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  1500. return val_type(val).template convert_to<R>();
  1501. }
  1502. template <class B, boost::multiprecision::expression_template_option ET>
  1503. struct is_complex_type<boost::multiprecision::number<B, ET> > : public std::integral_constant<bool, boost::multiprecision::number_category<B>::value == boost::multiprecision::number_kind_complex> {};
  1504. } // namespace tools
  1505. namespace constants {
  1506. template <class T>
  1507. struct is_explicitly_convertible_from_string;
  1508. template <class B, boost::multiprecision::expression_template_option ET>
  1509. struct is_explicitly_convertible_from_string<boost::multiprecision::number<B, ET> >
  1510. {
  1511. static constexpr bool value = true;
  1512. };
  1513. } // namespace constants
  1514. }} // namespace boost::math
  1515. #endif
  1516. #ifdef BOOST_MSVC
  1517. #pragma warning(pop)
  1518. #endif
  1519. #endif // BOOST_MP_NUMBER_BASE_HPP