default_ops.hpp 235 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2011-21 John Maddock.
  3. // Copyright 2021 Iskandarov Lev. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MP_DEFAULT_OPS
  7. #define BOOST_MP_DEFAULT_OPS
  8. #include <boost/multiprecision/detail/standalone_config.hpp>
  9. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  10. #include <boost/multiprecision/detail/number_base.hpp>
  11. #include <boost/multiprecision/detail/assert.hpp>
  12. #include <boost/multiprecision/traits/is_backend.hpp>
  13. #include <boost/multiprecision/detail/fpclassify.hpp>
  14. #include <cstdint>
  15. #include <complex>
  16. #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
  17. #include <string_view>
  18. #endif
  19. #ifdef BOOST_MP_MATH_AVAILABLE
  20. #include <boost/math/special_functions/fpclassify.hpp>
  21. #include <boost/math/special_functions/next.hpp>
  22. #include <boost/math/special_functions/hypot.hpp>
  23. #include <boost/math/policies/error_handling.hpp>
  24. #endif
  25. #ifndef INSTRUMENT_BACKEND
  26. #ifndef BOOST_MP_INSTRUMENT
  27. #define INSTRUMENT_BACKEND(x)
  28. #else
  29. #define INSTRUMENT_BACKEND(x) \
  30. std::cout << BOOST_STRINGIZE(x) << " = " << x.str(0, std::ios_base::scientific) << std::endl;
  31. #endif
  32. #endif
  33. namespace boost {
  34. namespace multiprecision {
  35. namespace detail {
  36. template <class To, class From>
  37. void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/);
  38. template <class To, class From>
  39. void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_integer>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/);
  40. template <class To, class From>
  41. void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_floating_point>& /*to_type*/, const std::integral_constant<int, number_kind_floating_point>& /*from_type*/);
  42. template <class To, class From>
  43. void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_rational>& /*from_type*/);
  44. template <class To, class From>
  45. void generic_interconvert(To& to, const From& from, const std::integral_constant<int, number_kind_rational>& /*to_type*/, const std::integral_constant<int, number_kind_integer>& /*from_type*/);
  46. template <class Integer>
  47. BOOST_MP_CXX14_CONSTEXPR Integer karatsuba_sqrt(const Integer& x, Integer& r, size_t bits);
  48. } // namespace detail
  49. namespace default_ops {
  50. template <class T>
  51. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_backend<T>::value, int>::type eval_signbit(const T& val);
  52. template <class T>
  53. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!boost::multiprecision::detail::is_backend<T>::value, int>::type eval_signbit(const T& val) { return val < 0; }
  54. inline int eval_signbit(float val) { return (std::signbit)(val); }
  55. inline int eval_signbit(double val) { return (std::signbit)(val); }
  56. inline int eval_signbit(long double val) { return (std::signbit)(val); }
  57. #ifdef BOOST_HAS_FLOAT128
  58. extern "C" int signbitq(float128_type) throw();
  59. inline int eval_signbit(float128_type val) { return signbitq(val); }
  60. #endif
  61. template <class T>
  62. BOOST_MP_CXX14_CONSTEXPR bool eval_is_zero(const T& val);
  63. #ifdef BOOST_MSVC
  64. // warning C4127: conditional expression is constant
  65. // warning C4146: unary minus operator applied to unsigned type, result still unsigned
  66. #pragma warning(push)
  67. #pragma warning(disable : 4127 4146)
  68. #endif
  69. //
  70. // Default versions of mixed arithmetic, these just construct a temporary
  71. // from the arithmetic value and then do the arithmetic on that, two versions
  72. // of each depending on whether the backend can be directly constructed from type V.
  73. //
  74. // Note that we have to provide *all* the template parameters to class number when used in
  75. // enable_if as MSVC-10 won't compile the code if we rely on a computed-default parameter.
  76. // Since the result of the test doesn't depend on whether expression templates are on or off
  77. // we just use et_on everywhere. We could use a BOOST_WORKAROUND but that just obfuscates the
  78. // code even more....
  79. //
  80. template <class T, class V>
  81. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_convertible<V, T>::value>::type
  82. eval_add(T& result, V const& v)
  83. {
  84. T t;
  85. t = v;
  86. eval_add(result, t);
  87. }
  88. template <class T, class V>
  89. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, T>::value>::type
  90. eval_add(T& result, V const& v)
  91. {
  92. T t(v);
  93. eval_add(result, t);
  94. }
  95. template <class T, class V>
  96. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_convertible<V, T>::value>::type
  97. eval_subtract(T& result, V const& v)
  98. {
  99. T t;
  100. t = v;
  101. eval_subtract(result, t);
  102. }
  103. template <class T, class V>
  104. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, T>::value>::type
  105. eval_subtract(T& result, V const& v)
  106. {
  107. T t(v);
  108. eval_subtract(result, t);
  109. }
  110. template <class T, class V>
  111. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_convertible<V, T>::value>::type
  112. eval_multiply(T& result, V const& v)
  113. {
  114. T t;
  115. t = v;
  116. eval_multiply(result, t);
  117. }
  118. template <class T, class V>
  119. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, T>::value>::type
  120. eval_multiply(T& result, V const& v)
  121. {
  122. T t(v);
  123. eval_multiply(result, t);
  124. }
  125. template <class T, class U, class V>
  126. BOOST_MP_CXX14_CONSTEXPR void eval_multiply(T& t, const U& u, const V& v);
  127. template <class T, class U, class V>
  128. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!std::is_same<T, U>::value && std::is_same<T, V>::value)>::type eval_multiply_add(T& t, const U& u, const V& v)
  129. {
  130. T z;
  131. eval_multiply(z, u, v);
  132. eval_add(t, z);
  133. }
  134. template <class T, class U, class V>
  135. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value && std::is_same<T, V>::value>::type eval_multiply_add(T& t, const U& u, const V& v)
  136. {
  137. eval_multiply_add(t, v, u);
  138. }
  139. template <class T, class U, class V>
  140. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!std::is_same<T, U>::value && std::is_same<T, V>::value)>::type eval_multiply_subtract(T& t, const U& u, const V& v)
  141. {
  142. T z;
  143. eval_multiply(z, u, v);
  144. eval_subtract(t, z);
  145. }
  146. template <class T, class U, class V>
  147. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value && std::is_same<T, V>::value>::type eval_multiply_subtract(T& t, const U& u, const V& v)
  148. {
  149. eval_multiply_subtract(t, v, u);
  150. }
  151. template <class T, class V>
  152. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
  153. eval_divide(T& result, V const& v)
  154. {
  155. T t;
  156. t = v;
  157. eval_divide(result, t);
  158. }
  159. template <class T, class V>
  160. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
  161. eval_divide(T& result, V const& v)
  162. {
  163. T t(v);
  164. eval_divide(result, t);
  165. }
  166. template <class T, class V>
  167. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
  168. eval_modulus(T& result, V const& v)
  169. {
  170. T t;
  171. t = v;
  172. eval_modulus(result, t);
  173. }
  174. template <class T, class V>
  175. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
  176. eval_modulus(T& result, V const& v)
  177. {
  178. T t(v);
  179. eval_modulus(result, t);
  180. }
  181. template <class T, class V>
  182. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
  183. eval_bitwise_and(T& result, V const& v)
  184. {
  185. T t;
  186. t = v;
  187. eval_bitwise_and(result, t);
  188. }
  189. template <class T, class V>
  190. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
  191. eval_bitwise_and(T& result, V const& v)
  192. {
  193. T t(v);
  194. eval_bitwise_and(result, t);
  195. }
  196. template <class T, class V>
  197. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
  198. eval_bitwise_or(T& result, V const& v)
  199. {
  200. T t;
  201. t = v;
  202. eval_bitwise_or(result, t);
  203. }
  204. template <class T, class V>
  205. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
  206. eval_bitwise_or(T& result, V const& v)
  207. {
  208. T t(v);
  209. eval_bitwise_or(result, t);
  210. }
  211. template <class T, class V>
  212. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
  213. eval_bitwise_xor(T& result, V const& v)
  214. {
  215. T t;
  216. t = v;
  217. eval_bitwise_xor(result, t);
  218. }
  219. template <class T, class V>
  220. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
  221. eval_bitwise_xor(T& result, V const& v)
  222. {
  223. T t(v);
  224. eval_bitwise_xor(result, t);
  225. }
  226. template <class T, class V>
  227. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && !std::is_convertible<V, T>::value>::type
  228. eval_complement(T& result, V const& v)
  229. {
  230. T t;
  231. t = v;
  232. eval_complement(result, t);
  233. }
  234. template <class T, class V>
  235. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<V, number<T, et_on> >::value && std::is_convertible<V, T>::value>::type
  236. eval_complement(T& result, V const& v)
  237. {
  238. T t(v);
  239. eval_complement(result, t);
  240. }
  241. //
  242. // Default versions of 3-arg arithmetic functions, these mostly just forward to the 2 arg versions:
  243. //
  244. template <class T, class U, class V>
  245. BOOST_MP_CXX14_CONSTEXPR void eval_add(T& t, const U& u, const V& v);
  246. template <class T>
  247. inline BOOST_MP_CXX14_CONSTEXPR void eval_add_default(T& t, const T& u, const T& v)
  248. {
  249. if (&t == &v)
  250. {
  251. eval_add(t, u);
  252. }
  253. else if (&t == &u)
  254. {
  255. eval_add(t, v);
  256. }
  257. else
  258. {
  259. t = u;
  260. eval_add(t, v);
  261. }
  262. }
  263. template <class T, class U>
  264. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_add_default(T& t, const T& u, const U& v)
  265. {
  266. T vv;
  267. vv = v;
  268. eval_add(t, u, vv);
  269. }
  270. template <class T, class U>
  271. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_add_default(T& t, const T& u, const U& v)
  272. {
  273. T vv(v);
  274. eval_add(t, u, vv);
  275. }
  276. template <class T, class U>
  277. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_add_default(T& t, const U& u, const T& v)
  278. {
  279. eval_add(t, v, u);
  280. }
  281. template <class T, class U, class V>
  282. inline BOOST_MP_CXX14_CONSTEXPR void eval_add_default(T& t, const U& u, const V& v)
  283. {
  284. BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
  285. {
  286. if ((void*)&t == (void*)&v)
  287. {
  288. eval_add(t, u);
  289. }
  290. else
  291. {
  292. t = u;
  293. eval_add(t, v);
  294. }
  295. }
  296. else
  297. {
  298. t = u;
  299. eval_add(t, v);
  300. }
  301. }
  302. template <class T, class U, class V>
  303. inline BOOST_MP_CXX14_CONSTEXPR void eval_add(T& t, const U& u, const V& v)
  304. {
  305. eval_add_default(t, u, v);
  306. }
  307. template <class T, class U, class V>
  308. void BOOST_MP_CXX14_CONSTEXPR eval_subtract(T& t, const U& u, const V& v);
  309. template <class T>
  310. inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract_default(T& t, const T& u, const T& v)
  311. {
  312. if ((&t == &v) && is_signed_number<T>::value)
  313. {
  314. eval_subtract(t, u);
  315. t.negate();
  316. }
  317. else if (&t == &u)
  318. {
  319. eval_subtract(t, v);
  320. }
  321. else
  322. {
  323. t = u;
  324. eval_subtract(t, v);
  325. }
  326. }
  327. template <class T, class U>
  328. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_subtract_default(T& t, const T& u, const U& v)
  329. {
  330. T vv;
  331. vv = v;
  332. eval_subtract(t, u, vv);
  333. }
  334. template <class T, class U>
  335. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_subtract_default(T& t, const T& u, const U& v)
  336. {
  337. T vv(v);
  338. eval_subtract(t, u, vv);
  339. }
  340. template <class T, class U>
  341. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && is_signed_number<T>::value && (number_category<T>::value != number_kind_complex)>::type eval_subtract_default(T& t, const U& u, const T& v)
  342. {
  343. eval_subtract(t, v, u);
  344. if(!eval_is_zero(t) || (eval_signbit(u) != eval_signbit(v)))
  345. t.negate();
  346. }
  347. template <class T, class U>
  348. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && is_signed_number<T>::value && (number_category<T>::value == number_kind_complex)>::type eval_subtract_default(T& t, const U& u, const T& v)
  349. {
  350. eval_subtract(t, v, u);
  351. t.negate();
  352. }
  353. template <class T, class U>
  354. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value && is_unsigned_number<T>::value>::type eval_subtract_default(T& t, const U& u, const T& v)
  355. {
  356. T temp;
  357. temp = u;
  358. eval_subtract(t, temp, v);
  359. }
  360. template <class T, class U>
  361. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value && is_unsigned_number<T>::value>::type eval_subtract_default(T& t, const U& u, const T& v)
  362. {
  363. T temp(u);
  364. eval_subtract(t, temp, v);
  365. }
  366. template <class T, class U, class V>
  367. inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract_default(T& t, const U& u, const V& v)
  368. {
  369. BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
  370. {
  371. if ((void*)&t == (void*)&v)
  372. {
  373. eval_subtract(t, u);
  374. t.negate();
  375. }
  376. else
  377. {
  378. t = u;
  379. eval_subtract(t, v);
  380. }
  381. }
  382. else
  383. {
  384. t = u;
  385. eval_subtract(t, v);
  386. }
  387. }
  388. template <class T, class U, class V>
  389. inline BOOST_MP_CXX14_CONSTEXPR void eval_subtract(T& t, const U& u, const V& v)
  390. {
  391. eval_subtract_default(t, u, v);
  392. }
  393. template <class T>
  394. inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply_default(T& t, const T& u, const T& v)
  395. {
  396. if (&t == &v)
  397. {
  398. eval_multiply(t, u);
  399. }
  400. else if (&t == &u)
  401. {
  402. eval_multiply(t, v);
  403. }
  404. else
  405. {
  406. t = u;
  407. eval_multiply(t, v);
  408. }
  409. }
  410. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
  411. template <class T, class U>
  412. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_multiply_default(T& t, const T& u, const U& v)
  413. {
  414. T vv;
  415. vv = v;
  416. eval_multiply(t, u, vv);
  417. }
  418. template <class T, class U>
  419. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_multiply_default(T& t, const T& u, const U& v)
  420. {
  421. T vv(v);
  422. eval_multiply(t, u, vv);
  423. }
  424. template <class T, class U>
  425. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_multiply_default(T& t, const U& u, const T& v)
  426. {
  427. eval_multiply(t, v, u);
  428. }
  429. #endif
  430. template <class T, class U, class V>
  431. inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply_default(T& t, const U& u, const V& v)
  432. {
  433. BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
  434. {
  435. if ((void*)&t == (void*)&v)
  436. {
  437. eval_multiply(t, u);
  438. }
  439. else
  440. {
  441. t = number<T>::canonical_value(u);
  442. eval_multiply(t, v);
  443. }
  444. }
  445. else
  446. {
  447. t = number<T>::canonical_value(u);
  448. eval_multiply(t, v);
  449. }
  450. }
  451. template <class T, class U, class V>
  452. inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply(T& t, const U& u, const V& v)
  453. {
  454. eval_multiply_default(t, u, v);
  455. }
  456. template <class T>
  457. inline BOOST_MP_CXX14_CONSTEXPR void eval_multiply_add(T& t, const T& u, const T& v, const T& x)
  458. {
  459. if ((void*)&x == (void*)&t)
  460. {
  461. T z;
  462. z = number<T>::canonical_value(x);
  463. eval_multiply_add(t, u, v, z);
  464. }
  465. else
  466. {
  467. eval_multiply(t, u, v);
  468. eval_add(t, x);
  469. }
  470. }
  471. template <class T, class U>
  472. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_same<T, U>::value, T>::type make_T(const U& u)
  473. {
  474. T t;
  475. t = number<T>::canonical_value(u);
  476. return t;
  477. } // LCOV_EXCL_LINE
  478. template <class T>
  479. inline BOOST_MP_CXX14_CONSTEXPR const T& make_T(const T& t)
  480. {
  481. return t;
  482. }
  483. template <class T, class U, class V, class X>
  484. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!std::is_same<T, U>::value && std::is_same<T, V>::value)>::type eval_multiply_add(T& t, const U& u, const V& v, const X& x)
  485. {
  486. eval_multiply_add(t, make_T<T>(u), make_T<T>(v), make_T<T>(x));
  487. }
  488. template <class T, class U, class V, class X>
  489. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value && std::is_same<T, V>::value>::type eval_multiply_add(T& t, const U& u, const V& v, const X& x)
  490. {
  491. eval_multiply_add(t, v, u, x);
  492. }
  493. template <class T, class U, class V, class X>
  494. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!(!std::is_same<T, U>::value && std::is_same<T, V>::value)>::type eval_multiply_subtract(T& t, const U& u, const V& v, const X& x)
  495. {
  496. if ((void*)&x == (void*)&t)
  497. {
  498. T z;
  499. z = x;
  500. eval_multiply_subtract(t, u, v, z);
  501. }
  502. else
  503. {
  504. eval_multiply(t, u, v);
  505. eval_subtract(t, x);
  506. }
  507. }
  508. template <class T, class U, class V, class X>
  509. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value && std::is_same<T, V>::value>::type eval_multiply_subtract(T& t, const U& u, const V& v, const X& x)
  510. {
  511. eval_multiply_subtract(t, v, u, x);
  512. }
  513. template <class T, class U, class V>
  514. BOOST_MP_CXX14_CONSTEXPR void eval_divide(T& t, const U& u, const V& v);
  515. template <class T>
  516. inline BOOST_MP_CXX14_CONSTEXPR void eval_divide_default(T& t, const T& u, const T& v)
  517. {
  518. if (&t == &u)
  519. eval_divide(t, v);
  520. else if (&t == &v)
  521. {
  522. T temp;
  523. eval_divide(temp, u, v);
  524. temp.swap(t);
  525. }
  526. else
  527. {
  528. t = u;
  529. eval_divide(t, v);
  530. }
  531. }
  532. #if !BOOST_WORKAROUND(BOOST_MSVC, < 1900)
  533. template <class T, class U>
  534. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_divide_default(T& t, const T& u, const U& v)
  535. {
  536. T vv;
  537. vv = v;
  538. eval_divide(t, u, vv);
  539. }
  540. template <class T, class U>
  541. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_divide_default(T& t, const T& u, const U& v)
  542. {
  543. T vv(v);
  544. eval_divide(t, u, vv);
  545. }
  546. template <class T, class U>
  547. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_divide_default(T& t, const U& u, const T& v)
  548. {
  549. T uu;
  550. uu = u;
  551. eval_divide(t, uu, v);
  552. }
  553. template <class T, class U>
  554. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_divide_default(T& t, const U& u, const T& v)
  555. {
  556. T uu(u);
  557. eval_divide(t, uu, v);
  558. }
  559. #endif
  560. template <class T, class U, class V>
  561. inline BOOST_MP_CXX14_CONSTEXPR void eval_divide_default(T& t, const U& u, const V& v)
  562. {
  563. BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
  564. {
  565. if ((void*)&t == (void*)&v)
  566. {
  567. T temp;
  568. temp = u;
  569. eval_divide(temp, v);
  570. t = temp;
  571. }
  572. else
  573. {
  574. t = u;
  575. eval_divide(t, v);
  576. }
  577. }
  578. else
  579. {
  580. t = u;
  581. eval_divide(t, v);
  582. }
  583. }
  584. template <class T, class U, class V>
  585. inline BOOST_MP_CXX14_CONSTEXPR void eval_divide(T& t, const U& u, const V& v)
  586. {
  587. eval_divide_default(t, u, v);
  588. }
  589. template <class T, class U, class V>
  590. BOOST_MP_CXX14_CONSTEXPR void eval_modulus(T& t, const U& u, const V& v);
  591. template <class T>
  592. inline BOOST_MP_CXX14_CONSTEXPR void eval_modulus_default(T& t, const T& u, const T& v)
  593. {
  594. if (&t == &u)
  595. eval_modulus(t, v);
  596. else if (&t == &v)
  597. {
  598. T temp;
  599. eval_modulus(temp, u, v);
  600. temp.swap(t);
  601. }
  602. else
  603. {
  604. t = u;
  605. eval_modulus(t, v);
  606. }
  607. }
  608. template <class T, class U>
  609. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_modulus_default(T& t, const T& u, const U& v)
  610. {
  611. T vv;
  612. vv = v;
  613. eval_modulus(t, u, vv);
  614. }
  615. template <class T, class U>
  616. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_modulus_default(T& t, const T& u, const U& v)
  617. {
  618. T vv(v);
  619. eval_modulus(t, u, vv);
  620. }
  621. template <class T, class U>
  622. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_modulus_default(T& t, const U& u, const T& v)
  623. {
  624. T uu;
  625. uu = u;
  626. eval_modulus(t, uu, v);
  627. }
  628. template <class T, class U>
  629. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_modulus_default(T& t, const U& u, const T& v)
  630. {
  631. T uu(u);
  632. eval_modulus(t, uu, v);
  633. }
  634. template <class T, class U, class V>
  635. inline BOOST_MP_CXX14_CONSTEXPR void eval_modulus_default(T& t, const U& u, const V& v)
  636. {
  637. BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
  638. {
  639. if ((void*)&t == (void*)&v)
  640. {
  641. T temp(u);
  642. eval_modulus(temp, v);
  643. t = temp;
  644. }
  645. else
  646. {
  647. t = u;
  648. eval_modulus(t, v);
  649. }
  650. }
  651. else
  652. {
  653. t = u;
  654. eval_modulus(t, v);
  655. }
  656. }
  657. template <class T, class U, class V>
  658. inline BOOST_MP_CXX14_CONSTEXPR void eval_modulus(T& t, const U& u, const V& v)
  659. {
  660. eval_modulus_default(t, u, v);
  661. }
  662. template <class T, class U, class V>
  663. BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_and(T& t, const U& u, const V& v);
  664. template <class T>
  665. inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_and_default(T& t, const T& u, const T& v)
  666. {
  667. if (&t == &v)
  668. {
  669. eval_bitwise_and(t, u);
  670. }
  671. else if (&t == &u)
  672. {
  673. eval_bitwise_and(t, v);
  674. }
  675. else
  676. {
  677. t = u;
  678. eval_bitwise_and(t, v);
  679. }
  680. }
  681. template <class T, class U>
  682. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !std::is_convertible<U, T>::value>::type eval_bitwise_and_default(T& t, const T& u, const U& v)
  683. {
  684. T vv;
  685. vv = v;
  686. eval_bitwise_and(t, u, vv);
  687. }
  688. template <class T, class U>
  689. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, T>::value>::type eval_bitwise_and_default(T& t, const T& u, const U& v)
  690. {
  691. T vv(v);
  692. eval_bitwise_and(t, u, vv);
  693. }
  694. template <class T, class U>
  695. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_and_default(T& t, const U& u, const T& v)
  696. {
  697. eval_bitwise_and(t, v, u);
  698. }
  699. template <class T, class U, class V>
  700. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<!std::is_same<T, U>::value || std::is_same<T, V>::value>::type eval_bitwise_and_default(T& t, const U& u, const V& v)
  701. {
  702. t = u;
  703. eval_bitwise_and(t, v);
  704. }
  705. template <class T, class U, class V>
  706. inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_and(T& t, const U& u, const V& v)
  707. {
  708. eval_bitwise_and_default(t, u, v);
  709. }
  710. template <class T, class U, class V>
  711. BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_or(T& t, const U& u, const V& v);
  712. template <class T>
  713. inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_or_default(T& t, const T& u, const T& v)
  714. {
  715. if (&t == &v)
  716. {
  717. eval_bitwise_or(t, u);
  718. }
  719. else if (&t == &u)
  720. {
  721. eval_bitwise_or(t, v);
  722. }
  723. else
  724. {
  725. t = u;
  726. eval_bitwise_or(t, v);
  727. }
  728. }
  729. template <class T, class U>
  730. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_bitwise_or_default(T& t, const T& u, const U& v)
  731. {
  732. T vv;
  733. vv = v;
  734. eval_bitwise_or(t, u, vv);
  735. }
  736. template <class T, class U>
  737. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_bitwise_or_default(T& t, const T& u, const U& v)
  738. {
  739. T vv(v);
  740. eval_bitwise_or(t, u, vv);
  741. }
  742. template <class T, class U>
  743. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_or_default(T& t, const U& u, const T& v)
  744. {
  745. eval_bitwise_or(t, v, u);
  746. }
  747. template <class T, class U, class V>
  748. inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_or_default(T& t, const U& u, const V& v)
  749. {
  750. BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
  751. {
  752. if ((void*)&t == (void*)&v)
  753. {
  754. eval_bitwise_or(t, u);
  755. }
  756. else
  757. {
  758. t = u;
  759. eval_bitwise_or(t, v);
  760. }
  761. }
  762. else
  763. {
  764. t = u;
  765. eval_bitwise_or(t, v);
  766. }
  767. }
  768. template <class T, class U, class V>
  769. inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_or(T& t, const U& u, const V& v)
  770. {
  771. eval_bitwise_or_default(t, u, v);
  772. }
  773. template <class T, class U, class V>
  774. BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_xor(T& t, const U& u, const V& v);
  775. template <class T>
  776. inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_xor_default(T& t, const T& u, const T& v)
  777. {
  778. if (&t == &v)
  779. {
  780. eval_bitwise_xor(t, u);
  781. }
  782. else if (&t == &u)
  783. {
  784. eval_bitwise_xor(t, v);
  785. }
  786. else
  787. {
  788. t = u;
  789. eval_bitwise_xor(t, v);
  790. }
  791. }
  792. template <class T, class U>
  793. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && !std::is_convertible<U, T>::value>::type eval_bitwise_xor_default(T& t, const T& u, const U& v)
  794. {
  795. T vv;
  796. vv = v;
  797. eval_bitwise_xor(t, u, vv);
  798. }
  799. template <class T, class U>
  800. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value && std::is_convertible<U, T>::value>::type eval_bitwise_xor_default(T& t, const T& u, const U& v)
  801. {
  802. T vv(v);
  803. eval_bitwise_xor(t, u, vv);
  804. }
  805. template <class T, class U>
  806. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_convertible<U, number<T, et_on> >::value>::type eval_bitwise_xor_default(T& t, const U& u, const T& v)
  807. {
  808. eval_bitwise_xor(t, v, u);
  809. }
  810. template <class T, class U, class V>
  811. inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_xor_default(T& t, const U& u, const V& v)
  812. {
  813. BOOST_IF_CONSTEXPR(std::is_same<T, V>::value)
  814. {
  815. if ((void*)&t == (void*)&v)
  816. {
  817. eval_bitwise_xor(t, u);
  818. }
  819. else
  820. {
  821. t = u;
  822. eval_bitwise_xor(t, v);
  823. }
  824. }
  825. else
  826. {
  827. t = u;
  828. eval_bitwise_xor(t, v);
  829. }
  830. }
  831. template <class T, class U, class V>
  832. inline BOOST_MP_CXX14_CONSTEXPR void eval_bitwise_xor(T& t, const U& u, const V& v)
  833. {
  834. eval_bitwise_xor_default(t, u, v);
  835. }
  836. template <class T>
  837. inline BOOST_MP_CXX14_CONSTEXPR void eval_increment(T& val)
  838. {
  839. using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
  840. eval_add(val, static_cast<ui_type>(1u));
  841. }
  842. template <class T>
  843. inline BOOST_MP_CXX14_CONSTEXPR void eval_decrement(T& val)
  844. {
  845. using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
  846. eval_subtract(val, static_cast<ui_type>(1u));
  847. }
  848. template <class T, class U, class V>
  849. inline BOOST_MP_CXX14_CONSTEXPR void eval_left_shift(T& result, const U& arg, const V val)
  850. {
  851. result = arg;
  852. eval_left_shift(result, val);
  853. }
  854. template <class T, class U, class V>
  855. inline BOOST_MP_CXX14_CONSTEXPR void eval_right_shift(T& result, const U& arg, const V val)
  856. {
  857. result = arg;
  858. eval_right_shift(result, val);
  859. }
  860. template <class T>
  861. inline BOOST_MP_CXX14_CONSTEXPR bool eval_is_zero(const T& val)
  862. {
  863. using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
  864. return val.compare(static_cast<ui_type>(0)) == 0;
  865. }
  866. template <class T>
  867. inline BOOST_MP_CXX14_CONSTEXPR int eval_get_sign(const T& val)
  868. {
  869. using ui_type = typename std::tuple_element<0, typename T::unsigned_types>::type;
  870. return val.compare(static_cast<ui_type>(0));
  871. }
  872. template <class T, class V, class U>
  873. inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp2(T& result, const V& v1, const U& v2, const std::false_type&, const std::false_type&)
  874. {
  875. using component_number_type = typename component_type<number<T> >::type;
  876. boost::multiprecision::detail::scoped_precision_options<component_number_type> sp(result);
  877. (void)sp;
  878. component_number_type x(v1), y(v2);
  879. assign_components(result, x.backend(), y.backend());
  880. }
  881. template <class T, class V, class U>
  882. inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp2(T& result, const V& v1, const U& v2, const std::true_type&, const std::false_type&)
  883. {
  884. boost::multiprecision::detail::scoped_source_precision<number<V>> scope;
  885. (void)scope;
  886. assign_components_imp2(result, number<V>(v1), v2, std::false_type(), std::false_type());
  887. }
  888. template <class T, class V, class U>
  889. inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp2(T& result, const V& v1, const U& v2, const std::true_type&, const std::true_type&)
  890. {
  891. boost::multiprecision::detail::scoped_source_precision<number<V>> scope1;
  892. boost::multiprecision::detail::scoped_source_precision<number<U>> scope2;
  893. (void)scope1;
  894. (void)scope2;
  895. assign_components_imp2(result, number<V>(v1), number<U>(v2), std::false_type(), std::false_type());
  896. }
  897. template <class T, class V, class U>
  898. inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp2(T& result, const V& v1, const U& v2, const std::false_type&, const std::true_type&)
  899. {
  900. boost::multiprecision::detail::scoped_source_precision<number<U>> scope;
  901. (void)scope;
  902. assign_components_imp2(result, v1, number<U>(v2), std::false_type(), std::false_type());
  903. }
  904. template <class T, class V, class U>
  905. inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp(T& result, const V& v1, const U& v2, const std::integral_constant<int, number_kind_rational>&)
  906. {
  907. result = v1;
  908. T t;
  909. t = v2;
  910. eval_divide(result, t);
  911. }
  912. template <class T, class V, class U, int N>
  913. inline BOOST_MP_CXX14_CONSTEXPR void assign_components_imp(T& result, const V& v1, const U& v2, const std::integral_constant<int, N>&)
  914. {
  915. assign_components_imp2(result, v1, v2, boost::multiprecision::detail::is_backend<V>(), boost::multiprecision::detail::is_backend<U>());
  916. }
  917. template <class T, class V, class U>
  918. inline BOOST_MP_CXX14_CONSTEXPR void assign_components(T& result, const V& v1, const U& v2)
  919. {
  920. return assign_components_imp(result, v1, v2, typename number_category<T>::type());
  921. }
  922. #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
  923. template <class Result, class Traits>
  924. inline void assign_from_string_view(Result& result, const std::basic_string_view<char, Traits>& view)
  925. {
  926. // since most (all?) backends require a const char* to construct from, we just
  927. // convert to that:
  928. std::string s(view);
  929. result = s.c_str();
  930. }
  931. template <class Result, class Traits>
  932. inline void assign_from_string_view(Result& result, const std::basic_string_view<char, Traits>& view_x, const std::basic_string_view<char, Traits>& view_y)
  933. {
  934. // since most (all?) backends require a const char* to construct from, we just
  935. // convert to that:
  936. std::string x(view_x), y(view_y);
  937. assign_components(result, x.c_str(), y.c_str());
  938. }
  939. #endif
  940. template <class R, int b>
  941. struct has_enough_bits
  942. {
  943. template <class T>
  944. struct type : public std::integral_constant<bool, !std::is_same<R, T>::value && (std::numeric_limits<T>::digits >= b)>
  945. {};
  946. };
  947. template <class R>
  948. struct terminal
  949. {
  950. BOOST_MP_CXX14_CONSTEXPR terminal(const R& v) : value(v) {}
  951. BOOST_MP_CXX14_CONSTEXPR terminal() {}
  952. BOOST_MP_CXX14_CONSTEXPR terminal& operator=(R val)
  953. {
  954. value = val;
  955. return *this;
  956. }
  957. R value;
  958. BOOST_MP_CXX14_CONSTEXPR operator R() const { return value; }
  959. };
  960. template <class Tuple, int i, class T, bool = (i == std::tuple_size<Tuple>::value)>
  961. struct find_index_of_type
  962. {
  963. static constexpr int value =
  964. std::is_same<T, typename std::tuple_element<static_cast<std::size_t>(i), Tuple>::type>::value
  965. ? i
  966. : find_index_of_type<Tuple, i + 1, T>::value;
  967. };
  968. template <class Tuple, int i, class T>
  969. struct find_index_of_type<Tuple, i, T, true>
  970. {
  971. static constexpr int value = -1;
  972. };
  973. template <class R, class B>
  974. struct calculate_next_larger_type
  975. {
  976. // Find which list we're looking through:
  977. using list_type = typename std::conditional<
  978. boost::multiprecision::detail::is_signed<R>::value && boost::multiprecision::detail::is_integral<R>::value,
  979. typename B::signed_types,
  980. typename std::conditional<
  981. boost::multiprecision::detail::is_unsigned<R>::value,
  982. typename B::unsigned_types,
  983. typename B::float_types>::type>::type;
  984. static constexpr int start = find_index_of_type<list_type, 0, R>::value;
  985. static constexpr int index_of_type = boost::multiprecision::detail::find_index_of_large_enough_type<list_type, start == INT_MAX ? 0 : start + 1, boost::multiprecision::detail::bits_of<R>::value> ::value;
  986. using type = typename boost::multiprecision::detail::dereference_tuple<index_of_type, list_type, terminal<R> >::type;
  987. };
  988. template <class R, class T>
  989. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<R>::value, bool>::type check_in_range(const T& t)
  990. {
  991. // Can t fit in an R?
  992. if ((t > 0) && std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::is_bounded && (t > (std::numeric_limits<R>::max)()))
  993. return true;
  994. else
  995. return false;
  996. }
  997. template <class R, class B>
  998. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<R>::value>::type eval_convert_to(R* result, const B& backend)
  999. {
  1000. using next_type = typename calculate_next_larger_type<R, B>::type;
  1001. next_type n = next_type();
  1002. eval_convert_to(&n, backend);
  1003. BOOST_IF_CONSTEXPR(!boost::multiprecision::detail::is_unsigned<R>::value && std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::is_bounded)
  1004. {
  1005. if(n > static_cast<next_type>((std::numeric_limits<R>::max)()))
  1006. {
  1007. *result = (std::numeric_limits<R>::max)();
  1008. return;
  1009. }
  1010. }
  1011. BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_specialized&& std::numeric_limits<R>::is_bounded)
  1012. {
  1013. if (n < static_cast<next_type>((std::numeric_limits<R>::min)()))
  1014. {
  1015. *result = (std::numeric_limits<R>::min)();
  1016. return;
  1017. }
  1018. }
  1019. *result = static_cast<R>(n);
  1020. }
  1021. template <class R, class B>
  1022. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< !boost::multiprecision::detail::is_integral<R>::value && !std::is_enum<R>::value>::type eval_convert_to(R* result, const B& backend)
  1023. {
  1024. using next_type = typename calculate_next_larger_type<R, B>::type;
  1025. next_type n = next_type();
  1026. eval_convert_to(&n, backend);
  1027. BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_specialized && std::numeric_limits<R>::is_bounded)
  1028. {
  1029. if ((n > (next_type)(std::numeric_limits<R>::max)() || (n < (next_type) - (std::numeric_limits<R>::max)())))
  1030. {
  1031. *result = n > 0 ? (std::numeric_limits<R>::max)() : -(std::numeric_limits<R>::max)();
  1032. }
  1033. else
  1034. *result = static_cast<R>(n);
  1035. }
  1036. else
  1037. *result = static_cast<R>(n);
  1038. }
  1039. template <class R, class B>
  1040. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_enum<R>::value>::type eval_convert_to(R* result, const B& backend)
  1041. {
  1042. typename std::underlying_type<R>::type t{};
  1043. eval_convert_to(&t, backend);
  1044. *result = static_cast<R>(t);
  1045. }
  1046. #ifndef BOOST_MP_STANDALONE
  1047. template <class R, class B>
  1048. inline void last_chance_eval_convert_to(terminal<R>* result, const B& backend, const std::integral_constant<bool, false>&)
  1049. {
  1050. //
  1051. // We ran out of types to try for the conversion, try
  1052. // a lexical_cast and hope for the best:
  1053. //
  1054. BOOST_IF_CONSTEXPR (std::numeric_limits<R>::is_integer && !std::numeric_limits<R>::is_signed)
  1055. if (eval_get_sign(backend) < 0)
  1056. BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to convert negative value to an unsigned integer results in undefined behaviour"));
  1057. BOOST_MP_TRY {
  1058. result->value = boost::lexical_cast<R>(backend.str(0, std::ios_base::fmtflags(0)));
  1059. }
  1060. BOOST_MP_CATCH (const bad_lexical_cast&)
  1061. {
  1062. if (eval_get_sign(backend) < 0)
  1063. {
  1064. BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_integer && !std::numeric_limits<R>::is_signed)
  1065. *result = (std::numeric_limits<R>::max)(); // we should never get here, exception above will be raised.
  1066. else BOOST_IF_CONSTEXPR(std::numeric_limits<R>::is_integer)
  1067. *result = (std::numeric_limits<R>::min)();
  1068. else
  1069. *result = -(std::numeric_limits<R>::max)();
  1070. }
  1071. else
  1072. *result = (std::numeric_limits<R>::max)();
  1073. }
  1074. BOOST_MP_CATCH_END
  1075. }
  1076. template <class R, class B>
  1077. inline void last_chance_eval_convert_to(terminal<R>* result, const B& backend, const std::integral_constant<bool, true>&)
  1078. {
  1079. //
  1080. // Last chance conversion to an unsigned integer.
  1081. // We ran out of types to try for the conversion, try
  1082. // a lexical_cast and hope for the best:
  1083. //
  1084. if (eval_get_sign(backend) < 0)
  1085. BOOST_MP_THROW_EXCEPTION(std::range_error("Attempt to convert negative value to an unsigned integer results in undefined behaviour"));
  1086. BOOST_MP_TRY {
  1087. B t(backend);
  1088. R mask = ~static_cast<R>(0u);
  1089. eval_bitwise_and(t, mask);
  1090. result->value = boost::lexical_cast<R>(t.str(0, std::ios_base::fmtflags(0)));
  1091. }
  1092. BOOST_MP_CATCH (const bad_lexical_cast&)
  1093. {
  1094. // We should never really get here...
  1095. *result = (std::numeric_limits<R>::max)();
  1096. }
  1097. BOOST_MP_CATCH_END
  1098. }
  1099. #else // Using standalone mode
  1100. template <class R, class B>
  1101. inline void last_chance_eval_convert_to(terminal<R>*, const B&, const std::integral_constant<bool, false>&)
  1102. {
  1103. static_assert(sizeof(R) == 1, "This type can not be used in standalone mode. Please de-activate and file a bug at https://github.com/boostorg/multiprecision/");
  1104. }
  1105. template <class R, class B>
  1106. inline void last_chance_eval_convert_to(terminal<R>* result, const B& backend, const std::integral_constant<bool, true>&)
  1107. {
  1108. static_cast<void>(result);
  1109. static_cast<void>(backend);
  1110. static_assert(sizeof(R) == 1, "This type can not be used in standalone mode. Please de-activate and file a bug at https://github.com/boostorg/multiprecision/");
  1111. }
  1112. #endif
  1113. template <class R, class B>
  1114. inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(terminal<R>* result, const B& backend)
  1115. {
  1116. using tag_type = std::integral_constant<bool, boost::multiprecision::detail::is_unsigned<R>::value && number_category<B>::value == number_kind_integer>;
  1117. last_chance_eval_convert_to(result, backend, tag_type());
  1118. }
  1119. template <class B1, class B2, expression_template_option et>
  1120. inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(terminal<number<B1, et> >* result, const B2& backend)
  1121. {
  1122. //
  1123. // We ran out of types to try for the conversion, try
  1124. // a generic conversion and hope for the best:
  1125. //
  1126. boost::multiprecision::detail::generic_interconvert(result->value.backend(), backend, number_category<B1>(), number_category<B2>());
  1127. }
  1128. template <class B>
  1129. inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(std::string* result, const B& backend)
  1130. {
  1131. *result = backend.str(0, std::ios_base::fmtflags(0));
  1132. }
  1133. template <class B>
  1134. inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(std::complex<float>* result, const B& backend)
  1135. {
  1136. using scalar_type = typename scalar_result_from_possible_complex<multiprecision::number<B> >::type;
  1137. scalar_type re, im;
  1138. eval_real(re.backend(), backend);
  1139. eval_imag(im.backend(), backend);
  1140. *result = std::complex<float>(re.template convert_to<float>(), im.template convert_to<float>());
  1141. }
  1142. template <class B>
  1143. inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(std::complex<double>* result, const B& backend)
  1144. {
  1145. using scalar_type = typename scalar_result_from_possible_complex<multiprecision::number<B> >::type;
  1146. scalar_type re, im;
  1147. eval_real(re.backend(), backend);
  1148. eval_imag(im.backend(), backend);
  1149. *result = std::complex<double>(re.template convert_to<double>(), im.template convert_to<double>());
  1150. }
  1151. template <class B>
  1152. inline BOOST_MP_CXX14_CONSTEXPR void eval_convert_to(std::complex<long double>* result, const B& backend)
  1153. {
  1154. using scalar_type = typename scalar_result_from_possible_complex<multiprecision::number<B> >::type;
  1155. scalar_type re, im;
  1156. eval_real(re.backend(), backend);
  1157. eval_imag(im.backend(), backend);
  1158. *result = std::complex<long double>(re.template convert_to<long double>(), im.template convert_to<long double>());
  1159. }
  1160. //
  1161. // Functions:
  1162. //
  1163. template <class T, class U>
  1164. inline BOOST_MP_CXX14_CONSTEXPR void eval_abs(T& result, const U& arg)
  1165. {
  1166. using type_list = typename U::signed_types ;
  1167. using front = typename std::tuple_element<0, type_list>::type;
  1168. result = arg;
  1169. if (arg.compare(front(0)) < 0)
  1170. result.negate();
  1171. }
  1172. template <class T, class U>
  1173. inline BOOST_MP_CXX14_CONSTEXPR void eval_fabs(T& result, const U& arg)
  1174. {
  1175. static_assert(number_category<T>::value == number_kind_floating_point, "The fabs function is only valid for floating point types.");
  1176. using type_list = typename U::signed_types ;
  1177. using front = typename std::tuple_element<0, type_list>::type;
  1178. result = arg;
  1179. if (arg.compare(front(0)) < 0)
  1180. result.negate();
  1181. }
  1182. template <class Backend>
  1183. inline BOOST_MP_CXX14_CONSTEXPR int eval_fpclassify(const Backend& arg)
  1184. {
  1185. static_assert(number_category<Backend>::value == number_kind_floating_point, "The fpclassify function is only valid for floating point types.");
  1186. return eval_is_zero(arg) ? FP_ZERO : FP_NORMAL;
  1187. }
  1188. template <class T>
  1189. inline BOOST_MP_CXX14_CONSTEXPR void eval_fmod(T& result, const T& a, const T& b)
  1190. {
  1191. static_assert(number_category<T>::value == number_kind_floating_point, "The fmod function is only valid for floating point types.");
  1192. if ((&result == &a) || (&result == &b))
  1193. {
  1194. T temp;
  1195. eval_fmod(temp, a, b);
  1196. result = temp;
  1197. return;
  1198. }
  1199. switch (eval_fpclassify(a))
  1200. {
  1201. case FP_ZERO:
  1202. result = a;
  1203. return;
  1204. case FP_INFINITE:
  1205. case FP_NAN:
  1206. result = std::numeric_limits<number<T> >::quiet_NaN().backend();
  1207. errno = EDOM;
  1208. return;
  1209. default:
  1210. break;
  1211. }
  1212. switch (eval_fpclassify(b))
  1213. {
  1214. case FP_ZERO:
  1215. case FP_NAN:
  1216. result = std::numeric_limits<number<T> >::quiet_NaN().backend();
  1217. errno = EDOM;
  1218. return;
  1219. default:
  1220. break;
  1221. }
  1222. T n;
  1223. eval_divide(result, a, b);
  1224. if (eval_get_sign(result) < 0)
  1225. eval_ceil(n, result);
  1226. else
  1227. eval_floor(n, result);
  1228. eval_multiply(n, b);
  1229. eval_subtract(result, a, n);
  1230. if (eval_get_sign(result) != 0)
  1231. {
  1232. //
  1233. // Sanity check, that due to rounding errors in division,
  1234. // we haven't accidently calculated the wrong value:
  1235. // See https://github.com/boostorg/multiprecision/issues/604 for an example.
  1236. //
  1237. if (eval_get_sign(result) == eval_get_sign(b))
  1238. {
  1239. if (result.compare(b) >= 0)
  1240. {
  1241. eval_subtract(result, b);
  1242. }
  1243. }
  1244. else
  1245. {
  1246. n = b;
  1247. n.negate();
  1248. if (result.compare(n) >= 0)
  1249. {
  1250. eval_subtract(result, n);
  1251. }
  1252. }
  1253. }
  1254. }
  1255. template <class T, class A>
  1256. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_fmod(T& result, const T& x, const A& a)
  1257. {
  1258. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1259. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  1260. cast_type c;
  1261. c = a;
  1262. eval_fmod(result, x, c);
  1263. }
  1264. template <class T, class A>
  1265. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_fmod(T& result, const A& x, const T& a)
  1266. {
  1267. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1268. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  1269. cast_type c;
  1270. c = x;
  1271. eval_fmod(result, c, a);
  1272. }
  1273. template <class T>
  1274. BOOST_MP_CXX14_CONSTEXPR void eval_round(T& result, const T& a);
  1275. template <class T>
  1276. inline BOOST_MP_CXX14_CONSTEXPR void eval_remquo(T& result, const T& a, const T& b, int* pi)
  1277. {
  1278. static_assert(number_category<T>::value == number_kind_floating_point, "The remquo function is only valid for floating point types.");
  1279. if ((&result == &a) || (&result == &b))
  1280. {
  1281. T temp;
  1282. eval_remquo(temp, a, b, pi);
  1283. result = temp;
  1284. return;
  1285. }
  1286. T n;
  1287. eval_divide(result, a, b);
  1288. eval_round(n, result);
  1289. eval_convert_to(pi, n);
  1290. eval_multiply(n, b);
  1291. eval_subtract(result, a, n);
  1292. if (eval_is_zero(result))
  1293. {
  1294. if (eval_signbit(a))
  1295. result.negate();
  1296. }
  1297. }
  1298. template <class T, class A>
  1299. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_remquo(T& result, const T& x, const A& a, int* pi)
  1300. {
  1301. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1302. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  1303. cast_type c = cast_type();
  1304. c = a;
  1305. eval_remquo(result, x, c, pi);
  1306. }
  1307. template <class T, class A>
  1308. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value, void>::type eval_remquo(T& result, const A& x, const T& a, int* pi)
  1309. {
  1310. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1311. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  1312. cast_type c = cast_type();
  1313. c = x;
  1314. eval_remquo(result, c, a, pi);
  1315. }
  1316. template <class T, class U, class V>
  1317. inline BOOST_MP_CXX14_CONSTEXPR void eval_remainder(T& result, const U& a, const V& b)
  1318. {
  1319. int i(0);
  1320. eval_remquo(result, a, b, &i);
  1321. }
  1322. template <class B>
  1323. BOOST_MP_CXX14_CONSTEXPR bool eval_gt(const B& a, const B& b);
  1324. template <class T, class U>
  1325. BOOST_MP_CXX14_CONSTEXPR bool eval_gt(const T& a, const U& b);
  1326. template <class B>
  1327. BOOST_MP_CXX14_CONSTEXPR bool eval_lt(const B& a, const B& b);
  1328. template <class T, class U>
  1329. BOOST_MP_CXX14_CONSTEXPR bool eval_lt(const T& a, const U& b);
  1330. template <class T>
  1331. inline BOOST_MP_CXX14_CONSTEXPR void eval_fdim(T& result, const T& a, const T& b)
  1332. {
  1333. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1334. const ui_type zero = 0u;
  1335. switch (eval_fpclassify(b))
  1336. {
  1337. case FP_NAN:
  1338. case FP_INFINITE:
  1339. result = zero;
  1340. return;
  1341. default:
  1342. break;
  1343. }
  1344. switch (eval_fpclassify(a))
  1345. {
  1346. case FP_NAN:
  1347. result = zero;
  1348. return;
  1349. case FP_INFINITE:
  1350. result = a;
  1351. return;
  1352. default:
  1353. break;
  1354. }
  1355. if (eval_gt(a, b))
  1356. {
  1357. eval_subtract(result, a, b);
  1358. }
  1359. else
  1360. result = zero;
  1361. }
  1362. template <class T, class A>
  1363. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value>::type eval_fdim(T& result, const T& a, const A& b)
  1364. {
  1365. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1366. using arithmetic_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1367. const ui_type zero = 0u;
  1368. arithmetic_type canonical_b = b;
  1369. switch (BOOST_MP_FPCLASSIFY(b))
  1370. {
  1371. case FP_NAN:
  1372. case FP_INFINITE:
  1373. result = zero;
  1374. return;
  1375. default:
  1376. break;
  1377. }
  1378. switch (eval_fpclassify(a))
  1379. {
  1380. case FP_NAN:
  1381. result = zero;
  1382. return;
  1383. case FP_INFINITE:
  1384. result = a;
  1385. return;
  1386. default:
  1387. break;
  1388. }
  1389. if (eval_gt(a, canonical_b))
  1390. {
  1391. eval_subtract(result, a, canonical_b);
  1392. }
  1393. else
  1394. result = zero;
  1395. }
  1396. template <class T, class A>
  1397. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<A>::value>::type eval_fdim(T& result, const A& a, const T& b)
  1398. {
  1399. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1400. using arithmetic_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1401. const ui_type zero = 0u;
  1402. arithmetic_type canonical_a = a;
  1403. switch (eval_fpclassify(b))
  1404. {
  1405. case FP_NAN:
  1406. case FP_INFINITE:
  1407. result = zero;
  1408. return;
  1409. default:
  1410. break;
  1411. }
  1412. switch (BOOST_MP_FPCLASSIFY(a))
  1413. {
  1414. case FP_NAN:
  1415. result = zero;
  1416. return;
  1417. case FP_INFINITE:
  1418. result = std::numeric_limits<number<T> >::infinity().backend();
  1419. return;
  1420. default:
  1421. break;
  1422. }
  1423. if (eval_gt(canonical_a, b))
  1424. {
  1425. eval_subtract(result, canonical_a, b);
  1426. }
  1427. else
  1428. result = zero;
  1429. }
  1430. template <class T>
  1431. inline BOOST_MP_CXX14_CONSTEXPR void eval_trunc(T& result, const T& a)
  1432. {
  1433. static_assert(number_category<T>::value == number_kind_floating_point, "The trunc function is only valid for floating point types.");
  1434. switch (eval_fpclassify(a))
  1435. {
  1436. case FP_NAN:
  1437. errno = EDOM;
  1438. // fallthrough...
  1439. case FP_ZERO:
  1440. case FP_INFINITE:
  1441. result = a;
  1442. return;
  1443. default:
  1444. break;
  1445. }
  1446. if (eval_get_sign(a) < 0)
  1447. eval_ceil(result, a);
  1448. else
  1449. eval_floor(result, a);
  1450. }
  1451. template <class T>
  1452. inline BOOST_MP_CXX14_CONSTEXPR void eval_modf(T& result, T const& arg, T* pipart)
  1453. {
  1454. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1455. int c = eval_fpclassify(arg);
  1456. if (c == static_cast<int>(FP_NAN))
  1457. {
  1458. if (pipart)
  1459. *pipart = arg;
  1460. result = arg;
  1461. return;
  1462. }
  1463. else if (c == static_cast<int>(FP_INFINITE))
  1464. {
  1465. if (pipart)
  1466. *pipart = arg;
  1467. result = ui_type(0u);
  1468. return;
  1469. }
  1470. if (pipart)
  1471. {
  1472. eval_trunc(*pipart, arg);
  1473. eval_subtract(result, arg, *pipart);
  1474. }
  1475. else
  1476. {
  1477. T ipart;
  1478. eval_trunc(ipart, arg);
  1479. eval_subtract(result, arg, ipart);
  1480. }
  1481. }
  1482. template <class T>
  1483. inline BOOST_MP_CXX14_CONSTEXPR void eval_round(T& result, const T& a)
  1484. {
  1485. static_assert(number_category<T>::value == number_kind_floating_point, "The round function is only valid for floating point types.");
  1486. using fp_type = typename boost::multiprecision::detail::canonical<float, T>::type;
  1487. int c = eval_fpclassify(a);
  1488. if (c == static_cast<int>(FP_NAN))
  1489. {
  1490. result = a;
  1491. errno = EDOM;
  1492. return;
  1493. }
  1494. if ((c == FP_ZERO) || (c == static_cast<int>(FP_INFINITE)))
  1495. {
  1496. result = a;
  1497. }
  1498. else if (eval_get_sign(a) < 0)
  1499. {
  1500. eval_subtract(result, a, fp_type(0.5f));
  1501. eval_ceil(result, result);
  1502. }
  1503. else
  1504. {
  1505. eval_add(result, a, fp_type(0.5f));
  1506. eval_floor(result, result);
  1507. }
  1508. }
  1509. template <class B>
  1510. BOOST_MP_CXX14_CONSTEXPR void eval_lcm(B& result, const B& a, const B& b);
  1511. template <class B>
  1512. BOOST_MP_CXX14_CONSTEXPR void eval_gcd(B& result, const B& a, const B& b);
  1513. template <class T, class Arithmetic>
  1514. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Arithmetic>::value >::type eval_gcd(T& result, const T& a, const Arithmetic& b)
  1515. {
  1516. using si_type = typename boost::multiprecision::detail::canonical<Arithmetic, T>::type;
  1517. using default_ops::eval_gcd;
  1518. T t;
  1519. t = static_cast<si_type>(b);
  1520. eval_gcd(result, a, t);
  1521. }
  1522. template <class T, class Arithmetic>
  1523. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Arithmetic>::value >::type eval_gcd(T& result, const Arithmetic& a, const T& b)
  1524. {
  1525. eval_gcd(result, b, a);
  1526. }
  1527. template <class T, class Arithmetic>
  1528. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Arithmetic>::value >::type eval_lcm(T& result, const T& a, const Arithmetic& b)
  1529. {
  1530. using si_type = typename boost::multiprecision::detail::canonical<Arithmetic, T>::type;
  1531. using default_ops::eval_lcm;
  1532. T t;
  1533. t = static_cast<si_type>(b);
  1534. eval_lcm(result, a, t);
  1535. }
  1536. template <class T, class Arithmetic>
  1537. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<Arithmetic>::value >::type eval_lcm(T& result, const Arithmetic& a, const T& b)
  1538. {
  1539. eval_lcm(result, b, a);
  1540. }
  1541. template <class T>
  1542. inline BOOST_MP_CXX14_CONSTEXPR std::size_t eval_lsb(const T& val)
  1543. {
  1544. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1545. int c = eval_get_sign(val);
  1546. if (c == 0)
  1547. {
  1548. BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
  1549. }
  1550. if (c < 0)
  1551. {
  1552. BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
  1553. }
  1554. std::size_t result = 0;
  1555. T mask, t;
  1556. mask = ui_type(1);
  1557. do
  1558. {
  1559. eval_bitwise_and(t, mask, val);
  1560. ++result;
  1561. eval_left_shift(mask, 1);
  1562. } while (eval_is_zero(t));
  1563. return --result;
  1564. }
  1565. template <class T>
  1566. inline BOOST_MP_CXX14_CONSTEXPR std::ptrdiff_t eval_msb(const T& val)
  1567. {
  1568. int c = eval_get_sign(val);
  1569. if (c == 0)
  1570. {
  1571. BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
  1572. }
  1573. if (c < 0)
  1574. {
  1575. BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
  1576. }
  1577. //
  1578. // This implementation is really really rubbish - it does
  1579. // a linear scan for the most-significant-bit. We should really
  1580. // do a binary search, but as none of our backends actually needs
  1581. // this implementation, we'll leave it for now. In fact for most
  1582. // backends it's likely that there will always be a more efficient
  1583. // native implementation possible.
  1584. //
  1585. std::size_t result = 0;
  1586. T t(val);
  1587. while (!eval_is_zero(t))
  1588. {
  1589. eval_right_shift(t, 1);
  1590. ++result;
  1591. }
  1592. --result;
  1593. return static_cast<std::ptrdiff_t>(result);
  1594. }
  1595. template <class T>
  1596. inline BOOST_MP_CXX14_CONSTEXPR bool eval_bit_test(const T& val, std::size_t index)
  1597. {
  1598. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1599. T mask, t;
  1600. mask = ui_type(1);
  1601. eval_left_shift(mask, index);
  1602. eval_bitwise_and(t, mask, val);
  1603. return !eval_is_zero(t);
  1604. }
  1605. template <class T>
  1606. inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_set(T& val, std::size_t index)
  1607. {
  1608. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1609. T mask;
  1610. mask = ui_type(1);
  1611. eval_left_shift(mask, index);
  1612. eval_bitwise_or(val, mask);
  1613. }
  1614. template <class T>
  1615. inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_flip(T& val, std::size_t index)
  1616. {
  1617. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1618. T mask;
  1619. mask = ui_type(1);
  1620. eval_left_shift(mask, index);
  1621. eval_bitwise_xor(val, mask);
  1622. }
  1623. template <class T>
  1624. inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_unset(T& val, std::size_t index)
  1625. {
  1626. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1627. T mask, t;
  1628. mask = ui_type(1);
  1629. eval_left_shift(mask, index);
  1630. eval_bitwise_and(t, mask, val);
  1631. if (!eval_is_zero(t))
  1632. eval_bitwise_xor(val, mask);
  1633. }
  1634. template <class Backend>
  1635. BOOST_MP_CXX14_CONSTEXPR void eval_qr(const Backend& x, const Backend& y, Backend& q, Backend& r);
  1636. template <class Backend>
  1637. BOOST_MP_CXX14_CONSTEXPR void eval_karatsuba_sqrt(Backend& result, const Backend& x, Backend& r, Backend& t, size_t bits)
  1638. {
  1639. using default_ops::eval_is_zero;
  1640. using default_ops::eval_subtract;
  1641. using default_ops::eval_right_shift;
  1642. using default_ops::eval_left_shift;
  1643. using default_ops::eval_bit_set;
  1644. using default_ops::eval_decrement;
  1645. using default_ops::eval_bitwise_and;
  1646. using default_ops::eval_add;
  1647. using default_ops::eval_qr;
  1648. using small_uint = typename std::tuple_element<0, typename Backend::unsigned_types>::type;
  1649. constexpr small_uint zero = 0u;
  1650. // we can calculate it faster with std::sqrt
  1651. #ifdef BOOST_HAS_INT128
  1652. if (bits <= 128)
  1653. {
  1654. uint128_type a{}, b{}, c{};
  1655. eval_convert_to(&a, x);
  1656. c = boost::multiprecision::detail::karatsuba_sqrt(a, b, bits);
  1657. r = number<Backend>::canonical_value(b);
  1658. result = number<Backend>::canonical_value(c);
  1659. return;
  1660. }
  1661. #else
  1662. if (bits <= std::numeric_limits<std::uintmax_t>::digits)
  1663. {
  1664. std::uintmax_t a{ 0 }, b{ 0 }, c{ 0 };
  1665. eval_convert_to(&a, x);
  1666. c = boost::multiprecision::detail::karatsuba_sqrt(a, b, bits);
  1667. r = number<Backend>::canonical_value(b);
  1668. result = number<Backend>::canonical_value(c);
  1669. return;
  1670. }
  1671. #endif
  1672. // https://hal.inria.fr/file/index/docid/72854/filename/RR-3805.pdf
  1673. std::size_t b = bits / 4;
  1674. Backend q(x);
  1675. eval_right_shift(q, b * 2);
  1676. Backend s;
  1677. eval_karatsuba_sqrt(s, q, r, t, bits - b * 2);
  1678. t = zero;
  1679. eval_bit_set(t, static_cast<unsigned>(b * 2));
  1680. eval_left_shift(r, b);
  1681. eval_decrement(t);
  1682. eval_bitwise_and(t, x);
  1683. eval_right_shift(t, b);
  1684. eval_add(t, r);
  1685. eval_left_shift(s, 1u);
  1686. eval_qr(t, s, q, r);
  1687. eval_left_shift(r, b);
  1688. t = zero;
  1689. eval_bit_set(t, static_cast<unsigned>(b));
  1690. eval_decrement(t);
  1691. eval_bitwise_and(t, x);
  1692. eval_add(r, t);
  1693. eval_left_shift(s, b - 1);
  1694. eval_add(s, q);
  1695. eval_multiply(q, q);
  1696. // we substract after, so it works for unsigned integers too
  1697. if (r.compare(q) < 0)
  1698. {
  1699. t = s;
  1700. eval_left_shift(t, 1u);
  1701. eval_decrement(t);
  1702. eval_add(r, t);
  1703. eval_decrement(s);
  1704. }
  1705. eval_subtract(r, q);
  1706. result = s;
  1707. }
  1708. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  1709. template <class B>
  1710. void BOOST_MP_CXX14_CONSTEXPR eval_integer_sqrt_bitwise(B& s, B& r, const B& x)
  1711. {
  1712. //
  1713. // This is slow bit-by-bit integer square root, see for example
  1714. // http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29
  1715. // There are better methods such as http://hal.inria.fr/docs/00/07/28/54/PDF/RR-3805.pdf
  1716. // and http://hal.inria.fr/docs/00/07/21/13/PDF/RR-4475.pdf which should be implemented
  1717. // at some point.
  1718. //
  1719. using ui_type = typename boost::multiprecision::detail::canonical<unsigned char, B>::type;
  1720. s = ui_type(0u);
  1721. if (eval_get_sign(x) == 0)
  1722. {
  1723. r = ui_type(0u);
  1724. return;
  1725. }
  1726. std::ptrdiff_t g = static_cast<std::ptrdiff_t>(eval_msb(x));
  1727. if (g <= 1)
  1728. {
  1729. s = ui_type(1);
  1730. eval_subtract(r, x, s);
  1731. return;
  1732. }
  1733. B t;
  1734. r = x;
  1735. g /= 2;
  1736. std::ptrdiff_t org_g = g;
  1737. eval_bit_set(s, static_cast<std::size_t>(g));
  1738. eval_bit_set(t, static_cast<std::size_t>(2 * g));
  1739. eval_subtract(r, x, t);
  1740. --g;
  1741. if (eval_get_sign(r) == 0)
  1742. return;
  1743. std::ptrdiff_t msbr = static_cast<std::ptrdiff_t>(eval_msb(r));
  1744. do
  1745. {
  1746. if (msbr >= org_g + g + 1)
  1747. {
  1748. t = s;
  1749. eval_left_shift(t, static_cast<std::size_t>(g + 1));
  1750. eval_bit_set(t, static_cast<std::size_t>(2 * g));
  1751. if (t.compare(r) <= 0)
  1752. {
  1753. BOOST_MP_ASSERT(g >= 0);
  1754. eval_bit_set(s, static_cast<std::size_t>(g));
  1755. eval_subtract(r, t);
  1756. if (eval_get_sign(r) == 0)
  1757. return;
  1758. msbr = static_cast<std::ptrdiff_t>(eval_msb(r));
  1759. }
  1760. }
  1761. --g;
  1762. } while (g >= 0);
  1763. }
  1764. #endif // !BOOST_MP_NO_CONSTEXPR_DETECTION
  1765. template <class Backend>
  1766. BOOST_MP_CXX14_CONSTEXPR void eval_integer_sqrt(Backend& result, Backend& r, const Backend& x)
  1767. {
  1768. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  1769. // recursive Karatsuba sqrt can cause issues in constexpr context:
  1770. if (BOOST_MP_IS_CONST_EVALUATED(result.size()))
  1771. return eval_integer_sqrt_bitwise(result, r, x);
  1772. #endif // !BOOST_MP_NO_CONSTEXPR_DETECTION
  1773. using small_uint = typename std::tuple_element<0, typename Backend::unsigned_types>::type;
  1774. constexpr small_uint zero = 0u;
  1775. if (eval_is_zero(x))
  1776. {
  1777. r = zero;
  1778. result = zero;
  1779. return;
  1780. }
  1781. Backend t;
  1782. eval_karatsuba_sqrt(result, x, r, t, eval_msb(x) + 1);
  1783. }
  1784. template <class B>
  1785. inline BOOST_MP_CXX14_CONSTEXPR void eval_conj(B& result, const B& val)
  1786. {
  1787. result = val; // assume non-complex result.
  1788. }
  1789. template <class B>
  1790. inline BOOST_MP_CXX14_CONSTEXPR void eval_proj(B& result, const B& val)
  1791. {
  1792. result = val; // assume non-complex result.
  1793. }
  1794. //
  1795. // These have to implemented by the backend, declared here so that our macro generated code compiles OK.
  1796. //
  1797. template <class T>
  1798. typename std::enable_if<sizeof(T) == 0>::type eval_floor();
  1799. template <class T>
  1800. typename std::enable_if<sizeof(T) == 0>::type eval_ceil();
  1801. template <class T>
  1802. typename std::enable_if<sizeof(T) == 0>::type eval_trunc();
  1803. template <class T>
  1804. typename std::enable_if<sizeof(T) == 0>::type eval_sqrt();
  1805. template <class T>
  1806. typename std::enable_if<sizeof(T) == 0>::type eval_ldexp();
  1807. template <class T>
  1808. typename std::enable_if<sizeof(T) == 0>::type eval_frexp();
  1809. // TODO implement default versions of these:
  1810. template <class T>
  1811. typename std::enable_if<sizeof(T) == 0>::type eval_asinh();
  1812. template <class T>
  1813. typename std::enable_if<sizeof(T) == 0>::type eval_acosh();
  1814. template <class T>
  1815. typename std::enable_if<sizeof(T) == 0>::type eval_atanh();
  1816. //
  1817. // eval_logb and eval_scalbn simply assume base 2 and forward to
  1818. // eval_ldexp and eval_frexp:
  1819. //
  1820. template <class B>
  1821. inline BOOST_MP_CXX14_CONSTEXPR typename B::exponent_type eval_ilogb(const B& val)
  1822. {
  1823. static_assert(!std::numeric_limits<number<B> >::is_specialized || (std::numeric_limits<number<B> >::radix == 2), "The default implementation of ilogb requires a base 2 number type");
  1824. typename B::exponent_type e(0);
  1825. switch (eval_fpclassify(val))
  1826. {
  1827. case FP_NAN:
  1828. #ifdef FP_ILOGBNAN
  1829. return FP_ILOGBNAN > 0 ? (std::numeric_limits<typename B::exponent_type>::max)() : (std::numeric_limits<typename B::exponent_type>::min)();
  1830. #else
  1831. return (std::numeric_limits<typename B::exponent_type>::max)();
  1832. #endif
  1833. case FP_INFINITE:
  1834. return (std::numeric_limits<typename B::exponent_type>::max)();
  1835. case FP_ZERO:
  1836. return (std::numeric_limits<typename B::exponent_type>::min)();
  1837. default:
  1838. break;
  1839. }
  1840. B result;
  1841. eval_frexp(result, val, &e);
  1842. return e - 1;
  1843. }
  1844. template <class B>
  1845. inline BOOST_MP_CXX14_CONSTEXPR void eval_logb(B& result, const B& val)
  1846. {
  1847. switch (eval_fpclassify(val))
  1848. {
  1849. case FP_NAN:
  1850. result = val;
  1851. errno = EDOM;
  1852. return;
  1853. case FP_ZERO:
  1854. result = std::numeric_limits<number<B> >::infinity().backend();
  1855. result.negate();
  1856. errno = ERANGE;
  1857. return;
  1858. case FP_INFINITE:
  1859. result = val;
  1860. if (eval_signbit(val))
  1861. result.negate();
  1862. return;
  1863. default:
  1864. break;
  1865. }
  1866. using max_t = typename std::conditional<std::is_same<std::intmax_t, long>::value, long long, std::intmax_t>::type;
  1867. result = static_cast<max_t>(eval_ilogb(val));
  1868. }
  1869. template <class B, class A>
  1870. inline BOOST_MP_CXX14_CONSTEXPR void eval_scalbn(B& result, const B& val, A e)
  1871. {
  1872. static_assert(!std::numeric_limits<number<B> >::is_specialized || (std::numeric_limits<number<B> >::radix == 2), "The default implementation of scalbn requires a base 2 number type");
  1873. eval_ldexp(result, val, static_cast<typename B::exponent_type>(e));
  1874. }
  1875. template <class B, class A>
  1876. inline BOOST_MP_CXX14_CONSTEXPR void eval_scalbln(B& result, const B& val, A e)
  1877. {
  1878. eval_scalbn(result, val, e);
  1879. }
  1880. template <class T>
  1881. inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val, std::integral_constant<bool, true> const&, const std::integral_constant<bool, false>&)
  1882. {
  1883. return eval_fpclassify(val) == FP_NAN;
  1884. }
  1885. template <class T>
  1886. inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val, std::integral_constant<bool, false> const&, const std::integral_constant<bool, true>&)
  1887. {
  1888. return BOOST_MP_ISNAN(val);
  1889. }
  1890. template <class T>
  1891. inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T&, std::integral_constant<bool, false> const&, const std::integral_constant<bool, false>&)
  1892. {
  1893. return false;
  1894. }
  1895. template <class T>
  1896. inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val)
  1897. {
  1898. return is_arg_nan(val, std::integral_constant<bool, boost::multiprecision::detail::is_backend<T>::value>(), std::is_floating_point<T>());
  1899. }
  1900. template <class T, class U, class V>
  1901. inline BOOST_MP_CXX14_CONSTEXPR void eval_fmax(T& result, const U& a, const V& b)
  1902. {
  1903. if (is_arg_nan(a))
  1904. result = number<T>::canonical_value(b);
  1905. else if (is_arg_nan(b))
  1906. result = number<T>::canonical_value(a);
  1907. else if (eval_lt(number<T>::canonical_value(a), number<T>::canonical_value(b)))
  1908. result = number<T>::canonical_value(b);
  1909. else
  1910. result = number<T>::canonical_value(a);
  1911. }
  1912. template <class T, class U, class V>
  1913. inline BOOST_MP_CXX14_CONSTEXPR void eval_fmin(T& result, const U& a, const V& b)
  1914. {
  1915. if (is_arg_nan(a))
  1916. result = number<T>::canonical_value(b);
  1917. else if (is_arg_nan(b))
  1918. result = number<T>::canonical_value(a);
  1919. else if (eval_lt(number<T>::canonical_value(a), number<T>::canonical_value(b)))
  1920. result = number<T>::canonical_value(a);
  1921. else
  1922. result = number<T>::canonical_value(b);
  1923. }
  1924. template <class R, class T, class U>
  1925. inline BOOST_MP_CXX14_CONSTEXPR void eval_hypot(R& result, const T& a, const U& b)
  1926. {
  1927. //
  1928. // Normalize x and y, so that both are positive and x >= y:
  1929. //
  1930. R x, y;
  1931. x = number<R>::canonical_value(a);
  1932. y = number<R>::canonical_value(b);
  1933. if (eval_get_sign(x) < 0)
  1934. x.negate();
  1935. if (eval_get_sign(y) < 0)
  1936. y.negate();
  1937. // Special case, see C99 Annex F.
  1938. // The order of the if's is important: do not change!
  1939. int c1 = eval_fpclassify(x);
  1940. int c2 = eval_fpclassify(y);
  1941. if (c1 == FP_ZERO)
  1942. {
  1943. result = y;
  1944. return;
  1945. }
  1946. if (c2 == FP_ZERO)
  1947. {
  1948. result = x;
  1949. return;
  1950. }
  1951. if (c1 == FP_INFINITE)
  1952. {
  1953. result = x;
  1954. return;
  1955. }
  1956. if ((c2 == FP_INFINITE) || (c2 == FP_NAN))
  1957. {
  1958. result = y;
  1959. return;
  1960. }
  1961. if (c1 == FP_NAN)
  1962. {
  1963. result = x;
  1964. return;
  1965. }
  1966. if (eval_gt(y, x))
  1967. x.swap(y);
  1968. eval_multiply(result, x, std::numeric_limits<number<R> >::epsilon().backend());
  1969. if (eval_gt(result, y))
  1970. {
  1971. result = x;
  1972. return;
  1973. }
  1974. R rat;
  1975. eval_divide(rat, y, x);
  1976. eval_multiply(result, rat, rat);
  1977. eval_increment(result);
  1978. eval_sqrt(rat, result);
  1979. eval_multiply(result, rat, x);
  1980. }
  1981. template <class R, class T>
  1982. inline BOOST_MP_CXX14_CONSTEXPR void eval_nearbyint(R& result, const T& a)
  1983. {
  1984. eval_round(result, a);
  1985. }
  1986. template <class R, class T>
  1987. inline BOOST_MP_CXX14_CONSTEXPR void eval_rint(R& result, const T& a)
  1988. {
  1989. eval_nearbyint(result, a);
  1990. }
  1991. template <class T>
  1992. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_backend<T>::value, int>::type eval_signbit(const T& val)
  1993. {
  1994. return eval_get_sign(val) < 0 ? 1 : 0;
  1995. }
  1996. //
  1997. // Real and imaginary parts:
  1998. //
  1999. template <class To, class From>
  2000. inline BOOST_MP_CXX14_CONSTEXPR void eval_real(To& to, const From& from)
  2001. {
  2002. to = from;
  2003. }
  2004. template <class To, class From>
  2005. inline BOOST_MP_CXX14_CONSTEXPR void eval_imag(To& to, const From&)
  2006. {
  2007. using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
  2008. to = ui_type(0);
  2009. }
  2010. } // namespace default_ops
  2011. namespace default_ops_adl {
  2012. template <class To, class From>
  2013. inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real_imp(To& to, const From& from)
  2014. {
  2015. using to_component_type = typename component_type<number<To> >::type;
  2016. typename to_component_type::backend_type to_component;
  2017. to_component = from;
  2018. eval_set_real(to, to_component);
  2019. }
  2020. template <class To, class From>
  2021. inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag_imp(To& to, const From& from)
  2022. {
  2023. using to_component_type = typename component_type<number<To> >::type;
  2024. typename to_component_type::backend_type to_component;
  2025. to_component = from;
  2026. eval_set_imag(to, to_component);
  2027. }
  2028. } // namespace default_ops_adl
  2029. namespace default_ops {
  2030. template <class To, class From>
  2031. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<To>::value == number_kind_complex>::type eval_set_real(To& to, const From& from)
  2032. {
  2033. default_ops_adl::eval_set_real_imp(to, from);
  2034. }
  2035. template <class To, class From>
  2036. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<To>::value != number_kind_complex>::type eval_set_real(To& to, const From& from)
  2037. {
  2038. to = from;
  2039. }
  2040. template <class To, class From>
  2041. inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(To& to, const From& from)
  2042. {
  2043. default_ops_adl::eval_set_imag_imp(to, from);
  2044. }
  2045. template <class T>
  2046. inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(T& to, const T& from)
  2047. {
  2048. to = from;
  2049. }
  2050. template <class T>
  2051. void BOOST_MP_CXX14_CONSTEXPR eval_set_imag(T&, const T&)
  2052. {
  2053. static_assert(sizeof(T) == INT_MAX, "eval_set_imag needs to be specialised for each specific backend");
  2054. }
  2055. //
  2056. // These functions are implemented in separate files, but expanded inline here,
  2057. // DO NOT CHANGE THE ORDER OF THESE INCLUDES:
  2058. //
  2059. #include <boost/multiprecision/detail/functions/constants.hpp>
  2060. #include <boost/multiprecision/detail/functions/pow.hpp>
  2061. #include <boost/multiprecision/detail/functions/trig.hpp>
  2062. } // namespace default_ops
  2063. //
  2064. // Default versions of floating point classification routines:
  2065. //
  2066. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2067. inline BOOST_MP_CXX14_CONSTEXPR int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2068. {
  2069. using multiprecision::default_ops::eval_fpclassify;
  2070. return eval_fpclassify(arg.backend());
  2071. }
  2072. template <class tag, class A1, class A2, class A3, class A4>
  2073. inline BOOST_MP_CXX14_CONSTEXPR int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2074. {
  2075. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2076. return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2077. }
  2078. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2079. inline BOOST_MP_CXX14_CONSTEXPR bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2080. {
  2081. int v = fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg);
  2082. return (v != static_cast<int>(FP_INFINITE)) && (v != static_cast<int>(FP_NAN));
  2083. }
  2084. template <class tag, class A1, class A2, class A3, class A4>
  2085. inline BOOST_MP_CXX14_CONSTEXPR bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2086. {
  2087. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2088. return isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2089. }
  2090. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2091. inline BOOST_MP_CXX14_CONSTEXPR bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2092. {
  2093. return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_NAN);
  2094. }
  2095. template <class tag, class A1, class A2, class A3, class A4>
  2096. inline BOOST_MP_CXX14_CONSTEXPR bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2097. {
  2098. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2099. return isnan BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2100. }
  2101. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2102. inline BOOST_MP_CXX14_CONSTEXPR bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2103. {
  2104. return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_INFINITE);
  2105. }
  2106. template <class tag, class A1, class A2, class A3, class A4>
  2107. inline BOOST_MP_CXX14_CONSTEXPR bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2108. {
  2109. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2110. return isinf BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2111. }
  2112. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2113. inline BOOST_MP_CXX14_CONSTEXPR bool isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2114. {
  2115. return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_NORMAL);
  2116. }
  2117. template <class tag, class A1, class A2, class A3, class A4>
  2118. inline BOOST_MP_CXX14_CONSTEXPR bool isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2119. {
  2120. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2121. return isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2122. }
  2123. // Default versions of sign manipulation functions, if individual backends can do better than this
  2124. // (for example with signed zero), then they should overload these functions further:
  2125. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2126. inline BOOST_MP_CXX14_CONSTEXPR int sign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2127. {
  2128. return arg.sign();
  2129. }
  2130. template <class tag, class A1, class A2, class A3, class A4>
  2131. inline BOOST_MP_CXX14_CONSTEXPR int sign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2132. {
  2133. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2134. return sign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2135. }
  2136. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2137. inline BOOST_MP_CXX14_CONSTEXPR bool signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2138. {
  2139. using default_ops::eval_signbit;
  2140. return static_cast<bool>(eval_signbit(arg.backend()));
  2141. }
  2142. template <class tag, class A1, class A2, class A3, class A4>
  2143. inline BOOST_MP_CXX14_CONSTEXPR bool signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2144. {
  2145. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2146. return static_cast<bool>(signbit BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg)));
  2147. }
  2148. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2149. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> changesign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2150. {
  2151. return -arg;
  2152. }
  2153. template <class tag, class A1, class A2, class A3, class A4>
  2154. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type changesign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2155. {
  2156. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2157. return changesign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2158. }
  2159. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2160. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
  2161. {
  2162. return (boost::multiprecision::signbit)(a) != (boost::multiprecision::signbit)(b) ? (boost::multiprecision::changesign)(a) : a;
  2163. }
  2164. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2165. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::detail::expression<tag, A1, A2, A3, A4>& b)
  2166. {
  2167. return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
  2168. }
  2169. template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2170. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
  2171. {
  2172. return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
  2173. }
  2174. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  2175. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
  2176. {
  2177. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2178. return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
  2179. }
  2180. //
  2181. // real and imag:
  2182. //
  2183. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2184. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type
  2185. real(const multiprecision::number<Backend, ExpressionTemplates>& a)
  2186. {
  2187. using default_ops::eval_real;
  2188. using result_type = typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type;
  2189. boost::multiprecision::detail::scoped_default_precision<result_type> precision_guard(a);
  2190. result_type result;
  2191. eval_real(result.backend(), a.backend());
  2192. return result;
  2193. }
  2194. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2195. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type
  2196. imag(const multiprecision::number<Backend, ExpressionTemplates>& a)
  2197. {
  2198. using default_ops::eval_imag;
  2199. using result_type = typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type;
  2200. boost::multiprecision::detail::scoped_default_precision<result_type> precision_guard(a);
  2201. result_type result;
  2202. eval_imag(result.backend(), a.backend());
  2203. return result;
  2204. }
  2205. template <class tag, class A1, class A2, class A3, class A4>
  2206. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2207. real(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2208. {
  2209. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2210. detail::scoped_default_precision<value_type> precision_guard(arg);
  2211. return real(value_type(arg));
  2212. }
  2213. template <class tag, class A1, class A2, class A3, class A4>
  2214. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2215. imag(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2216. {
  2217. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2218. detail::scoped_default_precision<value_type> precision_guard(arg);
  2219. return imag(value_type(arg));
  2220. }
  2221. //
  2222. // Complex number functions, these are overloaded at the Backend level, we just provide the
  2223. // expression template versions here, plus overloads for non-complex types:
  2224. //
  2225. #ifdef BOOST_MP_MATH_AVAILABLE
  2226. template <class T, expression_template_option ExpressionTemplates>
  2227. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_complex, component_type<number<T, ExpressionTemplates>>>::type::type
  2228. abs(const number<T, ExpressionTemplates>& v)
  2229. {
  2230. return boost::math::hypot(real(v), imag(v));
  2231. }
  2232. template <class tag, class A1, class A2, class A3, class A4>
  2233. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_complex, component_type<typename detail::expression<tag, A1, A2, A3, A4>::result_type>>::type::type
  2234. abs(const detail::expression<tag, A1, A2, A3, A4>& v)
  2235. {
  2236. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2237. return abs(static_cast<number_type>(v));
  2238. }
  2239. template <class T, expression_template_option ExpressionTemplates>
  2240. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_complex, typename scalar_result_from_possible_complex<number<T, ExpressionTemplates> >::type>::type
  2241. arg(const number<T, ExpressionTemplates>& v)
  2242. {
  2243. return atan2(imag(v), real(v));
  2244. }
  2245. template <class T, expression_template_option ExpressionTemplates>
  2246. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, typename scalar_result_from_possible_complex<number<T, ExpressionTemplates> >::type>::type
  2247. arg(const number<T, ExpressionTemplates>&)
  2248. {
  2249. return 0;
  2250. }
  2251. template <class tag, class A1, class A2, class A3, class A4>
  2252. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_complex || number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename scalar_result_from_possible_complex<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type>::type
  2253. arg(const detail::expression<tag, A1, A2, A3, A4>& v)
  2254. {
  2255. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2256. return arg(static_cast<number_type>(v));
  2257. }
  2258. #endif // BOOST_MP_MATH_AVAILABLE
  2259. template <class T, expression_template_option ExpressionTemplates>
  2260. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_complex, component_type<number<T, ExpressionTemplates>>>::type::type
  2261. norm(const number<T, ExpressionTemplates>& v)
  2262. {
  2263. typename component_type<number<T, ExpressionTemplates> >::type a(real(v)), b(imag(v));
  2264. return a * a + b * b;
  2265. }
  2266. template <class T, expression_template_option ExpressionTemplates>
  2267. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value != number_kind_complex, typename scalar_result_from_possible_complex<number<T, ExpressionTemplates> >::type>::type
  2268. norm(const number<T, ExpressionTemplates>& v)
  2269. {
  2270. return v * v;
  2271. }
  2272. template <class tag, class A1, class A2, class A3, class A4>
  2273. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2274. norm(const detail::expression<tag, A1, A2, A3, A4>& v)
  2275. {
  2276. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2277. return norm(static_cast<number_type>(v));
  2278. }
  2279. template <class Backend, expression_template_option ExpressionTemplates>
  2280. BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type polar(number<Backend, ExpressionTemplates> const& r, number<Backend, ExpressionTemplates> const& theta)
  2281. {
  2282. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
  2283. }
  2284. template <class tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
  2285. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_same<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, ExpressionTemplates> >::value,
  2286. typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type>::type
  2287. polar(detail::expression<tag, A1, A2, A3, A4> const& r, number<Backend, ExpressionTemplates> const& theta)
  2288. {
  2289. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
  2290. }
  2291. template <class Backend, expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2292. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_same<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, ExpressionTemplates> >::value,
  2293. typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type>::type
  2294. polar(number<Backend, ExpressionTemplates> const& r, detail::expression<tag, A1, A2, A3, A4> const& theta)
  2295. {
  2296. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
  2297. }
  2298. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  2299. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_same<typename detail::expression<tag, A1, A2, A3, A4>::result_type, typename detail::expression<tagb, A1b, A2b, A3b, A4b>::result_type>::value,
  2300. typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type>::type
  2301. polar(detail::expression<tag, A1, A2, A3, A4> const& r, detail::expression<tagb, A1b, A2b, A3b, A4b> const& theta)
  2302. {
  2303. using scalar_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2304. return typename complex_result_from_scalar<scalar_type>::type(scalar_type(r * cos(theta)), scalar_type(r * sin(theta)));
  2305. }
  2306. //
  2307. // We also allow the first argument to polar to be an arithmetic type (probably a literal):
  2308. //
  2309. template <class Scalar, class Backend, expression_template_option ExpressionTemplates>
  2310. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Scalar>::value, typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type>::type
  2311. polar(Scalar const& r, number<Backend, ExpressionTemplates> const& theta)
  2312. {
  2313. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
  2314. }
  2315. template <class tag, class A1, class A2, class A3, class A4, class Scalar>
  2316. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Scalar>::value,
  2317. typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type>::type
  2318. polar(Scalar const& r, detail::expression<tag, A1, A2, A3, A4> const& theta)
  2319. {
  2320. using scalar_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2321. return typename complex_result_from_scalar<scalar_type>::type(scalar_type(r * cos(theta)), scalar_type(r * sin(theta)));
  2322. }
  2323. //
  2324. // Single argument overloads:
  2325. //
  2326. template <class Backend, expression_template_option ExpressionTemplates>
  2327. BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type polar(number<Backend, ExpressionTemplates> const& r)
  2328. {
  2329. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(r);
  2330. }
  2331. template <class tag, class A1, class A2, class A3, class A4>
  2332. BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2333. polar(detail::expression<tag, A1, A2, A3, A4> const& r)
  2334. {
  2335. return typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type(r);
  2336. }
  2337. } // namespace multiprecision
  2338. namespace math {
  2339. //
  2340. // Import Math functions here, so they can be found by Boost.Math:
  2341. //
  2342. using boost::multiprecision::changesign;
  2343. using boost::multiprecision::copysign;
  2344. using boost::multiprecision::fpclassify;
  2345. using boost::multiprecision::isfinite;
  2346. using boost::multiprecision::isinf;
  2347. using boost::multiprecision::isnan;
  2348. using boost::multiprecision::isnormal;
  2349. using boost::multiprecision::sign;
  2350. using boost::multiprecision::signbit;
  2351. #ifndef BOOST_MP_MATH_AVAILABLE
  2352. namespace policies {
  2353. template <typename... Args>
  2354. class policy {};
  2355. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  2356. void raise_rounding_error(T1, T2, T3, T4, T5)
  2357. {
  2358. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Rounding error"));
  2359. }
  2360. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  2361. void raise_overflow_error(T1, T2, T3, T4, T5)
  2362. {
  2363. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Overflow error"));
  2364. }
  2365. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  2366. void raise_evaluation_error(T1, T2, T3, T4, T5)
  2367. {
  2368. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Evaluation error"));
  2369. }
  2370. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  2371. void raise_domain_error(T1, T2, T3, T4, T5)
  2372. {
  2373. BOOST_MP_THROW_EXCEPTION(std::domain_error("Domain error"));
  2374. }
  2375. template <typename T, typename... Args>
  2376. struct is_policy
  2377. {
  2378. static constexpr bool value = false;
  2379. };
  2380. template <typename... Args>
  2381. struct is_policy<policy<Args...>>
  2382. {
  2383. static constexpr bool value = true;
  2384. };
  2385. } // namespace policies
  2386. #endif
  2387. } // namespace math
  2388. namespace multiprecision {
  2389. #ifdef BOOST_MP_MATH_AVAILABLE
  2390. using c99_error_policy = ::boost::math::policies::policy<
  2391. ::boost::math::policies::domain_error< ::boost::math::policies::errno_on_error>,
  2392. ::boost::math::policies::pole_error< ::boost::math::policies::errno_on_error>,
  2393. ::boost::math::policies::overflow_error< ::boost::math::policies::errno_on_error>,
  2394. ::boost::math::policies::evaluation_error< ::boost::math::policies::errno_on_error>,
  2395. ::boost::math::policies::rounding_error< ::boost::math::policies::errno_on_error> >;
  2396. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2397. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
  2398. asinh
  2399. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2400. {
  2401. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2402. return boost::math::asinh(arg, c99_error_policy());
  2403. }
  2404. template <class tag, class A1, class A2, class A3, class A4>
  2405. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex, typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2406. asinh
  2407. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2408. {
  2409. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2410. detail::scoped_default_precision<value_type> precision_guard(arg);
  2411. return asinh(value_type(arg));
  2412. }
  2413. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2414. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
  2415. acosh
  2416. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2417. {
  2418. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2419. return boost::math::acosh(arg, c99_error_policy());
  2420. }
  2421. template <class tag, class A1, class A2, class A3, class A4>
  2422. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex, typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2423. acosh
  2424. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2425. {
  2426. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2427. detail::scoped_default_precision<value_type> precision_guard(arg);
  2428. return acosh(value_type(arg));
  2429. }
  2430. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2431. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
  2432. atanh
  2433. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2434. {
  2435. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2436. return boost::math::atanh(arg, c99_error_policy());
  2437. }
  2438. template <class tag, class A1, class A2, class A3, class A4>
  2439. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex, typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2440. atanh
  2441. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2442. {
  2443. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2444. detail::scoped_default_precision<value_type> precision_guard(arg);
  2445. return atanh(value_type(arg));
  2446. }
  2447. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2448. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2449. {
  2450. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2451. return boost::math::cbrt(arg, c99_error_policy());
  2452. }
  2453. template <class tag, class A1, class A2, class A3, class A4>
  2454. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2455. {
  2456. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2457. detail::scoped_default_precision<value_type> precision_guard(arg);
  2458. return cbrt(value_type(arg));
  2459. }
  2460. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2461. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2462. {
  2463. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2464. return boost::math::erf(arg, c99_error_policy());
  2465. }
  2466. template <class tag, class A1, class A2, class A3, class A4>
  2467. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type erf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2468. {
  2469. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2470. detail::scoped_default_precision<value_type> precision_guard(arg);
  2471. return erf(value_type(arg));
  2472. }
  2473. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2474. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2475. {
  2476. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2477. return boost::math::erfc(arg, c99_error_policy());
  2478. }
  2479. template <class tag, class A1, class A2, class A3, class A4>
  2480. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2481. {
  2482. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2483. detail::scoped_default_precision<value_type> precision_guard(arg);
  2484. return erfc(value_type(arg));
  2485. }
  2486. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2487. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2488. {
  2489. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2490. return boost::math::expm1(arg, c99_error_policy());
  2491. }
  2492. template <class tag, class A1, class A2, class A3, class A4>
  2493. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2494. {
  2495. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2496. detail::scoped_default_precision<value_type> precision_guard(arg);
  2497. return expm1(value_type(arg));
  2498. }
  2499. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2500. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2501. {
  2502. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2503. multiprecision::number<Backend, ExpressionTemplates> result;
  2504. result = boost::math::lgamma(arg, c99_error_policy());
  2505. if ((boost::multiprecision::isnan)(result) && !(boost::multiprecision::isnan)(arg))
  2506. {
  2507. result = std::numeric_limits<multiprecision::number<Backend, ExpressionTemplates> >::infinity();
  2508. errno = ERANGE;
  2509. }
  2510. return result;
  2511. }
  2512. template <class tag, class A1, class A2, class A3, class A4>
  2513. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2514. {
  2515. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2516. detail::scoped_default_precision<value_type> precision_guard(arg);
  2517. return lgamma(value_type(arg));
  2518. }
  2519. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2520. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2521. {
  2522. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2523. if ((arg == 0) && std::numeric_limits<multiprecision::number<Backend, ExpressionTemplates> >::has_infinity)
  2524. {
  2525. errno = ERANGE;
  2526. return 1 / arg;
  2527. }
  2528. return boost::math::tgamma(arg, c99_error_policy());
  2529. }
  2530. template <class tag, class A1, class A2, class A3, class A4>
  2531. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2532. {
  2533. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2534. detail::scoped_default_precision<value_type> precision_guard(arg);
  2535. return tgamma(value_type(arg));
  2536. }
  2537. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2538. inline BOOST_MP_CXX14_CONSTEXPR long lrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2539. {
  2540. return lround(arg);
  2541. }
  2542. template <class tag, class A1, class A2, class A3, class A4>
  2543. inline BOOST_MP_CXX14_CONSTEXPR long lrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2544. {
  2545. return lround(arg);
  2546. }
  2547. #ifndef BOOST_NO_LONG_LONG
  2548. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2549. inline BOOST_MP_CXX14_CONSTEXPR long long llrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2550. {
  2551. return llround(arg);
  2552. }
  2553. template <class tag, class A1, class A2, class A3, class A4>
  2554. inline BOOST_MP_CXX14_CONSTEXPR long long llrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2555. {
  2556. return llround(arg);
  2557. }
  2558. #endif
  2559. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2560. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2561. {
  2562. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2563. return boost::math::log1p(arg, c99_error_policy());
  2564. }
  2565. template <class tag, class A1, class A2, class A3, class A4>
  2566. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2567. {
  2568. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2569. detail::scoped_default_precision<value_type> precision_guard(arg);
  2570. return log1p(value_type(arg));
  2571. }
  2572. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2573. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
  2574. {
  2575. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2576. return boost::math::nextafter(a, b, c99_error_policy());
  2577. }
  2578. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2579. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::detail::expression<tag, A1, A2, A3, A4>& b)
  2580. {
  2581. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2582. return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
  2583. }
  2584. template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2585. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
  2586. {
  2587. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2588. return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
  2589. }
  2590. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  2591. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
  2592. {
  2593. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2594. detail::scoped_default_precision<value_type> precision_guard(a, b);
  2595. return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
  2596. }
  2597. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2598. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
  2599. {
  2600. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2601. return boost::math::nextafter(a, b, c99_error_policy());
  2602. }
  2603. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2604. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& a, const multiprecision::detail::expression<tag, A1, A2, A3, A4>& b)
  2605. {
  2606. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2607. return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
  2608. }
  2609. template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2610. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::number<Backend, ExpressionTemplates>& b)
  2611. {
  2612. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2613. return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
  2614. }
  2615. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  2616. inline BOOST_MP_CXX14_CONSTEXPR typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& a, const multiprecision::detail::expression<tagb, A1b, A2b, A3b, A4b>& b)
  2617. {
  2618. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2619. detail::scoped_default_precision<value_type> precision_guard(a, b);
  2620. return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
  2621. }
  2622. #endif // BOOST_MP_MATH_AVAILABLE
  2623. template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
  2624. inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& add(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
  2625. {
  2626. static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2627. static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2628. using default_ops::eval_add;
  2629. eval_add(result.backend(), a.backend(), b.backend());
  2630. return result;
  2631. }
  2632. template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
  2633. inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& subtract(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
  2634. {
  2635. static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2636. static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2637. using default_ops::eval_subtract;
  2638. eval_subtract(result.backend(), a.backend(), b.backend());
  2639. return result;
  2640. }
  2641. template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
  2642. inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& multiply(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
  2643. {
  2644. static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2645. static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2646. using default_ops::eval_multiply;
  2647. eval_multiply(result.backend(), a.backend(), b.backend());
  2648. return result;
  2649. }
  2650. template <class B, expression_template_option ET, class I>
  2651. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
  2652. add(number<B, ET>& result, const I& a, const I& b)
  2653. {
  2654. using default_ops::eval_add;
  2655. using canonical_type = typename detail::canonical<I, B>::type;
  2656. eval_add(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
  2657. return result;
  2658. }
  2659. template <class B, expression_template_option ET, class I>
  2660. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
  2661. subtract(number<B, ET>& result, const I& a, const I& b)
  2662. {
  2663. using default_ops::eval_subtract;
  2664. using canonical_type = typename detail::canonical<I, B>::type;
  2665. eval_subtract(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
  2666. return result;
  2667. }
  2668. template <class B, expression_template_option ET, class I>
  2669. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
  2670. multiply(number<B, ET>& result, const I& a, const I& b)
  2671. {
  2672. using default_ops::eval_multiply;
  2673. using canonical_type = typename detail::canonical<I, B>::type;
  2674. eval_multiply(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
  2675. return result;
  2676. }
  2677. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2678. inline BOOST_MP_CXX14_CONSTEXPR typename detail::expression<tag, A1, A2, A3, A4>::result_type trunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2679. {
  2680. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2681. return trunc(number_type(v), pol);
  2682. }
  2683. template <class Backend, expression_template_option ExpressionTemplates, class Policy>
  2684. inline BOOST_MP_CXX14_CONSTEXPR number<Backend, ExpressionTemplates> trunc(const number<Backend, ExpressionTemplates>& v, const Policy&)
  2685. {
  2686. using default_ops::eval_trunc;
  2687. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(v);
  2688. number<Backend, ExpressionTemplates> result;
  2689. eval_trunc(result.backend(), v.backend());
  2690. return result;
  2691. }
  2692. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2693. inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2694. {
  2695. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2696. number_type r(trunc(v, pol));
  2697. if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
  2698. return boost::math::policies::raise_rounding_error("boost::multiprecision::itrunc<%1%>(%1%)", nullptr, number_type(v), 0, pol);
  2699. return r.template convert_to<int>();
  2700. }
  2701. template <class tag, class A1, class A2, class A3, class A4>
  2702. inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
  2703. {
  2704. return itrunc(v, boost::math::policies::policy<>());
  2705. }
  2706. template <class Backend, expression_template_option ExpressionTemplates, class Policy>
  2707. inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const number<Backend, ExpressionTemplates>& v, const Policy& pol)
  2708. {
  2709. number<Backend, ExpressionTemplates> r(trunc(v, pol));
  2710. if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
  2711. return boost::math::policies::raise_rounding_error("boost::multiprecision::itrunc<%1%>(%1%)", nullptr, v, 0, pol);
  2712. return r.template convert_to<int>();
  2713. }
  2714. template <class Backend, expression_template_option ExpressionTemplates>
  2715. inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const number<Backend, ExpressionTemplates>& v)
  2716. {
  2717. return itrunc(v, boost::math::policies::policy<>());
  2718. }
  2719. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2720. inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2721. {
  2722. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2723. number_type r(trunc(v, pol));
  2724. if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
  2725. return boost::math::policies::raise_rounding_error("boost::multiprecision::ltrunc<%1%>(%1%)", nullptr, number_type(v), 0L, pol);
  2726. return r.template convert_to<long>();
  2727. }
  2728. template <class tag, class A1, class A2, class A3, class A4>
  2729. inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
  2730. {
  2731. return ltrunc(v, boost::math::policies::policy<>());
  2732. }
  2733. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2734. inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2735. {
  2736. number<T, ExpressionTemplates> r(trunc(v, pol));
  2737. if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
  2738. return boost::math::policies::raise_rounding_error("boost::multiprecision::ltrunc<%1%>(%1%)", nullptr, v, 0L, pol);
  2739. return r.template convert_to<long>();
  2740. }
  2741. template <class T, expression_template_option ExpressionTemplates>
  2742. inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const number<T, ExpressionTemplates>& v)
  2743. {
  2744. return ltrunc(v, boost::math::policies::policy<>());
  2745. }
  2746. #ifndef BOOST_NO_LONG_LONG
  2747. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2748. inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2749. {
  2750. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2751. number_type r(trunc(v, pol));
  2752. if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
  2753. return boost::math::policies::raise_rounding_error("boost::multiprecision::lltrunc<%1%>(%1%)", nullptr, number_type(v), 0LL, pol);
  2754. return r.template convert_to<long long>();
  2755. }
  2756. template <class tag, class A1, class A2, class A3, class A4>
  2757. inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
  2758. {
  2759. return lltrunc(v, boost::math::policies::policy<>());
  2760. }
  2761. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2762. inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2763. {
  2764. number<T, ExpressionTemplates> r(trunc(v, pol));
  2765. if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
  2766. return boost::math::policies::raise_rounding_error("boost::multiprecision::lltrunc<%1%>(%1%)", nullptr, v, 0LL, pol);
  2767. return r.template convert_to<long long>();
  2768. }
  2769. template <class T, expression_template_option ExpressionTemplates>
  2770. inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const number<T, ExpressionTemplates>& v)
  2771. {
  2772. return lltrunc(v, boost::math::policies::policy<>());
  2773. }
  2774. #endif
  2775. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2776. inline BOOST_MP_CXX14_CONSTEXPR typename detail::expression<tag, A1, A2, A3, A4>::result_type round(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2777. {
  2778. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2779. return round(static_cast<number_type>(v), pol);
  2780. }
  2781. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2782. inline BOOST_MP_CXX14_CONSTEXPR number<T, ExpressionTemplates> round(const number<T, ExpressionTemplates>& v, const Policy&)
  2783. {
  2784. using default_ops::eval_round;
  2785. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2786. number<T, ExpressionTemplates> result;
  2787. eval_round(result.backend(), v.backend());
  2788. return result;
  2789. }
  2790. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2791. inline BOOST_MP_CXX14_CONSTEXPR int iround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2792. {
  2793. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2794. number_type r(round(v, pol));
  2795. if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
  2796. return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, number_type(v), 0, pol);
  2797. return r.template convert_to<int>();
  2798. }
  2799. template <class tag, class A1, class A2, class A3, class A4>
  2800. inline BOOST_MP_CXX14_CONSTEXPR int iround(const detail::expression<tag, A1, A2, A3, A4>& v)
  2801. {
  2802. return iround(v, boost::math::policies::policy<>());
  2803. }
  2804. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2805. inline BOOST_MP_CXX14_CONSTEXPR int iround(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2806. {
  2807. number<T, ExpressionTemplates> r(round(v, pol));
  2808. if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
  2809. return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, v, 0, pol);
  2810. return r.template convert_to<int>();
  2811. }
  2812. template <class T, expression_template_option ExpressionTemplates>
  2813. inline BOOST_MP_CXX14_CONSTEXPR int iround(const number<T, ExpressionTemplates>& v)
  2814. {
  2815. return iround(v, boost::math::policies::policy<>());
  2816. }
  2817. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2818. inline BOOST_MP_CXX14_CONSTEXPR long lround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2819. {
  2820. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2821. number_type r(round(v, pol));
  2822. if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
  2823. return boost::math::policies::raise_rounding_error("boost::multiprecision::lround<%1%>(%1%)", nullptr, number_type(v), 0L, pol);
  2824. return r.template convert_to<long>();
  2825. }
  2826. template <class tag, class A1, class A2, class A3, class A4>
  2827. inline BOOST_MP_CXX14_CONSTEXPR long lround(const detail::expression<tag, A1, A2, A3, A4>& v)
  2828. {
  2829. return lround(v, boost::math::policies::policy<>());
  2830. }
  2831. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2832. inline BOOST_MP_CXX14_CONSTEXPR long lround(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2833. {
  2834. number<T, ExpressionTemplates> r(round(v, pol));
  2835. if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
  2836. return boost::math::policies::raise_rounding_error("boost::multiprecision::lround<%1%>(%1%)", nullptr, v, 0L, pol);
  2837. return r.template convert_to<long>();
  2838. }
  2839. template <class T, expression_template_option ExpressionTemplates>
  2840. inline BOOST_MP_CXX14_CONSTEXPR long lround(const number<T, ExpressionTemplates>& v)
  2841. {
  2842. return lround(v, boost::math::policies::policy<>());
  2843. }
  2844. #ifndef BOOST_NO_LONG_LONG
  2845. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2846. inline BOOST_MP_CXX14_CONSTEXPR long long llround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2847. {
  2848. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2849. number_type r(round(v, pol));
  2850. if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
  2851. return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, number_type(v), 0LL, pol);
  2852. return r.template convert_to<long long>();
  2853. }
  2854. template <class tag, class A1, class A2, class A3, class A4>
  2855. inline BOOST_MP_CXX14_CONSTEXPR long long llround(const detail::expression<tag, A1, A2, A3, A4>& v)
  2856. {
  2857. return llround(v, boost::math::policies::policy<>());
  2858. }
  2859. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2860. inline BOOST_MP_CXX14_CONSTEXPR long long llround(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2861. {
  2862. number<T, ExpressionTemplates> r(round(v, pol));
  2863. if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
  2864. return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, v, 0LL, pol);
  2865. return r.template convert_to<long long>();
  2866. }
  2867. template <class T, expression_template_option ExpressionTemplates>
  2868. inline BOOST_MP_CXX14_CONSTEXPR long long llround(const number<T, ExpressionTemplates>& v)
  2869. {
  2870. return llround(v, boost::math::policies::policy<>());
  2871. }
  2872. #endif
  2873. //
  2874. // frexp does not return an expression template since we require the
  2875. // integer argument to be evaluated even if the returned value is
  2876. // not assigned to anything...
  2877. //
  2878. template <class T, expression_template_option ExpressionTemplates>
  2879. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, short* pint)
  2880. {
  2881. using default_ops::eval_frexp;
  2882. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2883. number<T, ExpressionTemplates> result;
  2884. eval_frexp(result.backend(), v.backend(), pint);
  2885. return result;
  2886. }
  2887. template <class tag, class A1, class A2, class A3, class A4>
  2888. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2889. frexp(const detail::expression<tag, A1, A2, A3, A4>& v, short* pint)
  2890. {
  2891. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2892. return frexp(static_cast<number_type>(v), pint);
  2893. }
  2894. template <class T, expression_template_option ExpressionTemplates>
  2895. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, int* pint)
  2896. {
  2897. using default_ops::eval_frexp;
  2898. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2899. number<T, ExpressionTemplates> result;
  2900. eval_frexp(result.backend(), v.backend(), pint);
  2901. return result;
  2902. }
  2903. template <class tag, class A1, class A2, class A3, class A4>
  2904. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2905. frexp(const detail::expression<tag, A1, A2, A3, A4>& v, int* pint)
  2906. {
  2907. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2908. return frexp(static_cast<number_type>(v), pint);
  2909. }
  2910. template <class T, expression_template_option ExpressionTemplates>
  2911. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, long* pint)
  2912. {
  2913. using default_ops::eval_frexp;
  2914. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2915. number<T, ExpressionTemplates> result;
  2916. eval_frexp(result.backend(), v.backend(), pint);
  2917. return result;
  2918. }
  2919. template <class tag, class A1, class A2, class A3, class A4>
  2920. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2921. frexp(const detail::expression<tag, A1, A2, A3, A4>& v, long* pint)
  2922. {
  2923. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2924. return frexp(static_cast<number_type>(v), pint);
  2925. }
  2926. template <class T, expression_template_option ExpressionTemplates>
  2927. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type frexp(const number<T, ExpressionTemplates>& v, long long* pint)
  2928. {
  2929. using default_ops::eval_frexp;
  2930. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2931. number<T, ExpressionTemplates> result;
  2932. eval_frexp(result.backend(), v.backend(), pint);
  2933. return result;
  2934. }
  2935. template <class tag, class A1, class A2, class A3, class A4>
  2936. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_floating_point, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2937. frexp(const detail::expression<tag, A1, A2, A3, A4>& v, long long* pint)
  2938. {
  2939. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2940. return frexp(static_cast<number_type>(v), pint);
  2941. }
  2942. //
  2943. // modf does not return an expression template since we require the
  2944. // second argument to be evaluated even if the returned value is
  2945. // not assigned to anything...
  2946. //
  2947. template <class T, expression_template_option ExpressionTemplates>
  2948. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type modf(const number<T, ExpressionTemplates>& v, number<T, ExpressionTemplates>* pipart)
  2949. {
  2950. using default_ops::eval_modf;
  2951. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2952. number<T, ExpressionTemplates> result;
  2953. eval_modf(result.backend(), v.backend(), pipart ? &pipart->backend() : nullptr);
  2954. return result;
  2955. }
  2956. template <class T, expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2957. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_floating_point, number<T, ExpressionTemplates> >::type modf(const detail::expression<tag, A1, A2, A3, A4>& v, number<T, ExpressionTemplates>* pipart)
  2958. {
  2959. using default_ops::eval_modf;
  2960. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2961. number<T, ExpressionTemplates> result, arg(v);
  2962. eval_modf(result.backend(), arg.backend(), pipart ? &pipart->backend() : nullptr);
  2963. return result;
  2964. }
  2965. //
  2966. // Integer square root:
  2967. //
  2968. template <class B, expression_template_option ExpressionTemplates>
  2969. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
  2970. sqrt(const number<B, ExpressionTemplates>& x)
  2971. {
  2972. using default_ops::eval_integer_sqrt;
  2973. number<B, ExpressionTemplates> s, r;
  2974. eval_integer_sqrt(s.backend(), r.backend(), x.backend());
  2975. return s;
  2976. }
  2977. template <class tag, class A1, class A2, class A3, class A4>
  2978. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value == number_kind_integer, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2979. sqrt(const detail::expression<tag, A1, A2, A3, A4>& arg)
  2980. {
  2981. using default_ops::eval_integer_sqrt;
  2982. using result_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2983. detail::scoped_default_precision<result_type> precision_guard(arg);
  2984. result_type result, v(arg), r;
  2985. eval_integer_sqrt(result.backend(), r.backend(), v.backend());
  2986. return result;
  2987. }
  2988. //
  2989. // fma:
  2990. //
  2991. namespace default_ops {
  2992. struct fma_func
  2993. {
  2994. template <class B, class T, class U, class V>
  2995. BOOST_MP_CXX14_CONSTEXPR void operator()(B& result, const T& a, const U& b, const V& c) const
  2996. {
  2997. eval_multiply_add(result, a, b, c);
  2998. }
  2999. };
  3000. } // namespace default_ops
  3001. template <class Backend, class U, class V>
  3002. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3003. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
  3004. (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
  3005. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  3006. detail::expression<detail::function, default_ops::fma_func, number<Backend, et_on>, U, V> >::type
  3007. fma(const number<Backend, et_on>& a, const U& b, const V& c)
  3008. {
  3009. return detail::expression<detail::function, default_ops::fma_func, number<Backend, et_on>, U, V>(
  3010. default_ops::fma_func(), a, b, c);
  3011. }
  3012. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class U, class V>
  3013. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3014. (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
  3015. (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
  3016. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  3017. detail::expression<detail::function, default_ops::fma_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, V> >::type
  3018. fma(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& a, const U& b, const V& c)
  3019. {
  3020. return detail::expression<detail::function, default_ops::fma_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, V>(
  3021. default_ops::fma_func(), a, b, c);
  3022. }
  3023. template <class Backend, class U, class V>
  3024. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3025. (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
  3026. (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
  3027. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  3028. number<Backend, et_off> >::type
  3029. fma(const number<Backend, et_off>& a, const U& b, const V& c)
  3030. {
  3031. using default_ops::eval_multiply_add;
  3032. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
  3033. number<Backend, et_off> result;
  3034. eval_multiply_add(result.backend(), number<Backend, et_off>::canonical_value(a), number<Backend, et_off>::canonical_value(b), number<Backend, et_off>::canonical_value(c));
  3035. return result;
  3036. }
  3037. template <class U, class Backend, class V>
  3038. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3039. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
  3040. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3041. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  3042. detail::expression<detail::function, default_ops::fma_func, U, number<Backend, et_on>, V> >::type
  3043. fma(const U& a, const number<Backend, et_on>& b, const V& c)
  3044. {
  3045. return detail::expression<detail::function, default_ops::fma_func, U, number<Backend, et_on>, V>(
  3046. default_ops::fma_func(), a, b, c);
  3047. }
  3048. template <class U, class tag, class Arg1, class Arg2, class Arg3, class Arg4, class V>
  3049. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3050. (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
  3051. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3052. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  3053. detail::expression<detail::function, default_ops::fma_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, V> >::type
  3054. fma(const U& a, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& b, const V& c)
  3055. {
  3056. return detail::expression<detail::function, default_ops::fma_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, V>(
  3057. default_ops::fma_func(), a, b, c);
  3058. }
  3059. template <class U, class Backend, class V>
  3060. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3061. (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
  3062. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3063. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  3064. number<Backend, et_off> >::type
  3065. fma(const U& a, const number<Backend, et_off>& b, const V& c)
  3066. {
  3067. using default_ops::eval_multiply_add;
  3068. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
  3069. number<Backend, et_off> result;
  3070. eval_multiply_add(result.backend(), number<Backend, et_off>::canonical_value(a), number<Backend, et_off>::canonical_value(b), number<Backend, et_off>::canonical_value(c));
  3071. return result;
  3072. }
  3073. template <class U, class V, class Backend>
  3074. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3075. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
  3076. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3077. boost::multiprecision::detail::is_arithmetic<V>::value,
  3078. detail::expression<detail::function, default_ops::fma_func, U, V, number<Backend, et_on> > >::type
  3079. fma(const U& a, const V& b, const number<Backend, et_on>& c)
  3080. {
  3081. return detail::expression<detail::function, default_ops::fma_func, U, V, number<Backend, et_on> >(
  3082. default_ops::fma_func(), a, b, c);
  3083. }
  3084. template <class U, class V, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  3085. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3086. (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
  3087. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3088. boost::multiprecision::detail::is_arithmetic<V>::value,
  3089. detail::expression<detail::function, default_ops::fma_func, U, V, detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > >::type
  3090. fma(const U& a, const V& b, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& c)
  3091. {
  3092. return detail::expression<detail::function, default_ops::fma_func, U, V, detail::expression<tag, Arg1, Arg2, Arg3, Arg4> >(
  3093. default_ops::fma_func(), a, b, c);
  3094. }
  3095. template <class U, class V, class Backend>
  3096. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3097. (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
  3098. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3099. boost::multiprecision::detail::is_arithmetic<V>::value,
  3100. number<Backend, et_off> >::type
  3101. fma(const U& a, const V& b, const number<Backend, et_off>& c)
  3102. {
  3103. using default_ops::eval_multiply_add;
  3104. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
  3105. number<Backend, et_off> result;
  3106. eval_multiply_add(result.backend(), number<Backend, et_off>::canonical_value(a), number<Backend, et_off>::canonical_value(b), number<Backend, et_off>::canonical_value(c));
  3107. return result;
  3108. }
  3109. namespace default_ops {
  3110. struct remquo_func
  3111. {
  3112. template <class B, class T, class U>
  3113. BOOST_MP_CXX14_CONSTEXPR void operator()(B& result, const T& a, const U& b, int* pi) const
  3114. {
  3115. eval_remquo(result, a, b, pi);
  3116. }
  3117. };
  3118. } // namespace default_ops
  3119. template <class Backend, class U>
  3120. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3121. number_category<number<Backend, et_on> >::value == number_kind_floating_point,
  3122. detail::expression<detail::function, default_ops::remquo_func, number<Backend, et_on>, U, int*> >::type
  3123. remquo(const number<Backend, et_on>& a, const U& b, int* pi)
  3124. {
  3125. return detail::expression<detail::function, default_ops::remquo_func, number<Backend, et_on>, U, int*>(
  3126. default_ops::remquo_func(), a, b, pi);
  3127. }
  3128. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class U>
  3129. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3130. number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point,
  3131. detail::expression<detail::function, default_ops::remquo_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, int*> >::type
  3132. remquo(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& a, const U& b, int* pi)
  3133. {
  3134. return detail::expression<detail::function, default_ops::remquo_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, int*>(
  3135. default_ops::remquo_func(), a, b, pi);
  3136. }
  3137. template <class U, class Backend>
  3138. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3139. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) && !is_number<U>::value && !is_number_expression<U>::value,
  3140. detail::expression<detail::function, default_ops::remquo_func, U, number<Backend, et_on>, int*> >::type
  3141. remquo(const U& a, const number<Backend, et_on>& b, int* pi)
  3142. {
  3143. return detail::expression<detail::function, default_ops::remquo_func, U, number<Backend, et_on>, int*>(
  3144. default_ops::remquo_func(), a, b, pi);
  3145. }
  3146. template <class U, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  3147. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3148. (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) && !is_number<U>::value && !is_number_expression<U>::value,
  3149. detail::expression<detail::function, default_ops::remquo_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, int*> >::type
  3150. remquo(const U& a, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& b, int* pi)
  3151. {
  3152. return detail::expression<detail::function, default_ops::remquo_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, int*>(
  3153. default_ops::remquo_func(), a, b, pi);
  3154. }
  3155. template <class Backend, class U>
  3156. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3157. number_category<number<Backend, et_on> >::value == number_kind_floating_point,
  3158. number<Backend, et_off> >::type
  3159. remquo(const number<Backend, et_off>& a, const U& b, int* pi)
  3160. {
  3161. using default_ops::eval_remquo;
  3162. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b);
  3163. number<Backend, et_off> result;
  3164. eval_remquo(result.backend(), a.backend(), number<Backend, et_off>::canonical_value(b), pi);
  3165. return result;
  3166. }
  3167. template <class U, class Backend>
  3168. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3169. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) && !is_number<U>::value && !is_number_expression<U>::value,
  3170. number<Backend, et_off> >::type
  3171. remquo(const U& a, const number<Backend, et_off>& b, int* pi)
  3172. {
  3173. using default_ops::eval_remquo;
  3174. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b);
  3175. number<Backend, et_off> result;
  3176. eval_remquo(result.backend(), number<Backend, et_off>::canonical_value(a), b.backend(), pi);
  3177. return result;
  3178. }
  3179. template <class B, expression_template_option ExpressionTemplates>
  3180. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
  3181. sqrt(const number<B, ExpressionTemplates>& x, number<B, ExpressionTemplates>& r)
  3182. {
  3183. using default_ops::eval_integer_sqrt;
  3184. detail::scoped_default_precision<multiprecision::number<B, ExpressionTemplates> > precision_guard(x, r);
  3185. number<B, ExpressionTemplates> s;
  3186. eval_integer_sqrt(s.backend(), r.backend(), x.backend());
  3187. return s;
  3188. }
  3189. template <class B, expression_template_option ExpressionTemplates, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  3190. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
  3191. sqrt(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& arg, number<B, ExpressionTemplates>& r)
  3192. {
  3193. using default_ops::eval_integer_sqrt;
  3194. detail::scoped_default_precision<multiprecision::number<B, ExpressionTemplates> > precision_guard(r);
  3195. number<B, ExpressionTemplates> s;
  3196. number<B, ExpressionTemplates> x(arg);
  3197. eval_integer_sqrt(s.backend(), r.backend(), x.backend());
  3198. return s;
  3199. }
  3200. // clang-format off
  3201. //
  3202. // Regrettably, when the argument to a function is an rvalue we must return by value, and not return an
  3203. // expression template, otherwise we can end up with dangling references.
  3204. // See https://github.com/boostorg/multiprecision/issues/175.
  3205. //
  3206. #define UNARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
  3207. template <class Backend> \
  3208. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> > ::type \
  3209. func(number<Backend, et_on>&& arg) \
  3210. { \
  3211. detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg); \
  3212. number<Backend, et_on> result; \
  3213. using default_ops::BOOST_JOIN(eval_, func); \
  3214. BOOST_JOIN(eval_, func)(result.backend(), arg.backend()); \
  3215. return result; \
  3216. } \
  3217. #define BINARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
  3218. template <class Backend> \
  3219. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> >::type func(number<Backend, et_on>&& arg, const number<Backend, et_on>& a) \
  3220. { \
  3221. detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a); \
  3222. number<Backend, et_on> result; \
  3223. using default_ops::BOOST_JOIN(eval_, func); \
  3224. BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend()); \
  3225. return result; \
  3226. } \
  3227. template <class Backend> \
  3228. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> >::type func(const number<Backend, et_on>& arg, number<Backend, et_on>&& a) \
  3229. { \
  3230. detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a); \
  3231. number<Backend, et_on> result; \
  3232. using default_ops::BOOST_JOIN(eval_, func); \
  3233. BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend()); \
  3234. return result; \
  3235. } \
  3236. template <class Backend> \
  3237. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> >::type func(number<Backend, et_on>&& arg, number<Backend, et_on>&& a) \
  3238. { \
  3239. detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a); \
  3240. number<Backend, et_on> result; \
  3241. using default_ops::BOOST_JOIN(eval_, func); \
  3242. BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend()); \
  3243. return result; \
  3244. } \
  3245. template <class Backend, class tag, class A1, class A2, class A3, class A4> \
  3246. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category) && (std::is_convertible<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, et_on> >::value), \
  3247. number<Backend, et_on> > ::type \
  3248. func(number<Backend, et_on>&& arg, const detail::expression<tag, A1, A2, A3, A4>& a) \
  3249. { \
  3250. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3251. number<Backend, et_on>, detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3252. } \
  3253. template <class tag, class A1, class A2, class A3, class A4, class Backend> \
  3254. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category) && (std::is_convertible<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, et_on> >::value), \
  3255. number<Backend, et_on> > ::type \
  3256. func(const detail::expression<tag, A1, A2, A3, A4>& arg, number<Backend, et_on>&& a) \
  3257. { \
  3258. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3259. detail::expression<tag, A1, A2, A3, A4>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3260. } \
  3261. template <class Backend, class Arithmetic> \
  3262. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3263. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category), \
  3264. number<Backend, et_on> >::type \
  3265. func(number<Backend, et_on>&& arg, const Arithmetic& a) \
  3266. { \
  3267. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, \
  3268. number<Backend, et_on>, Arithmetic > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3269. } \
  3270. template <class Backend, class Arithmetic> \
  3271. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3272. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category), \
  3273. number<Backend, et_on> > ::type \
  3274. func(const Arithmetic& arg, number<Backend, et_on>&& a) \
  3275. { \
  3276. return detail::expression< \
  3277. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3278. Arithmetic, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a); \
  3279. } \
  3280. #define UNARY_OP_FUNCTOR(func, category) \
  3281. namespace detail { \
  3282. template <class Backend> \
  3283. struct BOOST_JOIN(category, BOOST_JOIN(func, _funct)) \
  3284. { \
  3285. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const \
  3286. { \
  3287. using default_ops::BOOST_JOIN(eval_, func); \
  3288. BOOST_JOIN(eval_, func) \
  3289. (result, arg); \
  3290. } \
  3291. template <class U> \
  3292. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg) const \
  3293. { \
  3294. using default_ops::BOOST_JOIN(eval_, func); \
  3295. Backend temp; \
  3296. BOOST_JOIN(eval_, func) \
  3297. (temp, arg); \
  3298. result = std::move(temp); \
  3299. } \
  3300. }; \
  3301. } \
  3302. \
  3303. template <class tag, class A1, class A2, class A3, class A4> \
  3304. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category, \
  3305. detail::expression<detail::function, \
  3306. detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3307. detail::expression<tag, A1, A2, A3, A4> > > ::type \
  3308. func(const detail::expression<tag, A1, A2, A3, A4>& arg) \
  3309. { \
  3310. return detail::expression< \
  3311. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3312. detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type > (), arg); \
  3313. } \
  3314. template <class Backend> \
  3315. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, \
  3316. detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, number<Backend, et_on> > > ::type \
  3317. func(const number<Backend, et_on>& arg) \
  3318. { \
  3319. return detail::expression< \
  3320. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3321. number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg); \
  3322. } \
  3323. template <class Backend> \
  3324. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3325. boost::multiprecision::number_category<Backend>::value == category, \
  3326. number<Backend, et_off> >::type \
  3327. func(const number<Backend, et_off>& arg) \
  3328. { \
  3329. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg); \
  3330. number<Backend, et_off> result; \
  3331. using default_ops::BOOST_JOIN(eval_, func); \
  3332. BOOST_JOIN(eval_, func)(result.backend(), arg.backend()); \
  3333. return result; \
  3334. }\
  3335. UNARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
  3336. #define BINARY_OP_FUNCTOR(func, category) \
  3337. namespace detail { \
  3338. template <class Backend> \
  3339. struct BOOST_JOIN(category, BOOST_JOIN(func, _funct)) \
  3340. { \
  3341. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg, const Backend& a) const \
  3342. { \
  3343. using default_ops::BOOST_JOIN(eval_, func); \
  3344. BOOST_JOIN(eval_, func) \
  3345. (result, arg, a); \
  3346. } \
  3347. template <class Arithmetic> \
  3348. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg, const Arithmetic& a) const \
  3349. { \
  3350. using default_ops::BOOST_JOIN(eval_, func); \
  3351. BOOST_JOIN(eval_, func) \
  3352. (result, arg, number<Backend>::canonical_value(a)); \
  3353. } \
  3354. template <class Arithmetic> \
  3355. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Arithmetic& arg, const Backend& a) const \
  3356. { \
  3357. using default_ops::BOOST_JOIN(eval_, func); \
  3358. BOOST_JOIN(eval_, func) \
  3359. (result, number<Backend>::canonical_value(arg), a); \
  3360. } \
  3361. template <class U> \
  3362. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg, const Backend& a) const \
  3363. { \
  3364. using default_ops::BOOST_JOIN(eval_, func); \
  3365. Backend r; \
  3366. BOOST_JOIN(eval_, func) \
  3367. (r, arg, a); \
  3368. result = std::move(r); \
  3369. } \
  3370. template <class U, class Arithmetic> \
  3371. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg, const Arithmetic& a) const \
  3372. { \
  3373. using default_ops::BOOST_JOIN(eval_, func); \
  3374. Backend r; \
  3375. BOOST_JOIN(eval_, func) \
  3376. (r, arg, number<Backend>::canonical_value(a)); \
  3377. result = std::move(r); \
  3378. } \
  3379. template <class U, class Arithmetic> \
  3380. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Arithmetic& arg, const Backend& a) const \
  3381. { \
  3382. using default_ops::BOOST_JOIN(eval_, func); \
  3383. Backend r; \
  3384. BOOST_JOIN(eval_, func) \
  3385. (r, number<Backend>::canonical_value(arg), a); \
  3386. result = std::move(r); \
  3387. } \
  3388. }; \
  3389. } \
  3390. template <class Backend> \
  3391. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, detail::expression<detail::function, \
  3392. detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, number<Backend, et_on>, number<Backend, et_on> > > ::type \
  3393. func(const number<Backend, et_on>& arg, const number<Backend, et_on>& a) \
  3394. { \
  3395. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3396. number<Backend, et_on>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3397. } \
  3398. template <class Backend, class tag, class A1, class A2, class A3, class A4> \
  3399. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category) && (std::is_convertible<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, et_on> >::value), \
  3400. detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, number<Backend, et_on>, detail::expression<tag, A1, A2, A3, A4> > > ::type \
  3401. func(const number<Backend, et_on>& arg, const detail::expression<tag, A1, A2, A3, A4>& a) \
  3402. { \
  3403. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3404. number<Backend, et_on>, detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3405. } \
  3406. template <class tag, class A1, class A2, class A3, class A4, class Backend> \
  3407. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category) && (std::is_convertible<typename detail::expression<tag, A1, A2, A3, A4>::result_type, number<Backend, et_on> >::value), \
  3408. detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, detail::expression<tag, A1, A2, A3, A4>, number<Backend, et_on> > > ::type \
  3409. func(const detail::expression<tag, A1, A2, A3, A4>& arg, const number<Backend, et_on>& a) \
  3410. { \
  3411. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3412. detail::expression<tag, A1, A2, A3, A4>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3413. } \
  3414. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b> \
  3415. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category) && (number_category<detail::expression<tagb, A1b, A2b, A3b, A4b> >::value == category), \
  3416. detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3417. detail::expression<tag, A1, A2, A3, A4>, detail::expression<tagb, A1b, A2b, A3b, A4b> > > ::type \
  3418. func(const detail::expression<tag, A1, A2, A3, A4>& arg, const detail::expression<tagb, A1b, A2b, A3b, A4b>& a) \
  3419. { \
  3420. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3421. detail::expression<tag, A1, A2, A3, A4>, detail::expression<tagb, A1b, A2b, A3b, A4b> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg, a); \
  3422. } \
  3423. template <class Backend, class Arithmetic> \
  3424. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3425. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category), \
  3426. detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3427. number<Backend, et_on>, Arithmetic> > ::type \
  3428. func(const number<Backend, et_on>& arg, const Arithmetic& a) \
  3429. { \
  3430. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, \
  3431. number<Backend, et_on>, Arithmetic > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3432. } \
  3433. template <class tag, class A1, class A2, class A3, class A4, class Arithmetic> \
  3434. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3435. is_compatible_arithmetic_type<Arithmetic, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value && (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category), \
  3436. detail::expression< \
  3437. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3438. detail::expression<tag, A1, A2, A3, A4>, Arithmetic> > ::type \
  3439. func(const detail::expression<tag, A1, A2, A3, A4>& arg, const Arithmetic& a) \
  3440. { \
  3441. return detail::expression< \
  3442. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3443. detail::expression<tag, A1, A2, A3, A4>, Arithmetic > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type > (), arg, a); \
  3444. } \
  3445. template <class Backend, class Arithmetic> \
  3446. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3447. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category), \
  3448. detail::expression< \
  3449. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3450. Arithmetic, number<Backend, et_on> > > ::type \
  3451. func(const Arithmetic& arg, const number<Backend, et_on>& a) \
  3452. { \
  3453. return detail::expression< \
  3454. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3455. Arithmetic, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a); \
  3456. } \
  3457. template <class tag, class A1, class A2, class A3, class A4, class Arithmetic> \
  3458. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3459. is_compatible_arithmetic_type<Arithmetic, typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value && (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category), \
  3460. detail::expression< \
  3461. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3462. Arithmetic, detail::expression<tag, A1, A2, A3, A4> > > ::type \
  3463. func(const Arithmetic& arg, const detail::expression<tag, A1, A2, A3, A4>& a) \
  3464. { \
  3465. return detail::expression< \
  3466. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3467. Arithmetic, detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type > (), arg, a); \
  3468. } \
  3469. template <class Backend> \
  3470. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category), number<Backend, et_off> >::type \
  3471. func(const number<Backend, et_off>& arg, const number<Backend, et_off>& a) \
  3472. { \
  3473. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg, a); \
  3474. number<Backend, et_off> result; \
  3475. using default_ops::BOOST_JOIN(eval_, func); \
  3476. BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend()); \
  3477. return result; \
  3478. } \
  3479. template <class Backend, class Arithmetic> \
  3480. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3481. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_off> >::value && (number_category<Backend>::value == category), \
  3482. number<Backend, et_off> >::type \
  3483. func(const number<Backend, et_off>& arg, const Arithmetic& a) \
  3484. { \
  3485. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg); \
  3486. number<Backend, et_off> result; \
  3487. using default_ops::BOOST_JOIN(eval_, func); \
  3488. BOOST_JOIN(eval_, func) \
  3489. (result.backend(), arg.backend(), number<Backend, et_off>::canonical_value(a)); \
  3490. return result; \
  3491. } \
  3492. template <class Backend, class Arithmetic> \
  3493. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3494. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_off> >::value && (number_category<Backend>::value == category), \
  3495. number<Backend, et_off> >::type \
  3496. func(const Arithmetic& a, const number<Backend, et_off>& arg) \
  3497. { \
  3498. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg); \
  3499. number<Backend, et_off> result; \
  3500. using default_ops::BOOST_JOIN(eval_, func); \
  3501. BOOST_JOIN(eval_, func) \
  3502. (result.backend(), number<Backend, et_off>::canonical_value(a), arg.backend()); \
  3503. return result; \
  3504. }\
  3505. BINARY_OP_FUNCTOR_CXX11_RVALUE(func, category)
  3506. #define HETERO_BINARY_OP_FUNCTOR_B(func, Arg2, category) \
  3507. template <class tag, class A1, class A2, class A3, class A4> \
  3508. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3509. (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category), \
  3510. detail::expression< \
  3511. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3512. detail::expression<tag, A1, A2, A3, A4>, Arg2> > ::type \
  3513. func(const detail::expression<tag, A1, A2, A3, A4>& arg, Arg2 const& a) \
  3514. { \
  3515. return detail::expression< \
  3516. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3517. detail::expression<tag, A1, A2, A3, A4>, Arg2 > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type > (), arg, a); \
  3518. } \
  3519. template <class Backend> \
  3520. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3521. (number_category<Backend>::value == category), \
  3522. detail::expression< \
  3523. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3524. number<Backend, et_on>, Arg2> > ::type \
  3525. func(const number<Backend, et_on>& arg, Arg2 const& a) \
  3526. { \
  3527. return detail::expression< \
  3528. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3529. number<Backend, et_on>, Arg2 > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a); \
  3530. } \
  3531. template <class Backend> \
  3532. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3533. (number_category<Backend>::value == category), \
  3534. number<Backend, et_off> >::type \
  3535. func(const number<Backend, et_off>& arg, Arg2 const& a) \
  3536. { \
  3537. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg, a); \
  3538. number<Backend, et_off> result; \
  3539. using default_ops::BOOST_JOIN(eval_, func); \
  3540. BOOST_JOIN(eval_, func) \
  3541. (result.backend(), arg.backend(), a); \
  3542. return result; \
  3543. }
  3544. #define HETERO_BINARY_OP_FUNCTOR(func, Arg2, category) \
  3545. namespace detail { \
  3546. template <class Backend> \
  3547. struct BOOST_JOIN(category, BOOST_JOIN(func, _funct)) \
  3548. { \
  3549. template <class Arg> \
  3550. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, Backend const& arg, Arg a) const \
  3551. { \
  3552. using default_ops::BOOST_JOIN(eval_, func); \
  3553. BOOST_JOIN(eval_, func) \
  3554. (result, arg, a); \
  3555. } \
  3556. template <class U, class Arg> \
  3557. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, Backend const& arg, Arg a) const \
  3558. { \
  3559. using default_ops::BOOST_JOIN(eval_, func); \
  3560. Backend temp; \
  3561. BOOST_JOIN(eval_, func) \
  3562. (temp, arg, a); \
  3563. result = std::move(temp); \
  3564. } \
  3565. }; \
  3566. } \
  3567. \
  3568. HETERO_BINARY_OP_FUNCTOR_B(func, Arg2, category)
  3569. // clang-format on
  3570. namespace detail {
  3571. template <class Backend>
  3572. struct abs_funct
  3573. {
  3574. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
  3575. {
  3576. using default_ops::eval_abs;
  3577. eval_abs(result, arg);
  3578. }
  3579. };
  3580. template <class Backend>
  3581. struct conj_funct
  3582. {
  3583. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
  3584. {
  3585. using default_ops::eval_conj;
  3586. eval_conj(result, arg);
  3587. }
  3588. //
  3589. // To allow for mixed complex/scalar arithmetic where conj is called on the scalar type (as in Eigen)
  3590. // we provide an overload that will promote the arg to the distination type:
  3591. //
  3592. template <class Other>
  3593. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_constructible<Other, Backend>::value>::type operator()(Other& result, const Backend& arg) const
  3594. {
  3595. using default_ops::eval_conj;
  3596. Other t(arg);
  3597. eval_conj(result, t);
  3598. }
  3599. };
  3600. template <class Backend>
  3601. struct proj_funct
  3602. {
  3603. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
  3604. {
  3605. using default_ops::eval_proj;
  3606. eval_proj(result, arg);
  3607. }
  3608. };
  3609. } // namespace detail
  3610. template <class tag, class A1, class A2, class A3, class A4>
  3611. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::value != number_kind_complex,
  3612. detail::expression<
  3613. detail::function, detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> > >::type
  3614. abs(const detail::expression<tag, A1, A2, A3, A4>& arg)
  3615. {
  3616. return detail::expression<
  3617. detail::function, detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
  3618. detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
  3619. }
  3620. template <class Backend>
  3621. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex,
  3622. detail::expression<
  3623. detail::function, detail::abs_funct<Backend>, number<Backend, et_on> > >::type
  3624. abs(const number<Backend, et_on>& arg)
  3625. {
  3626. return detail::expression<
  3627. detail::function, detail::abs_funct<Backend>, number<Backend, et_on> >(
  3628. detail::abs_funct<Backend>(), arg);
  3629. }
  3630. template <class Backend>
  3631. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, number<Backend, et_off> >::type
  3632. abs(const number<Backend, et_off>& arg)
  3633. {
  3634. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
  3635. number<Backend, et_off> result;
  3636. using default_ops::eval_abs;
  3637. eval_abs(result.backend(), arg.backend());
  3638. return result;
  3639. }
  3640. template <class tag, class A1, class A2, class A3, class A4>
  3641. inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
  3642. detail::function, detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >
  3643. conj(const detail::expression<tag, A1, A2, A3, A4>& arg)
  3644. {
  3645. return detail::expression<
  3646. detail::function, detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
  3647. detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
  3648. }
  3649. template <class Backend>
  3650. inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
  3651. detail::function, detail::conj_funct<Backend>, number<Backend, et_on> >
  3652. conj(const number<Backend, et_on>& arg)
  3653. {
  3654. return detail::expression<
  3655. detail::function, detail::conj_funct<Backend>, number<Backend, et_on> >(
  3656. detail::conj_funct<Backend>(), arg);
  3657. }
  3658. template <class Backend>
  3659. inline BOOST_MP_CXX14_CONSTEXPR number<Backend, et_off>
  3660. conj(const number<Backend, et_off>& arg)
  3661. {
  3662. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
  3663. number<Backend, et_off> result;
  3664. using default_ops::eval_conj;
  3665. eval_conj(result.backend(), arg.backend());
  3666. return result;
  3667. } // LCOV_EXCL_LINE
  3668. template <class tag, class A1, class A2, class A3, class A4>
  3669. inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
  3670. detail::function, detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >
  3671. proj(const detail::expression<tag, A1, A2, A3, A4>& arg)
  3672. {
  3673. return detail::expression<
  3674. detail::function, detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
  3675. detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
  3676. }
  3677. template <class Backend>
  3678. inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
  3679. detail::function, detail::proj_funct<Backend>, number<Backend, et_on> >
  3680. proj(const number<Backend, et_on>& arg)
  3681. {
  3682. return detail::expression<
  3683. detail::function, detail::proj_funct<Backend>, number<Backend, et_on> >(
  3684. detail::proj_funct<Backend>(), arg);
  3685. }
  3686. template <class Backend>
  3687. inline BOOST_MP_CXX14_CONSTEXPR number<Backend, et_off>
  3688. proj(const number<Backend, et_off>& arg)
  3689. {
  3690. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
  3691. number<Backend, et_off> result;
  3692. using default_ops::eval_proj;
  3693. eval_proj(result.backend(), arg.backend());
  3694. return result;
  3695. }
  3696. UNARY_OP_FUNCTOR(fabs, number_kind_floating_point)
  3697. UNARY_OP_FUNCTOR(sqrt, number_kind_floating_point)
  3698. UNARY_OP_FUNCTOR(floor, number_kind_floating_point)
  3699. UNARY_OP_FUNCTOR(ceil, number_kind_floating_point)
  3700. UNARY_OP_FUNCTOR(trunc, number_kind_floating_point)
  3701. UNARY_OP_FUNCTOR(round, number_kind_floating_point)
  3702. UNARY_OP_FUNCTOR(exp, number_kind_floating_point)
  3703. UNARY_OP_FUNCTOR(exp2, number_kind_floating_point)
  3704. UNARY_OP_FUNCTOR(log, number_kind_floating_point)
  3705. UNARY_OP_FUNCTOR(log10, number_kind_floating_point)
  3706. UNARY_OP_FUNCTOR(cos, number_kind_floating_point)
  3707. UNARY_OP_FUNCTOR(sin, number_kind_floating_point)
  3708. UNARY_OP_FUNCTOR(tan, number_kind_floating_point)
  3709. UNARY_OP_FUNCTOR(asin, number_kind_floating_point)
  3710. UNARY_OP_FUNCTOR(acos, number_kind_floating_point)
  3711. UNARY_OP_FUNCTOR(atan, number_kind_floating_point)
  3712. UNARY_OP_FUNCTOR(cosh, number_kind_floating_point)
  3713. UNARY_OP_FUNCTOR(sinh, number_kind_floating_point)
  3714. UNARY_OP_FUNCTOR(tanh, number_kind_floating_point)
  3715. UNARY_OP_FUNCTOR(log2, number_kind_floating_point)
  3716. UNARY_OP_FUNCTOR(nearbyint, number_kind_floating_point)
  3717. UNARY_OP_FUNCTOR(rint, number_kind_floating_point)
  3718. HETERO_BINARY_OP_FUNCTOR(ldexp, short, number_kind_floating_point)
  3719. //HETERO_BINARY_OP_FUNCTOR(frexp, short*, number_kind_floating_point)
  3720. HETERO_BINARY_OP_FUNCTOR_B(ldexp, int, number_kind_floating_point)
  3721. //HETERO_BINARY_OP_FUNCTOR_B(frexp, int*, number_kind_floating_point)
  3722. HETERO_BINARY_OP_FUNCTOR_B(ldexp, long, number_kind_floating_point)
  3723. //HETERO_BINARY_OP_FUNCTOR_B(frexp, long*, number_kind_floating_point)
  3724. HETERO_BINARY_OP_FUNCTOR_B(ldexp, long long, number_kind_floating_point)
  3725. //HETERO_BINARY_OP_FUNCTOR_B(frexp, long long*, number_kind_floating_point)
  3726. BINARY_OP_FUNCTOR(pow, number_kind_floating_point)
  3727. BINARY_OP_FUNCTOR(fmod, number_kind_floating_point)
  3728. BINARY_OP_FUNCTOR(fmax, number_kind_floating_point)
  3729. BINARY_OP_FUNCTOR(fmin, number_kind_floating_point)
  3730. BINARY_OP_FUNCTOR(atan2, number_kind_floating_point)
  3731. BINARY_OP_FUNCTOR(fdim, number_kind_floating_point)
  3732. BINARY_OP_FUNCTOR(hypot, number_kind_floating_point)
  3733. BINARY_OP_FUNCTOR(remainder, number_kind_floating_point)
  3734. UNARY_OP_FUNCTOR(logb, number_kind_floating_point)
  3735. HETERO_BINARY_OP_FUNCTOR(scalbn, short, number_kind_floating_point)
  3736. HETERO_BINARY_OP_FUNCTOR(scalbln, short, number_kind_floating_point)
  3737. HETERO_BINARY_OP_FUNCTOR_B(scalbn, int, number_kind_floating_point)
  3738. HETERO_BINARY_OP_FUNCTOR_B(scalbln, int, number_kind_floating_point)
  3739. HETERO_BINARY_OP_FUNCTOR_B(scalbn, long, number_kind_floating_point)
  3740. HETERO_BINARY_OP_FUNCTOR_B(scalbln, long, number_kind_floating_point)
  3741. HETERO_BINARY_OP_FUNCTOR_B(scalbn, long long, number_kind_floating_point)
  3742. HETERO_BINARY_OP_FUNCTOR_B(scalbln, long long, number_kind_floating_point)
  3743. //
  3744. // Complex functions:
  3745. //
  3746. UNARY_OP_FUNCTOR(exp, number_kind_complex)
  3747. UNARY_OP_FUNCTOR(log, number_kind_complex)
  3748. UNARY_OP_FUNCTOR(log10, number_kind_complex)
  3749. BINARY_OP_FUNCTOR(pow, number_kind_complex)
  3750. UNARY_OP_FUNCTOR(sqrt, number_kind_complex)
  3751. UNARY_OP_FUNCTOR(sin, number_kind_complex)
  3752. UNARY_OP_FUNCTOR(cos, number_kind_complex)
  3753. UNARY_OP_FUNCTOR(tan, number_kind_complex)
  3754. UNARY_OP_FUNCTOR(asin, number_kind_complex)
  3755. UNARY_OP_FUNCTOR(acos, number_kind_complex)
  3756. UNARY_OP_FUNCTOR(atan, number_kind_complex)
  3757. UNARY_OP_FUNCTOR(sinh, number_kind_complex)
  3758. UNARY_OP_FUNCTOR(cosh, number_kind_complex)
  3759. UNARY_OP_FUNCTOR(tanh, number_kind_complex)
  3760. UNARY_OP_FUNCTOR(asinh, number_kind_complex)
  3761. UNARY_OP_FUNCTOR(acosh, number_kind_complex)
  3762. UNARY_OP_FUNCTOR(atanh, number_kind_complex)
  3763. //
  3764. // Integer functions:
  3765. //
  3766. BINARY_OP_FUNCTOR(gcd, number_kind_integer)
  3767. BINARY_OP_FUNCTOR(lcm, number_kind_integer)
  3768. HETERO_BINARY_OP_FUNCTOR(pow, unsigned, number_kind_integer)
  3769. #undef BINARY_OP_FUNCTOR
  3770. #undef UNARY_OP_FUNCTOR
  3771. //
  3772. // ilogb:
  3773. //
  3774. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  3775. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_floating_point, typename Backend::exponent_type>::type
  3776. ilogb(const multiprecision::number<Backend, ExpressionTemplates>& val)
  3777. {
  3778. using default_ops::eval_ilogb;
  3779. return eval_ilogb(val.backend());
  3780. }
  3781. template <class tag, class A1, class A2, class A3, class A4>
  3782. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<detail::expression<tag, A1, A2, A3, A4> >::value == number_kind_floating_point, typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type::backend_type::exponent_type>::type
  3783. ilogb(const detail::expression<tag, A1, A2, A3, A4>& val)
  3784. {
  3785. using default_ops::eval_ilogb;
  3786. typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type arg(val);
  3787. return eval_ilogb(arg.backend());
  3788. }
  3789. } //namespace multiprecision
  3790. namespace math {
  3791. //
  3792. // Overload of Boost.Math functions that find the wrong overload when used with number:
  3793. //
  3794. namespace detail {
  3795. template <class T>
  3796. T sinc_pi_imp(T);
  3797. template <class T, class Policy>
  3798. T sinhc_pi_imp(T, const Policy&);
  3799. } // namespace detail
  3800. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  3801. inline multiprecision::number<Backend, ExpressionTemplates> sinc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x)
  3802. {
  3803. boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
  3804. return detail::sinc_pi_imp(x);
  3805. }
  3806. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class Policy>
  3807. inline multiprecision::number<Backend, ExpressionTemplates> sinc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x, const Policy&)
  3808. {
  3809. boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
  3810. return detail::sinc_pi_imp(x);
  3811. }
  3812. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  3813. inline multiprecision::number<Backend, ExpressionTemplates> sinhc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x)
  3814. {
  3815. boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
  3816. return detail::sinhc_pi_imp(x, boost::math::policies::policy<>());
  3817. }
  3818. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class Policy>
  3819. inline multiprecision::number<Backend, ExpressionTemplates> sinhc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x, const Policy& pol)
  3820. {
  3821. boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x, pol);
  3822. return detail::sinhc_pi_imp(x, pol);
  3823. }
  3824. using boost::multiprecision::gcd;
  3825. using boost::multiprecision::lcm;
  3826. #ifdef BOOST_MSVC
  3827. #pragma warning(pop)
  3828. #endif
  3829. } // namespace math
  3830. namespace integer {
  3831. using boost::multiprecision::gcd;
  3832. using boost::multiprecision::lcm;
  3833. } // namespace integer
  3834. } // namespace boost
  3835. //
  3836. // This has to come last of all:
  3837. //
  3838. #include <boost/multiprecision/detail/no_et_ops.hpp>
  3839. #include <boost/multiprecision/detail/et_ops.hpp>
  3840. //
  3841. // min/max overloads:
  3842. //
  3843. #include <boost/multiprecision/detail/min_max.hpp>
  3844. #endif