default_ops.hpp 235 KB

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