number_base.hpp 73 KB

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