default_ops.hpp 138 KB

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