default_ops.hpp 208 KB

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