number.hpp 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236
  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_EXTENDED_REAL_HPP
  6. #define BOOST_MATH_EXTENDED_REAL_HPP
  7. #include <boost/config.hpp>
  8. #include <boost/cstdint.hpp>
  9. #include <boost/mpl/max.hpp>
  10. #include <boost/mpl/plus.hpp>
  11. #include <boost/mpl/or.hpp>
  12. #include <boost/mpl/find_if.hpp>
  13. #include <boost/assert.hpp>
  14. #include <boost/type_traits/remove_pointer.hpp>
  15. #include <boost/type_traits/is_signed.hpp>
  16. #include <boost/type_traits/is_unsigned.hpp>
  17. #include <boost/type_traits/is_floating_point.hpp>
  18. #include <boost/type_traits/is_integral.hpp>
  19. #include <boost/type_traits/is_complex.hpp>
  20. #include <boost/type_traits/make_unsigned.hpp>
  21. #include <boost/type_traits/is_convertible.hpp>
  22. #include <boost/throw_exception.hpp>
  23. #include <boost/multiprecision/detail/precision.hpp>
  24. #include <boost/multiprecision/detail/generic_interconvert.hpp>
  25. #include <boost/multiprecision/detail/number_compare.hpp>
  26. #include <boost/multiprecision/traits/is_restricted_conversion.hpp>
  27. #include <boost/container_hash/hash.hpp>
  28. #include <istream> // stream operators
  29. #include <cstdio> // EOF
  30. #include <cctype> // isspace
  31. #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
  32. #include <string_view>
  33. #endif
  34. namespace boost {
  35. namespace multiprecision {
  36. #ifdef BOOST_MSVC
  37. // warning C4127: conditional expression is constant
  38. // warning C4714: function marked as __forceinline not inlined
  39. #pragma warning(push)
  40. #pragma warning(disable : 4127 4714 6326)
  41. #endif
  42. template <class Backend, expression_template_option ExpressionTemplates>
  43. class number
  44. {
  45. typedef number<Backend, ExpressionTemplates> self_type;
  46. public:
  47. typedef Backend backend_type;
  48. typedef typename component_type<self_type>::type value_type;
  49. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number() BOOST_MP_NOEXCEPT_IF(noexcept(Backend())) {}
  50. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const number& e) BOOST_MP_NOEXCEPT_IF(noexcept(Backend(std::declval<Backend const&>()))) : m_backend(e.m_backend) {}
  51. template <class V>
  52. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number(const V& v, typename boost::enable_if_c<
  53. (boost::is_arithmetic<V>::value || is_same<std::string, V>::value || is_convertible<V, const char*>::value) && !is_convertible<typename detail::canonical<V, Backend>::type, Backend>::value && !detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value
  54. #ifdef BOOST_HAS_FLOAT128
  55. && !boost::is_same<V, __float128>::value
  56. #endif
  57. >::type* = 0)
  58. {
  59. m_backend = canonical_value(v);
  60. }
  61. template <class V>
  62. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const V& v, typename boost::enable_if_c<
  63. is_convertible<typename detail::canonical<V, Backend>::type, Backend>::value && !detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value>::type* = 0)
  64. #ifndef BOOST_INTEL
  65. BOOST_MP_NOEXCEPT_IF(noexcept(Backend(std::declval<typename detail::canonical<V, Backend>::type const&>())))
  66. #endif
  67. : m_backend(canonical_value(v))
  68. {}
  69. template <class V>
  70. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const V& v, unsigned digits10, typename boost::enable_if_c<(boost::is_arithmetic<V>::value || is_same<std::string, V>::value || is_convertible<V, const char*>::value) && !detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value && (boost::multiprecision::number_category<Backend>::value != boost::multiprecision::number_kind_complex) && (boost::multiprecision::number_category<Backend>::value != boost::multiprecision::number_kind_rational)
  71. #ifdef BOOST_HAS_FLOAT128
  72. && !boost::is_same<V, __float128>::value
  73. #endif
  74. >::type* = 0)
  75. : m_backend(canonical_value(v), digits10)
  76. {}
  77. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const number& e, unsigned digits10)
  78. BOOST_MP_NOEXCEPT_IF(noexcept(Backend(std::declval<Backend const&>(), std::declval<unsigned>())))
  79. : m_backend(e.m_backend, digits10) {}
  80. template <class V>
  81. explicit BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number(const V& v, typename boost::enable_if_c<
  82. (boost::is_arithmetic<V>::value || is_same<std::string, V>::value || is_convertible<V, const char*>::value) && !detail::is_explicitly_convertible<typename detail::canonical<V, Backend>::type, Backend>::value && detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value>::type* = 0)
  83. BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<Backend&>() = std::declval<typename detail::canonical<V, Backend>::type const&>()))
  84. {
  85. m_backend = canonical_value(v);
  86. }
  87. template <class V>
  88. explicit BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const V& v, typename boost::enable_if_c<
  89. detail::is_explicitly_convertible<typename detail::canonical<V, Backend>::type, Backend>::value && (detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value || !is_convertible<typename detail::canonical<V, Backend>::type, Backend>::value)>::type* = 0)
  90. BOOST_MP_NOEXCEPT_IF(noexcept(Backend(std::declval<typename detail::canonical<V, Backend>::type const&>())))
  91. : m_backend(canonical_value(v)) {}
  92. template <class V>
  93. explicit BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const V& v, unsigned digits10, typename boost::enable_if_c<(boost::is_arithmetic<V>::value || is_same<std::string, V>::value || is_convertible<V, const char*>::value) && detail::is_restricted_conversion<typename detail::canonical<V, Backend>::type, Backend>::value && (boost::multiprecision::number_category<Backend>::value != boost::multiprecision::number_kind_complex) && (boost::multiprecision::number_category<Backend>::value != boost::multiprecision::number_kind_rational)>::type* = 0)
  94. : m_backend(canonical_value(v), digits10) {}
  95. template <expression_template_option ET>
  96. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(const number<Backend, ET>& val)
  97. BOOST_MP_NOEXCEPT_IF(noexcept(Backend(std::declval<Backend const&>()))) : m_backend(val.backend()) {}
  98. template <class Other, expression_template_option ET>
  99. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number(const number<Other, ET>& val,
  100. typename boost::enable_if_c<(boost::is_convertible<Other, Backend>::value && !detail::is_restricted_conversion<Other, Backend>::value)>::type* = 0)
  101. BOOST_MP_NOEXCEPT_IF(noexcept(Backend(std::declval<Other const&>())))
  102. : m_backend(val.backend()) {}
  103. template <class Other, expression_template_option ET>
  104. explicit BOOST_MP_CXX14_CONSTEXPR number(const number<Other, ET>& val, typename boost::enable_if_c<
  105. (!detail::is_explicitly_convertible<Other, Backend>::value)>::type* = 0)
  106. {
  107. //
  108. // Attempt a generic interconvertion:
  109. //
  110. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard_1(val);
  111. detail::scoped_default_precision<number<Other, ET> > precision_guard_2(val);
  112. using detail::generic_interconvert;
  113. generic_interconvert(backend(), val.backend(), number_category<Backend>(), number_category<Other>());
  114. }
  115. template <class Other, expression_template_option ET>
  116. explicit BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number(const number<Other, ET>& val, typename boost::enable_if_c<
  117. (detail::is_explicitly_convertible<Other, Backend>::value && (detail::is_restricted_conversion<Other, Backend>::value || !boost::is_convertible<Other, Backend>::value))>::type* = 0) BOOST_MP_NOEXCEPT_IF(noexcept(Backend(std::declval<Other const&>())))
  118. : m_backend(val.backend()) {}
  119. template <class V, class U>
  120. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number(const V& v1, const U& v2,
  121. typename boost::enable_if_c<(is_convertible<V, value_type>::value && is_convertible<U, value_type>::value && !is_same<typename component_type<self_type>::type, self_type>::value)>::type* = 0)
  122. {
  123. using default_ops::assign_components;
  124. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(v1, v2);
  125. assign_components(m_backend, canonical_value(detail::evaluate_if_expression(v1)), canonical_value(detail::evaluate_if_expression(v2)));
  126. }
  127. template <class V, class U>
  128. BOOST_MP_FORCEINLINE explicit BOOST_MP_CXX14_CONSTEXPR number(const V& v1, const U& v2,
  129. typename boost::enable_if_c<
  130. (is_constructible<value_type, V>::value || is_convertible<V, std::string>::value) && (is_constructible<value_type, U>::value || is_convertible<U, std::string>::value) && !is_same<typename component_type<self_type>::type, self_type>::value && !is_same<V, self_type>::value && !(is_convertible<V, value_type>::value && is_convertible<U, value_type>::value)>::type* = 0)
  131. {
  132. using default_ops::assign_components;
  133. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(v1, v2);
  134. assign_components(m_backend, canonical_value(detail::evaluate_if_expression(v1)), canonical_value(detail::evaluate_if_expression(v2)));
  135. }
  136. #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
  137. //
  138. // Support for new types in C++17
  139. //
  140. template <class Traits>
  141. explicit inline BOOST_MP_CXX14_CONSTEXPR number(const std::basic_string_view<char, Traits>& view)
  142. {
  143. using default_ops::assign_from_string_view;
  144. assign_from_string_view(this->backend(), view);
  145. }
  146. template <class Traits>
  147. explicit inline BOOST_MP_CXX14_CONSTEXPR number(const std::basic_string_view<char, Traits>& view_x, const std::basic_string_view<char, Traits>& view_y)
  148. {
  149. using default_ops::assign_from_string_view;
  150. assign_from_string_view(this->backend(), view_x, view_y);
  151. }
  152. template <class Traits>
  153. explicit BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number(const std::basic_string_view<char, Traits>& v, unsigned digits10)
  154. : m_backend(canonical_value(v), digits10) {}
  155. template <class Traits>
  156. BOOST_MP_CXX14_CONSTEXPR number& assign(const std::basic_string_view<char, Traits>& view)
  157. {
  158. using default_ops::assign_from_string_view;
  159. assign_from_string_view(this->backend(), view);
  160. return *this;
  161. }
  162. #endif
  163. template <class V, class U>
  164. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number(const V& v1, const U& v2, unsigned digits10,
  165. typename boost::enable_if_c<(is_convertible<V, value_type>::value && is_convertible<U, value_type>::value && !is_same<typename component_type<self_type>::type, self_type>::value)>::type* = 0)
  166. : m_backend(canonical_value(detail::evaluate_if_expression(v1)), canonical_value(detail::evaluate_if_expression(v2)), digits10)
  167. {}
  168. template <class V, class U>
  169. BOOST_MP_FORCEINLINE explicit BOOST_MP_CXX14_CONSTEXPR number(const V& v1, const U& v2, unsigned digits10,
  170. typename boost::enable_if_c<((is_constructible<value_type, V>::value || is_convertible<V, std::string>::value) && (is_constructible<value_type, U>::value || is_convertible<U, std::string>::value) && !is_same<typename component_type<self_type>::type, self_type>::value) && !(is_convertible<V, value_type>::value && is_convertible<U, value_type>::value)>::type* = 0)
  171. : m_backend(canonical_value(detail::evaluate_if_expression(v1)), canonical_value(detail::evaluate_if_expression(v2)), digits10) {}
  172. template <class Other, expression_template_option ET>
  173. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number(const number<Other, ET>& v1, const number<Other, ET>& v2, typename boost::enable_if_c<boost::is_convertible<Other, Backend>::value>::type* = 0)
  174. {
  175. using default_ops::assign_components;
  176. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(v1, v2);
  177. assign_components(m_backend, v1.backend(), v2.backend());
  178. }
  179. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  180. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  181. {
  182. typedef mpl::bool_<is_same<number, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value> tag_type;
  183. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(e);
  184. //
  185. // If the current precision of *this differs from that of expression e, then we
  186. // create a temporary (which will have the correct precision thanks to precision_guard)
  187. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  188. // which causes this code to be eliminated in the common case that this type is
  189. // not actually variable precision. Pre C++17 this code should still be mostly
  190. // optimised away, but we can't prevent instantiation of the dead code leading
  191. // to longer build and possibly link times.
  192. //
  193. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  194. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  195. {
  196. number t(e);
  197. return *this = BOOST_MP_MOVE(t);
  198. }
  199. do_assign(e, tag_type());
  200. return *this;
  201. }
  202. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  203. BOOST_MP_CXX14_CONSTEXPR number& assign(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  204. {
  205. typedef mpl::bool_<is_same<number, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value> tag_type;
  206. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(e);
  207. //
  208. // If the current precision of *this differs from that of expression e, then we
  209. // create a temporary (which will have the correct precision thanks to precision_guard)
  210. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  211. // which causes this code to be eliminated in the common case that this type is
  212. // not actually variable precision. Pre C++17 this code should still be mostly
  213. // optimised away, but we can't prevent instantiation of the dead code leading
  214. // to longer build and possibly link times.
  215. //
  216. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  217. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  218. {
  219. number t;
  220. t.assign(e);
  221. return *this = BOOST_MP_MOVE(t);
  222. }
  223. do_assign(e, tag_type());
  224. return *this;
  225. }
  226. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number& operator=(const number& e)
  227. BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<Backend&>() = std::declval<Backend const&>()))
  228. {
  229. m_backend = e.m_backend;
  230. return *this;
  231. }
  232. template <class V>
  233. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  234. operator=(const V& v)
  235. BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<Backend&>() = std::declval<const typename detail::canonical<V, Backend>::type&>()))
  236. {
  237. m_backend = canonical_value(v);
  238. return *this;
  239. }
  240. template <class V>
  241. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<Backend, ExpressionTemplates>& assign(const V& v)
  242. BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<Backend&>() = std::declval<const typename detail::canonical<V, Backend>::type&>()))
  243. {
  244. m_backend = canonical_value(v);
  245. return *this;
  246. }
  247. template <class V>
  248. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number<Backend, ExpressionTemplates>& assign(const V& v, unsigned digits10)
  249. BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<Backend&>() = std::declval<const typename detail::canonical<V, Backend>::type&>()))
  250. {
  251. number t(v, digits10);
  252. return *this = t;
  253. }
  254. template <class Other, expression_template_option ET>
  255. BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if<boost::multiprecision::detail::is_explicitly_convertible<Other, Backend>, number<Backend, ExpressionTemplates>&>::type
  256. assign(const number<Other, ET>& v)
  257. {
  258. //
  259. // Attempt a generic interconvertion:
  260. //
  261. using detail::generic_interconvert;
  262. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(v);
  263. detail::scoped_default_precision<number<Other, ET> > precision_guard2(v);
  264. //
  265. // If the current precision of *this differs from that of value v, then we
  266. // create a temporary (which will have the correct precision thanks to precision_guard)
  267. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  268. // which causes this code to be eliminated in the common case that this type is
  269. // not actually variable precision. Pre C++17 this code should still be mostly
  270. // optimised away, but we can't prevent instantiation of the dead code leading
  271. // to longer build and possibly link times.
  272. //
  273. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  274. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  275. {
  276. number t(v);
  277. return *this = BOOST_MP_MOVE(t);
  278. }
  279. generic_interconvert(backend(), v.backend(), number_category<Backend>(), number_category<Other>());
  280. return *this;
  281. }
  282. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  283. BOOST_MP_CXX14_CONSTEXPR number(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e, typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value>::type* = 0)
  284. {
  285. //
  286. // No preicsion guard here, we already have one in operator=
  287. //
  288. *this = e;
  289. }
  290. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  291. explicit BOOST_MP_CXX14_CONSTEXPR number(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e,
  292. typename boost::enable_if_c<!is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value && boost::multiprecision::detail::is_explicitly_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value>::type* = 0)
  293. {
  294. //
  295. // No precision guard as assign has one already:
  296. //
  297. assign(e);
  298. }
  299. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  300. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR number(number&& r)
  301. BOOST_MP_NOEXCEPT_IF(noexcept(Backend(std::declval<Backend>())))
  302. : m_backend(static_cast<Backend&&>(r.m_backend))
  303. {}
  304. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number& operator=(number&& r) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<Backend&>() = std::declval<Backend>()))
  305. {
  306. m_backend = static_cast<Backend&&>(r.m_backend);
  307. return *this;
  308. }
  309. #endif
  310. BOOST_MP_CXX14_CONSTEXPR number& operator+=(const self_type& val)
  311. {
  312. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, val);
  313. //
  314. // If the current precision of *this differs from that of expression e, then we
  315. // create a temporary (which will have the correct precision thanks to precision_guard)
  316. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  317. // which causes this code to be eliminated in the common case that this type is
  318. // not actually variable precision. Pre C++17 this code should still be mostly
  319. // optimised away, but we can't prevent instantiation of the dead code leading
  320. // to longer build and possibly link times.
  321. //
  322. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  323. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  324. {
  325. number t(*this + val);
  326. return *this = BOOST_MP_MOVE(t);
  327. }
  328. do_add(detail::expression<detail::terminal, self_type>(val), detail::terminal());
  329. return *this;
  330. }
  331. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  332. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator+=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  333. {
  334. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  335. // Create a copy if e contains this, but not if we're just doing a
  336. // x += x
  337. if ((contains_self(e) && !is_self(e)))
  338. {
  339. self_type temp(e);
  340. do_add(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  341. }
  342. else
  343. {
  344. do_add(e, tag());
  345. }
  346. return *this;
  347. }
  348. template <class Arg1, class Arg2, class Arg3, class Arg4>
  349. BOOST_MP_CXX14_CONSTEXPR number& operator+=(const detail::expression<detail::multiply_immediates, Arg1, Arg2, Arg3, Arg4>& e)
  350. {
  351. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  352. //
  353. // If the current precision of *this differs from that of expression e, then we
  354. // create a temporary (which will have the correct precision thanks to precision_guard)
  355. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  356. // which causes this code to be eliminated in the common case that this type is
  357. // not actually variable precision. Pre C++17 this code should still be mostly
  358. // optimised away, but we can't prevent instantiation of the dead code leading
  359. // to longer build and possibly link times.
  360. //
  361. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  362. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  363. {
  364. number t(*this + e);
  365. return *this = BOOST_MP_MOVE(t);
  366. }
  367. //
  368. // Fused multiply-add:
  369. //
  370. using default_ops::eval_multiply_add;
  371. eval_multiply_add(m_backend, canonical_value(e.left_ref()), canonical_value(e.right_ref()));
  372. return *this;
  373. }
  374. template <class V>
  375. typename boost::enable_if_c<boost::is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  376. BOOST_MP_CXX14_CONSTEXPR operator+=(const V& v)
  377. {
  378. using default_ops::eval_add;
  379. eval_add(m_backend, canonical_value(v));
  380. return *this;
  381. }
  382. BOOST_MP_CXX14_CONSTEXPR number& operator-=(const self_type& val)
  383. {
  384. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, val);
  385. //
  386. // If the current precision of *this differs from that of expression e, then we
  387. // create a temporary (which will have the correct precision thanks to precision_guard)
  388. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  389. // which causes this code to be eliminated in the common case that this type is
  390. // not actually variable precision. Pre C++17 this code should still be mostly
  391. // optimised away, but we can't prevent instantiation of the dead code leading
  392. // to longer build and possibly link times.
  393. //
  394. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  395. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  396. {
  397. number t(*this - val);
  398. return *this = BOOST_MP_MOVE(t);
  399. }
  400. do_subtract(detail::expression<detail::terminal, self_type>(val), detail::terminal());
  401. return *this;
  402. }
  403. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  404. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator-=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  405. {
  406. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  407. // Create a copy if e contains this:
  408. if (contains_self(e))
  409. {
  410. self_type temp(e);
  411. do_subtract(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  412. }
  413. else
  414. {
  415. do_subtract(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  416. }
  417. return *this;
  418. }
  419. template <class V>
  420. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  421. operator-=(const V& v)
  422. {
  423. using default_ops::eval_subtract;
  424. eval_subtract(m_backend, canonical_value(v));
  425. return *this;
  426. }
  427. template <class Arg1, class Arg2, class Arg3, class Arg4>
  428. BOOST_MP_CXX14_CONSTEXPR number& operator-=(const detail::expression<detail::multiply_immediates, Arg1, Arg2, Arg3, Arg4>& e)
  429. {
  430. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  431. //
  432. // If the current precision of *this differs from that of expression e, then we
  433. // create a temporary (which will have the correct precision thanks to precision_guard)
  434. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  435. // which causes this code to be eliminated in the common case that this type is
  436. // not actually variable precision. Pre C++17 this code should still be mostly
  437. // optimised away, but we can't prevent instantiation of the dead code leading
  438. // to longer build and possibly link times.
  439. //
  440. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  441. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  442. {
  443. number t(*this - e);
  444. return *this = BOOST_MP_MOVE(t);
  445. }
  446. //
  447. // Fused multiply-subtract:
  448. //
  449. using default_ops::eval_multiply_subtract;
  450. eval_multiply_subtract(m_backend, canonical_value(e.left_ref()), canonical_value(e.right_ref()));
  451. return *this;
  452. }
  453. BOOST_MP_CXX14_CONSTEXPR number& operator*=(const self_type& e)
  454. {
  455. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  456. //
  457. // If the current precision of *this differs from that of expression e, then we
  458. // create a temporary (which will have the correct precision thanks to precision_guard)
  459. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  460. // which causes this code to be eliminated in the common case that this type is
  461. // not actually variable precision. Pre C++17 this code should still be mostly
  462. // optimised away, but we can't prevent instantiation of the dead code leading
  463. // to longer build and possibly link times.
  464. //
  465. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  466. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  467. {
  468. number t(*this * e);
  469. return *this = BOOST_MP_MOVE(t);
  470. }
  471. do_multiplies(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  472. return *this;
  473. }
  474. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  475. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator*=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  476. {
  477. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  478. // Create a temporary if the RHS references *this, but not
  479. // if we're just doing an x *= x;
  480. if ((contains_self(e) && !is_self(e)))
  481. {
  482. self_type temp(e);
  483. do_multiplies(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  484. }
  485. else
  486. {
  487. do_multiplies(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  488. }
  489. return *this;
  490. }
  491. template <class V>
  492. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  493. operator*=(const V& v)
  494. {
  495. using default_ops::eval_multiply;
  496. eval_multiply(m_backend, canonical_value(v));
  497. return *this;
  498. }
  499. BOOST_MP_CXX14_CONSTEXPR number& operator%=(const self_type& e)
  500. {
  501. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  502. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  503. //
  504. // If the current precision of *this differs from that of expression e, then we
  505. // create a temporary (which will have the correct precision thanks to precision_guard)
  506. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  507. // which causes this code to be eliminated in the common case that this type is
  508. // not actually variable precision. Pre C++17 this code should still be mostly
  509. // optimised away, but we can't prevent instantiation of the dead code leading
  510. // to longer build and possibly link times.
  511. //
  512. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  513. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  514. {
  515. number t(*this % e);
  516. return *this = BOOST_MP_MOVE(t);
  517. }
  518. do_modulus(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  519. return *this;
  520. }
  521. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  522. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator%=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  523. {
  524. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  525. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  526. // Create a temporary if the RHS references *this:
  527. if (contains_self(e))
  528. {
  529. self_type temp(e);
  530. do_modulus(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  531. }
  532. else
  533. {
  534. do_modulus(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  535. }
  536. return *this;
  537. }
  538. template <class V>
  539. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  540. operator%=(const V& v)
  541. {
  542. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  543. using default_ops::eval_modulus;
  544. eval_modulus(m_backend, canonical_value(v));
  545. return *this;
  546. }
  547. //
  548. // These operators are *not* proto-ized.
  549. // The issue is that the increment/decrement must happen
  550. // even if the result of the operator *is never used*.
  551. // Possibly we could modify our expression wrapper to
  552. // execute the increment/decrement on destruction, but
  553. // correct implementation will be tricky, so defered for now...
  554. //
  555. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number& operator++()
  556. {
  557. using default_ops::eval_increment;
  558. eval_increment(m_backend);
  559. return *this;
  560. }
  561. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number& operator--()
  562. {
  563. using default_ops::eval_decrement;
  564. eval_decrement(m_backend);
  565. return *this;
  566. }
  567. inline BOOST_MP_CXX14_CONSTEXPR number operator++(int)
  568. {
  569. using default_ops::eval_increment;
  570. self_type temp(*this);
  571. eval_increment(m_backend);
  572. return temp;
  573. }
  574. inline BOOST_MP_CXX14_CONSTEXPR number operator--(int)
  575. {
  576. using default_ops::eval_decrement;
  577. self_type temp(*this);
  578. eval_decrement(m_backend);
  579. return temp;
  580. }
  581. template <class V>
  582. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_integral<V>::value, number&>::type operator<<=(V val)
  583. {
  584. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The left-shift operation is only valid for integer types");
  585. detail::check_shift_range(val, mpl::bool_<(sizeof(V) > sizeof(std::size_t))>(), mpl::bool_<is_signed<V>::value>());
  586. eval_left_shift(m_backend, static_cast<std::size_t>(canonical_value(val)));
  587. return *this;
  588. }
  589. template <class V>
  590. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_integral<V>::value, number&>::type operator>>=(V val)
  591. {
  592. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The right-shift operation is only valid for integer types");
  593. detail::check_shift_range(val, mpl::bool_<(sizeof(V) > sizeof(std::size_t))>(), mpl::bool_<is_signed<V>::value>());
  594. eval_right_shift(m_backend, static_cast<std::size_t>(canonical_value(val)));
  595. return *this;
  596. }
  597. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number& operator/=(const self_type& e)
  598. {
  599. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  600. //
  601. // If the current precision of *this differs from that of expression e, then we
  602. // create a temporary (which will have the correct precision thanks to precision_guard)
  603. // and then move the result into *this. In C++17 we add a leading "if constexpr"
  604. // which causes this code to be eliminated in the common case that this type is
  605. // not actually variable precision. Pre C++17 this code should still be mostly
  606. // optimised away, but we can't prevent instantiation of the dead code leading
  607. // to longer build and possibly link times.
  608. //
  609. BOOST_MP_CONSTEXPR_IF_VARIABLE_PRECISION(number)
  610. if (precision_guard.precision() != boost::multiprecision::detail::current_precision_of(*this))
  611. {
  612. number t(*this / e);
  613. return *this = BOOST_MP_MOVE(t);
  614. }
  615. do_divide(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  616. return *this;
  617. }
  618. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  619. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator/=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  620. {
  621. detail::scoped_default_precision<number<Backend, ExpressionTemplates> > precision_guard(*this, e);
  622. // Create a temporary if the RHS references *this:
  623. if (contains_self(e))
  624. {
  625. self_type temp(e);
  626. do_divide(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  627. }
  628. else
  629. {
  630. do_divide(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  631. }
  632. return *this;
  633. }
  634. template <class V>
  635. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  636. operator/=(const V& v)
  637. {
  638. using default_ops::eval_divide;
  639. eval_divide(m_backend, canonical_value(v));
  640. return *this;
  641. }
  642. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number& operator&=(const self_type& e)
  643. {
  644. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  645. do_bitwise_and(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  646. return *this;
  647. }
  648. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  649. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator&=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  650. {
  651. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  652. // Create a temporary if the RHS references *this, but not
  653. // if we're just doing an x &= x;
  654. if (contains_self(e) && !is_self(e))
  655. {
  656. self_type temp(e);
  657. do_bitwise_and(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  658. }
  659. else
  660. {
  661. do_bitwise_and(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  662. }
  663. return *this;
  664. }
  665. template <class V>
  666. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  667. operator&=(const V& v)
  668. {
  669. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  670. using default_ops::eval_bitwise_and;
  671. eval_bitwise_and(m_backend, canonical_value(v));
  672. return *this;
  673. }
  674. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number& operator|=(const self_type& e)
  675. {
  676. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  677. do_bitwise_or(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  678. return *this;
  679. }
  680. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  681. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator|=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  682. {
  683. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  684. // Create a temporary if the RHS references *this, but not
  685. // if we're just doing an x |= x;
  686. if (contains_self(e) && !is_self(e))
  687. {
  688. self_type temp(e);
  689. do_bitwise_or(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  690. }
  691. else
  692. {
  693. do_bitwise_or(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  694. }
  695. return *this;
  696. }
  697. template <class V>
  698. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  699. operator|=(const V& v)
  700. {
  701. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  702. using default_ops::eval_bitwise_or;
  703. eval_bitwise_or(m_backend, canonical_value(v));
  704. return *this;
  705. }
  706. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR number& operator^=(const self_type& e)
  707. {
  708. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  709. do_bitwise_xor(detail::expression<detail::terminal, self_type>(e), detail::terminal());
  710. return *this;
  711. }
  712. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  713. BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_convertible<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type, self_type>::value, number&>::type operator^=(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e)
  714. {
  715. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  716. if (contains_self(e))
  717. {
  718. self_type temp(e);
  719. do_bitwise_xor(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  720. }
  721. else
  722. {
  723. do_bitwise_xor(e, typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::tag_type());
  724. }
  725. return *this;
  726. }
  727. template <class V>
  728. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<V, self_type>::value, number<Backend, ExpressionTemplates>&>::type
  729. operator^=(const V& v)
  730. {
  731. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  732. using default_ops::eval_bitwise_xor;
  733. eval_bitwise_xor(m_backend, canonical_value(v));
  734. return *this;
  735. }
  736. //
  737. // swap:
  738. //
  739. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void swap(self_type& other) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<Backend>().swap(std::declval<Backend&>())))
  740. {
  741. m_backend.swap(other.backend());
  742. }
  743. //
  744. // Zero and sign:
  745. //
  746. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool is_zero() const
  747. {
  748. using default_ops::eval_is_zero;
  749. return eval_is_zero(m_backend);
  750. }
  751. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR int sign() const
  752. {
  753. using default_ops::eval_get_sign;
  754. return eval_get_sign(m_backend);
  755. }
  756. //
  757. // String conversion functions:
  758. //
  759. std::string str(std::streamsize digits = 0, std::ios_base::fmtflags f = std::ios_base::fmtflags(0)) const
  760. {
  761. return m_backend.str(digits, f);
  762. }
  763. template <class Archive>
  764. void serialize(Archive& ar, const unsigned int /*version*/)
  765. {
  766. ar& boost::make_nvp("backend", m_backend);
  767. }
  768. private:
  769. template <class T>
  770. BOOST_MP_CXX14_CONSTEXPR void convert_to_imp(T* result) const
  771. {
  772. using default_ops::eval_convert_to;
  773. eval_convert_to(result, m_backend);
  774. }
  775. template <class B2, expression_template_option ET>
  776. BOOST_MP_CXX14_CONSTEXPR void convert_to_imp(number<B2, ET>* result) const
  777. {
  778. result->assign(*this);
  779. }
  780. BOOST_MP_CXX14_CONSTEXPR void convert_to_imp(std::string* result) const
  781. {
  782. *result = this->str();
  783. }
  784. public:
  785. template <class T>
  786. BOOST_MP_CXX14_CONSTEXPR T convert_to() const
  787. {
  788. T result = T();
  789. convert_to_imp(&result);
  790. return result;
  791. }
  792. //
  793. // Use in boolean context, and explicit conversion operators:
  794. //
  795. #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
  796. #if (defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ < 7) && !defined(__clang__)) || (defined(BOOST_INTEL) && (BOOST_INTEL <= 1500))
  797. //
  798. // Horrible workaround for gcc-4.6.x which always prefers the template
  799. // operator bool() rather than the non-template operator when converting to
  800. // an arithmetic type:
  801. //
  802. template <class T, typename boost::enable_if_c<is_same<T, bool>::value, int>::type = 0>
  803. explicit operator T() const
  804. {
  805. using default_ops::eval_is_zero;
  806. return !eval_is_zero(backend());
  807. }
  808. template <class T, typename boost::disable_if_c<is_same<T, bool>::value || is_void<T>::value, int>::type = 0>
  809. explicit operator T() const
  810. {
  811. return this->template convert_to<T>();
  812. }
  813. #else
  814. #if BOOST_WORKAROUND(BOOST_MSVC, < 1900) || (defined(__apple_build_version__) && BOOST_WORKAROUND(__clang_major__, < 9))
  815. template <class T>
  816. #else
  817. template <class T, class = typename boost::disable_if_c<boost::is_constructible<T, self_type const&>::value || !boost::is_default_constructible<T>::value || (!boost::is_arithmetic<T>::value && !boost::is_complex<T>::value), T>::type>
  818. #endif
  819. explicit BOOST_MP_CXX14_CONSTEXPR operator T() const
  820. {
  821. return this->template convert_to<T>();
  822. }
  823. BOOST_MP_FORCEINLINE explicit BOOST_MP_CXX14_CONSTEXPR operator bool() const
  824. {
  825. return !is_zero();
  826. }
  827. #if BOOST_WORKAROUND(BOOST_GCC_VERSION, < 40800)
  828. BOOST_MP_FORCEINLINE explicit operator void() const
  829. {}
  830. #endif
  831. #endif
  832. #else
  833. typedef bool (self_type::*unmentionable_type)() const;
  834. BOOST_MP_FORCEINLINE operator unmentionable_type() const
  835. {
  836. return is_zero() ? 0 : &self_type::is_zero;
  837. }
  838. #endif
  839. //
  840. // Default precision:
  841. //
  842. static BOOST_MP_CXX14_CONSTEXPR unsigned default_precision() BOOST_NOEXCEPT
  843. {
  844. return Backend::default_precision();
  845. }
  846. static BOOST_MP_CXX14_CONSTEXPR void default_precision(unsigned digits10)
  847. {
  848. Backend::default_precision(digits10);
  849. }
  850. BOOST_MP_CXX14_CONSTEXPR unsigned precision() const BOOST_NOEXCEPT
  851. {
  852. return m_backend.precision();
  853. }
  854. BOOST_MP_CXX14_CONSTEXPR void precision(unsigned digits10)
  855. {
  856. m_backend.precision(digits10);
  857. }
  858. //
  859. // Comparison:
  860. //
  861. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR int compare(const number<Backend, ExpressionTemplates>& o) const
  862. BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<Backend>().compare(std::declval<Backend>())))
  863. {
  864. return m_backend.compare(o.m_backend);
  865. }
  866. template <class V>
  867. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_arithmetic<V>::value && (number_category<Backend>::value != number_kind_complex), int>::type compare(const V& o) const
  868. {
  869. using default_ops::eval_get_sign;
  870. if (o == 0)
  871. return eval_get_sign(m_backend);
  872. return m_backend.compare(canonical_value(o));
  873. }
  874. template <class V>
  875. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR typename boost::enable_if_c<is_arithmetic<V>::value && (number_category<Backend>::value == number_kind_complex), int>::type compare(const V& o) const
  876. {
  877. using default_ops::eval_get_sign;
  878. return m_backend.compare(canonical_value(o));
  879. }
  880. //
  881. // Direct access to the underlying backend:
  882. //
  883. #if !(defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_NO_CXX11_REF_QUALIFIERS) || BOOST_WORKAROUND(BOOST_GCC, < 50000))
  884. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR Backend& backend() & BOOST_NOEXCEPT
  885. {
  886. return m_backend;
  887. }
  888. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& backend() const& BOOST_NOEXCEPT { return m_backend; }
  889. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR Backend&& backend() && BOOST_NOEXCEPT { return static_cast<Backend&&>(m_backend); }
  890. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR Backend const&& backend() const&& BOOST_NOEXCEPT { return static_cast<Backend const&&>(m_backend); }
  891. #else
  892. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR Backend& backend() BOOST_NOEXCEPT
  893. {
  894. return m_backend;
  895. }
  896. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& backend() const BOOST_NOEXCEPT { return m_backend; }
  897. #endif
  898. //
  899. // Complex number real and imag:
  900. //
  901. BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<number<Backend, ExpressionTemplates> >::type
  902. real() const
  903. {
  904. using default_ops::eval_real;
  905. detail::scoped_default_precision<typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type> precision_guard(*this);
  906. typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type result;
  907. eval_real(result.backend(), backend());
  908. return result;
  909. }
  910. BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<number<Backend, ExpressionTemplates> >::type
  911. imag() const
  912. {
  913. using default_ops::eval_imag;
  914. detail::scoped_default_precision<typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type> precision_guard(*this);
  915. typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type result;
  916. eval_imag(result.backend(), backend());
  917. return result;
  918. }
  919. template <class T>
  920. inline BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<boost::is_convertible<T, self_type>::value, self_type&>::type real(const T& val)
  921. {
  922. using default_ops::eval_set_real;
  923. eval_set_real(backend(), canonical_value(val));
  924. return *this;
  925. }
  926. template <class T>
  927. inline BOOST_MP_CXX14_CONSTEXPR typename enable_if_c<boost::is_convertible<T, self_type>::value && number_category<self_type>::value == number_kind_complex, self_type&>::type imag(const T& val)
  928. {
  929. using default_ops::eval_set_imag;
  930. eval_set_imag(backend(), canonical_value(val));
  931. return *this;
  932. }
  933. private:
  934. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  935. BOOST_MP_CXX14_CONSTEXPR void do_assign(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e, const mpl::true_&)
  936. {
  937. do_assign(e, tag());
  938. }
  939. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  940. BOOST_MP_CXX14_CONSTEXPR void do_assign(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& e, const mpl::false_&)
  941. {
  942. // The result of the expression isn't the same type as this -
  943. // create a temporary result and assign it to *this:
  944. typedef typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type temp_type;
  945. temp_type t(e);
  946. this->assign(t);
  947. }
  948. template <class Exp>
  949. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::add_immediates&)
  950. {
  951. using default_ops::eval_add;
  952. boost::multiprecision::detail::maybe_promote_precision(this);
  953. eval_add(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  954. }
  955. template <class Exp>
  956. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::subtract_immediates&)
  957. {
  958. using default_ops::eval_subtract;
  959. boost::multiprecision::detail::maybe_promote_precision(this);
  960. eval_subtract(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  961. }
  962. template <class Exp>
  963. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::multiply_immediates&)
  964. {
  965. using default_ops::eval_multiply;
  966. boost::multiprecision::detail::maybe_promote_precision(this);
  967. eval_multiply(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  968. }
  969. template <class Exp>
  970. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::multiply_add&)
  971. {
  972. using default_ops::eval_multiply_add;
  973. boost::multiprecision::detail::maybe_promote_precision(this);
  974. eval_multiply_add(m_backend, canonical_value(e.left().value()), canonical_value(e.middle().value()), canonical_value(e.right().value()));
  975. }
  976. template <class Exp>
  977. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::multiply_subtract&)
  978. {
  979. using default_ops::eval_multiply_subtract;
  980. boost::multiprecision::detail::maybe_promote_precision(this);
  981. eval_multiply_subtract(m_backend, canonical_value(e.left().value()), canonical_value(e.middle().value()), canonical_value(e.right().value()));
  982. }
  983. template <class Exp>
  984. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::divide_immediates&)
  985. {
  986. using default_ops::eval_divide;
  987. boost::multiprecision::detail::maybe_promote_precision(this);
  988. eval_divide(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  989. }
  990. template <class Exp>
  991. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::negate&)
  992. {
  993. typedef typename Exp::left_type left_type;
  994. do_assign(e.left(), typename left_type::tag_type());
  995. m_backend.negate();
  996. }
  997. template <class Exp>
  998. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::plus&)
  999. {
  1000. typedef typename Exp::left_type left_type;
  1001. typedef typename Exp::right_type right_type;
  1002. BOOST_CONSTEXPR int const left_depth = left_type::depth;
  1003. BOOST_CONSTEXPR int const right_depth = right_type::depth;
  1004. bool bl = contains_self(e.left());
  1005. bool br = contains_self(e.right());
  1006. if (bl && br)
  1007. {
  1008. self_type temp(e);
  1009. temp.m_backend.swap(this->m_backend);
  1010. }
  1011. else if (bl && is_self(e.left()))
  1012. {
  1013. // Ignore the left node, it's *this, just add the right:
  1014. do_add(e.right(), typename right_type::tag_type());
  1015. }
  1016. else if (br && is_self(e.right()))
  1017. {
  1018. // Ignore the right node, it's *this, just add the left:
  1019. do_add(e.left(), typename left_type::tag_type());
  1020. }
  1021. else if (!br && (bl || (left_depth >= right_depth)))
  1022. { // br is always false, but if bl is true we must take the this branch:
  1023. do_assign(e.left(), typename left_type::tag_type());
  1024. do_add(e.right(), typename right_type::tag_type());
  1025. }
  1026. else
  1027. {
  1028. do_assign(e.right(), typename right_type::tag_type());
  1029. do_add(e.left(), typename left_type::tag_type());
  1030. }
  1031. }
  1032. template <class Exp>
  1033. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::minus&)
  1034. {
  1035. typedef typename Exp::left_type left_type;
  1036. typedef typename Exp::right_type right_type;
  1037. BOOST_CONSTEXPR int const left_depth = left_type::depth;
  1038. BOOST_CONSTEXPR int const right_depth = right_type::depth;
  1039. bool bl = contains_self(e.left());
  1040. bool br = contains_self(e.right());
  1041. if (bl && br)
  1042. {
  1043. self_type temp(e);
  1044. temp.m_backend.swap(this->m_backend);
  1045. }
  1046. else if (bl && is_self(e.left()))
  1047. {
  1048. // Ignore the left node, it's *this, just subtract the right:
  1049. do_subtract(e.right(), typename right_type::tag_type());
  1050. }
  1051. else if (br && is_self(e.right()))
  1052. {
  1053. // Ignore the right node, it's *this, just subtract the left and negate the result:
  1054. do_subtract(e.left(), typename left_type::tag_type());
  1055. m_backend.negate();
  1056. }
  1057. else if (!br && (bl || (left_depth >= right_depth)))
  1058. { // br is always false, but if bl is true we must take the this branch:
  1059. do_assign(e.left(), typename left_type::tag_type());
  1060. do_subtract(e.right(), typename right_type::tag_type());
  1061. }
  1062. else
  1063. {
  1064. do_assign(e.right(), typename right_type::tag_type());
  1065. do_subtract(e.left(), typename left_type::tag_type());
  1066. m_backend.negate();
  1067. }
  1068. }
  1069. template <class Exp>
  1070. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::multiplies&)
  1071. {
  1072. typedef typename Exp::left_type left_type;
  1073. typedef typename Exp::right_type right_type;
  1074. BOOST_CONSTEXPR int const left_depth = left_type::depth;
  1075. BOOST_CONSTEXPR int const right_depth = right_type::depth;
  1076. bool bl = contains_self(e.left());
  1077. bool br = contains_self(e.right());
  1078. if (bl && br)
  1079. {
  1080. self_type temp(e);
  1081. temp.m_backend.swap(this->m_backend);
  1082. }
  1083. else if (bl && is_self(e.left()))
  1084. {
  1085. // Ignore the left node, it's *this, just add the right:
  1086. do_multiplies(e.right(), typename right_type::tag_type());
  1087. }
  1088. else if (br && is_self(e.right()))
  1089. {
  1090. // Ignore the right node, it's *this, just add the left:
  1091. do_multiplies(e.left(), typename left_type::tag_type());
  1092. }
  1093. else if (!br && (bl || (left_depth >= right_depth)))
  1094. { // br is always false, but if bl is true we must take the this branch:
  1095. do_assign(e.left(), typename left_type::tag_type());
  1096. do_multiplies(e.right(), typename right_type::tag_type());
  1097. }
  1098. else
  1099. {
  1100. do_assign(e.right(), typename right_type::tag_type());
  1101. do_multiplies(e.left(), typename left_type::tag_type());
  1102. }
  1103. }
  1104. template <class Exp>
  1105. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::divides&)
  1106. {
  1107. typedef typename Exp::left_type left_type;
  1108. typedef typename Exp::right_type right_type;
  1109. bool bl = contains_self(e.left());
  1110. bool br = contains_self(e.right());
  1111. if (bl && is_self(e.left()))
  1112. {
  1113. // Ignore the left node, it's *this, just add the right:
  1114. do_divide(e.right(), typename right_type::tag_type());
  1115. }
  1116. else if (br)
  1117. {
  1118. self_type temp(e);
  1119. temp.m_backend.swap(this->m_backend);
  1120. }
  1121. else
  1122. {
  1123. do_assign(e.left(), typename left_type::tag_type());
  1124. do_divide(e.right(), typename right_type::tag_type());
  1125. }
  1126. }
  1127. template <class Exp>
  1128. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::modulus&)
  1129. {
  1130. //
  1131. // This operation is only valid for integer backends:
  1132. //
  1133. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  1134. typedef typename Exp::left_type left_type;
  1135. typedef typename Exp::right_type right_type;
  1136. bool bl = contains_self(e.left());
  1137. bool br = contains_self(e.right());
  1138. if (bl && is_self(e.left()))
  1139. {
  1140. // Ignore the left node, it's *this, just add the right:
  1141. do_modulus(e.right(), typename right_type::tag_type());
  1142. }
  1143. else if (br)
  1144. {
  1145. self_type temp(e);
  1146. temp.m_backend.swap(this->m_backend);
  1147. }
  1148. else
  1149. {
  1150. do_assign(e.left(), typename left_type::tag_type());
  1151. do_modulus(e.right(), typename right_type::tag_type());
  1152. }
  1153. }
  1154. template <class Exp>
  1155. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::modulus_immediates&)
  1156. {
  1157. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  1158. using default_ops::eval_modulus;
  1159. boost::multiprecision::detail::maybe_promote_precision(this);
  1160. eval_modulus(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  1161. }
  1162. template <class Exp>
  1163. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::bitwise_and&)
  1164. {
  1165. //
  1166. // This operation is only valid for integer backends:
  1167. //
  1168. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  1169. typedef typename Exp::left_type left_type;
  1170. typedef typename Exp::right_type right_type;
  1171. BOOST_CONSTEXPR int const left_depth = left_type::depth;
  1172. BOOST_CONSTEXPR int const right_depth = right_type::depth;
  1173. bool bl = contains_self(e.left());
  1174. bool br = contains_self(e.right());
  1175. if (bl && is_self(e.left()))
  1176. {
  1177. // Ignore the left node, it's *this, just add the right:
  1178. do_bitwise_and(e.right(), typename right_type::tag_type());
  1179. }
  1180. else if (br && is_self(e.right()))
  1181. {
  1182. do_bitwise_and(e.left(), typename left_type::tag_type());
  1183. }
  1184. else if (!br && (bl || (left_depth >= right_depth)))
  1185. {
  1186. do_assign(e.left(), typename left_type::tag_type());
  1187. do_bitwise_and(e.right(), typename right_type::tag_type());
  1188. }
  1189. else
  1190. {
  1191. do_assign(e.right(), typename right_type::tag_type());
  1192. do_bitwise_and(e.left(), typename left_type::tag_type());
  1193. }
  1194. }
  1195. template <class Exp>
  1196. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::bitwise_and_immediates&)
  1197. {
  1198. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  1199. using default_ops::eval_bitwise_and;
  1200. eval_bitwise_and(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  1201. }
  1202. template <class Exp>
  1203. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::bitwise_or&)
  1204. {
  1205. //
  1206. // This operation is only valid for integer backends:
  1207. //
  1208. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  1209. typedef typename Exp::left_type left_type;
  1210. typedef typename Exp::right_type right_type;
  1211. BOOST_CONSTEXPR int const left_depth = left_type::depth;
  1212. BOOST_CONSTEXPR int const right_depth = right_type::depth;
  1213. bool bl = contains_self(e.left());
  1214. bool br = contains_self(e.right());
  1215. if (bl && is_self(e.left()))
  1216. {
  1217. // Ignore the left node, it's *this, just add the right:
  1218. do_bitwise_or(e.right(), typename right_type::tag_type());
  1219. }
  1220. else if (br && is_self(e.right()))
  1221. {
  1222. do_bitwise_or(e.left(), typename left_type::tag_type());
  1223. }
  1224. else if (!br && (bl || (left_depth >= right_depth)))
  1225. {
  1226. do_assign(e.left(), typename left_type::tag_type());
  1227. do_bitwise_or(e.right(), typename right_type::tag_type());
  1228. }
  1229. else
  1230. {
  1231. do_assign(e.right(), typename right_type::tag_type());
  1232. do_bitwise_or(e.left(), typename left_type::tag_type());
  1233. }
  1234. }
  1235. template <class Exp>
  1236. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::bitwise_or_immediates&)
  1237. {
  1238. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  1239. using default_ops::eval_bitwise_or;
  1240. eval_bitwise_or(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  1241. }
  1242. template <class Exp>
  1243. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::bitwise_xor&)
  1244. {
  1245. //
  1246. // This operation is only valid for integer backends:
  1247. //
  1248. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  1249. typedef typename Exp::left_type left_type;
  1250. typedef typename Exp::right_type right_type;
  1251. BOOST_CONSTEXPR int const left_depth = left_type::depth;
  1252. BOOST_CONSTEXPR int const right_depth = right_type::depth;
  1253. bool bl = contains_self(e.left());
  1254. bool br = contains_self(e.right());
  1255. if (bl && is_self(e.left()))
  1256. {
  1257. // Ignore the left node, it's *this, just add the right:
  1258. do_bitwise_xor(e.right(), typename right_type::tag_type());
  1259. }
  1260. else if (br && is_self(e.right()))
  1261. {
  1262. do_bitwise_xor(e.left(), typename left_type::tag_type());
  1263. }
  1264. else if (!br && (bl || (left_depth >= right_depth)))
  1265. {
  1266. do_assign(e.left(), typename left_type::tag_type());
  1267. do_bitwise_xor(e.right(), typename right_type::tag_type());
  1268. }
  1269. else
  1270. {
  1271. do_assign(e.right(), typename right_type::tag_type());
  1272. do_bitwise_xor(e.left(), typename left_type::tag_type());
  1273. }
  1274. }
  1275. template <class Exp>
  1276. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::bitwise_xor_immediates&)
  1277. {
  1278. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "Bitwise operations are only valid for integer types");
  1279. using default_ops::eval_bitwise_xor;
  1280. eval_bitwise_xor(m_backend, canonical_value(e.left().value()), canonical_value(e.right().value()));
  1281. }
  1282. template <class Exp>
  1283. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::terminal&)
  1284. {
  1285. if (!is_self(e))
  1286. {
  1287. m_backend = canonical_value(e.value());
  1288. }
  1289. }
  1290. template <class Exp>
  1291. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::function&)
  1292. {
  1293. typedef typename Exp::arity tag_type;
  1294. boost::multiprecision::detail::maybe_promote_precision(this);
  1295. do_assign_function(e, tag_type());
  1296. }
  1297. template <class Exp>
  1298. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::shift_left&)
  1299. {
  1300. // We can only shift by an integer value, not an arbitrary expression:
  1301. typedef typename Exp::left_type left_type;
  1302. typedef typename Exp::right_type right_type;
  1303. typedef typename right_type::arity right_arity;
  1304. BOOST_STATIC_ASSERT_MSG(right_arity::value == 0, "The left shift operator requires an integer value for the shift operand.");
  1305. typedef typename right_type::result_type right_value_type;
  1306. BOOST_STATIC_ASSERT_MSG(is_integral<right_value_type>::value, "The left shift operator requires an integer value for the shift operand.");
  1307. typedef typename left_type::tag_type tag_type;
  1308. do_assign_left_shift(e.left(), canonical_value(e.right().value()), tag_type());
  1309. }
  1310. template <class Exp>
  1311. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::shift_right&)
  1312. {
  1313. // We can only shift by an integer value, not an arbitrary expression:
  1314. typedef typename Exp::left_type left_type;
  1315. typedef typename Exp::right_type right_type;
  1316. typedef typename right_type::arity right_arity;
  1317. BOOST_STATIC_ASSERT_MSG(right_arity::value == 0, "The left shift operator requires an integer value for the shift operand.");
  1318. typedef typename right_type::result_type right_value_type;
  1319. BOOST_STATIC_ASSERT_MSG(is_integral<right_value_type>::value, "The left shift operator requires an integer value for the shift operand.");
  1320. typedef typename left_type::tag_type tag_type;
  1321. do_assign_right_shift(e.left(), canonical_value(e.right().value()), tag_type());
  1322. }
  1323. template <class Exp>
  1324. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::bitwise_complement&)
  1325. {
  1326. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ~ operation is only valid for integer types");
  1327. using default_ops::eval_complement;
  1328. self_type temp(e.left());
  1329. eval_complement(m_backend, temp.backend());
  1330. }
  1331. template <class Exp>
  1332. BOOST_MP_CXX14_CONSTEXPR void do_assign(const Exp& e, const detail::complement_immediates&)
  1333. {
  1334. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ~ operation is only valid for integer types");
  1335. using default_ops::eval_complement;
  1336. eval_complement(m_backend, canonical_value(e.left().value()));
  1337. }
  1338. template <class Exp, class Val>
  1339. BOOST_MP_CXX14_CONSTEXPR void do_assign_right_shift(const Exp& e, const Val& val, const detail::terminal&)
  1340. {
  1341. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The right shift operation is only valid for integer types");
  1342. using default_ops::eval_right_shift;
  1343. detail::check_shift_range(val, mpl::bool_<(sizeof(Val) > sizeof(std::size_t))>(), mpl::bool_<is_signed<Val>::value>());
  1344. eval_right_shift(m_backend, canonical_value(e.value()), static_cast<std::size_t>(val));
  1345. }
  1346. template <class Exp, class Val>
  1347. BOOST_MP_CXX14_CONSTEXPR void do_assign_left_shift(const Exp& e, const Val& val, const detail::terminal&)
  1348. {
  1349. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The left shift operation is only valid for integer types");
  1350. using default_ops::eval_left_shift;
  1351. detail::check_shift_range(val, mpl::bool_<(sizeof(Val) > sizeof(std::size_t))>(), mpl::bool_<is_signed<Val>::value>());
  1352. eval_left_shift(m_backend, canonical_value(e.value()), static_cast<std::size_t>(val));
  1353. }
  1354. template <class Exp, class Val, class Tag>
  1355. BOOST_MP_CXX14_CONSTEXPR void do_assign_right_shift(const Exp& e, const Val& val, const Tag&)
  1356. {
  1357. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The right shift operation is only valid for integer types");
  1358. using default_ops::eval_right_shift;
  1359. self_type temp(e);
  1360. detail::check_shift_range(val, mpl::bool_<(sizeof(Val) > sizeof(std::size_t))>(), mpl::bool_<is_signed<Val>::value>());
  1361. eval_right_shift(m_backend, temp.backend(), static_cast<std::size_t>(val));
  1362. }
  1363. template <class Exp, class Val, class Tag>
  1364. BOOST_MP_CXX14_CONSTEXPR void do_assign_left_shift(const Exp& e, const Val& val, const Tag&)
  1365. {
  1366. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The left shift operation is only valid for integer types");
  1367. using default_ops::eval_left_shift;
  1368. self_type temp(e);
  1369. detail::check_shift_range(val, mpl::bool_<(sizeof(Val) > sizeof(std::size_t))>(), mpl::bool_<is_signed<Val>::value>());
  1370. eval_left_shift(m_backend, temp.backend(), static_cast<std::size_t>(val));
  1371. }
  1372. template <class Exp>
  1373. BOOST_MP_CXX14_CONSTEXPR void do_assign_function(const Exp& e, const mpl::int_<1>&)
  1374. {
  1375. e.left().value()(&m_backend);
  1376. }
  1377. template <class Exp>
  1378. BOOST_MP_CXX14_CONSTEXPR void do_assign_function(const Exp& e, const mpl::int_<2>&)
  1379. {
  1380. typedef typename Exp::right_type right_type;
  1381. typedef typename right_type::tag_type tag_type;
  1382. do_assign_function_1(e.left().value(), e.right_ref(), tag_type());
  1383. }
  1384. template <class F, class Exp>
  1385. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_1(const F& f, const Exp& val, const detail::terminal&)
  1386. {
  1387. f(m_backend, function_arg_value(val));
  1388. }
  1389. template <class F, class Exp, class Tag>
  1390. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_1(const F& f, const Exp& val, const Tag&)
  1391. {
  1392. typename Exp::result_type t(val);
  1393. f(m_backend, t.backend());
  1394. }
  1395. template <class Exp>
  1396. BOOST_MP_CXX14_CONSTEXPR void do_assign_function(const Exp& e, const mpl::int_<3>&)
  1397. {
  1398. typedef typename Exp::middle_type middle_type;
  1399. typedef typename middle_type::tag_type tag_type;
  1400. typedef typename Exp::right_type end_type;
  1401. typedef typename end_type::tag_type end_tag;
  1402. do_assign_function_2(e.left().value(), e.middle_ref(), e.right_ref(), tag_type(), end_tag());
  1403. }
  1404. template <class F, class Exp1, class Exp2>
  1405. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_2(const F& f, const Exp1& val1, const Exp2& val2, const detail::terminal&, const detail::terminal&)
  1406. {
  1407. f(m_backend, function_arg_value(val1), function_arg_value(val2));
  1408. }
  1409. template <class F, class Exp1, class Exp2, class Tag1>
  1410. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_2(const F& f, const Exp1& val1, const Exp2& val2, const Tag1&, const detail::terminal&)
  1411. {
  1412. typename Exp1::result_type temp1(val1);
  1413. f(m_backend, BOOST_MP_MOVE(temp1.backend()), function_arg_value(val2));
  1414. }
  1415. template <class F, class Exp1, class Exp2, class Tag2>
  1416. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_2(const F& f, const Exp1& val1, const Exp2& val2, const detail::terminal&, const Tag2&)
  1417. {
  1418. typename Exp2::result_type temp2(val2);
  1419. f(m_backend, function_arg_value(val1), BOOST_MP_MOVE(temp2.backend()));
  1420. }
  1421. template <class F, class Exp1, class Exp2, class Tag1, class Tag2>
  1422. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_2(const F& f, const Exp1& val1, const Exp2& val2, const Tag1&, const Tag2&)
  1423. {
  1424. typename Exp1::result_type temp1(val1);
  1425. typename Exp2::result_type temp2(val2);
  1426. f(m_backend, BOOST_MP_MOVE(temp1.backend()), BOOST_MP_MOVE(temp2.backend()));
  1427. }
  1428. template <class Exp>
  1429. BOOST_MP_CXX14_CONSTEXPR void do_assign_function(const Exp& e, const mpl::int_<4>&)
  1430. {
  1431. typedef typename Exp::left_middle_type left_type;
  1432. typedef typename left_type::tag_type left_tag_type;
  1433. typedef typename Exp::right_middle_type middle_type;
  1434. typedef typename middle_type::tag_type middle_tag_type;
  1435. typedef typename Exp::right_type right_type;
  1436. typedef typename right_type::tag_type right_tag_type;
  1437. do_assign_function_3a(e.left().value(), e.left_middle_ref(), e.right_middle_ref(), e.right_ref(), left_tag_type(), middle_tag_type(), right_tag_type());
  1438. }
  1439. template <class F, class Exp1, class Exp2, class Exp3, class Tag2, class Tag3>
  1440. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_3a(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const detail::terminal&, const Tag2& t2, const Tag3& t3)
  1441. {
  1442. do_assign_function_3b(f, val1, val2, val3, t2, t3);
  1443. }
  1444. template <class F, class Exp1, class Exp2, class Exp3, class Tag1, class Tag2, class Tag3>
  1445. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_3a(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag1&, const Tag2& t2, const Tag3& t3)
  1446. {
  1447. typename Exp1::result_type t(val1);
  1448. do_assign_function_3b(f, BOOST_MP_MOVE(t), val2, val3, t2, t3);
  1449. }
  1450. template <class F, class Exp1, class Exp2, class Exp3, class Tag3>
  1451. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_3b(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const detail::terminal&, const Tag3& t3)
  1452. {
  1453. do_assign_function_3c(f, val1, val2, val3, t3);
  1454. }
  1455. template <class F, class Exp1, class Exp2, class Exp3, class Tag2, class Tag3>
  1456. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_3b(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag2& /*t2*/, const Tag3& t3)
  1457. {
  1458. typename Exp2::result_type t(val2);
  1459. do_assign_function_3c(f, val1, BOOST_MP_MOVE(t), val3, t3);
  1460. }
  1461. template <class F, class Exp1, class Exp2, class Exp3>
  1462. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_3c(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const detail::terminal&)
  1463. {
  1464. f(m_backend, function_arg_value(val1), function_arg_value(val2), function_arg_value(val3));
  1465. }
  1466. template <class F, class Exp1, class Exp2, class Exp3, class Tag3>
  1467. BOOST_MP_CXX14_CONSTEXPR void do_assign_function_3c(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag3& /*t3*/)
  1468. {
  1469. typename Exp3::result_type t(val3);
  1470. do_assign_function_3c(f, val1, val2, BOOST_MP_MOVE(t), detail::terminal());
  1471. }
  1472. template <class Exp>
  1473. BOOST_MP_CXX14_CONSTEXPR void do_add(const Exp& e, const detail::terminal&)
  1474. {
  1475. using default_ops::eval_add;
  1476. boost::multiprecision::detail::maybe_promote_precision(this);
  1477. eval_add(m_backend, canonical_value(e.value()));
  1478. }
  1479. template <class Exp>
  1480. BOOST_MP_CXX14_CONSTEXPR void do_add(const Exp& e, const detail::negate&)
  1481. {
  1482. typedef typename Exp::left_type left_type;
  1483. boost::multiprecision::detail::maybe_promote_precision(this);
  1484. do_subtract(e.left(), typename left_type::tag_type());
  1485. }
  1486. template <class Exp>
  1487. BOOST_MP_CXX14_CONSTEXPR void do_add(const Exp& e, const detail::plus&)
  1488. {
  1489. typedef typename Exp::left_type left_type;
  1490. typedef typename Exp::right_type right_type;
  1491. do_add(e.left(), typename left_type::tag_type());
  1492. do_add(e.right(), typename right_type::tag_type());
  1493. }
  1494. template <class Exp>
  1495. BOOST_MP_CXX14_CONSTEXPR void do_add(const Exp& e, const detail::minus&)
  1496. {
  1497. typedef typename Exp::left_type left_type;
  1498. typedef typename Exp::right_type right_type;
  1499. do_add(e.left(), typename left_type::tag_type());
  1500. do_subtract(e.right(), typename right_type::tag_type());
  1501. }
  1502. template <class Exp, class unknown>
  1503. BOOST_MP_CXX14_CONSTEXPR void do_add(const Exp& e, const unknown&)
  1504. {
  1505. self_type temp(e);
  1506. do_add(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  1507. }
  1508. template <class Exp>
  1509. BOOST_MP_CXX14_CONSTEXPR void do_add(const Exp& e, const detail::add_immediates&)
  1510. {
  1511. using default_ops::eval_add;
  1512. boost::multiprecision::detail::maybe_promote_precision(this);
  1513. eval_add(m_backend, canonical_value(e.left().value()));
  1514. eval_add(m_backend, canonical_value(e.right().value()));
  1515. }
  1516. template <class Exp>
  1517. BOOST_MP_CXX14_CONSTEXPR void do_add(const Exp& e, const detail::subtract_immediates&)
  1518. {
  1519. using default_ops::eval_add;
  1520. using default_ops::eval_subtract;
  1521. boost::multiprecision::detail::maybe_promote_precision(this);
  1522. eval_add(m_backend, canonical_value(e.left().value()));
  1523. eval_subtract(m_backend, canonical_value(e.right().value()));
  1524. }
  1525. template <class Exp>
  1526. BOOST_MP_CXX14_CONSTEXPR void do_subtract(const Exp& e, const detail::terminal&)
  1527. {
  1528. using default_ops::eval_subtract;
  1529. boost::multiprecision::detail::maybe_promote_precision(this);
  1530. eval_subtract(m_backend, canonical_value(e.value()));
  1531. }
  1532. template <class Exp>
  1533. BOOST_MP_CXX14_CONSTEXPR void do_subtract(const Exp& e, const detail::negate&)
  1534. {
  1535. typedef typename Exp::left_type left_type;
  1536. do_add(e.left(), typename left_type::tag_type());
  1537. }
  1538. template <class Exp>
  1539. BOOST_MP_CXX14_CONSTEXPR void do_subtract(const Exp& e, const detail::plus&)
  1540. {
  1541. typedef typename Exp::left_type left_type;
  1542. typedef typename Exp::right_type right_type;
  1543. do_subtract(e.left(), typename left_type::tag_type());
  1544. do_subtract(e.right(), typename right_type::tag_type());
  1545. }
  1546. template <class Exp>
  1547. BOOST_MP_CXX14_CONSTEXPR void do_subtract(const Exp& e, const detail::minus&)
  1548. {
  1549. typedef typename Exp::left_type left_type;
  1550. typedef typename Exp::right_type right_type;
  1551. do_subtract(e.left(), typename left_type::tag_type());
  1552. do_add(e.right(), typename right_type::tag_type());
  1553. }
  1554. template <class Exp>
  1555. BOOST_MP_CXX14_CONSTEXPR void do_subtract(const Exp& e, const detail::add_immediates&)
  1556. {
  1557. using default_ops::eval_subtract;
  1558. boost::multiprecision::detail::maybe_promote_precision(this);
  1559. eval_subtract(m_backend, canonical_value(e.left().value()));
  1560. eval_subtract(m_backend, canonical_value(e.right().value()));
  1561. }
  1562. template <class Exp>
  1563. BOOST_MP_CXX14_CONSTEXPR void do_subtract(const Exp& e, const detail::subtract_immediates&)
  1564. {
  1565. using default_ops::eval_add;
  1566. using default_ops::eval_subtract;
  1567. eval_subtract(m_backend, canonical_value(e.left().value()));
  1568. eval_add(m_backend, canonical_value(e.right().value()));
  1569. }
  1570. template <class Exp, class unknown>
  1571. BOOST_MP_CXX14_CONSTEXPR void do_subtract(const Exp& e, const unknown&)
  1572. {
  1573. self_type temp(e);
  1574. do_subtract(detail::expression<detail::terminal, self_type>(temp), detail::terminal());
  1575. }
  1576. template <class Exp>
  1577. BOOST_MP_CXX14_CONSTEXPR void do_multiplies(const Exp& e, const detail::terminal&)
  1578. {
  1579. using default_ops::eval_multiply;
  1580. boost::multiprecision::detail::maybe_promote_precision(this);
  1581. eval_multiply(m_backend, canonical_value(e.value()));
  1582. }
  1583. template <class Exp>
  1584. BOOST_MP_CXX14_CONSTEXPR void do_multiplies(const Exp& e, const detail::negate&)
  1585. {
  1586. typedef typename Exp::left_type left_type;
  1587. do_multiplies(e.left(), typename left_type::tag_type());
  1588. m_backend.negate();
  1589. }
  1590. template <class Exp>
  1591. BOOST_MP_CXX14_CONSTEXPR void do_multiplies(const Exp& e, const detail::multiplies&)
  1592. {
  1593. typedef typename Exp::left_type left_type;
  1594. typedef typename Exp::right_type right_type;
  1595. do_multiplies(e.left(), typename left_type::tag_type());
  1596. do_multiplies(e.right(), typename right_type::tag_type());
  1597. }
  1598. //
  1599. // This rearrangement is disabled for integer types, the test on sizeof(Exp) is simply to make
  1600. // the disable_if dependent on the template argument (the size of 1 can never occur in practice).
  1601. //
  1602. template <class Exp>
  1603. BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer || sizeof(Exp) == 1>::type
  1604. do_multiplies(const Exp& e, const detail::divides&)
  1605. {
  1606. typedef typename Exp::left_type left_type;
  1607. typedef typename Exp::right_type right_type;
  1608. do_multiplies(e.left(), typename left_type::tag_type());
  1609. do_divide(e.right(), typename right_type::tag_type());
  1610. }
  1611. template <class Exp>
  1612. BOOST_MP_CXX14_CONSTEXPR void do_multiplies(const Exp& e, const detail::multiply_immediates&)
  1613. {
  1614. using default_ops::eval_multiply;
  1615. boost::multiprecision::detail::maybe_promote_precision(this);
  1616. eval_multiply(m_backend, canonical_value(e.left().value()));
  1617. eval_multiply(m_backend, canonical_value(e.right().value()));
  1618. }
  1619. //
  1620. // This rearrangement is disabled for integer types, the test on sizeof(Exp) is simply to make
  1621. // the disable_if dependent on the template argument (the size of 1 can never occur in practice).
  1622. //
  1623. template <class Exp>
  1624. BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer || sizeof(Exp) == 1>::type
  1625. do_multiplies(const Exp& e, const detail::divide_immediates&)
  1626. {
  1627. using default_ops::eval_divide;
  1628. using default_ops::eval_multiply;
  1629. boost::multiprecision::detail::maybe_promote_precision(this);
  1630. eval_multiply(m_backend, canonical_value(e.left().value()));
  1631. eval_divide(m_backend, canonical_value(e.right().value()));
  1632. }
  1633. template <class Exp, class unknown>
  1634. BOOST_MP_CXX14_CONSTEXPR void do_multiplies(const Exp& e, const unknown&)
  1635. {
  1636. using default_ops::eval_multiply;
  1637. boost::multiprecision::detail::maybe_promote_precision(this);
  1638. self_type temp(e);
  1639. eval_multiply(m_backend, temp.m_backend);
  1640. }
  1641. template <class Exp>
  1642. BOOST_MP_CXX14_CONSTEXPR void do_divide(const Exp& e, const detail::terminal&)
  1643. {
  1644. using default_ops::eval_divide;
  1645. boost::multiprecision::detail::maybe_promote_precision(this);
  1646. eval_divide(m_backend, canonical_value(e.value()));
  1647. }
  1648. template <class Exp>
  1649. BOOST_MP_CXX14_CONSTEXPR void do_divide(const Exp& e, const detail::negate&)
  1650. {
  1651. typedef typename Exp::left_type left_type;
  1652. do_divide(e.left(), typename left_type::tag_type());
  1653. m_backend.negate();
  1654. }
  1655. //
  1656. // This rearrangement is disabled for integer types, the test on sizeof(Exp) is simply to make
  1657. // the disable_if dependent on the template argument (the size of 1 can never occur in practice).
  1658. //
  1659. template <class Exp>
  1660. BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer || sizeof(Exp) == 1>::type
  1661. do_divide(const Exp& e, const detail::multiplies&)
  1662. {
  1663. typedef typename Exp::left_type left_type;
  1664. typedef typename Exp::right_type right_type;
  1665. do_divide(e.left(), typename left_type::tag_type());
  1666. do_divide(e.right(), typename right_type::tag_type());
  1667. }
  1668. //
  1669. // This rearrangement is disabled for integer types, the test on sizeof(Exp) is simply to make
  1670. // the disable_if dependent on the template argument (the size of 1 can never occur in practice).
  1671. //
  1672. template <class Exp>
  1673. BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer || sizeof(Exp) == 1>::type
  1674. do_divide(const Exp& e, const detail::divides&)
  1675. {
  1676. typedef typename Exp::left_type left_type;
  1677. typedef typename Exp::right_type right_type;
  1678. do_divide(e.left(), typename left_type::tag_type());
  1679. do_multiplies(e.right(), typename right_type::tag_type());
  1680. }
  1681. //
  1682. // This rearrangement is disabled for integer types, the test on sizeof(Exp) is simply to make
  1683. // the disable_if dependent on the template argument (the size of 1 can never occur in practice).
  1684. //
  1685. template <class Exp>
  1686. BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer || sizeof(Exp) == 1>::type
  1687. do_divides(const Exp& e, const detail::multiply_immediates&)
  1688. {
  1689. using default_ops::eval_divide;
  1690. boost::multiprecision::detail::maybe_promote_precision(this);
  1691. eval_divide(m_backend, canonical_value(e.left().value()));
  1692. eval_divide(m_backend, canonical_value(e.right().value()));
  1693. }
  1694. //
  1695. // This rearrangement is disabled for integer types, the test on sizeof(Exp) is simply to make
  1696. // the disable_if dependent on the template argument (the size of 1 can never occur in practice).
  1697. //
  1698. template <class Exp>
  1699. BOOST_MP_CXX14_CONSTEXPR typename boost::disable_if_c<boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer || sizeof(Exp) == 1>::type
  1700. do_divides(const Exp& e, const detail::divide_immediates&)
  1701. {
  1702. using default_ops::eval_divide;
  1703. using default_ops::eval_multiply;
  1704. boost::multiprecision::detail::maybe_promote_precision(this);
  1705. eval_divide(m_backend, canonical_value(e.left().value()));
  1706. mutiply(m_backend, canonical_value(e.right().value()));
  1707. }
  1708. template <class Exp, class unknown>
  1709. BOOST_MP_CXX14_CONSTEXPR void do_divide(const Exp& e, const unknown&)
  1710. {
  1711. using default_ops::eval_multiply;
  1712. boost::multiprecision::detail::maybe_promote_precision(this);
  1713. self_type temp(e);
  1714. eval_divide(m_backend, temp.m_backend);
  1715. }
  1716. template <class Exp>
  1717. BOOST_MP_CXX14_CONSTEXPR void do_modulus(const Exp& e, const detail::terminal&)
  1718. {
  1719. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  1720. using default_ops::eval_modulus;
  1721. boost::multiprecision::detail::maybe_promote_precision(this);
  1722. eval_modulus(m_backend, canonical_value(e.value()));
  1723. }
  1724. template <class Exp, class Unknown>
  1725. BOOST_MP_CXX14_CONSTEXPR void do_modulus(const Exp& e, const Unknown&)
  1726. {
  1727. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The modulus operation is only valid for integer types");
  1728. using default_ops::eval_modulus;
  1729. boost::multiprecision::detail::maybe_promote_precision(this);
  1730. self_type temp(e);
  1731. eval_modulus(m_backend, canonical_value(temp));
  1732. }
  1733. template <class Exp>
  1734. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_and(const Exp& e, const detail::terminal&)
  1735. {
  1736. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  1737. using default_ops::eval_bitwise_and;
  1738. eval_bitwise_and(m_backend, canonical_value(e.value()));
  1739. }
  1740. template <class Exp>
  1741. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_and(const Exp& e, const detail::bitwise_and&)
  1742. {
  1743. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  1744. typedef typename Exp::left_type left_type;
  1745. typedef typename Exp::right_type right_type;
  1746. do_bitwise_and(e.left(), typename left_type::tag_type());
  1747. do_bitwise_and(e.right(), typename right_type::tag_type());
  1748. }
  1749. template <class Exp, class unknown>
  1750. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_and(const Exp& e, const unknown&)
  1751. {
  1752. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise & operation is only valid for integer types");
  1753. using default_ops::eval_bitwise_and;
  1754. self_type temp(e);
  1755. eval_bitwise_and(m_backend, temp.m_backend);
  1756. }
  1757. template <class Exp>
  1758. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_or(const Exp& e, const detail::terminal&)
  1759. {
  1760. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  1761. using default_ops::eval_bitwise_or;
  1762. eval_bitwise_or(m_backend, canonical_value(e.value()));
  1763. }
  1764. template <class Exp>
  1765. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_or(const Exp& e, const detail::bitwise_or&)
  1766. {
  1767. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  1768. typedef typename Exp::left_type left_type;
  1769. typedef typename Exp::right_type right_type;
  1770. do_bitwise_or(e.left(), typename left_type::tag_type());
  1771. do_bitwise_or(e.right(), typename right_type::tag_type());
  1772. }
  1773. template <class Exp, class unknown>
  1774. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_or(const Exp& e, const unknown&)
  1775. {
  1776. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise | operation is only valid for integer types");
  1777. using default_ops::eval_bitwise_or;
  1778. self_type temp(e);
  1779. eval_bitwise_or(m_backend, temp.m_backend);
  1780. }
  1781. template <class Exp>
  1782. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_xor(const Exp& e, const detail::terminal&)
  1783. {
  1784. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  1785. using default_ops::eval_bitwise_xor;
  1786. eval_bitwise_xor(m_backend, canonical_value(e.value()));
  1787. }
  1788. template <class Exp>
  1789. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_xor(const Exp& e, const detail::bitwise_xor&)
  1790. {
  1791. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  1792. typedef typename Exp::left_type left_type;
  1793. typedef typename Exp::right_type right_type;
  1794. do_bitwise_xor(e.left(), typename left_type::tag_type());
  1795. do_bitwise_xor(e.right(), typename right_type::tag_type());
  1796. }
  1797. template <class Exp, class unknown>
  1798. BOOST_MP_CXX14_CONSTEXPR void do_bitwise_xor(const Exp& e, const unknown&)
  1799. {
  1800. BOOST_STATIC_ASSERT_MSG(number_category<Backend>::value == number_kind_integer, "The bitwise ^ operation is only valid for integer types");
  1801. using default_ops::eval_bitwise_xor;
  1802. self_type temp(e);
  1803. eval_bitwise_xor(m_backend, temp.m_backend);
  1804. }
  1805. // Tests if the expression contains a reference to *this:
  1806. template <class Exp>
  1807. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool contains_self(const Exp& e) const BOOST_NOEXCEPT
  1808. {
  1809. return contains_self(e, typename Exp::arity());
  1810. }
  1811. template <class Exp>
  1812. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool contains_self(const Exp& e, mpl::int_<0> const&) const BOOST_NOEXCEPT
  1813. {
  1814. return is_realy_self(e.value());
  1815. }
  1816. template <class Exp>
  1817. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool contains_self(const Exp& e, mpl::int_<1> const&) const BOOST_NOEXCEPT
  1818. {
  1819. typedef typename Exp::left_type child_type;
  1820. return contains_self(e.left(), typename child_type::arity());
  1821. }
  1822. template <class Exp>
  1823. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool contains_self(const Exp& e, mpl::int_<2> const&) const BOOST_NOEXCEPT
  1824. {
  1825. typedef typename Exp::left_type child0_type;
  1826. typedef typename Exp::right_type child1_type;
  1827. return contains_self(e.left(), typename child0_type::arity()) || contains_self(e.right(), typename child1_type::arity());
  1828. }
  1829. template <class Exp>
  1830. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR bool contains_self(const Exp& e, mpl::int_<3> const&) const BOOST_NOEXCEPT
  1831. {
  1832. typedef typename Exp::left_type child0_type;
  1833. typedef typename Exp::middle_type child1_type;
  1834. typedef typename Exp::right_type child2_type;
  1835. return contains_self(e.left(), typename child0_type::arity()) || contains_self(e.middle(), typename child1_type::arity()) || contains_self(e.right(), typename child2_type::arity());
  1836. }
  1837. // Test if the expression is a reference to *this:
  1838. template <class Exp>
  1839. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_self(const Exp& e) const BOOST_NOEXCEPT
  1840. {
  1841. return is_self(e, typename Exp::arity());
  1842. }
  1843. template <class Exp>
  1844. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_self(const Exp& e, mpl::int_<0> const&) const BOOST_NOEXCEPT
  1845. {
  1846. return is_realy_self(e.value());
  1847. }
  1848. template <class Exp, int v>
  1849. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_self(const Exp&, mpl::int_<v> const&) const BOOST_NOEXCEPT
  1850. {
  1851. return false;
  1852. }
  1853. template <class Val>
  1854. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_realy_self(const Val&) const BOOST_NOEXCEPT { return false; }
  1855. BOOST_MP_FORCEINLINE BOOST_CONSTEXPR bool is_realy_self(const self_type& v) const BOOST_NOEXCEPT { return &v == this; }
  1856. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& function_arg_value(const self_type& v) BOOST_NOEXCEPT { return v.backend(); }
  1857. template <class Other, expression_template_option ET2>
  1858. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Other& function_arg_value(const number<Other, ET2>& v) BOOST_NOEXCEPT { return v.backend(); }
  1859. template <class V>
  1860. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const V& function_arg_value(const V& v) BOOST_NOEXCEPT { return v; }
  1861. template <class A1, class A2, class A3, class A4>
  1862. static BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR const A1& function_arg_value(const detail::expression<detail::terminal, A1, A2, A3, A4>& exp) BOOST_NOEXCEPT { return exp.value(); }
  1863. template <class A2, class A3, class A4>
  1864. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& function_arg_value(const detail::expression<detail::terminal, number<Backend>, A2, A3, A4>& exp) BOOST_NOEXCEPT { return exp.value().backend(); }
  1865. Backend m_backend;
  1866. public:
  1867. //
  1868. // These shouldn't really need to be public, or even member functions, but it makes implementing
  1869. // the non-member operators way easier if they are:
  1870. //
  1871. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const Backend& canonical_value(const self_type& v) BOOST_NOEXCEPT { return v.m_backend; }
  1872. template <class B2, expression_template_option ET>
  1873. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR const B2& canonical_value(const number<B2, ET>& v) BOOST_NOEXCEPT { return v.backend(); }
  1874. template <class V>
  1875. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR typename boost::disable_if<is_same<typename detail::canonical<V, Backend>::type, V>, typename detail::canonical<V, Backend>::type>::type
  1876. canonical_value(const V& v) BOOST_NOEXCEPT { return static_cast<typename detail::canonical<V, Backend>::type>(v); }
  1877. template <class V>
  1878. static BOOST_MP_FORCEINLINE BOOST_CONSTEXPR typename boost::enable_if_c<is_same<typename detail::canonical<V, Backend>::type, V>::value, const V&>::type
  1879. canonical_value(const V& v) BOOST_NOEXCEPT { return v; }
  1880. static BOOST_MP_FORCEINLINE typename detail::canonical<std::string, Backend>::type canonical_value(const std::string& v) BOOST_NOEXCEPT { return v.c_str(); }
  1881. };
  1882. template <class Backend, expression_template_option ExpressionTemplates>
  1883. inline std::ostream& operator<<(std::ostream& os, const number<Backend, ExpressionTemplates>& r)
  1884. {
  1885. std::streamsize d = os.precision();
  1886. std::string s = r.str(d, os.flags());
  1887. std::streamsize ss = os.width();
  1888. if (ss > static_cast<std::streamsize>(s.size()))
  1889. {
  1890. char fill = os.fill();
  1891. if ((os.flags() & std::ios_base::left) == std::ios_base::left)
  1892. s.append(static_cast<std::string::size_type>(ss - s.size()), fill);
  1893. else
  1894. s.insert(static_cast<std::string::size_type>(0), static_cast<std::string::size_type>(ss - s.size()), fill);
  1895. }
  1896. return os << s;
  1897. }
  1898. namespace detail {
  1899. template <class tag, class A1, class A2, class A3, class A4>
  1900. inline std::ostream& operator<<(std::ostream& os, const expression<tag, A1, A2, A3, A4>& r)
  1901. {
  1902. typedef typename expression<tag, A1, A2, A3, A4>::result_type value_type;
  1903. value_type temp(r);
  1904. return os << temp;
  1905. }
  1906. //
  1907. // What follows is the input streaming code: this is not "proper" iostream code at all
  1908. // but that's fiendishly hard to write when dealing with multiple backends all
  1909. // with different requirements... yes we could deligate this to the backend author...
  1910. // but we really want backends to be EASY to write!
  1911. // For now just pull in all the characters that could possibly form the number
  1912. // and let the backend's string parser make use of it. This fixes most use cases
  1913. // including CSV type formats such as those used by the Random lib.
  1914. //
  1915. inline std::string read_string_while(std::istream& is, std::string const& permitted_chars)
  1916. {
  1917. std::ios_base::iostate state = std::ios_base::goodbit;
  1918. const std::istream::sentry sentry_check(is);
  1919. std::string result;
  1920. if (sentry_check)
  1921. {
  1922. int c = is.rdbuf()->sgetc();
  1923. for (;; c = is.rdbuf()->snextc())
  1924. if (std::istream::traits_type::eq_int_type(std::istream::traits_type::eof(), c))
  1925. { // end of file:
  1926. state |= std::ios_base::eofbit;
  1927. break;
  1928. }
  1929. else if (permitted_chars.find_first_of(std::istream::traits_type::to_char_type(c)) == std::string::npos)
  1930. {
  1931. // Invalid numeric character, stop reading:
  1932. //is.rdbuf()->sputbackc(static_cast<char>(c));
  1933. break;
  1934. }
  1935. else
  1936. {
  1937. result.append(1, std::istream::traits_type::to_char_type(c));
  1938. }
  1939. }
  1940. if (!result.size())
  1941. state |= std::ios_base::failbit;
  1942. is.setstate(state);
  1943. return result;
  1944. }
  1945. } // namespace detail
  1946. template <class Backend, expression_template_option ExpressionTemplates>
  1947. inline std::istream& operator>>(std::istream& is, number<Backend, ExpressionTemplates>& r)
  1948. {
  1949. bool hex_format = (is.flags() & std::ios_base::hex) == std::ios_base::hex;
  1950. bool oct_format = (is.flags() & std::ios_base::oct) == std::ios_base::oct;
  1951. std::string s;
  1952. switch (boost::multiprecision::number_category<number<Backend, ExpressionTemplates> >::value)
  1953. {
  1954. case boost::multiprecision::number_kind_integer:
  1955. if (oct_format)
  1956. s = detail::read_string_while(is, "+-01234567");
  1957. else if (hex_format)
  1958. s = detail::read_string_while(is, "+-xXabcdefABCDEF0123456789");
  1959. else
  1960. s = detail::read_string_while(is, "+-0123456789");
  1961. break;
  1962. case boost::multiprecision::number_kind_floating_point:
  1963. s = detail::read_string_while(is, "+-eE.0123456789infINFnanNANinfinityINFINITY");
  1964. break;
  1965. default:
  1966. is >> s;
  1967. }
  1968. if (s.size())
  1969. {
  1970. if (hex_format && (number_category<Backend>::value == number_kind_integer) && ((s[0] != '0') || (s[1] != 'x')))
  1971. s.insert(s.find_first_not_of("+-"), "0x");
  1972. if (oct_format && (number_category<Backend>::value == number_kind_integer) && (s[0] != '0'))
  1973. s.insert(s.find_first_not_of("+-"), "0");
  1974. r.assign(s);
  1975. }
  1976. else if (!is.fail())
  1977. is.setstate(std::istream::failbit);
  1978. return is;
  1979. }
  1980. template <class Backend, expression_template_option ExpressionTemplates>
  1981. BOOST_MP_FORCEINLINE BOOST_MP_CXX14_CONSTEXPR void swap(number<Backend, ExpressionTemplates>& a, number<Backend, ExpressionTemplates>& b)
  1982. BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<number<Backend, ExpressionTemplates>&>() = std::declval<number<Backend, ExpressionTemplates>&>()))
  1983. {
  1984. a.swap(b);
  1985. }
  1986. //
  1987. // Boost.Hash support, just call hash_value for the backend, which may or may not be supported:
  1988. //
  1989. template <class Backend, expression_template_option ExpressionTemplates>
  1990. inline BOOST_MP_CXX14_CONSTEXPR std::size_t hash_value(const number<Backend, ExpressionTemplates>& val)
  1991. {
  1992. return hash_value(val.backend());
  1993. }
  1994. } // namespace multiprecision
  1995. template <class T>
  1996. class rational;
  1997. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  1998. inline std::istream& operator>>(std::istream& is, rational<multiprecision::number<Backend, ExpressionTemplates> >& r)
  1999. {
  2000. std::string s1;
  2001. multiprecision::number<Backend, ExpressionTemplates> v1, v2;
  2002. char c;
  2003. bool have_hex = false;
  2004. bool hex_format = (is.flags() & std::ios_base::hex) == std::ios_base::hex;
  2005. bool oct_format = (is.flags() & std::ios_base::oct) == std::ios_base::oct;
  2006. while ((EOF != (c = static_cast<char>(is.peek()))) && (c == 'x' || c == 'X' || c == '-' || c == '+' || (c >= '0' && c <= '9') || (have_hex && (c >= 'a' && c <= 'f')) || (have_hex && (c >= 'A' && c <= 'F'))))
  2007. {
  2008. if (c == 'x' || c == 'X')
  2009. have_hex = true;
  2010. s1.append(1, c);
  2011. is.get();
  2012. }
  2013. if (hex_format && ((s1[0] != '0') || (s1[1] != 'x')))
  2014. s1.insert(static_cast<std::string::size_type>(0), "0x");
  2015. if (oct_format && (s1[0] != '0'))
  2016. s1.insert(static_cast<std::string::size_type>(0), "0");
  2017. v1.assign(s1);
  2018. s1.erase();
  2019. if (c == '/')
  2020. {
  2021. is.get();
  2022. while ((EOF != (c = static_cast<char>(is.peek()))) && (c == 'x' || c == 'X' || c == '-' || c == '+' || (c >= '0' && c <= '9') || (have_hex && (c >= 'a' && c <= 'f')) || (have_hex && (c >= 'A' && c <= 'F'))))
  2023. {
  2024. if (c == 'x' || c == 'X')
  2025. have_hex = true;
  2026. s1.append(1, c);
  2027. is.get();
  2028. }
  2029. if (hex_format && ((s1[0] != '0') || (s1[1] != 'x')))
  2030. s1.insert(static_cast<std::string::size_type>(0), "0x");
  2031. if (oct_format && (s1[0] != '0'))
  2032. s1.insert(static_cast<std::string::size_type>(0), "0");
  2033. v2.assign(s1);
  2034. }
  2035. else
  2036. v2 = 1;
  2037. r.assign(v1, v2);
  2038. return is;
  2039. }
  2040. template <class T, multiprecision::expression_template_option ExpressionTemplates>
  2041. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<T, ExpressionTemplates> numerator(const rational<multiprecision::number<T, ExpressionTemplates> >& a)
  2042. {
  2043. return a.numerator();
  2044. }
  2045. template <class T, multiprecision::expression_template_option ExpressionTemplates>
  2046. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<T, ExpressionTemplates> denominator(const rational<multiprecision::number<T, ExpressionTemplates> >& a)
  2047. {
  2048. return a.denominator();
  2049. }
  2050. template <class T, multiprecision::expression_template_option ExpressionTemplates>
  2051. inline BOOST_MP_CXX14_CONSTEXPR std::size_t hash_value(const rational<multiprecision::number<T, ExpressionTemplates> >& val)
  2052. {
  2053. std::size_t result = hash_value(val.numerator());
  2054. boost::hash_combine(result, hash_value(val.denominator()));
  2055. return result;
  2056. }
  2057. namespace multiprecision {
  2058. template <class I>
  2059. struct component_type<boost::rational<I> >
  2060. {
  2061. typedef I type;
  2062. };
  2063. } // namespace multiprecision
  2064. #ifdef BOOST_MSVC
  2065. #pragma warning(pop)
  2066. #endif
  2067. } // namespace boost
  2068. #ifndef BOOST_NO_CXX11_HDR_FUNCTIONAL
  2069. #include <functional>
  2070. namespace std {
  2071. template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates>
  2072. struct hash<boost::multiprecision::number<Backend, ExpressionTemplates> >
  2073. {
  2074. BOOST_MP_CXX14_CONSTEXPR std::size_t operator()(const boost::multiprecision::number<Backend, ExpressionTemplates>& val) const { return hash_value(val); }
  2075. };
  2076. template <class Backend, boost::multiprecision::expression_template_option ExpressionTemplates>
  2077. struct hash<boost::rational<boost::multiprecision::number<Backend, ExpressionTemplates> > >
  2078. {
  2079. BOOST_MP_CXX14_CONSTEXPR std::size_t operator()(const boost::rational<boost::multiprecision::number<Backend, ExpressionTemplates> >& val) const
  2080. {
  2081. std::size_t result = hash_value(val.numerator());
  2082. boost::hash_combine(result, hash_value(val.denominator()));
  2083. return result;
  2084. }
  2085. };
  2086. } // namespace std
  2087. #endif
  2088. #include <boost/multiprecision/detail/ublas_interop.hpp>
  2089. #endif