default_ops.hpp 223 KB

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