default_ops.hpp 238 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017
  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. }
  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. }
  1210. switch (eval_fpclassify(b))
  1211. {
  1212. case FP_ZERO:
  1213. case FP_NAN:
  1214. result = std::numeric_limits<number<T> >::quiet_NaN().backend();
  1215. errno = EDOM;
  1216. return;
  1217. }
  1218. T n;
  1219. eval_divide(result, a, b);
  1220. if (eval_get_sign(result) < 0)
  1221. eval_ceil(n, result);
  1222. else
  1223. eval_floor(n, result);
  1224. eval_multiply(n, b);
  1225. eval_subtract(result, a, n);
  1226. }
  1227. template <class T, class A>
  1228. 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)
  1229. {
  1230. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1231. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  1232. cast_type c;
  1233. c = a;
  1234. eval_fmod(result, x, c);
  1235. }
  1236. template <class T, class A>
  1237. 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)
  1238. {
  1239. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1240. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  1241. cast_type c;
  1242. c = x;
  1243. eval_fmod(result, c, a);
  1244. }
  1245. template <class T>
  1246. BOOST_MP_CXX14_CONSTEXPR void eval_round(T& result, const T& a);
  1247. template <class T>
  1248. inline BOOST_MP_CXX14_CONSTEXPR void eval_remquo(T& result, const T& a, const T& b, int* pi)
  1249. {
  1250. static_assert(number_category<T>::value == number_kind_floating_point, "The remquo function is only valid for floating point types.");
  1251. if ((&result == &a) || (&result == &b))
  1252. {
  1253. T temp;
  1254. eval_remquo(temp, a, b, pi);
  1255. result = temp;
  1256. return;
  1257. }
  1258. T n;
  1259. eval_divide(result, a, b);
  1260. eval_round(n, result);
  1261. eval_convert_to(pi, n);
  1262. eval_multiply(n, b);
  1263. eval_subtract(result, a, n);
  1264. if (eval_is_zero(result))
  1265. {
  1266. if (eval_signbit(a))
  1267. result.negate();
  1268. }
  1269. }
  1270. template <class T, class A>
  1271. 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)
  1272. {
  1273. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1274. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  1275. cast_type c = cast_type();
  1276. c = a;
  1277. eval_remquo(result, x, c, pi);
  1278. }
  1279. template <class T, class A>
  1280. 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)
  1281. {
  1282. using canonical_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1283. using cast_type = typename std::conditional<std::is_same<A, canonical_type>::value, T, canonical_type>::type;
  1284. cast_type c = cast_type();
  1285. c = x;
  1286. eval_remquo(result, c, a, pi);
  1287. }
  1288. template <class T, class U, class V>
  1289. inline BOOST_MP_CXX14_CONSTEXPR void eval_remainder(T& result, const U& a, const V& b)
  1290. {
  1291. int i(0);
  1292. eval_remquo(result, a, b, &i);
  1293. }
  1294. template <class B>
  1295. BOOST_MP_CXX14_CONSTEXPR bool eval_gt(const B& a, const B& b);
  1296. template <class T, class U>
  1297. BOOST_MP_CXX14_CONSTEXPR bool eval_gt(const T& a, const U& b);
  1298. template <class B>
  1299. BOOST_MP_CXX14_CONSTEXPR bool eval_lt(const B& a, const B& b);
  1300. template <class T, class U>
  1301. BOOST_MP_CXX14_CONSTEXPR bool eval_lt(const T& a, const U& b);
  1302. template <class T>
  1303. inline BOOST_MP_CXX14_CONSTEXPR void eval_fdim(T& result, const T& a, const T& b)
  1304. {
  1305. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1306. const ui_type zero = 0u;
  1307. switch (eval_fpclassify(b))
  1308. {
  1309. case FP_NAN:
  1310. case FP_INFINITE:
  1311. result = zero;
  1312. return;
  1313. }
  1314. switch (eval_fpclassify(a))
  1315. {
  1316. case FP_NAN:
  1317. result = zero;
  1318. return;
  1319. case FP_INFINITE:
  1320. result = a;
  1321. return;
  1322. }
  1323. if (eval_gt(a, b))
  1324. {
  1325. eval_subtract(result, a, b);
  1326. }
  1327. else
  1328. result = zero;
  1329. }
  1330. template <class T, class A>
  1331. 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)
  1332. {
  1333. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1334. using arithmetic_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1335. const ui_type zero = 0u;
  1336. arithmetic_type canonical_b = b;
  1337. switch (BOOST_MP_FPCLASSIFY(b))
  1338. {
  1339. case FP_NAN:
  1340. case FP_INFINITE:
  1341. result = zero;
  1342. return;
  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. }
  1353. if (eval_gt(a, canonical_b))
  1354. {
  1355. eval_subtract(result, a, canonical_b);
  1356. }
  1357. else
  1358. result = zero;
  1359. }
  1360. template <class T, class A>
  1361. 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)
  1362. {
  1363. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1364. using arithmetic_type = typename boost::multiprecision::detail::canonical<A, T>::type ;
  1365. const ui_type zero = 0u;
  1366. arithmetic_type canonical_a = a;
  1367. switch (eval_fpclassify(b))
  1368. {
  1369. case FP_NAN:
  1370. case FP_INFINITE:
  1371. result = zero;
  1372. return;
  1373. }
  1374. switch (BOOST_MP_FPCLASSIFY(a))
  1375. {
  1376. case FP_NAN:
  1377. result = zero;
  1378. return;
  1379. case FP_INFINITE:
  1380. result = std::numeric_limits<number<T> >::infinity().backend();
  1381. return;
  1382. }
  1383. if (eval_gt(canonical_a, b))
  1384. {
  1385. eval_subtract(result, canonical_a, b);
  1386. }
  1387. else
  1388. result = zero;
  1389. }
  1390. template <class T>
  1391. inline BOOST_MP_CXX14_CONSTEXPR void eval_trunc(T& result, const T& a)
  1392. {
  1393. static_assert(number_category<T>::value == number_kind_floating_point, "The trunc function is only valid for floating point types.");
  1394. switch (eval_fpclassify(a))
  1395. {
  1396. case FP_NAN:
  1397. errno = EDOM;
  1398. // fallthrough...
  1399. case FP_ZERO:
  1400. case FP_INFINITE:
  1401. result = a;
  1402. return;
  1403. }
  1404. if (eval_get_sign(a) < 0)
  1405. eval_ceil(result, a);
  1406. else
  1407. eval_floor(result, a);
  1408. }
  1409. template <class T>
  1410. inline BOOST_MP_CXX14_CONSTEXPR void eval_modf(T& result, T const& arg, T* pipart)
  1411. {
  1412. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1413. int c = eval_fpclassify(arg);
  1414. if (c == static_cast<int>(FP_NAN))
  1415. {
  1416. if (pipart)
  1417. *pipart = arg;
  1418. result = arg;
  1419. return;
  1420. }
  1421. else if (c == static_cast<int>(FP_INFINITE))
  1422. {
  1423. if (pipart)
  1424. *pipart = arg;
  1425. result = ui_type(0u);
  1426. return;
  1427. }
  1428. if (pipart)
  1429. {
  1430. eval_trunc(*pipart, arg);
  1431. eval_subtract(result, arg, *pipart);
  1432. }
  1433. else
  1434. {
  1435. T ipart;
  1436. eval_trunc(ipart, arg);
  1437. eval_subtract(result, arg, ipart);
  1438. }
  1439. }
  1440. template <class T>
  1441. inline BOOST_MP_CXX14_CONSTEXPR void eval_round(T& result, const T& a)
  1442. {
  1443. static_assert(number_category<T>::value == number_kind_floating_point, "The round function is only valid for floating point types.");
  1444. using fp_type = typename boost::multiprecision::detail::canonical<float, T>::type;
  1445. int c = eval_fpclassify(a);
  1446. if (c == static_cast<int>(FP_NAN))
  1447. {
  1448. result = a;
  1449. errno = EDOM;
  1450. return;
  1451. }
  1452. if ((c == FP_ZERO) || (c == static_cast<int>(FP_INFINITE)))
  1453. {
  1454. result = a;
  1455. }
  1456. else if (eval_get_sign(a) < 0)
  1457. {
  1458. eval_subtract(result, a, fp_type(0.5f));
  1459. eval_ceil(result, result);
  1460. }
  1461. else
  1462. {
  1463. eval_add(result, a, fp_type(0.5f));
  1464. eval_floor(result, result);
  1465. }
  1466. }
  1467. template <class B>
  1468. BOOST_MP_CXX14_CONSTEXPR void eval_lcm(B& result, const B& a, const B& b);
  1469. template <class B>
  1470. BOOST_MP_CXX14_CONSTEXPR void eval_gcd(B& result, const B& a, const B& b);
  1471. template <class T, class Arithmetic>
  1472. 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)
  1473. {
  1474. using si_type = typename boost::multiprecision::detail::canonical<Arithmetic, T>::type;
  1475. using default_ops::eval_gcd;
  1476. T t;
  1477. t = static_cast<si_type>(b);
  1478. eval_gcd(result, a, t);
  1479. }
  1480. template <class T, class Arithmetic>
  1481. 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)
  1482. {
  1483. eval_gcd(result, b, a);
  1484. }
  1485. template <class T, class Arithmetic>
  1486. 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)
  1487. {
  1488. using si_type = typename boost::multiprecision::detail::canonical<Arithmetic, T>::type;
  1489. using default_ops::eval_lcm;
  1490. T t;
  1491. t = static_cast<si_type>(b);
  1492. eval_lcm(result, a, t);
  1493. }
  1494. template <class T, class Arithmetic>
  1495. 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)
  1496. {
  1497. eval_lcm(result, b, a);
  1498. }
  1499. template <class T>
  1500. inline BOOST_MP_CXX14_CONSTEXPR std::size_t eval_lsb(const T& val)
  1501. {
  1502. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1503. int c = eval_get_sign(val);
  1504. if (c == 0)
  1505. {
  1506. BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
  1507. }
  1508. if (c < 0)
  1509. {
  1510. BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
  1511. }
  1512. std::size_t result = 0;
  1513. T mask, t;
  1514. mask = ui_type(1);
  1515. do
  1516. {
  1517. eval_bitwise_and(t, mask, val);
  1518. ++result;
  1519. eval_left_shift(mask, 1);
  1520. } while (eval_is_zero(t));
  1521. return --result;
  1522. }
  1523. template <class T>
  1524. inline BOOST_MP_CXX14_CONSTEXPR std::ptrdiff_t eval_msb(const T& val)
  1525. {
  1526. int c = eval_get_sign(val);
  1527. if (c == 0)
  1528. {
  1529. BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
  1530. }
  1531. if (c < 0)
  1532. {
  1533. BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
  1534. }
  1535. //
  1536. // This implementation is really really rubbish - it does
  1537. // a linear scan for the most-significant-bit. We should really
  1538. // do a binary search, but as none of our backends actually needs
  1539. // this implementation, we'll leave it for now. In fact for most
  1540. // backends it's likely that there will always be a more efficient
  1541. // native implementation possible.
  1542. //
  1543. std::size_t result = 0;
  1544. T t(val);
  1545. while (!eval_is_zero(t))
  1546. {
  1547. eval_right_shift(t, 1);
  1548. ++result;
  1549. }
  1550. --result;
  1551. return static_cast<std::ptrdiff_t>(result);
  1552. }
  1553. template <class T>
  1554. inline BOOST_MP_CXX14_CONSTEXPR bool eval_bit_test(const T& val, std::size_t index)
  1555. {
  1556. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1557. T mask, t;
  1558. mask = ui_type(1);
  1559. eval_left_shift(mask, index);
  1560. eval_bitwise_and(t, mask, val);
  1561. return !eval_is_zero(t);
  1562. }
  1563. template <class T>
  1564. inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_set(T& val, std::size_t index)
  1565. {
  1566. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1567. T mask;
  1568. mask = ui_type(1);
  1569. eval_left_shift(mask, index);
  1570. eval_bitwise_or(val, mask);
  1571. }
  1572. template <class T>
  1573. inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_flip(T& val, std::size_t index)
  1574. {
  1575. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1576. T mask;
  1577. mask = ui_type(1);
  1578. eval_left_shift(mask, index);
  1579. eval_bitwise_xor(val, mask);
  1580. }
  1581. template <class T>
  1582. inline BOOST_MP_CXX14_CONSTEXPR void eval_bit_unset(T& val, std::size_t index)
  1583. {
  1584. using ui_type = typename boost::multiprecision::detail::canonical<unsigned, T>::type;
  1585. T mask, t;
  1586. mask = ui_type(1);
  1587. eval_left_shift(mask, index);
  1588. eval_bitwise_and(t, mask, val);
  1589. if (!eval_is_zero(t))
  1590. eval_bitwise_xor(val, mask);
  1591. }
  1592. template <class Backend>
  1593. BOOST_MP_CXX14_CONSTEXPR void eval_qr(const Backend& x, const Backend& y, Backend& q, Backend& r);
  1594. template <class Backend>
  1595. BOOST_MP_CXX14_CONSTEXPR void eval_karatsuba_sqrt(Backend& result, const Backend& x, Backend& r, Backend& t, size_t bits)
  1596. {
  1597. using default_ops::eval_is_zero;
  1598. using default_ops::eval_subtract;
  1599. using default_ops::eval_right_shift;
  1600. using default_ops::eval_left_shift;
  1601. using default_ops::eval_bit_set;
  1602. using default_ops::eval_decrement;
  1603. using default_ops::eval_bitwise_and;
  1604. using default_ops::eval_add;
  1605. using default_ops::eval_qr;
  1606. using small_uint = typename std::tuple_element<0, typename Backend::unsigned_types>::type;
  1607. constexpr small_uint zero = 0u;
  1608. // we can calculate it faster with std::sqrt
  1609. #ifdef BOOST_HAS_INT128
  1610. if (bits <= 128)
  1611. {
  1612. uint128_type a{}, b{}, c{};
  1613. eval_convert_to(&a, x);
  1614. c = boost::multiprecision::detail::karatsuba_sqrt(a, b, bits);
  1615. r = number<Backend>::canonical_value(b);
  1616. result = number<Backend>::canonical_value(c);
  1617. return;
  1618. }
  1619. #else
  1620. if (bits <= std::numeric_limits<std::uintmax_t>::digits)
  1621. {
  1622. std::uintmax_t a{ 0 }, b{ 0 }, c{ 0 };
  1623. eval_convert_to(&a, x);
  1624. c = boost::multiprecision::detail::karatsuba_sqrt(a, b, bits);
  1625. r = number<Backend>::canonical_value(b);
  1626. result = number<Backend>::canonical_value(c);
  1627. return;
  1628. }
  1629. #endif
  1630. // https://hal.inria.fr/file/index/docid/72854/filename/RR-3805.pdf
  1631. std::size_t b = bits / 4;
  1632. Backend q(x);
  1633. eval_right_shift(q, b * 2);
  1634. Backend s;
  1635. eval_karatsuba_sqrt(s, q, r, t, bits - b * 2);
  1636. t = zero;
  1637. eval_bit_set(t, static_cast<unsigned>(b * 2));
  1638. eval_left_shift(r, b);
  1639. eval_decrement(t);
  1640. eval_bitwise_and(t, x);
  1641. eval_right_shift(t, b);
  1642. eval_add(t, r);
  1643. eval_left_shift(s, 1u);
  1644. eval_qr(t, s, q, r);
  1645. eval_left_shift(r, b);
  1646. t = zero;
  1647. eval_bit_set(t, static_cast<unsigned>(b));
  1648. eval_decrement(t);
  1649. eval_bitwise_and(t, x);
  1650. eval_add(r, t);
  1651. eval_left_shift(s, b - 1);
  1652. eval_add(s, q);
  1653. eval_multiply(q, q);
  1654. // we substract after, so it works for unsigned integers too
  1655. if (r.compare(q) < 0)
  1656. {
  1657. t = s;
  1658. eval_left_shift(t, 1u);
  1659. eval_decrement(t);
  1660. eval_add(r, t);
  1661. eval_decrement(s);
  1662. }
  1663. eval_subtract(r, q);
  1664. result = s;
  1665. }
  1666. template <class B>
  1667. void BOOST_MP_CXX14_CONSTEXPR eval_integer_sqrt_bitwise(B& s, B& r, const B& x)
  1668. {
  1669. //
  1670. // This is slow bit-by-bit integer square root, see for example
  1671. // http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_.28base_2.29
  1672. // There are better methods such as http://hal.inria.fr/docs/00/07/28/54/PDF/RR-3805.pdf
  1673. // and http://hal.inria.fr/docs/00/07/21/13/PDF/RR-4475.pdf which should be implemented
  1674. // at some point.
  1675. //
  1676. using ui_type = typename boost::multiprecision::detail::canonical<unsigned char, B>::type;
  1677. s = ui_type(0u);
  1678. if (eval_get_sign(x) == 0)
  1679. {
  1680. r = ui_type(0u);
  1681. return;
  1682. }
  1683. std::ptrdiff_t g = static_cast<std::ptrdiff_t>(eval_msb(x));
  1684. if (g <= 1)
  1685. {
  1686. s = ui_type(1);
  1687. eval_subtract(r, x, s);
  1688. return;
  1689. }
  1690. B t;
  1691. r = x;
  1692. g /= 2;
  1693. std::ptrdiff_t org_g = g;
  1694. eval_bit_set(s, static_cast<std::size_t>(g));
  1695. eval_bit_set(t, static_cast<std::size_t>(2 * g));
  1696. eval_subtract(r, x, t);
  1697. --g;
  1698. if (eval_get_sign(r) == 0)
  1699. return;
  1700. std::ptrdiff_t msbr = static_cast<std::ptrdiff_t>(eval_msb(r));
  1701. do
  1702. {
  1703. if (msbr >= org_g + g + 1)
  1704. {
  1705. t = s;
  1706. eval_left_shift(t, static_cast<std::size_t>(g + 1));
  1707. eval_bit_set(t, static_cast<std::size_t>(2 * g));
  1708. if (t.compare(r) <= 0)
  1709. {
  1710. BOOST_MP_ASSERT(g >= 0);
  1711. eval_bit_set(s, static_cast<std::size_t>(g));
  1712. eval_subtract(r, t);
  1713. if (eval_get_sign(r) == 0)
  1714. return;
  1715. msbr = static_cast<std::ptrdiff_t>(eval_msb(r));
  1716. }
  1717. }
  1718. --g;
  1719. } while (g >= 0);
  1720. }
  1721. template <class Backend>
  1722. BOOST_MP_CXX14_CONSTEXPR void eval_integer_sqrt(Backend& result, Backend& r, const Backend& x)
  1723. {
  1724. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  1725. // recursive Karatsuba sqrt can cause issues in constexpr context:
  1726. if (BOOST_MP_IS_CONST_EVALUATED(result.size()))
  1727. return eval_integer_sqrt_bitwise(result, r, x);
  1728. #endif
  1729. using small_uint = typename std::tuple_element<0, typename Backend::unsigned_types>::type;
  1730. constexpr small_uint zero = 0u;
  1731. if (eval_is_zero(x))
  1732. {
  1733. r = zero;
  1734. result = zero;
  1735. return;
  1736. }
  1737. Backend t;
  1738. eval_karatsuba_sqrt(result, x, r, t, eval_msb(x) + 1);
  1739. }
  1740. template <class B>
  1741. inline BOOST_MP_CXX14_CONSTEXPR void eval_conj(B& result, const B& val)
  1742. {
  1743. result = val; // assume non-complex result.
  1744. }
  1745. template <class B>
  1746. inline BOOST_MP_CXX14_CONSTEXPR void eval_proj(B& result, const B& val)
  1747. {
  1748. result = val; // assume non-complex result.
  1749. }
  1750. //
  1751. // These have to implemented by the backend, declared here so that our macro generated code compiles OK.
  1752. //
  1753. template <class T>
  1754. typename std::enable_if<sizeof(T) == 0>::type eval_floor();
  1755. template <class T>
  1756. typename std::enable_if<sizeof(T) == 0>::type eval_ceil();
  1757. template <class T>
  1758. typename std::enable_if<sizeof(T) == 0>::type eval_trunc();
  1759. template <class T>
  1760. typename std::enable_if<sizeof(T) == 0>::type eval_sqrt();
  1761. template <class T>
  1762. typename std::enable_if<sizeof(T) == 0>::type eval_ldexp();
  1763. template <class T>
  1764. typename std::enable_if<sizeof(T) == 0>::type eval_frexp();
  1765. // TODO implement default versions of these:
  1766. template <class T>
  1767. typename std::enable_if<sizeof(T) == 0>::type eval_asinh();
  1768. template <class T>
  1769. typename std::enable_if<sizeof(T) == 0>::type eval_acosh();
  1770. template <class T>
  1771. typename std::enable_if<sizeof(T) == 0>::type eval_atanh();
  1772. //
  1773. // eval_logb and eval_scalbn simply assume base 2 and forward to
  1774. // eval_ldexp and eval_frexp:
  1775. //
  1776. template <class B>
  1777. inline BOOST_MP_CXX14_CONSTEXPR typename B::exponent_type eval_ilogb(const B& val)
  1778. {
  1779. 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");
  1780. typename B::exponent_type e(0);
  1781. switch (eval_fpclassify(val))
  1782. {
  1783. case FP_NAN:
  1784. #ifdef FP_ILOGBNAN
  1785. return FP_ILOGBNAN > 0 ? (std::numeric_limits<typename B::exponent_type>::max)() : (std::numeric_limits<typename B::exponent_type>::min)();
  1786. #else
  1787. return (std::numeric_limits<typename B::exponent_type>::max)();
  1788. #endif
  1789. case FP_INFINITE:
  1790. return (std::numeric_limits<typename B::exponent_type>::max)();
  1791. case FP_ZERO:
  1792. return (std::numeric_limits<typename B::exponent_type>::min)();
  1793. }
  1794. B result;
  1795. eval_frexp(result, val, &e);
  1796. return e - 1;
  1797. }
  1798. template <class B>
  1799. inline BOOST_MP_CXX14_CONSTEXPR void eval_logb(B& result, const B& val)
  1800. {
  1801. switch (eval_fpclassify(val))
  1802. {
  1803. case FP_NAN:
  1804. result = val;
  1805. errno = EDOM;
  1806. return;
  1807. case FP_ZERO:
  1808. result = std::numeric_limits<number<B> >::infinity().backend();
  1809. result.negate();
  1810. errno = ERANGE;
  1811. return;
  1812. case FP_INFINITE:
  1813. result = val;
  1814. if (eval_signbit(val))
  1815. result.negate();
  1816. return;
  1817. }
  1818. using max_t = typename std::conditional<std::is_same<std::intmax_t, long>::value, long long, std::intmax_t>::type;
  1819. result = static_cast<max_t>(eval_ilogb(val));
  1820. }
  1821. template <class B, class A>
  1822. inline BOOST_MP_CXX14_CONSTEXPR void eval_scalbn(B& result, const B& val, A e)
  1823. {
  1824. 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");
  1825. eval_ldexp(result, val, static_cast<typename B::exponent_type>(e));
  1826. }
  1827. template <class B, class A>
  1828. inline BOOST_MP_CXX14_CONSTEXPR void eval_scalbln(B& result, const B& val, A e)
  1829. {
  1830. eval_scalbn(result, val, e);
  1831. }
  1832. template <class T>
  1833. inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val, std::integral_constant<bool, true> const&, const std::integral_constant<bool, false>&)
  1834. {
  1835. return eval_fpclassify(val) == FP_NAN;
  1836. }
  1837. template <class T>
  1838. inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val, std::integral_constant<bool, false> const&, const std::integral_constant<bool, true>&)
  1839. {
  1840. return BOOST_MP_ISNAN(val);
  1841. }
  1842. template <class T>
  1843. inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T&, std::integral_constant<bool, false> const&, const std::integral_constant<bool, false>&)
  1844. {
  1845. return false;
  1846. }
  1847. template <class T>
  1848. inline BOOST_MP_CXX14_CONSTEXPR bool is_arg_nan(const T& val)
  1849. {
  1850. return is_arg_nan(val, std::integral_constant<bool, boost::multiprecision::detail::is_backend<T>::value>(), std::is_floating_point<T>());
  1851. }
  1852. template <class T, class U, class V>
  1853. inline BOOST_MP_CXX14_CONSTEXPR void eval_fmax(T& result, const U& a, const V& b)
  1854. {
  1855. if (is_arg_nan(a))
  1856. result = number<T>::canonical_value(b);
  1857. else if (is_arg_nan(b))
  1858. result = number<T>::canonical_value(a);
  1859. else if (eval_lt(number<T>::canonical_value(a), number<T>::canonical_value(b)))
  1860. result = number<T>::canonical_value(b);
  1861. else
  1862. result = number<T>::canonical_value(a);
  1863. }
  1864. template <class T, class U, class V>
  1865. inline BOOST_MP_CXX14_CONSTEXPR void eval_fmin(T& result, const U& a, const V& b)
  1866. {
  1867. if (is_arg_nan(a))
  1868. result = number<T>::canonical_value(b);
  1869. else if (is_arg_nan(b))
  1870. result = number<T>::canonical_value(a);
  1871. else if (eval_lt(number<T>::canonical_value(a), number<T>::canonical_value(b)))
  1872. result = number<T>::canonical_value(a);
  1873. else
  1874. result = number<T>::canonical_value(b);
  1875. }
  1876. template <class R, class T, class U>
  1877. inline BOOST_MP_CXX14_CONSTEXPR void eval_hypot(R& result, const T& a, const U& b)
  1878. {
  1879. //
  1880. // Normalize x and y, so that both are positive and x >= y:
  1881. //
  1882. R x, y;
  1883. x = number<R>::canonical_value(a);
  1884. y = number<R>::canonical_value(b);
  1885. if (eval_get_sign(x) < 0)
  1886. x.negate();
  1887. if (eval_get_sign(y) < 0)
  1888. y.negate();
  1889. // Special case, see C99 Annex F.
  1890. // The order of the if's is important: do not change!
  1891. int c1 = eval_fpclassify(x);
  1892. int c2 = eval_fpclassify(y);
  1893. if (c1 == FP_ZERO)
  1894. {
  1895. result = y;
  1896. return;
  1897. }
  1898. if (c2 == FP_ZERO)
  1899. {
  1900. result = x;
  1901. return;
  1902. }
  1903. if (c1 == FP_INFINITE)
  1904. {
  1905. result = x;
  1906. return;
  1907. }
  1908. if ((c2 == FP_INFINITE) || (c2 == FP_NAN))
  1909. {
  1910. result = y;
  1911. return;
  1912. }
  1913. if (c1 == FP_NAN)
  1914. {
  1915. result = x;
  1916. return;
  1917. }
  1918. if (eval_gt(y, x))
  1919. x.swap(y);
  1920. eval_multiply(result, x, std::numeric_limits<number<R> >::epsilon().backend());
  1921. if (eval_gt(result, y))
  1922. {
  1923. result = x;
  1924. return;
  1925. }
  1926. R rat;
  1927. eval_divide(rat, y, x);
  1928. eval_multiply(result, rat, rat);
  1929. eval_increment(result);
  1930. eval_sqrt(rat, result);
  1931. eval_multiply(result, rat, x);
  1932. }
  1933. template <class R, class T>
  1934. inline BOOST_MP_CXX14_CONSTEXPR void eval_nearbyint(R& result, const T& a)
  1935. {
  1936. eval_round(result, a);
  1937. }
  1938. template <class R, class T>
  1939. inline BOOST_MP_CXX14_CONSTEXPR void eval_rint(R& result, const T& a)
  1940. {
  1941. eval_nearbyint(result, a);
  1942. }
  1943. template <class T>
  1944. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_backend<T>::value, int>::type eval_signbit(const T& val)
  1945. {
  1946. return eval_get_sign(val) < 0 ? 1 : 0;
  1947. }
  1948. //
  1949. // Real and imaginary parts:
  1950. //
  1951. template <class To, class From>
  1952. inline BOOST_MP_CXX14_CONSTEXPR void eval_real(To& to, const From& from)
  1953. {
  1954. to = from;
  1955. }
  1956. template <class To, class From>
  1957. inline BOOST_MP_CXX14_CONSTEXPR void eval_imag(To& to, const From&)
  1958. {
  1959. using ui_type = typename std::tuple_element<0, typename To::unsigned_types>::type;
  1960. to = ui_type(0);
  1961. }
  1962. } // namespace default_ops
  1963. namespace default_ops_adl {
  1964. template <class To, class From>
  1965. inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real_imp(To& to, const From& from)
  1966. {
  1967. using to_component_type = typename component_type<number<To> >::type;
  1968. typename to_component_type::backend_type to_component;
  1969. to_component = from;
  1970. eval_set_real(to, to_component);
  1971. }
  1972. template <class To, class From>
  1973. inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag_imp(To& to, const From& from)
  1974. {
  1975. using to_component_type = typename component_type<number<To> >::type;
  1976. typename to_component_type::backend_type to_component;
  1977. to_component = from;
  1978. eval_set_imag(to, to_component);
  1979. }
  1980. } // namespace default_ops_adl
  1981. namespace default_ops {
  1982. template <class To, class From>
  1983. 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)
  1984. {
  1985. default_ops_adl::eval_set_real_imp(to, from);
  1986. }
  1987. template <class To, class From>
  1988. 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)
  1989. {
  1990. to = from;
  1991. }
  1992. template <class To, class From>
  1993. inline BOOST_MP_CXX14_CONSTEXPR void eval_set_imag(To& to, const From& from)
  1994. {
  1995. default_ops_adl::eval_set_imag_imp(to, from);
  1996. }
  1997. template <class T>
  1998. inline BOOST_MP_CXX14_CONSTEXPR void eval_set_real(T& to, const T& from)
  1999. {
  2000. to = from;
  2001. }
  2002. template <class T>
  2003. void BOOST_MP_CXX14_CONSTEXPR eval_set_imag(T&, const T&)
  2004. {
  2005. static_assert(sizeof(T) == INT_MAX, "eval_set_imag needs to be specialised for each specific backend");
  2006. }
  2007. //
  2008. // These functions are implemented in separate files, but expanded inline here,
  2009. // DO NOT CHANGE THE ORDER OF THESE INCLUDES:
  2010. //
  2011. #include <boost/multiprecision/detail/functions/constants.hpp>
  2012. #include <boost/multiprecision/detail/functions/pow.hpp>
  2013. #include <boost/multiprecision/detail/functions/trig.hpp>
  2014. } // namespace default_ops
  2015. //
  2016. // Default versions of floating point classification routines:
  2017. //
  2018. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2019. inline BOOST_MP_CXX14_CONSTEXPR int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2020. {
  2021. using multiprecision::default_ops::eval_fpclassify;
  2022. return eval_fpclassify(arg.backend());
  2023. }
  2024. template <class tag, class A1, class A2, class A3, class A4>
  2025. inline BOOST_MP_CXX14_CONSTEXPR int fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2026. {
  2027. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2028. return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2029. }
  2030. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2031. inline BOOST_MP_CXX14_CONSTEXPR bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2032. {
  2033. int v = fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg);
  2034. return (v != static_cast<int>(FP_INFINITE)) && (v != static_cast<int>(FP_NAN));
  2035. }
  2036. template <class tag, class A1, class A2, class A3, class A4>
  2037. inline BOOST_MP_CXX14_CONSTEXPR bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2038. {
  2039. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2040. return isfinite BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2041. }
  2042. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2043. inline BOOST_MP_CXX14_CONSTEXPR bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2044. {
  2045. return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_NAN);
  2046. }
  2047. template <class tag, class A1, class A2, class A3, class A4>
  2048. inline BOOST_MP_CXX14_CONSTEXPR bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2049. {
  2050. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2051. return isnan BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2052. }
  2053. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2054. inline BOOST_MP_CXX14_CONSTEXPR bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2055. {
  2056. return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_INFINITE);
  2057. }
  2058. template <class tag, class A1, class A2, class A3, class A4>
  2059. inline BOOST_MP_CXX14_CONSTEXPR bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2060. {
  2061. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2062. return isinf BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2063. }
  2064. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2065. inline BOOST_MP_CXX14_CONSTEXPR bool isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2066. {
  2067. return fpclassify BOOST_PREVENT_MACRO_SUBSTITUTION(arg) == static_cast<int>(FP_NORMAL);
  2068. }
  2069. template <class tag, class A1, class A2, class A3, class A4>
  2070. inline BOOST_MP_CXX14_CONSTEXPR bool isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2071. {
  2072. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2073. return isnormal BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2074. }
  2075. // Default versions of sign manipulation functions, if individual backends can do better than this
  2076. // (for example with signed zero), then they should overload these functions further:
  2077. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2078. inline BOOST_MP_CXX14_CONSTEXPR int sign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2079. {
  2080. return arg.sign();
  2081. }
  2082. template <class tag, class A1, class A2, class A3, class A4>
  2083. inline BOOST_MP_CXX14_CONSTEXPR int sign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2084. {
  2085. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2086. return sign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2087. }
  2088. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2089. inline BOOST_MP_CXX14_CONSTEXPR bool signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2090. {
  2091. using default_ops::eval_signbit;
  2092. return static_cast<bool>(eval_signbit(arg.backend()));
  2093. }
  2094. template <class tag, class A1, class A2, class A3, class A4>
  2095. inline BOOST_MP_CXX14_CONSTEXPR bool signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2096. {
  2097. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2098. return static_cast<bool>(signbit BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg)));
  2099. }
  2100. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2101. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> changesign BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2102. {
  2103. return -arg;
  2104. }
  2105. template <class tag, class A1, class A2, class A3, class A4>
  2106. 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)
  2107. {
  2108. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2109. return changesign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(arg));
  2110. }
  2111. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2112. 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)
  2113. {
  2114. return (boost::multiprecision::signbit)(a) != (boost::multiprecision::signbit)(b) ? (boost::multiprecision::changesign)(a) : a;
  2115. }
  2116. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2117. 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)
  2118. {
  2119. return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
  2120. }
  2121. template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2122. 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)
  2123. {
  2124. return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
  2125. }
  2126. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  2127. 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)
  2128. {
  2129. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2130. return copysign BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
  2131. }
  2132. //
  2133. // real and imag:
  2134. //
  2135. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2136. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type
  2137. real(const multiprecision::number<Backend, ExpressionTemplates>& a)
  2138. {
  2139. using default_ops::eval_real;
  2140. using result_type = typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type;
  2141. boost::multiprecision::detail::scoped_default_precision<result_type> precision_guard(a);
  2142. result_type result;
  2143. eval_real(result.backend(), a.backend());
  2144. return result;
  2145. }
  2146. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2147. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type
  2148. imag(const multiprecision::number<Backend, ExpressionTemplates>& a)
  2149. {
  2150. using default_ops::eval_imag;
  2151. using result_type = typename scalar_result_from_possible_complex<multiprecision::number<Backend, ExpressionTemplates> >::type;
  2152. boost::multiprecision::detail::scoped_default_precision<result_type> precision_guard(a);
  2153. result_type result;
  2154. eval_imag(result.backend(), a.backend());
  2155. return result;
  2156. }
  2157. template <class tag, class A1, class A2, class A3, class A4>
  2158. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2159. real(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2160. {
  2161. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2162. detail::scoped_default_precision<value_type> precision_guard(arg);
  2163. return real(value_type(arg));
  2164. }
  2165. template <class tag, class A1, class A2, class A3, class A4>
  2166. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2167. imag(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2168. {
  2169. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2170. detail::scoped_default_precision<value_type> precision_guard(arg);
  2171. return imag(value_type(arg));
  2172. }
  2173. //
  2174. // Complex number functions, these are overloaded at the Backend level, we just provide the
  2175. // expression template versions here, plus overloads for non-complex types:
  2176. //
  2177. #ifdef BOOST_MP_MATH_AVAILABLE
  2178. template <class T, expression_template_option ExpressionTemplates>
  2179. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_complex, component_type<number<T, ExpressionTemplates>>>::type::type
  2180. abs(const number<T, ExpressionTemplates>& v)
  2181. {
  2182. return std::move(boost::math::hypot(real(v), imag(v)));
  2183. }
  2184. template <class tag, class A1, class A2, class A3, class A4>
  2185. 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
  2186. abs(const detail::expression<tag, A1, A2, A3, A4>& v)
  2187. {
  2188. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2189. return std::move(abs(static_cast<number_type>(v)));
  2190. }
  2191. template <class T, expression_template_option ExpressionTemplates>
  2192. 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
  2193. arg(const number<T, ExpressionTemplates>& v)
  2194. {
  2195. return std::move(atan2(imag(v), real(v)));
  2196. }
  2197. template <class T, expression_template_option ExpressionTemplates>
  2198. 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
  2199. arg(const number<T, ExpressionTemplates>&)
  2200. {
  2201. return 0;
  2202. }
  2203. template <class tag, class A1, class A2, class A3, class A4>
  2204. 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
  2205. arg(const detail::expression<tag, A1, A2, A3, A4>& v)
  2206. {
  2207. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2208. return std::move(arg(static_cast<number_type>(v)));
  2209. }
  2210. #endif // BOOST_MP_MATH_AVAILABLE
  2211. template <class T, expression_template_option ExpressionTemplates>
  2212. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<T>::value == number_kind_complex, component_type<number<T, ExpressionTemplates>>>::type::type
  2213. norm(const number<T, ExpressionTemplates>& v)
  2214. {
  2215. typename component_type<number<T, ExpressionTemplates> >::type a(real(v)), b(imag(v));
  2216. return std::move(a * a + b * b);
  2217. }
  2218. template <class T, expression_template_option ExpressionTemplates>
  2219. 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
  2220. norm(const number<T, ExpressionTemplates>& v)
  2221. {
  2222. return v * v;
  2223. }
  2224. template <class tag, class A1, class A2, class A3, class A4>
  2225. inline BOOST_MP_CXX14_CONSTEXPR typename scalar_result_from_possible_complex<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2226. norm(const detail::expression<tag, A1, A2, A3, A4>& v)
  2227. {
  2228. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2229. return std::move(norm(static_cast<number_type>(v)));
  2230. }
  2231. template <class Backend, expression_template_option ExpressionTemplates>
  2232. BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type polar(number<Backend, ExpressionTemplates> const& r, number<Backend, ExpressionTemplates> const& theta)
  2233. {
  2234. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
  2235. }
  2236. template <class tag, class A1, class A2, class A3, class A4, class Backend, expression_template_option ExpressionTemplates>
  2237. 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,
  2238. typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type>::type
  2239. polar(detail::expression<tag, A1, A2, A3, A4> const& r, number<Backend, ExpressionTemplates> const& theta)
  2240. {
  2241. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
  2242. }
  2243. template <class Backend, expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2244. 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,
  2245. typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type>::type
  2246. polar(number<Backend, ExpressionTemplates> const& r, detail::expression<tag, A1, A2, A3, A4> const& theta)
  2247. {
  2248. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
  2249. }
  2250. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  2251. 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,
  2252. typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type>::type
  2253. polar(detail::expression<tag, A1, A2, A3, A4> const& r, detail::expression<tagb, A1b, A2b, A3b, A4b> const& theta)
  2254. {
  2255. using scalar_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2256. return typename complex_result_from_scalar<scalar_type>::type(scalar_type(r * cos(theta)), scalar_type(r * sin(theta)));
  2257. }
  2258. //
  2259. // We also allow the first argument to polar to be an arithmetic type (probably a literal):
  2260. //
  2261. template <class Scalar, class Backend, expression_template_option ExpressionTemplates>
  2262. 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
  2263. polar(Scalar const& r, number<Backend, ExpressionTemplates> const& theta)
  2264. {
  2265. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(number<Backend, ExpressionTemplates>(r * cos(theta)), number<Backend, ExpressionTemplates>(r * sin(theta)));
  2266. }
  2267. template <class tag, class A1, class A2, class A3, class A4, class Scalar>
  2268. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_arithmetic<Scalar>::value,
  2269. typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type>::type
  2270. polar(Scalar const& r, detail::expression<tag, A1, A2, A3, A4> const& theta)
  2271. {
  2272. using scalar_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2273. return typename complex_result_from_scalar<scalar_type>::type(scalar_type(r * cos(theta)), scalar_type(r * sin(theta)));
  2274. }
  2275. //
  2276. // Single argument overloads:
  2277. //
  2278. template <class Backend, expression_template_option ExpressionTemplates>
  2279. BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type polar(number<Backend, ExpressionTemplates> const& r)
  2280. {
  2281. return typename complex_result_from_scalar<number<Backend, ExpressionTemplates> >::type(r);
  2282. }
  2283. template <class tag, class A1, class A2, class A3, class A4>
  2284. BOOST_MP_CXX14_CONSTEXPR typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type
  2285. polar(detail::expression<tag, A1, A2, A3, A4> const& r)
  2286. {
  2287. return typename complex_result_from_scalar<typename detail::expression<tag, A1, A2, A3, A4>::result_type>::type(r);
  2288. }
  2289. } // namespace multiprecision
  2290. namespace math {
  2291. //
  2292. // Import Math functions here, so they can be found by Boost.Math:
  2293. //
  2294. using boost::multiprecision::changesign;
  2295. using boost::multiprecision::copysign;
  2296. using boost::multiprecision::fpclassify;
  2297. using boost::multiprecision::isfinite;
  2298. using boost::multiprecision::isinf;
  2299. using boost::multiprecision::isnan;
  2300. using boost::multiprecision::isnormal;
  2301. using boost::multiprecision::sign;
  2302. using boost::multiprecision::signbit;
  2303. #ifndef BOOST_MP_MATH_AVAILABLE
  2304. namespace policies {
  2305. template <typename... Args>
  2306. class policy {};
  2307. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  2308. void raise_rounding_error(T1, T2, T3, T4, T5)
  2309. {
  2310. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Rounding error"));
  2311. }
  2312. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  2313. void raise_overflow_error(T1, T2, T3, T4, T5)
  2314. {
  2315. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Overflow error"));
  2316. }
  2317. template <typename T1, typename T2, typename T3, typename T4, typename T5>
  2318. void raise_evaluation_error(T1, T2, T3, T4, T5)
  2319. {
  2320. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Evaluation error"));
  2321. }
  2322. template <typename T, typename... Args>
  2323. struct is_policy
  2324. {
  2325. static constexpr bool value = false;
  2326. };
  2327. template <typename... Args>
  2328. struct is_policy<policy<Args...>>
  2329. {
  2330. static constexpr bool value = true;
  2331. };
  2332. } // namespace policies
  2333. #endif
  2334. } // namespace math
  2335. namespace multiprecision {
  2336. #ifdef BOOST_MP_MATH_AVAILABLE
  2337. using c99_error_policy = ::boost::math::policies::policy<
  2338. ::boost::math::policies::domain_error< ::boost::math::policies::errno_on_error>,
  2339. ::boost::math::policies::pole_error< ::boost::math::policies::errno_on_error>,
  2340. ::boost::math::policies::overflow_error< ::boost::math::policies::errno_on_error>,
  2341. ::boost::math::policies::evaluation_error< ::boost::math::policies::errno_on_error>,
  2342. ::boost::math::policies::rounding_error< ::boost::math::policies::errno_on_error> >;
  2343. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2344. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
  2345. asinh
  2346. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2347. {
  2348. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2349. return boost::math::asinh(arg, c99_error_policy());
  2350. }
  2351. template <class tag, class A1, class A2, class A3, class A4>
  2352. 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
  2353. asinh
  2354. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2355. {
  2356. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2357. detail::scoped_default_precision<value_type> precision_guard(arg);
  2358. return asinh(value_type(arg));
  2359. }
  2360. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2361. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
  2362. acosh
  2363. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2364. {
  2365. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2366. return boost::math::acosh(arg, c99_error_policy());
  2367. }
  2368. template <class tag, class A1, class A2, class A3, class A4>
  2369. 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
  2370. acosh
  2371. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2372. {
  2373. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2374. detail::scoped_default_precision<value_type> precision_guard(arg);
  2375. return acosh(value_type(arg));
  2376. }
  2377. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2378. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, multiprecision::number<Backend, ExpressionTemplates> >::type
  2379. atanh
  2380. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2381. {
  2382. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2383. return boost::math::atanh(arg, c99_error_policy());
  2384. }
  2385. template <class tag, class A1, class A2, class A3, class A4>
  2386. 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
  2387. atanh
  2388. BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2389. {
  2390. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2391. detail::scoped_default_precision<value_type> precision_guard(arg);
  2392. return atanh(value_type(arg));
  2393. }
  2394. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2395. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2396. {
  2397. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2398. return boost::math::cbrt(arg, c99_error_policy());
  2399. }
  2400. template <class tag, class A1, class A2, class A3, class A4>
  2401. 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)
  2402. {
  2403. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2404. detail::scoped_default_precision<value_type> precision_guard(arg);
  2405. return cbrt(value_type(arg));
  2406. }
  2407. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2408. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2409. {
  2410. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2411. return boost::math::erf(arg, c99_error_policy());
  2412. }
  2413. template <class tag, class A1, class A2, class A3, class A4>
  2414. 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)
  2415. {
  2416. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2417. detail::scoped_default_precision<value_type> precision_guard(arg);
  2418. return erf(value_type(arg));
  2419. }
  2420. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2421. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2422. {
  2423. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2424. return boost::math::erfc(arg, c99_error_policy());
  2425. }
  2426. template <class tag, class A1, class A2, class A3, class A4>
  2427. 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)
  2428. {
  2429. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2430. detail::scoped_default_precision<value_type> precision_guard(arg);
  2431. return erfc(value_type(arg));
  2432. }
  2433. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2434. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2435. {
  2436. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2437. return boost::math::expm1(arg, c99_error_policy());
  2438. }
  2439. template <class tag, class A1, class A2, class A3, class A4>
  2440. 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)
  2441. {
  2442. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2443. detail::scoped_default_precision<value_type> precision_guard(arg);
  2444. return expm1(value_type(arg));
  2445. }
  2446. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2447. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2448. {
  2449. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2450. multiprecision::number<Backend, ExpressionTemplates> result;
  2451. result = boost::math::lgamma(arg, c99_error_policy());
  2452. if ((boost::multiprecision::isnan)(result) && !(boost::multiprecision::isnan)(arg))
  2453. {
  2454. result = std::numeric_limits<multiprecision::number<Backend, ExpressionTemplates> >::infinity();
  2455. errno = ERANGE;
  2456. }
  2457. return result;
  2458. }
  2459. template <class tag, class A1, class A2, class A3, class A4>
  2460. 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)
  2461. {
  2462. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2463. detail::scoped_default_precision<value_type> precision_guard(arg);
  2464. return lgamma(value_type(arg));
  2465. }
  2466. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2467. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2468. {
  2469. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2470. if ((arg == 0) && std::numeric_limits<multiprecision::number<Backend, ExpressionTemplates> >::has_infinity)
  2471. {
  2472. errno = ERANGE;
  2473. return 1 / arg;
  2474. }
  2475. return boost::math::tgamma(arg, c99_error_policy());
  2476. }
  2477. template <class tag, class A1, class A2, class A3, class A4>
  2478. 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)
  2479. {
  2480. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2481. detail::scoped_default_precision<value_type> precision_guard(arg);
  2482. return tgamma(value_type(arg));
  2483. }
  2484. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2485. inline BOOST_MP_CXX14_CONSTEXPR long lrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2486. {
  2487. return lround(arg);
  2488. }
  2489. template <class tag, class A1, class A2, class A3, class A4>
  2490. inline BOOST_MP_CXX14_CONSTEXPR long lrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2491. {
  2492. return lround(arg);
  2493. }
  2494. #ifndef BOOST_NO_LONG_LONG
  2495. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2496. inline BOOST_MP_CXX14_CONSTEXPR long long llrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2497. {
  2498. return llround(arg);
  2499. }
  2500. template <class tag, class A1, class A2, class A3, class A4>
  2501. inline BOOST_MP_CXX14_CONSTEXPR long long llrint BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::detail::expression<tag, A1, A2, A3, A4>& arg)
  2502. {
  2503. return llround(arg);
  2504. }
  2505. #endif
  2506. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2507. inline BOOST_MP_CXX14_CONSTEXPR multiprecision::number<Backend, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const multiprecision::number<Backend, ExpressionTemplates>& arg)
  2508. {
  2509. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(arg);
  2510. return boost::math::log1p(arg, c99_error_policy());
  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 log1p 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 log1p(value_type(arg));
  2518. }
  2519. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2520. 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)
  2521. {
  2522. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2523. return boost::math::nextafter(a, b, c99_error_policy());
  2524. }
  2525. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2526. 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)
  2527. {
  2528. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2529. return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
  2530. }
  2531. template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2532. 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)
  2533. {
  2534. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2535. return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
  2536. }
  2537. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  2538. 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)
  2539. {
  2540. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2541. detail::scoped_default_precision<value_type> precision_guard(a, b);
  2542. return nextafter BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
  2543. }
  2544. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2545. 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)
  2546. {
  2547. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2548. return boost::math::nextafter(a, b, c99_error_policy());
  2549. }
  2550. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2551. 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)
  2552. {
  2553. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2554. return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(a, multiprecision::number<Backend, ExpressionTemplates>(b));
  2555. }
  2556. template <class tag, class A1, class A2, class A3, class A4, class Backend, multiprecision::expression_template_option ExpressionTemplates>
  2557. 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)
  2558. {
  2559. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(a, b);
  2560. return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(multiprecision::number<Backend, ExpressionTemplates>(a), b);
  2561. }
  2562. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b>
  2563. 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)
  2564. {
  2565. using value_type = typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type;
  2566. detail::scoped_default_precision<value_type> precision_guard(a, b);
  2567. return nexttoward BOOST_PREVENT_MACRO_SUBSTITUTION(value_type(a), value_type(b));
  2568. }
  2569. #endif // BOOST_MP_MATH_AVAILABLE
  2570. template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
  2571. inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& add(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
  2572. {
  2573. static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2574. static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2575. using default_ops::eval_add;
  2576. eval_add(result.backend(), a.backend(), b.backend());
  2577. return result;
  2578. }
  2579. template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
  2580. inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& subtract(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
  2581. {
  2582. static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2583. static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2584. using default_ops::eval_subtract;
  2585. eval_subtract(result.backend(), a.backend(), b.backend());
  2586. return result;
  2587. }
  2588. template <class B1, class B2, class B3, expression_template_option ET1, expression_template_option ET2, expression_template_option ET3>
  2589. inline BOOST_MP_CXX14_CONSTEXPR number<B1, ET1>& multiply(number<B1, ET1>& result, const number<B2, ET2>& a, const number<B3, ET3>& b)
  2590. {
  2591. static_assert((std::is_convertible<B2, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2592. static_assert((std::is_convertible<B3, B1>::value), "No conversion to the target of a mixed precision addition exists");
  2593. using default_ops::eval_multiply;
  2594. eval_multiply(result.backend(), a.backend(), b.backend());
  2595. return result;
  2596. }
  2597. template <class B, expression_template_option ET, class I>
  2598. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
  2599. add(number<B, ET>& result, const I& a, const I& b)
  2600. {
  2601. using default_ops::eval_add;
  2602. using canonical_type = typename detail::canonical<I, B>::type;
  2603. eval_add(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
  2604. return result;
  2605. }
  2606. template <class B, expression_template_option ET, class I>
  2607. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
  2608. subtract(number<B, ET>& result, const I& a, const I& b)
  2609. {
  2610. using default_ops::eval_subtract;
  2611. using canonical_type = typename detail::canonical<I, B>::type;
  2612. eval_subtract(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
  2613. return result;
  2614. }
  2615. template <class B, expression_template_option ET, class I>
  2616. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<boost::multiprecision::detail::is_integral<I>::value, number<B, ET>&>::type
  2617. multiply(number<B, ET>& result, const I& a, const I& b)
  2618. {
  2619. using default_ops::eval_multiply;
  2620. using canonical_type = typename detail::canonical<I, B>::type;
  2621. eval_multiply(result.backend(), static_cast<canonical_type>(a), static_cast<canonical_type>(b));
  2622. return result;
  2623. }
  2624. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2625. 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)
  2626. {
  2627. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2628. return std::move(trunc(number_type(v), pol));
  2629. }
  2630. template <class Backend, expression_template_option ExpressionTemplates, class Policy>
  2631. inline BOOST_MP_CXX14_CONSTEXPR number<Backend, ExpressionTemplates> trunc(const number<Backend, ExpressionTemplates>& v, const Policy&)
  2632. {
  2633. using default_ops::eval_trunc;
  2634. detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(v);
  2635. number<Backend, ExpressionTemplates> result;
  2636. eval_trunc(result.backend(), v.backend());
  2637. return result;
  2638. }
  2639. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2640. inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2641. {
  2642. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2643. number_type r(trunc(v, pol));
  2644. if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
  2645. return boost::math::policies::raise_rounding_error("boost::multiprecision::itrunc<%1%>(%1%)", nullptr, number_type(v), 0, pol);
  2646. return r.template convert_to<int>();
  2647. }
  2648. template <class tag, class A1, class A2, class A3, class A4>
  2649. inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
  2650. {
  2651. return itrunc(v, boost::math::policies::policy<>());
  2652. }
  2653. template <class Backend, expression_template_option ExpressionTemplates, class Policy>
  2654. inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const number<Backend, ExpressionTemplates>& v, const Policy& pol)
  2655. {
  2656. number<Backend, ExpressionTemplates> r(trunc(v, pol));
  2657. if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
  2658. return boost::math::policies::raise_rounding_error("boost::multiprecision::itrunc<%1%>(%1%)", nullptr, v, 0, pol);
  2659. return r.template convert_to<int>();
  2660. }
  2661. template <class Backend, expression_template_option ExpressionTemplates>
  2662. inline BOOST_MP_CXX14_CONSTEXPR int itrunc(const number<Backend, ExpressionTemplates>& v)
  2663. {
  2664. return itrunc(v, boost::math::policies::policy<>());
  2665. }
  2666. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2667. inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2668. {
  2669. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2670. number_type r(trunc(v, pol));
  2671. if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
  2672. return boost::math::policies::raise_rounding_error("boost::multiprecision::ltrunc<%1%>(%1%)", nullptr, number_type(v), 0L, pol);
  2673. return r.template convert_to<long>();
  2674. }
  2675. template <class tag, class A1, class A2, class A3, class A4>
  2676. inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
  2677. {
  2678. return ltrunc(v, boost::math::policies::policy<>());
  2679. }
  2680. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2681. inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2682. {
  2683. number<T, ExpressionTemplates> r(trunc(v, pol));
  2684. if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
  2685. return boost::math::policies::raise_rounding_error("boost::multiprecision::ltrunc<%1%>(%1%)", nullptr, v, 0L, pol);
  2686. return r.template convert_to<long>();
  2687. }
  2688. template <class T, expression_template_option ExpressionTemplates>
  2689. inline BOOST_MP_CXX14_CONSTEXPR long ltrunc(const number<T, ExpressionTemplates>& v)
  2690. {
  2691. return ltrunc(v, boost::math::policies::policy<>());
  2692. }
  2693. #ifndef BOOST_NO_LONG_LONG
  2694. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2695. inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2696. {
  2697. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2698. number_type r(trunc(v, pol));
  2699. if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
  2700. return boost::math::policies::raise_rounding_error("boost::multiprecision::lltrunc<%1%>(%1%)", nullptr, number_type(v), 0LL, pol);
  2701. return r.template convert_to<long long>();
  2702. }
  2703. template <class tag, class A1, class A2, class A3, class A4>
  2704. inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const detail::expression<tag, A1, A2, A3, A4>& v)
  2705. {
  2706. return lltrunc(v, boost::math::policies::policy<>());
  2707. }
  2708. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2709. inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2710. {
  2711. number<T, ExpressionTemplates> r(trunc(v, pol));
  2712. if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
  2713. return boost::math::policies::raise_rounding_error("boost::multiprecision::lltrunc<%1%>(%1%)", nullptr, v, 0LL, pol);
  2714. return r.template convert_to<long long>();
  2715. }
  2716. template <class T, expression_template_option ExpressionTemplates>
  2717. inline BOOST_MP_CXX14_CONSTEXPR long long lltrunc(const number<T, ExpressionTemplates>& v)
  2718. {
  2719. return lltrunc(v, boost::math::policies::policy<>());
  2720. }
  2721. #endif
  2722. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2723. 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)
  2724. {
  2725. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2726. return std::move(round(static_cast<number_type>(v), pol));
  2727. }
  2728. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2729. inline BOOST_MP_CXX14_CONSTEXPR number<T, ExpressionTemplates> round(const number<T, ExpressionTemplates>& v, const Policy&)
  2730. {
  2731. using default_ops::eval_round;
  2732. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2733. number<T, ExpressionTemplates> result;
  2734. eval_round(result.backend(), v.backend());
  2735. return result;
  2736. }
  2737. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2738. inline BOOST_MP_CXX14_CONSTEXPR int iround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2739. {
  2740. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2741. number_type r(round(v, pol));
  2742. if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
  2743. return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, number_type(v), 0, pol);
  2744. return r.template convert_to<int>();
  2745. }
  2746. template <class tag, class A1, class A2, class A3, class A4>
  2747. inline BOOST_MP_CXX14_CONSTEXPR int iround(const detail::expression<tag, A1, A2, A3, A4>& v)
  2748. {
  2749. return iround(v, boost::math::policies::policy<>());
  2750. }
  2751. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2752. inline BOOST_MP_CXX14_CONSTEXPR int iround(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2753. {
  2754. number<T, ExpressionTemplates> r(round(v, pol));
  2755. if ((r > (std::numeric_limits<int>::max)()) || r < (std::numeric_limits<int>::min)() || !BOOST_MP_ISFINITE(v))
  2756. return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, v, 0, pol);
  2757. return r.template convert_to<int>();
  2758. }
  2759. template <class T, expression_template_option ExpressionTemplates>
  2760. inline BOOST_MP_CXX14_CONSTEXPR int iround(const number<T, ExpressionTemplates>& v)
  2761. {
  2762. return iround(v, boost::math::policies::policy<>());
  2763. }
  2764. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2765. inline BOOST_MP_CXX14_CONSTEXPR long lround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2766. {
  2767. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2768. number_type r(round(v, pol));
  2769. if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
  2770. return boost::math::policies::raise_rounding_error("boost::multiprecision::lround<%1%>(%1%)", nullptr, number_type(v), 0L, pol);
  2771. return r.template convert_to<long>();
  2772. }
  2773. template <class tag, class A1, class A2, class A3, class A4>
  2774. inline BOOST_MP_CXX14_CONSTEXPR long lround(const detail::expression<tag, A1, A2, A3, A4>& v)
  2775. {
  2776. return lround(v, boost::math::policies::policy<>());
  2777. }
  2778. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2779. inline BOOST_MP_CXX14_CONSTEXPR long lround(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2780. {
  2781. number<T, ExpressionTemplates> r(round(v, pol));
  2782. if ((r > (std::numeric_limits<long>::max)()) || r < (std::numeric_limits<long>::min)() || !BOOST_MP_ISFINITE(v))
  2783. return boost::math::policies::raise_rounding_error("boost::multiprecision::lround<%1%>(%1%)", nullptr, v, 0L, pol);
  2784. return r.template convert_to<long>();
  2785. }
  2786. template <class T, expression_template_option ExpressionTemplates>
  2787. inline BOOST_MP_CXX14_CONSTEXPR long lround(const number<T, ExpressionTemplates>& v)
  2788. {
  2789. return lround(v, boost::math::policies::policy<>());
  2790. }
  2791. #ifndef BOOST_NO_LONG_LONG
  2792. template <class tag, class A1, class A2, class A3, class A4, class Policy>
  2793. inline BOOST_MP_CXX14_CONSTEXPR long long llround(const detail::expression<tag, A1, A2, A3, A4>& v, const Policy& pol)
  2794. {
  2795. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2796. number_type r(round(v, pol));
  2797. if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
  2798. return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, number_type(v), 0LL, pol);
  2799. return r.template convert_to<long long>();
  2800. }
  2801. template <class tag, class A1, class A2, class A3, class A4>
  2802. inline BOOST_MP_CXX14_CONSTEXPR long long llround(const detail::expression<tag, A1, A2, A3, A4>& v)
  2803. {
  2804. return llround(v, boost::math::policies::policy<>());
  2805. }
  2806. template <class T, expression_template_option ExpressionTemplates, class Policy>
  2807. inline BOOST_MP_CXX14_CONSTEXPR long long llround(const number<T, ExpressionTemplates>& v, const Policy& pol)
  2808. {
  2809. number<T, ExpressionTemplates> r(round(v, pol));
  2810. if ((r > (std::numeric_limits<long long>::max)()) || r < (std::numeric_limits<long long>::min)() || !BOOST_MP_ISFINITE(v))
  2811. return boost::math::policies::raise_rounding_error("boost::multiprecision::iround<%1%>(%1%)", nullptr, v, 0LL, pol);
  2812. return r.template convert_to<long long>();
  2813. }
  2814. template <class T, expression_template_option ExpressionTemplates>
  2815. inline BOOST_MP_CXX14_CONSTEXPR long long llround(const number<T, ExpressionTemplates>& v)
  2816. {
  2817. return llround(v, boost::math::policies::policy<>());
  2818. }
  2819. #endif
  2820. //
  2821. // frexp does not return an expression template since we require the
  2822. // integer argument to be evaluated even if the returned value is
  2823. // not assigned to anything...
  2824. //
  2825. template <class T, expression_template_option ExpressionTemplates>
  2826. 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)
  2827. {
  2828. using default_ops::eval_frexp;
  2829. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2830. number<T, ExpressionTemplates> result;
  2831. eval_frexp(result.backend(), v.backend(), pint);
  2832. return result;
  2833. }
  2834. template <class tag, class A1, class A2, class A3, class A4>
  2835. 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
  2836. frexp(const detail::expression<tag, A1, A2, A3, A4>& v, short* pint)
  2837. {
  2838. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2839. return std::move(frexp(static_cast<number_type>(v), pint));
  2840. }
  2841. template <class T, expression_template_option ExpressionTemplates>
  2842. 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)
  2843. {
  2844. using default_ops::eval_frexp;
  2845. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2846. number<T, ExpressionTemplates> result;
  2847. eval_frexp(result.backend(), v.backend(), pint);
  2848. return result;
  2849. }
  2850. template <class tag, class A1, class A2, class A3, class A4>
  2851. 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
  2852. frexp(const detail::expression<tag, A1, A2, A3, A4>& v, int* pint)
  2853. {
  2854. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2855. return std::move(frexp(static_cast<number_type>(v), pint));
  2856. }
  2857. template <class T, expression_template_option ExpressionTemplates>
  2858. 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)
  2859. {
  2860. using default_ops::eval_frexp;
  2861. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2862. number<T, ExpressionTemplates> result;
  2863. eval_frexp(result.backend(), v.backend(), pint);
  2864. return result;
  2865. }
  2866. template <class tag, class A1, class A2, class A3, class A4>
  2867. 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
  2868. frexp(const detail::expression<tag, A1, A2, A3, A4>& v, long* pint)
  2869. {
  2870. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2871. return std::move(frexp(static_cast<number_type>(v), pint));
  2872. }
  2873. template <class T, expression_template_option ExpressionTemplates>
  2874. 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)
  2875. {
  2876. using default_ops::eval_frexp;
  2877. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2878. number<T, ExpressionTemplates> result;
  2879. eval_frexp(result.backend(), v.backend(), pint);
  2880. return result;
  2881. }
  2882. template <class tag, class A1, class A2, class A3, class A4>
  2883. 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
  2884. frexp(const detail::expression<tag, A1, A2, A3, A4>& v, long long* pint)
  2885. {
  2886. using number_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2887. return std::move(frexp(static_cast<number_type>(v), pint));
  2888. }
  2889. //
  2890. // modf does not return an expression template since we require the
  2891. // second argument to be evaluated even if the returned value is
  2892. // not assigned to anything...
  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 modf(const number<T, ExpressionTemplates>& v, number<T, ExpressionTemplates>* pipart)
  2896. {
  2897. using default_ops::eval_modf;
  2898. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2899. number<T, ExpressionTemplates> result;
  2900. eval_modf(result.backend(), v.backend(), pipart ? &pipart->backend() : nullptr);
  2901. return result;
  2902. }
  2903. template <class T, expression_template_option ExpressionTemplates, class tag, class A1, class A2, class A3, class A4>
  2904. 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)
  2905. {
  2906. using default_ops::eval_modf;
  2907. detail::scoped_default_precision<multiprecision::number<T, ExpressionTemplates> > precision_guard(v);
  2908. number<T, ExpressionTemplates> result, arg(v);
  2909. eval_modf(result.backend(), arg.backend(), pipart ? &pipart->backend() : nullptr);
  2910. return result;
  2911. }
  2912. //
  2913. // Integer square root:
  2914. //
  2915. template <class B, expression_template_option ExpressionTemplates>
  2916. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
  2917. sqrt(const number<B, ExpressionTemplates>& x)
  2918. {
  2919. using default_ops::eval_integer_sqrt;
  2920. number<B, ExpressionTemplates> s, r;
  2921. eval_integer_sqrt(s.backend(), r.backend(), x.backend());
  2922. return s;
  2923. }
  2924. template <class tag, class A1, class A2, class A3, class A4>
  2925. 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
  2926. sqrt(const detail::expression<tag, A1, A2, A3, A4>& arg)
  2927. {
  2928. using default_ops::eval_integer_sqrt;
  2929. using result_type = typename detail::expression<tag, A1, A2, A3, A4>::result_type;
  2930. detail::scoped_default_precision<result_type> precision_guard(arg);
  2931. result_type result, v(arg), r;
  2932. eval_integer_sqrt(result.backend(), r.backend(), v.backend());
  2933. return result;
  2934. }
  2935. //
  2936. // fma:
  2937. //
  2938. namespace default_ops {
  2939. struct fma_func
  2940. {
  2941. template <class B, class T, class U, class V>
  2942. BOOST_MP_CXX14_CONSTEXPR void operator()(B& result, const T& a, const U& b, const V& c) const
  2943. {
  2944. eval_multiply_add(result, a, b, c);
  2945. }
  2946. };
  2947. } // namespace default_ops
  2948. template <class Backend, class U, class V>
  2949. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  2950. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
  2951. (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
  2952. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  2953. detail::expression<detail::function, default_ops::fma_func, number<Backend, et_on>, U, V> >::type
  2954. fma(const number<Backend, et_on>& a, const U& b, const V& c)
  2955. {
  2956. return detail::expression<detail::function, default_ops::fma_func, number<Backend, et_on>, U, V>(
  2957. default_ops::fma_func(), a, b, c);
  2958. }
  2959. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class U, class V>
  2960. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  2961. (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
  2962. (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
  2963. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  2964. detail::expression<detail::function, default_ops::fma_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, V> >::type
  2965. fma(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& a, const U& b, const V& c)
  2966. {
  2967. return detail::expression<detail::function, default_ops::fma_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, V>(
  2968. default_ops::fma_func(), a, b, c);
  2969. }
  2970. template <class Backend, class U, class V>
  2971. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  2972. (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
  2973. (is_number<U>::value || is_number_expression<U>::value || boost::multiprecision::detail::is_arithmetic<U>::value) &&
  2974. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  2975. number<Backend, et_off> >::type
  2976. fma(const number<Backend, et_off>& a, const U& b, const V& c)
  2977. {
  2978. using default_ops::eval_multiply_add;
  2979. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
  2980. number<Backend, et_off> result;
  2981. 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));
  2982. return result;
  2983. }
  2984. template <class U, class Backend, class V>
  2985. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  2986. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
  2987. boost::multiprecision::detail::is_arithmetic<U>::value &&
  2988. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  2989. detail::expression<detail::function, default_ops::fma_func, U, number<Backend, et_on>, V> >::type
  2990. fma(const U& a, const number<Backend, et_on>& b, const V& c)
  2991. {
  2992. return detail::expression<detail::function, default_ops::fma_func, U, number<Backend, et_on>, V>(
  2993. default_ops::fma_func(), a, b, c);
  2994. }
  2995. template <class U, class tag, class Arg1, class Arg2, class Arg3, class Arg4, class V>
  2996. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  2997. (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
  2998. boost::multiprecision::detail::is_arithmetic<U>::value &&
  2999. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  3000. detail::expression<detail::function, default_ops::fma_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, V> >::type
  3001. fma(const U& a, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& b, const V& c)
  3002. {
  3003. return detail::expression<detail::function, default_ops::fma_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, V>(
  3004. default_ops::fma_func(), a, b, c);
  3005. }
  3006. template <class U, class Backend, class V>
  3007. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3008. (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
  3009. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3010. (is_number<V>::value || is_number_expression<V>::value || boost::multiprecision::detail::is_arithmetic<V>::value),
  3011. number<Backend, et_off> >::type
  3012. fma(const U& a, const number<Backend, et_off>& b, const V& c)
  3013. {
  3014. using default_ops::eval_multiply_add;
  3015. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
  3016. number<Backend, et_off> result;
  3017. 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));
  3018. return result;
  3019. }
  3020. template <class U, class V, class Backend>
  3021. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3022. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) &&
  3023. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3024. boost::multiprecision::detail::is_arithmetic<V>::value,
  3025. detail::expression<detail::function, default_ops::fma_func, U, V, number<Backend, et_on> > >::type
  3026. fma(const U& a, const V& b, const number<Backend, et_on>& c)
  3027. {
  3028. return detail::expression<detail::function, default_ops::fma_func, U, V, number<Backend, et_on> >(
  3029. default_ops::fma_func(), a, b, c);
  3030. }
  3031. template <class U, class V, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  3032. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3033. (number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point) &&
  3034. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3035. boost::multiprecision::detail::is_arithmetic<V>::value,
  3036. detail::expression<detail::function, default_ops::fma_func, U, V, detail::expression<tag, Arg1, Arg2, Arg3, Arg4> > >::type
  3037. fma(const U& a, const V& b, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& c)
  3038. {
  3039. return detail::expression<detail::function, default_ops::fma_func, U, V, detail::expression<tag, Arg1, Arg2, Arg3, Arg4> >(
  3040. default_ops::fma_func(), a, b, c);
  3041. }
  3042. template <class U, class V, class Backend>
  3043. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3044. (number_category<number<Backend, et_off> >::value == number_kind_floating_point) &&
  3045. boost::multiprecision::detail::is_arithmetic<U>::value &&
  3046. boost::multiprecision::detail::is_arithmetic<V>::value,
  3047. number<Backend, et_off> >::type
  3048. fma(const U& a, const V& b, const number<Backend, et_off>& c)
  3049. {
  3050. using default_ops::eval_multiply_add;
  3051. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b, c);
  3052. number<Backend, et_off> result;
  3053. 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));
  3054. return result;
  3055. }
  3056. namespace default_ops {
  3057. struct remquo_func
  3058. {
  3059. template <class B, class T, class U>
  3060. BOOST_MP_CXX14_CONSTEXPR void operator()(B& result, const T& a, const U& b, int* pi) const
  3061. {
  3062. eval_remquo(result, a, b, pi);
  3063. }
  3064. };
  3065. } // namespace default_ops
  3066. template <class Backend, class U>
  3067. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3068. number_category<number<Backend, et_on> >::value == number_kind_floating_point,
  3069. detail::expression<detail::function, default_ops::remquo_func, number<Backend, et_on>, U, int*> >::type
  3070. remquo(const number<Backend, et_on>& a, const U& b, int* pi)
  3071. {
  3072. return detail::expression<detail::function, default_ops::remquo_func, number<Backend, et_on>, U, int*>(
  3073. default_ops::remquo_func(), a, b, pi);
  3074. }
  3075. template <class tag, class Arg1, class Arg2, class Arg3, class Arg4, class U>
  3076. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3077. number_category<typename detail::expression<tag, Arg1, Arg2, Arg3, Arg4>::result_type>::value == number_kind_floating_point,
  3078. detail::expression<detail::function, default_ops::remquo_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, int*> >::type
  3079. remquo(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& a, const U& b, int* pi)
  3080. {
  3081. return detail::expression<detail::function, default_ops::remquo_func, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, U, int*>(
  3082. default_ops::remquo_func(), a, b, pi);
  3083. }
  3084. template <class U, class Backend>
  3085. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3086. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) && !is_number<U>::value && !is_number_expression<U>::value,
  3087. detail::expression<detail::function, default_ops::remquo_func, U, number<Backend, et_on>, int*> >::type
  3088. remquo(const U& a, const number<Backend, et_on>& b, int* pi)
  3089. {
  3090. return detail::expression<detail::function, default_ops::remquo_func, U, number<Backend, et_on>, int*>(
  3091. default_ops::remquo_func(), a, b, pi);
  3092. }
  3093. template <class U, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  3094. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3095. (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,
  3096. detail::expression<detail::function, default_ops::remquo_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, int*> >::type
  3097. remquo(const U& a, const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& b, int* pi)
  3098. {
  3099. return detail::expression<detail::function, default_ops::remquo_func, U, detail::expression<tag, Arg1, Arg2, Arg3, Arg4>, int*>(
  3100. default_ops::remquo_func(), a, b, pi);
  3101. }
  3102. template <class Backend, class U>
  3103. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3104. number_category<number<Backend, et_on> >::value == number_kind_floating_point,
  3105. number<Backend, et_off> >::type
  3106. remquo(const number<Backend, et_off>& a, const U& b, int* pi)
  3107. {
  3108. using default_ops::eval_remquo;
  3109. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b);
  3110. number<Backend, et_off> result;
  3111. eval_remquo(result.backend(), a.backend(), number<Backend, et_off>::canonical_value(b), pi);
  3112. return result;
  3113. }
  3114. template <class U, class Backend>
  3115. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<
  3116. (number_category<number<Backend, et_on> >::value == number_kind_floating_point) && !is_number<U>::value && !is_number_expression<U>::value,
  3117. number<Backend, et_off> >::type
  3118. remquo(const U& a, const number<Backend, et_off>& b, int* pi)
  3119. {
  3120. using default_ops::eval_remquo;
  3121. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(a, b);
  3122. number<Backend, et_off> result;
  3123. eval_remquo(result.backend(), number<Backend, et_off>::canonical_value(a), b.backend(), pi);
  3124. return result;
  3125. }
  3126. template <class B, expression_template_option ExpressionTemplates>
  3127. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
  3128. sqrt(const number<B, ExpressionTemplates>& x, number<B, ExpressionTemplates>& r)
  3129. {
  3130. using default_ops::eval_integer_sqrt;
  3131. detail::scoped_default_precision<multiprecision::number<B, ExpressionTemplates> > precision_guard(x, r);
  3132. number<B, ExpressionTemplates> s;
  3133. eval_integer_sqrt(s.backend(), r.backend(), x.backend());
  3134. return s;
  3135. }
  3136. template <class B, expression_template_option ExpressionTemplates, class tag, class Arg1, class Arg2, class Arg3, class Arg4>
  3137. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<B>::value == number_kind_integer, number<B, ExpressionTemplates> >::type
  3138. sqrt(const detail::expression<tag, Arg1, Arg2, Arg3, Arg4>& arg, number<B, ExpressionTemplates>& r)
  3139. {
  3140. using default_ops::eval_integer_sqrt;
  3141. detail::scoped_default_precision<multiprecision::number<B, ExpressionTemplates> > precision_guard(r);
  3142. number<B, ExpressionTemplates> s;
  3143. number<B, ExpressionTemplates> x(arg);
  3144. eval_integer_sqrt(s.backend(), r.backend(), x.backend());
  3145. return s;
  3146. }
  3147. // clang-format off
  3148. //
  3149. // Regrettably, when the argument to a function is an rvalue we must return by value, and not return an
  3150. // expression template, otherwise we can end up with dangling references.
  3151. // See https://github.com/boostorg/multiprecision/issues/175.
  3152. //
  3153. #define UNARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
  3154. template <class Backend> \
  3155. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, number<Backend, et_on> > ::type \
  3156. func(number<Backend, et_on>&& arg) \
  3157. { \
  3158. detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg); \
  3159. number<Backend, et_on> result; \
  3160. using default_ops::BOOST_JOIN(eval_, func); \
  3161. BOOST_JOIN(eval_, func)(result.backend(), arg.backend()); \
  3162. return result; \
  3163. } \
  3164. #define BINARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
  3165. template <class Backend> \
  3166. 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) \
  3167. { \
  3168. detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a); \
  3169. number<Backend, et_on> result; \
  3170. using default_ops::BOOST_JOIN(eval_, func); \
  3171. BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend()); \
  3172. return result; \
  3173. } \
  3174. template <class Backend> \
  3175. 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) \
  3176. { \
  3177. detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a); \
  3178. number<Backend, et_on> result; \
  3179. using default_ops::BOOST_JOIN(eval_, func); \
  3180. BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend()); \
  3181. return result; \
  3182. } \
  3183. template <class Backend> \
  3184. 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) \
  3185. { \
  3186. detail::scoped_default_precision<multiprecision::number<Backend, et_on> > precision_guard(arg, a); \
  3187. number<Backend, et_on> result; \
  3188. using default_ops::BOOST_JOIN(eval_, func); \
  3189. BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend()); \
  3190. return result; \
  3191. } \
  3192. template <class Backend, class tag, class A1, class A2, class A3, class A4> \
  3193. 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), \
  3194. number<Backend, et_on> > ::type \
  3195. func(number<Backend, et_on>&& arg, const detail::expression<tag, A1, A2, A3, A4>& a) \
  3196. { \
  3197. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3198. number<Backend, et_on>, detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3199. } \
  3200. template <class tag, class A1, class A2, class A3, class A4, class Backend> \
  3201. 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), \
  3202. number<Backend, et_on> > ::type \
  3203. func(const detail::expression<tag, A1, A2, A3, A4>& arg, number<Backend, et_on>&& a) \
  3204. { \
  3205. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3206. detail::expression<tag, A1, A2, A3, A4>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3207. } \
  3208. template <class Backend, class Arithmetic> \
  3209. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3210. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category), \
  3211. number<Backend, et_on> >::type \
  3212. func(number<Backend, et_on>&& arg, const Arithmetic& a) \
  3213. { \
  3214. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, \
  3215. number<Backend, et_on>, Arithmetic > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3216. } \
  3217. template <class Backend, class Arithmetic> \
  3218. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3219. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category), \
  3220. number<Backend, et_on> > ::type \
  3221. func(const Arithmetic& arg, number<Backend, et_on>&& a) \
  3222. { \
  3223. return detail::expression< \
  3224. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3225. Arithmetic, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a); \
  3226. } \
  3227. #define UNARY_OP_FUNCTOR(func, category) \
  3228. namespace detail { \
  3229. template <class Backend> \
  3230. struct BOOST_JOIN(category, BOOST_JOIN(func, _funct)) \
  3231. { \
  3232. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const \
  3233. { \
  3234. using default_ops::BOOST_JOIN(eval_, func); \
  3235. BOOST_JOIN(eval_, func) \
  3236. (result, arg); \
  3237. } \
  3238. template <class U> \
  3239. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg) const \
  3240. { \
  3241. using default_ops::BOOST_JOIN(eval_, func); \
  3242. Backend temp; \
  3243. BOOST_JOIN(eval_, func) \
  3244. (temp, arg); \
  3245. result = std::move(temp); \
  3246. } \
  3247. }; \
  3248. } \
  3249. \
  3250. template <class tag, class A1, class A2, class A3, class A4> \
  3251. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category, \
  3252. detail::expression<detail::function, \
  3253. detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3254. detail::expression<tag, A1, A2, A3, A4> > > ::type \
  3255. func(const detail::expression<tag, A1, A2, A3, A4>& arg) \
  3256. { \
  3257. return detail::expression< \
  3258. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3259. 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); \
  3260. } \
  3261. template <class Backend> \
  3262. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, \
  3263. detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, number<Backend, et_on> > > ::type \
  3264. func(const number<Backend, et_on>& arg) \
  3265. { \
  3266. return detail::expression< \
  3267. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3268. number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg); \
  3269. } \
  3270. template <class Backend> \
  3271. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3272. boost::multiprecision::number_category<Backend>::value == category, \
  3273. number<Backend, et_off> >::type \
  3274. func(const number<Backend, et_off>& arg) \
  3275. { \
  3276. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg); \
  3277. number<Backend, et_off> result; \
  3278. using default_ops::BOOST_JOIN(eval_, func); \
  3279. BOOST_JOIN(eval_, func)(result.backend(), arg.backend()); \
  3280. return result; \
  3281. }\
  3282. UNARY_OP_FUNCTOR_CXX11_RVALUE(func, category)\
  3283. #define BINARY_OP_FUNCTOR(func, category) \
  3284. namespace detail { \
  3285. template <class Backend> \
  3286. struct BOOST_JOIN(category, BOOST_JOIN(func, _funct)) \
  3287. { \
  3288. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg, const Backend& a) const \
  3289. { \
  3290. using default_ops::BOOST_JOIN(eval_, func); \
  3291. BOOST_JOIN(eval_, func) \
  3292. (result, arg, a); \
  3293. } \
  3294. template <class Arithmetic> \
  3295. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg, const Arithmetic& a) const \
  3296. { \
  3297. using default_ops::BOOST_JOIN(eval_, func); \
  3298. BOOST_JOIN(eval_, func) \
  3299. (result, arg, number<Backend>::canonical_value(a)); \
  3300. } \
  3301. template <class Arithmetic> \
  3302. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Arithmetic& arg, const Backend& a) const \
  3303. { \
  3304. using default_ops::BOOST_JOIN(eval_, func); \
  3305. BOOST_JOIN(eval_, func) \
  3306. (result, number<Backend>::canonical_value(arg), a); \
  3307. } \
  3308. template <class U> \
  3309. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg, const Backend& a) const \
  3310. { \
  3311. using default_ops::BOOST_JOIN(eval_, func); \
  3312. Backend r; \
  3313. BOOST_JOIN(eval_, func) \
  3314. (r, arg, a); \
  3315. result = std::move(r); \
  3316. } \
  3317. template <class U, class Arithmetic> \
  3318. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Backend& arg, const Arithmetic& a) const \
  3319. { \
  3320. using default_ops::BOOST_JOIN(eval_, func); \
  3321. Backend r; \
  3322. BOOST_JOIN(eval_, func) \
  3323. (r, arg, number<Backend>::canonical_value(a)); \
  3324. result = std::move(r); \
  3325. } \
  3326. template <class U, class Arithmetic> \
  3327. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, const Arithmetic& arg, const Backend& a) const \
  3328. { \
  3329. using default_ops::BOOST_JOIN(eval_, func); \
  3330. Backend r; \
  3331. BOOST_JOIN(eval_, func) \
  3332. (r, number<Backend>::canonical_value(arg), a); \
  3333. result = std::move(r); \
  3334. } \
  3335. }; \
  3336. } \
  3337. template <class Backend> \
  3338. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == category, detail::expression<detail::function, \
  3339. detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, number<Backend, et_on>, number<Backend, et_on> > > ::type \
  3340. func(const number<Backend, et_on>& arg, const number<Backend, et_on>& a) \
  3341. { \
  3342. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3343. number<Backend, et_on>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3344. } \
  3345. template <class Backend, class tag, class A1, class A2, class A3, class A4> \
  3346. 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), \
  3347. 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 \
  3348. func(const number<Backend, et_on>& arg, const detail::expression<tag, A1, A2, A3, A4>& a) \
  3349. { \
  3350. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3351. number<Backend, et_on>, detail::expression<tag, A1, A2, A3, A4> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3352. } \
  3353. template <class tag, class A1, class A2, class A3, class A4, class Backend> \
  3354. 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), \
  3355. 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 \
  3356. func(const detail::expression<tag, A1, A2, A3, A4>& arg, const number<Backend, et_on>& a) \
  3357. { \
  3358. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3359. detail::expression<tag, A1, A2, A3, A4>, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3360. } \
  3361. template <class tag, class A1, class A2, class A3, class A4, class tagb, class A1b, class A2b, class A3b, class A4b> \
  3362. 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), \
  3363. detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3364. detail::expression<tag, A1, A2, A3, A4>, detail::expression<tagb, A1b, A2b, A3b, A4b> > > ::type \
  3365. func(const detail::expression<tag, A1, A2, A3, A4>& arg, const detail::expression<tagb, A1b, A2b, A3b, A4b>& a) \
  3366. { \
  3367. 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>, \
  3368. 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); \
  3369. } \
  3370. template <class Backend, class Arithmetic> \
  3371. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3372. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category), \
  3373. detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3374. number<Backend, et_on>, Arithmetic> > ::type \
  3375. func(const number<Backend, et_on>& arg, const Arithmetic& a) \
  3376. { \
  3377. return detail::expression<detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>, \
  3378. number<Backend, et_on>, Arithmetic > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct))<Backend>(), arg, a); \
  3379. } \
  3380. template <class tag, class A1, class A2, class A3, class A4, class Arithmetic> \
  3381. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3382. 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), \
  3383. detail::expression< \
  3384. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3385. detail::expression<tag, A1, A2, A3, A4>, Arithmetic> > ::type \
  3386. func(const detail::expression<tag, A1, A2, A3, A4>& arg, const Arithmetic& a) \
  3387. { \
  3388. return detail::expression< \
  3389. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3390. 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); \
  3391. } \
  3392. template <class Backend, class Arithmetic> \
  3393. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3394. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_on> >::value && (number_category<Backend>::value == category), \
  3395. detail::expression< \
  3396. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3397. Arithmetic, number<Backend, et_on> > > ::type \
  3398. func(const Arithmetic& arg, const number<Backend, et_on>& a) \
  3399. { \
  3400. return detail::expression< \
  3401. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3402. Arithmetic, number<Backend, et_on> > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a); \
  3403. } \
  3404. template <class tag, class A1, class A2, class A3, class A4, class Arithmetic> \
  3405. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3406. 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), \
  3407. detail::expression< \
  3408. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3409. Arithmetic, detail::expression<tag, A1, A2, A3, A4> > > ::type \
  3410. func(const Arithmetic& arg, const detail::expression<tag, A1, A2, A3, A4>& a) \
  3411. { \
  3412. return detail::expression< \
  3413. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3414. 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); \
  3415. } \
  3416. template <class Backend> \
  3417. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<(number_category<Backend>::value == category), number<Backend, et_off> >::type \
  3418. func(const number<Backend, et_off>& arg, const number<Backend, et_off>& a) \
  3419. { \
  3420. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg, a); \
  3421. number<Backend, et_off> result; \
  3422. using default_ops::BOOST_JOIN(eval_, func); \
  3423. BOOST_JOIN(eval_, func)(result.backend(), arg.backend(), a.backend()); \
  3424. return result; \
  3425. } \
  3426. template <class Backend, class Arithmetic> \
  3427. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3428. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_off> >::value && (number_category<Backend>::value == category), \
  3429. number<Backend, et_off> >::type \
  3430. func(const number<Backend, et_off>& arg, const Arithmetic& a) \
  3431. { \
  3432. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg); \
  3433. number<Backend, et_off> result; \
  3434. using default_ops::BOOST_JOIN(eval_, func); \
  3435. BOOST_JOIN(eval_, func) \
  3436. (result.backend(), arg.backend(), number<Backend, et_off>::canonical_value(a)); \
  3437. return result; \
  3438. } \
  3439. template <class Backend, class Arithmetic> \
  3440. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3441. is_compatible_arithmetic_type<Arithmetic, number<Backend, et_off> >::value && (number_category<Backend>::value == category), \
  3442. number<Backend, et_off> >::type \
  3443. func(const Arithmetic& a, const number<Backend, et_off>& arg) \
  3444. { \
  3445. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg); \
  3446. number<Backend, et_off> result; \
  3447. using default_ops::BOOST_JOIN(eval_, func); \
  3448. BOOST_JOIN(eval_, func) \
  3449. (result.backend(), number<Backend, et_off>::canonical_value(a), arg.backend()); \
  3450. return result; \
  3451. }\
  3452. BINARY_OP_FUNCTOR_CXX11_RVALUE(func, category)
  3453. #define HETERO_BINARY_OP_FUNCTOR_B(func, Arg2, category) \
  3454. template <class tag, class A1, class A2, class A3, class A4> \
  3455. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3456. (number_category<detail::expression<tag, A1, A2, A3, A4> >::value == category), \
  3457. detail::expression< \
  3458. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3459. detail::expression<tag, A1, A2, A3, A4>, Arg2> > ::type \
  3460. func(const detail::expression<tag, A1, A2, A3, A4>& arg, Arg2 const& a) \
  3461. { \
  3462. return detail::expression< \
  3463. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, \
  3464. 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); \
  3465. } \
  3466. template <class Backend> \
  3467. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3468. (number_category<Backend>::value == category), \
  3469. detail::expression< \
  3470. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3471. number<Backend, et_on>, Arg2> > ::type \
  3472. func(const number<Backend, et_on>& arg, Arg2 const& a) \
  3473. { \
  3474. return detail::expression< \
  3475. detail::function, detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend>, \
  3476. number<Backend, et_on>, Arg2 > (detail::BOOST_JOIN(category, BOOST_JOIN(func, _funct)) < Backend > (), arg, a); \
  3477. } \
  3478. template <class Backend> \
  3479. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if< \
  3480. (number_category<Backend>::value == category), \
  3481. number<Backend, et_off> >::type \
  3482. func(const number<Backend, et_off>& arg, Arg2 const& a) \
  3483. { \
  3484. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg, a); \
  3485. number<Backend, et_off> result; \
  3486. using default_ops::BOOST_JOIN(eval_, func); \
  3487. BOOST_JOIN(eval_, func) \
  3488. (result.backend(), arg.backend(), a); \
  3489. return result; \
  3490. }
  3491. #define HETERO_BINARY_OP_FUNCTOR(func, Arg2, category) \
  3492. namespace detail { \
  3493. template <class Backend> \
  3494. struct BOOST_JOIN(category, BOOST_JOIN(func, _funct)) \
  3495. { \
  3496. template <class Arg> \
  3497. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, Backend const& arg, Arg a) const \
  3498. { \
  3499. using default_ops::BOOST_JOIN(eval_, func); \
  3500. BOOST_JOIN(eval_, func) \
  3501. (result, arg, a); \
  3502. } \
  3503. template <class U, class Arg> \
  3504. BOOST_MP_CXX14_CONSTEXPR void operator()(U& result, Backend const& arg, Arg a) const \
  3505. { \
  3506. using default_ops::BOOST_JOIN(eval_, func); \
  3507. Backend temp; \
  3508. BOOST_JOIN(eval_, func) \
  3509. (temp, arg, a); \
  3510. result = std::move(temp); \
  3511. } \
  3512. }; \
  3513. } \
  3514. \
  3515. HETERO_BINARY_OP_FUNCTOR_B(func, Arg2, category)
  3516. // clang-format on
  3517. namespace detail {
  3518. template <class Backend>
  3519. struct abs_funct
  3520. {
  3521. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
  3522. {
  3523. using default_ops::eval_abs;
  3524. eval_abs(result, arg);
  3525. }
  3526. };
  3527. template <class Backend>
  3528. struct conj_funct
  3529. {
  3530. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
  3531. {
  3532. using default_ops::eval_conj;
  3533. eval_conj(result, arg);
  3534. }
  3535. //
  3536. // To allow for mixed complex/scalar arithmetic where conj is called on the scalar type (as in Eigen)
  3537. // we provide an overload that will promote the arg to the distination type:
  3538. //
  3539. template <class Other>
  3540. BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<std::is_constructible<Other, Backend>::value>::type operator()(Other& result, const Backend& arg) const
  3541. {
  3542. using default_ops::eval_conj;
  3543. Other t(arg);
  3544. eval_conj(result, t);
  3545. }
  3546. };
  3547. template <class Backend>
  3548. struct proj_funct
  3549. {
  3550. BOOST_MP_CXX14_CONSTEXPR void operator()(Backend& result, const Backend& arg) const
  3551. {
  3552. using default_ops::eval_proj;
  3553. eval_proj(result, arg);
  3554. }
  3555. };
  3556. } // namespace detail
  3557. template <class tag, class A1, class A2, class A3, class A4>
  3558. 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,
  3559. detail::expression<
  3560. 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
  3561. abs(const detail::expression<tag, A1, A2, A3, A4>& arg)
  3562. {
  3563. return detail::expression<
  3564. detail::function, detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
  3565. detail::abs_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
  3566. }
  3567. template <class Backend>
  3568. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex,
  3569. detail::expression<
  3570. detail::function, detail::abs_funct<Backend>, number<Backend, et_on> > >::type
  3571. abs(const number<Backend, et_on>& arg)
  3572. {
  3573. return detail::expression<
  3574. detail::function, detail::abs_funct<Backend>, number<Backend, et_on> >(
  3575. detail::abs_funct<Backend>(), arg);
  3576. }
  3577. template <class Backend>
  3578. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value != number_kind_complex, number<Backend, et_off> >::type
  3579. abs(const number<Backend, et_off>& arg)
  3580. {
  3581. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
  3582. number<Backend, et_off> result;
  3583. using default_ops::eval_abs;
  3584. eval_abs(result.backend(), arg.backend());
  3585. return result;
  3586. }
  3587. template <class tag, class A1, class A2, class A3, class A4>
  3588. inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
  3589. detail::function, detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >
  3590. conj(const detail::expression<tag, A1, A2, A3, A4>& arg)
  3591. {
  3592. return detail::expression<
  3593. detail::function, detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
  3594. detail::conj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
  3595. }
  3596. template <class Backend>
  3597. inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
  3598. detail::function, detail::conj_funct<Backend>, number<Backend, et_on> >
  3599. conj(const number<Backend, et_on>& arg)
  3600. {
  3601. return detail::expression<
  3602. detail::function, detail::conj_funct<Backend>, number<Backend, et_on> >(
  3603. detail::conj_funct<Backend>(), arg);
  3604. }
  3605. template <class Backend>
  3606. inline BOOST_MP_CXX14_CONSTEXPR number<Backend, et_off>
  3607. conj(const number<Backend, et_off>& arg)
  3608. {
  3609. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
  3610. number<Backend, et_off> result;
  3611. using default_ops::eval_conj;
  3612. eval_conj(result.backend(), arg.backend());
  3613. return result;
  3614. }
  3615. template <class tag, class A1, class A2, class A3, class A4>
  3616. inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
  3617. detail::function, detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >
  3618. proj(const detail::expression<tag, A1, A2, A3, A4>& arg)
  3619. {
  3620. return detail::expression<
  3621. detail::function, detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>, detail::expression<tag, A1, A2, A3, A4> >(
  3622. detail::proj_funct<typename detail::backend_type<detail::expression<tag, A1, A2, A3, A4> >::type>(), arg);
  3623. }
  3624. template <class Backend>
  3625. inline BOOST_MP_CXX14_CONSTEXPR detail::expression<
  3626. detail::function, detail::proj_funct<Backend>, number<Backend, et_on> >
  3627. proj(const number<Backend, et_on>& arg)
  3628. {
  3629. return detail::expression<
  3630. detail::function, detail::proj_funct<Backend>, number<Backend, et_on> >(
  3631. detail::proj_funct<Backend>(), arg);
  3632. }
  3633. template <class Backend>
  3634. inline BOOST_MP_CXX14_CONSTEXPR number<Backend, et_off>
  3635. proj(const number<Backend, et_off>& arg)
  3636. {
  3637. detail::scoped_default_precision<multiprecision::number<Backend, et_off> > precision_guard(arg);
  3638. number<Backend, et_off> result;
  3639. using default_ops::eval_proj;
  3640. eval_proj(result.backend(), arg.backend());
  3641. return result;
  3642. }
  3643. UNARY_OP_FUNCTOR(fabs, number_kind_floating_point)
  3644. UNARY_OP_FUNCTOR(sqrt, number_kind_floating_point)
  3645. UNARY_OP_FUNCTOR(floor, number_kind_floating_point)
  3646. UNARY_OP_FUNCTOR(ceil, number_kind_floating_point)
  3647. UNARY_OP_FUNCTOR(trunc, number_kind_floating_point)
  3648. UNARY_OP_FUNCTOR(round, number_kind_floating_point)
  3649. UNARY_OP_FUNCTOR(exp, number_kind_floating_point)
  3650. UNARY_OP_FUNCTOR(exp2, number_kind_floating_point)
  3651. UNARY_OP_FUNCTOR(log, number_kind_floating_point)
  3652. UNARY_OP_FUNCTOR(log10, number_kind_floating_point)
  3653. UNARY_OP_FUNCTOR(cos, number_kind_floating_point)
  3654. UNARY_OP_FUNCTOR(sin, number_kind_floating_point)
  3655. UNARY_OP_FUNCTOR(tan, number_kind_floating_point)
  3656. UNARY_OP_FUNCTOR(asin, number_kind_floating_point)
  3657. UNARY_OP_FUNCTOR(acos, number_kind_floating_point)
  3658. UNARY_OP_FUNCTOR(atan, number_kind_floating_point)
  3659. UNARY_OP_FUNCTOR(cosh, number_kind_floating_point)
  3660. UNARY_OP_FUNCTOR(sinh, number_kind_floating_point)
  3661. UNARY_OP_FUNCTOR(tanh, number_kind_floating_point)
  3662. UNARY_OP_FUNCTOR(log2, number_kind_floating_point)
  3663. UNARY_OP_FUNCTOR(nearbyint, number_kind_floating_point)
  3664. UNARY_OP_FUNCTOR(rint, number_kind_floating_point)
  3665. HETERO_BINARY_OP_FUNCTOR(ldexp, short, number_kind_floating_point)
  3666. //HETERO_BINARY_OP_FUNCTOR(frexp, short*, number_kind_floating_point)
  3667. HETERO_BINARY_OP_FUNCTOR_B(ldexp, int, number_kind_floating_point)
  3668. //HETERO_BINARY_OP_FUNCTOR_B(frexp, int*, number_kind_floating_point)
  3669. HETERO_BINARY_OP_FUNCTOR_B(ldexp, long, number_kind_floating_point)
  3670. //HETERO_BINARY_OP_FUNCTOR_B(frexp, long*, number_kind_floating_point)
  3671. HETERO_BINARY_OP_FUNCTOR_B(ldexp, long long, number_kind_floating_point)
  3672. //HETERO_BINARY_OP_FUNCTOR_B(frexp, long long*, number_kind_floating_point)
  3673. BINARY_OP_FUNCTOR(pow, number_kind_floating_point)
  3674. BINARY_OP_FUNCTOR(fmod, number_kind_floating_point)
  3675. BINARY_OP_FUNCTOR(fmax, number_kind_floating_point)
  3676. BINARY_OP_FUNCTOR(fmin, number_kind_floating_point)
  3677. BINARY_OP_FUNCTOR(atan2, number_kind_floating_point)
  3678. BINARY_OP_FUNCTOR(fdim, number_kind_floating_point)
  3679. BINARY_OP_FUNCTOR(hypot, number_kind_floating_point)
  3680. BINARY_OP_FUNCTOR(remainder, number_kind_floating_point)
  3681. UNARY_OP_FUNCTOR(logb, number_kind_floating_point)
  3682. HETERO_BINARY_OP_FUNCTOR(scalbn, short, number_kind_floating_point)
  3683. HETERO_BINARY_OP_FUNCTOR(scalbln, short, number_kind_floating_point)
  3684. HETERO_BINARY_OP_FUNCTOR_B(scalbn, int, number_kind_floating_point)
  3685. HETERO_BINARY_OP_FUNCTOR_B(scalbln, int, number_kind_floating_point)
  3686. HETERO_BINARY_OP_FUNCTOR_B(scalbn, long, number_kind_floating_point)
  3687. HETERO_BINARY_OP_FUNCTOR_B(scalbln, long, number_kind_floating_point)
  3688. HETERO_BINARY_OP_FUNCTOR_B(scalbn, long long, number_kind_floating_point)
  3689. HETERO_BINARY_OP_FUNCTOR_B(scalbln, long long, number_kind_floating_point)
  3690. //
  3691. // Complex functions:
  3692. //
  3693. UNARY_OP_FUNCTOR(exp, number_kind_complex)
  3694. UNARY_OP_FUNCTOR(log, number_kind_complex)
  3695. UNARY_OP_FUNCTOR(log10, number_kind_complex)
  3696. BINARY_OP_FUNCTOR(pow, number_kind_complex)
  3697. UNARY_OP_FUNCTOR(sqrt, number_kind_complex)
  3698. UNARY_OP_FUNCTOR(sin, number_kind_complex)
  3699. UNARY_OP_FUNCTOR(cos, number_kind_complex)
  3700. UNARY_OP_FUNCTOR(tan, number_kind_complex)
  3701. UNARY_OP_FUNCTOR(asin, number_kind_complex)
  3702. UNARY_OP_FUNCTOR(acos, number_kind_complex)
  3703. UNARY_OP_FUNCTOR(atan, number_kind_complex)
  3704. UNARY_OP_FUNCTOR(sinh, number_kind_complex)
  3705. UNARY_OP_FUNCTOR(cosh, number_kind_complex)
  3706. UNARY_OP_FUNCTOR(tanh, number_kind_complex)
  3707. UNARY_OP_FUNCTOR(asinh, number_kind_complex)
  3708. UNARY_OP_FUNCTOR(acosh, number_kind_complex)
  3709. UNARY_OP_FUNCTOR(atanh, number_kind_complex)
  3710. //
  3711. // Integer functions:
  3712. //
  3713. BINARY_OP_FUNCTOR(gcd, number_kind_integer)
  3714. BINARY_OP_FUNCTOR(lcm, number_kind_integer)
  3715. HETERO_BINARY_OP_FUNCTOR(pow, unsigned, number_kind_integer)
  3716. #undef BINARY_OP_FUNCTOR
  3717. #undef UNARY_OP_FUNCTOR
  3718. //
  3719. // ilogb:
  3720. //
  3721. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  3722. inline BOOST_MP_CXX14_CONSTEXPR typename std::enable_if<number_category<Backend>::value == number_kind_floating_point, typename Backend::exponent_type>::type
  3723. ilogb(const multiprecision::number<Backend, ExpressionTemplates>& val)
  3724. {
  3725. using default_ops::eval_ilogb;
  3726. return eval_ilogb(val.backend());
  3727. }
  3728. template <class tag, class A1, class A2, class A3, class A4>
  3729. 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
  3730. ilogb(const detail::expression<tag, A1, A2, A3, A4>& val)
  3731. {
  3732. using default_ops::eval_ilogb;
  3733. typename multiprecision::detail::expression<tag, A1, A2, A3, A4>::result_type arg(val);
  3734. return eval_ilogb(arg.backend());
  3735. }
  3736. } //namespace multiprecision
  3737. namespace math {
  3738. //
  3739. // Overload of Boost.Math functions that find the wrong overload when used with number:
  3740. //
  3741. namespace detail {
  3742. template <class T>
  3743. T sinc_pi_imp(T);
  3744. template <class T>
  3745. T sinhc_pi_imp(T);
  3746. } // namespace detail
  3747. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  3748. inline multiprecision::number<Backend, ExpressionTemplates> sinc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x)
  3749. {
  3750. boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
  3751. return std::move(detail::sinc_pi_imp(x));
  3752. }
  3753. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class Policy>
  3754. inline multiprecision::number<Backend, ExpressionTemplates> sinc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x, const Policy&)
  3755. {
  3756. boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
  3757. return std::move(detail::sinc_pi_imp(x));
  3758. }
  3759. template <class Backend, multiprecision::expression_template_option ExpressionTemplates>
  3760. inline multiprecision::number<Backend, ExpressionTemplates> sinhc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x)
  3761. {
  3762. boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
  3763. return std::move(detail::sinhc_pi_imp(x));
  3764. }
  3765. template <class Backend, multiprecision::expression_template_option ExpressionTemplates, class Policy>
  3766. inline multiprecision::number<Backend, ExpressionTemplates> sinhc_pi(const multiprecision::number<Backend, ExpressionTemplates>& x, const Policy&)
  3767. {
  3768. boost::multiprecision::detail::scoped_default_precision<multiprecision::number<Backend, ExpressionTemplates> > precision_guard(x);
  3769. return std::move(boost::math::sinhc_pi(x));
  3770. }
  3771. using boost::multiprecision::gcd;
  3772. using boost::multiprecision::lcm;
  3773. #ifdef BOOST_MSVC
  3774. #pragma warning(pop)
  3775. #endif
  3776. } // namespace math
  3777. namespace integer {
  3778. using boost::multiprecision::gcd;
  3779. using boost::multiprecision::lcm;
  3780. } // namespace integer
  3781. } // namespace boost
  3782. //
  3783. // This has to come last of all:
  3784. //
  3785. #include <boost/multiprecision/detail/no_et_ops.hpp>
  3786. #include <boost/multiprecision/detail/et_ops.hpp>
  3787. //
  3788. // min/max overloads:
  3789. //
  3790. #include <boost/multiprecision/detail/min_max.hpp>
  3791. #endif