mpfr.hpp 124 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651
  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_BN_MPFR_HPP
  6. #define BOOST_MATH_BN_MPFR_HPP
  7. #include <boost/multiprecision/number.hpp>
  8. #include <boost/multiprecision/debug_adaptor.hpp>
  9. #include <boost/multiprecision/gmp.hpp>
  10. #include <boost/math/special_functions/fpclassify.hpp>
  11. #include <cstdint>
  12. #include <boost/multiprecision/detail/big_lanczos.hpp>
  13. #include <boost/multiprecision/detail/digits.hpp>
  14. #include <boost/multiprecision/detail/atomic.hpp>
  15. #include <boost/multiprecision/traits/max_digits10.hpp>
  16. #include <mpfr.h>
  17. #include <cmath>
  18. #include <algorithm>
  19. #include <utility>
  20. #include <type_traits>
  21. #ifndef BOOST_MULTIPRECISION_MPFR_DEFAULT_PRECISION
  22. #define BOOST_MULTIPRECISION_MPFR_DEFAULT_PRECISION 20
  23. #endif
  24. namespace boost {
  25. namespace multiprecision {
  26. enum mpfr_allocation_type
  27. {
  28. allocate_stack,
  29. allocate_dynamic
  30. };
  31. namespace backends {
  32. template <unsigned digits10, mpfr_allocation_type AllocationType = allocate_dynamic>
  33. struct mpfr_float_backend;
  34. template <>
  35. struct mpfr_float_backend<0, allocate_stack>;
  36. } // namespace backends
  37. template <unsigned digits10, mpfr_allocation_type AllocationType>
  38. struct number_category<backends::mpfr_float_backend<digits10, AllocationType> > : public std::integral_constant<int, number_kind_floating_point>
  39. {};
  40. namespace backends {
  41. namespace detail {
  42. template <bool b>
  43. struct mpfr_cleanup
  44. {
  45. //
  46. // There are 2 seperate cleanup objects here, one calls
  47. // mpfr_free_cache on destruction to perform global cleanup
  48. // the other is declared thread_local and calls
  49. // mpfr_free_cache2(MPFR_FREE_LOCAL_CACHE) to free thread local data.
  50. //
  51. struct initializer
  52. {
  53. initializer() {}
  54. ~initializer() { mpfr_free_cache(); }
  55. void force_instantiate() const {}
  56. };
  57. #if MPFR_VERSION_MAJOR >= 4
  58. struct thread_initializer
  59. {
  60. thread_initializer() {}
  61. ~thread_initializer() { mpfr_free_cache2(MPFR_FREE_LOCAL_CACHE); }
  62. void force_instantiate() const {}
  63. };
  64. #endif
  65. static const initializer init;
  66. static void force_instantiate()
  67. {
  68. #if MPFR_VERSION_MAJOR >= 4
  69. static const BOOST_MP_THREAD_LOCAL thread_initializer thread_init;
  70. thread_init.force_instantiate();
  71. #endif
  72. init.force_instantiate();
  73. }
  74. };
  75. template <bool b>
  76. typename mpfr_cleanup<b>::initializer const mpfr_cleanup<b>::init;
  77. inline void mpfr_copy_precision(mpfr_t dest, const mpfr_t src)
  78. {
  79. mpfr_prec_t p_dest = mpfr_get_prec(dest);
  80. mpfr_prec_t p_src = mpfr_get_prec(src);
  81. if (p_dest != p_src)
  82. mpfr_set_prec(dest, p_src);
  83. }
  84. inline void mpfr_copy_precision(mpfr_t dest, const mpfr_t src1, const mpfr_t src2)
  85. {
  86. mpfr_prec_t p_dest = mpfr_get_prec(dest);
  87. mpfr_prec_t p_src1 = mpfr_get_prec(src1);
  88. mpfr_prec_t p_src2 = mpfr_get_prec(src2);
  89. if (p_src2 > p_src1)
  90. p_src1 = p_src2;
  91. if (p_dest != p_src1)
  92. mpfr_set_prec(dest, p_src1);
  93. }
  94. template <unsigned digits10, mpfr_allocation_type AllocationType>
  95. struct mpfr_float_imp;
  96. template <unsigned digits10>
  97. struct mpfr_float_imp<digits10, allocate_dynamic>
  98. {
  99. #ifdef BOOST_HAS_LONG_LONG
  100. using signed_types = std::tuple<long, boost::long_long_type> ;
  101. using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
  102. #else
  103. using signed_types = std::tuple<long> ;
  104. using unsigned_types = std::tuple<unsigned long>;
  105. #endif
  106. using float_types = std::tuple<double, long double>;
  107. using exponent_type = long ;
  108. mpfr_float_imp()
  109. {
  110. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  111. mpfr_set_ui(m_data, 0u, GMP_RNDN);
  112. }
  113. mpfr_float_imp(unsigned digits2)
  114. {
  115. mpfr_init2(m_data, digits2);
  116. mpfr_set_ui(m_data, 0u, GMP_RNDN);
  117. }
  118. mpfr_float_imp(const mpfr_float_imp& o)
  119. {
  120. mpfr_init2(m_data, mpfr_get_prec(o.m_data));
  121. if (o.m_data[0]._mpfr_d)
  122. mpfr_set(m_data, o.m_data, GMP_RNDN);
  123. }
  124. // rvalue copy
  125. mpfr_float_imp(mpfr_float_imp&& o) noexcept
  126. {
  127. m_data[0] = o.m_data[0];
  128. o.m_data[0]._mpfr_d = 0;
  129. }
  130. mpfr_float_imp& operator=(const mpfr_float_imp& o)
  131. {
  132. if ((o.m_data[0]._mpfr_d) && (this != &o))
  133. {
  134. if (m_data[0]._mpfr_d == 0)
  135. mpfr_init2(m_data, mpfr_get_prec(o.m_data));
  136. mpfr_set(m_data, o.m_data, GMP_RNDN);
  137. }
  138. return *this;
  139. }
  140. // rvalue assign
  141. mpfr_float_imp& operator=(mpfr_float_imp&& o) noexcept
  142. {
  143. mpfr_swap(m_data, o.m_data);
  144. return *this;
  145. }
  146. #ifdef BOOST_HAS_LONG_LONG
  147. #ifdef _MPFR_H_HAVE_INTMAX_T
  148. mpfr_float_imp& operator=(boost::ulong_long_type i)
  149. {
  150. if (m_data[0]._mpfr_d == 0)
  151. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  152. mpfr_set_uj(m_data, i, GMP_RNDN);
  153. return *this;
  154. }
  155. mpfr_float_imp& operator=(boost::long_long_type i)
  156. {
  157. if (m_data[0]._mpfr_d == 0)
  158. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  159. mpfr_set_sj(m_data, i, GMP_RNDN);
  160. return *this;
  161. }
  162. #else
  163. mpfr_float_imp& operator=(boost::ulong_long_type i)
  164. {
  165. if (m_data[0]._mpfr_d == 0)
  166. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  167. boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
  168. unsigned shift = 0;
  169. mpfr_t t;
  170. mpfr_init2(t, (std::max)(static_cast<mpfr_prec_t>(std::numeric_limits<boost::ulong_long_type>::digits), static_cast<mpfr_prec_t>(mpfr_get_prec(m_data))));
  171. mpfr_set_ui(m_data, 0, GMP_RNDN);
  172. while (i)
  173. {
  174. mpfr_set_ui(t, static_cast<unsigned long>(i & mask), GMP_RNDN);
  175. if (shift)
  176. mpfr_mul_2exp(t, t, shift, GMP_RNDN);
  177. mpfr_add(m_data, m_data, t, GMP_RNDN);
  178. shift += std::numeric_limits<unsigned long>::digits;
  179. i >>= std::numeric_limits<unsigned long>::digits;
  180. }
  181. mpfr_clear(t);
  182. return *this;
  183. }
  184. mpfr_float_imp& operator=(boost::long_long_type i)
  185. {
  186. if (m_data[0]._mpfr_d == 0)
  187. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  188. bool neg = i < 0;
  189. *this = boost::multiprecision::detail::unsigned_abs(i);
  190. if (neg)
  191. mpfr_neg(m_data, m_data, GMP_RNDN);
  192. return *this;
  193. }
  194. #endif
  195. #endif
  196. mpfr_float_imp& operator=(unsigned long i)
  197. {
  198. if (m_data[0]._mpfr_d == 0)
  199. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  200. mpfr_set_ui(m_data, i, GMP_RNDN);
  201. return *this;
  202. }
  203. mpfr_float_imp& operator=(long i)
  204. {
  205. if (m_data[0]._mpfr_d == 0)
  206. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  207. mpfr_set_si(m_data, i, GMP_RNDN);
  208. return *this;
  209. }
  210. mpfr_float_imp& operator=(double d)
  211. {
  212. if (m_data[0]._mpfr_d == 0)
  213. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  214. mpfr_set_d(m_data, d, GMP_RNDN);
  215. return *this;
  216. }
  217. mpfr_float_imp& operator=(long double a)
  218. {
  219. if (m_data[0]._mpfr_d == 0)
  220. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  221. mpfr_set_ld(m_data, a, GMP_RNDN);
  222. return *this;
  223. }
  224. mpfr_float_imp& operator=(const char* s)
  225. {
  226. if (m_data[0]._mpfr_d == 0)
  227. mpfr_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  228. if (mpfr_set_str(m_data, s, 10, GMP_RNDN) != 0)
  229. {
  230. BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Unable to parse string \"") + s + std::string("\"as a valid floating point number.")));
  231. }
  232. return *this;
  233. }
  234. void swap(mpfr_float_imp& o) noexcept
  235. {
  236. mpfr_swap(m_data, o.m_data);
  237. }
  238. std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
  239. {
  240. BOOST_ASSERT(m_data[0]._mpfr_d);
  241. bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
  242. bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
  243. std::streamsize org_digits(digits);
  244. if (scientific && digits)
  245. ++digits;
  246. std::string result;
  247. mp_exp_t e;
  248. if (mpfr_inf_p(m_data))
  249. {
  250. if (mpfr_sgn(m_data) < 0)
  251. result = "-inf";
  252. else if (f & std::ios_base::showpos)
  253. result = "+inf";
  254. else
  255. result = "inf";
  256. return result;
  257. }
  258. if (mpfr_nan_p(m_data))
  259. {
  260. result = "nan";
  261. return result;
  262. }
  263. if (mpfr_zero_p(m_data))
  264. {
  265. e = 0;
  266. result = "0";
  267. }
  268. else
  269. {
  270. char* ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data, GMP_RNDN);
  271. --e; // To match with what our formatter expects.
  272. if (fixed && e != -1)
  273. {
  274. // Oops we actually need a different number of digits to what we asked for:
  275. mpfr_free_str(ps);
  276. digits += e + 1;
  277. if (digits == 0)
  278. {
  279. // We need to get *all* the digits and then possibly round up,
  280. // we end up with either "0" or "1" as the result.
  281. ps = mpfr_get_str(0, &e, 10, 0, m_data, GMP_RNDN);
  282. --e;
  283. unsigned offset = *ps == '-' ? 1 : 0;
  284. if (ps[offset] > '5')
  285. {
  286. ++e;
  287. ps[offset] = '1';
  288. ps[offset + 1] = 0;
  289. }
  290. else if (ps[offset] == '5')
  291. {
  292. unsigned i = offset + 1;
  293. bool round_up = false;
  294. while (ps[i] != 0)
  295. {
  296. if (ps[i] != '0')
  297. {
  298. round_up = true;
  299. break;
  300. }
  301. ++i;
  302. }
  303. if (round_up)
  304. {
  305. ++e;
  306. ps[offset] = '1';
  307. ps[offset + 1] = 0;
  308. }
  309. else
  310. {
  311. ps[offset] = '0';
  312. ps[offset + 1] = 0;
  313. }
  314. }
  315. else
  316. {
  317. ps[offset] = '0';
  318. ps[offset + 1] = 0;
  319. }
  320. }
  321. else if (digits > 0)
  322. {
  323. mp_exp_t old_e = e;
  324. ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data, GMP_RNDN);
  325. --e; // To match with what our formatter expects.
  326. if (old_e > e)
  327. {
  328. // in some cases, when we ask for more digits of precision, it will
  329. // change the number of digits to the left of the decimal, if that
  330. // happens, account for it here.
  331. // example: cout << fixed << setprecision(3) << mpf_float_50("99.9809")
  332. mpfr_free_str(ps);
  333. digits -= old_e - e;
  334. ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data, GMP_RNDN);
  335. --e; // To match with what our formatter expects.
  336. }
  337. }
  338. else
  339. {
  340. ps = mpfr_get_str(0, &e, 10, 1, m_data, GMP_RNDN);
  341. --e;
  342. unsigned offset = *ps == '-' ? 1 : 0;
  343. ps[offset] = '0';
  344. ps[offset + 1] = 0;
  345. }
  346. }
  347. result = ps ? ps : "0";
  348. if (ps)
  349. mpfr_free_str(ps);
  350. }
  351. boost::multiprecision::detail::format_float_string(result, e, org_digits, f, 0 != mpfr_zero_p(m_data));
  352. return result;
  353. }
  354. ~mpfr_float_imp() noexcept
  355. {
  356. if (m_data[0]._mpfr_d)
  357. mpfr_clear(m_data);
  358. detail::mpfr_cleanup<true>::force_instantiate();
  359. }
  360. void negate() noexcept
  361. {
  362. BOOST_ASSERT(m_data[0]._mpfr_d);
  363. mpfr_neg(m_data, m_data, GMP_RNDN);
  364. }
  365. template <mpfr_allocation_type AllocationType>
  366. int compare(const mpfr_float_backend<digits10, AllocationType>& o) const
  367. {
  368. BOOST_ASSERT(m_data[0]._mpfr_d && o.m_data[0]._mpfr_d);
  369. return mpfr_cmp(m_data, o.m_data);
  370. }
  371. int compare(long i) const
  372. {
  373. BOOST_ASSERT(m_data[0]._mpfr_d);
  374. return mpfr_cmp_si(m_data, i);
  375. }
  376. int compare(double i) const
  377. {
  378. BOOST_ASSERT(m_data[0]._mpfr_d);
  379. return mpfr_cmp_d(m_data, i);
  380. }
  381. int compare(long double i) const
  382. {
  383. BOOST_ASSERT(m_data[0]._mpfr_d);
  384. return mpfr_cmp_ld(m_data, i);
  385. }
  386. int compare(unsigned long i) const
  387. {
  388. BOOST_ASSERT(m_data[0]._mpfr_d);
  389. return mpfr_cmp_ui(m_data, i);
  390. }
  391. template <class V>
  392. int compare(V v) const
  393. {
  394. mpfr_float_backend<digits10, allocate_dynamic> d(0uL, mpfr_get_prec(m_data));
  395. d = v;
  396. return compare(d);
  397. }
  398. mpfr_t& data() noexcept
  399. {
  400. BOOST_ASSERT(m_data[0]._mpfr_d);
  401. return m_data;
  402. }
  403. const mpfr_t& data() const noexcept
  404. {
  405. BOOST_ASSERT(m_data[0]._mpfr_d);
  406. return m_data;
  407. }
  408. protected:
  409. mpfr_t m_data;
  410. static boost::multiprecision::detail::precision_type& get_default_precision() noexcept
  411. {
  412. static boost::multiprecision::detail::precision_type val(BOOST_MULTIPRECISION_MPFR_DEFAULT_PRECISION);
  413. return val;
  414. }
  415. };
  416. #ifdef BOOST_MSVC
  417. #pragma warning(push)
  418. #pragma warning(disable : 4127) // Conditional expression is constant
  419. #endif
  420. template <unsigned digits10>
  421. struct mpfr_float_imp<digits10, allocate_stack>
  422. {
  423. #ifdef BOOST_HAS_LONG_LONG
  424. using signed_types = std::tuple<long, boost::long_long_type> ;
  425. using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
  426. #else
  427. using signed_types = std::tuple<long> ;
  428. using unsigned_types = std::tuple<unsigned long>;
  429. #endif
  430. using float_types = std::tuple<double, long double>;
  431. using exponent_type = long ;
  432. static constexpr const unsigned digits2 = (digits10 * 1000uL) / 301uL + ((digits10 * 1000uL) % 301 ? 2u : 1u);
  433. static constexpr const unsigned limb_count = mpfr_custom_get_size(digits2) / sizeof(mp_limb_t);
  434. ~mpfr_float_imp() noexcept
  435. {
  436. detail::mpfr_cleanup<true>::force_instantiate();
  437. }
  438. mpfr_float_imp()
  439. {
  440. mpfr_custom_init(m_buffer, digits2);
  441. mpfr_custom_init_set(m_data, MPFR_NAN_KIND, 0, digits2, m_buffer);
  442. mpfr_set_ui(m_data, 0u, GMP_RNDN);
  443. }
  444. mpfr_float_imp(const mpfr_float_imp& o)
  445. {
  446. mpfr_custom_init(m_buffer, digits2);
  447. mpfr_custom_init_set(m_data, MPFR_NAN_KIND, 0, digits2, m_buffer);
  448. mpfr_set(m_data, o.m_data, GMP_RNDN);
  449. }
  450. mpfr_float_imp& operator=(const mpfr_float_imp& o)
  451. {
  452. mpfr_set(m_data, o.m_data, GMP_RNDN);
  453. return *this;
  454. }
  455. #ifdef BOOST_HAS_LONG_LONG
  456. #ifdef _MPFR_H_HAVE_INTMAX_T
  457. mpfr_float_imp& operator=(boost::ulong_long_type i)
  458. {
  459. mpfr_set_uj(m_data, i, GMP_RNDN);
  460. return *this;
  461. }
  462. mpfr_float_imp& operator=(boost::long_long_type i)
  463. {
  464. mpfr_set_sj(m_data, i, GMP_RNDN);
  465. return *this;
  466. }
  467. #else
  468. mpfr_float_imp& operator=(boost::ulong_long_type i)
  469. {
  470. boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uL);
  471. unsigned shift = 0;
  472. mpfr_t t;
  473. mp_limb_t t_limbs[limb_count];
  474. mpfr_custom_init(t_limbs, digits2);
  475. mpfr_custom_init_set(t, MPFR_NAN_KIND, 0, digits2, t_limbs);
  476. mpfr_set_ui(m_data, 0, GMP_RNDN);
  477. while (i)
  478. {
  479. mpfr_set_ui(t, static_cast<unsigned long>(i & mask), GMP_RNDN);
  480. if (shift)
  481. mpfr_mul_2exp(t, t, shift, GMP_RNDN);
  482. mpfr_add(m_data, m_data, t, GMP_RNDN);
  483. shift += std::numeric_limits<unsigned long>::digits;
  484. i >>= std::numeric_limits<unsigned long>::digits;
  485. }
  486. return *this;
  487. }
  488. mpfr_float_imp& operator=(boost::long_long_type i)
  489. {
  490. bool neg = i < 0;
  491. *this = boost::multiprecision::detail::unsigned_abs(i);
  492. if (neg)
  493. mpfr_neg(m_data, m_data, GMP_RNDN);
  494. return *this;
  495. }
  496. #endif
  497. #endif
  498. mpfr_float_imp& operator=(unsigned long i)
  499. {
  500. mpfr_set_ui(m_data, i, GMP_RNDN);
  501. return *this;
  502. }
  503. mpfr_float_imp& operator=(long i)
  504. {
  505. mpfr_set_si(m_data, i, GMP_RNDN);
  506. return *this;
  507. }
  508. mpfr_float_imp& operator=(double d)
  509. {
  510. mpfr_set_d(m_data, d, GMP_RNDN);
  511. return *this;
  512. }
  513. mpfr_float_imp& operator=(long double a)
  514. {
  515. mpfr_set_ld(m_data, a, GMP_RNDN);
  516. return *this;
  517. }
  518. mpfr_float_imp& operator=(const char* s)
  519. {
  520. if (mpfr_set_str(m_data, s, 10, GMP_RNDN) != 0)
  521. {
  522. BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Unable to parse string \"") + s + std::string("\"as a valid floating point number.")));
  523. }
  524. return *this;
  525. }
  526. void swap(mpfr_float_imp& o) noexcept
  527. {
  528. // We have to swap by copying:
  529. mpfr_float_imp t(*this);
  530. *this = o;
  531. o = t;
  532. }
  533. std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
  534. {
  535. BOOST_ASSERT(m_data[0]._mpfr_d);
  536. bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
  537. bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
  538. std::streamsize org_digits(digits);
  539. if (scientific && digits)
  540. ++digits;
  541. std::string result;
  542. mp_exp_t e;
  543. if (mpfr_inf_p(m_data))
  544. {
  545. if (mpfr_sgn(m_data) < 0)
  546. result = "-inf";
  547. else if (f & std::ios_base::showpos)
  548. result = "+inf";
  549. else
  550. result = "inf";
  551. return result;
  552. }
  553. if (mpfr_nan_p(m_data))
  554. {
  555. result = "nan";
  556. return result;
  557. }
  558. if (mpfr_zero_p(m_data))
  559. {
  560. e = 0;
  561. result = "0";
  562. }
  563. else
  564. {
  565. char* ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data, GMP_RNDN);
  566. --e; // To match with what our formatter expects.
  567. if (fixed && e != -1)
  568. {
  569. // Oops we actually need a different number of digits to what we asked for:
  570. mpfr_free_str(ps);
  571. digits += e + 1;
  572. if (digits == 0)
  573. {
  574. // We need to get *all* the digits and then possibly round up,
  575. // we end up with either "0" or "1" as the result.
  576. ps = mpfr_get_str(0, &e, 10, 0, m_data, GMP_RNDN);
  577. --e;
  578. unsigned offset = *ps == '-' ? 1 : 0;
  579. if (ps[offset] > '5')
  580. {
  581. ++e;
  582. ps[offset] = '1';
  583. ps[offset + 1] = 0;
  584. }
  585. else if (ps[offset] == '5')
  586. {
  587. unsigned i = offset + 1;
  588. bool round_up = false;
  589. while (ps[i] != 0)
  590. {
  591. if (ps[i] != '0')
  592. {
  593. round_up = true;
  594. break;
  595. }
  596. }
  597. if (round_up)
  598. {
  599. ++e;
  600. ps[offset] = '1';
  601. ps[offset + 1] = 0;
  602. }
  603. else
  604. {
  605. ps[offset] = '0';
  606. ps[offset + 1] = 0;
  607. }
  608. }
  609. else
  610. {
  611. ps[offset] = '0';
  612. ps[offset + 1] = 0;
  613. }
  614. }
  615. else if (digits > 0)
  616. {
  617. ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data, GMP_RNDN);
  618. --e; // To match with what our formatter expects.
  619. }
  620. else
  621. {
  622. ps = mpfr_get_str(0, &e, 10, 1, m_data, GMP_RNDN);
  623. --e;
  624. unsigned offset = *ps == '-' ? 1 : 0;
  625. ps[offset] = '0';
  626. ps[offset + 1] = 0;
  627. }
  628. }
  629. result = ps ? ps : "0";
  630. if (ps)
  631. mpfr_free_str(ps);
  632. }
  633. boost::multiprecision::detail::format_float_string(result, e, org_digits, f, 0 != mpfr_zero_p(m_data));
  634. return result;
  635. }
  636. void negate() noexcept
  637. {
  638. mpfr_neg(m_data, m_data, GMP_RNDN);
  639. }
  640. template <mpfr_allocation_type AllocationType>
  641. int compare(const mpfr_float_backend<digits10, AllocationType>& o) const
  642. {
  643. return mpfr_cmp(m_data, o.m_data);
  644. }
  645. int compare(long i) const
  646. {
  647. return mpfr_cmp_si(m_data, i);
  648. }
  649. int compare(unsigned long i) const
  650. {
  651. return mpfr_cmp_ui(m_data, i);
  652. }
  653. int compare(double i) const
  654. {
  655. return mpfr_cmp_d(m_data, i);
  656. }
  657. int compare(long double i) const
  658. {
  659. return mpfr_cmp_ld(m_data, i);
  660. }
  661. template <class V>
  662. int compare(V v) const
  663. {
  664. mpfr_float_backend<digits10, allocate_stack> d;
  665. d = v;
  666. return compare(d);
  667. }
  668. mpfr_t& data() noexcept
  669. {
  670. return m_data;
  671. }
  672. const mpfr_t& data() const noexcept
  673. {
  674. return m_data;
  675. }
  676. protected:
  677. mpfr_t m_data;
  678. mp_limb_t m_buffer[limb_count];
  679. };
  680. #ifdef BOOST_MSVC
  681. #pragma warning(pop)
  682. #endif
  683. } // namespace detail
  684. template <unsigned digits10, mpfr_allocation_type AllocationType>
  685. struct mpfr_float_backend : public detail::mpfr_float_imp<digits10, AllocationType>
  686. {
  687. mpfr_float_backend() : detail::mpfr_float_imp<digits10, AllocationType>() {}
  688. mpfr_float_backend(const mpfr_float_backend& o) : detail::mpfr_float_imp<digits10, AllocationType>(o) {}
  689. // rvalue copy
  690. mpfr_float_backend(mpfr_float_backend&& o) noexcept : detail::mpfr_float_imp<digits10, AllocationType>(static_cast<detail::mpfr_float_imp<digits10, AllocationType>&&>(o))
  691. {}
  692. template <unsigned D, mpfr_allocation_type AT>
  693. mpfr_float_backend(const mpfr_float_backend<D, AT>& val, typename std::enable_if<D <= digits10>::type* = 0)
  694. : detail::mpfr_float_imp<digits10, AllocationType>()
  695. {
  696. mpfr_set(this->m_data, val.data(), GMP_RNDN);
  697. }
  698. template <unsigned D, mpfr_allocation_type AT>
  699. explicit mpfr_float_backend(const mpfr_float_backend<D, AT>& val, typename std::enable_if<!(D <= digits10)>::type* = 0)
  700. : detail::mpfr_float_imp<digits10, AllocationType>()
  701. {
  702. mpfr_set(this->m_data, val.data(), GMP_RNDN);
  703. }
  704. template <unsigned D>
  705. mpfr_float_backend(const gmp_float<D>& val, typename std::enable_if<D <= digits10>::type* = 0)
  706. : detail::mpfr_float_imp<digits10, AllocationType>()
  707. {
  708. mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
  709. }
  710. template <unsigned D>
  711. mpfr_float_backend(const gmp_float<D>& val, typename std::enable_if<!(D <= digits10)>::type* = 0)
  712. : detail::mpfr_float_imp<digits10, AllocationType>()
  713. {
  714. mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
  715. }
  716. mpfr_float_backend(const gmp_int& val)
  717. : detail::mpfr_float_imp<digits10, AllocationType>()
  718. {
  719. mpfr_set_z(this->m_data, val.data(), GMP_RNDN);
  720. }
  721. mpfr_float_backend(const gmp_rational& val)
  722. : detail::mpfr_float_imp<digits10, AllocationType>()
  723. {
  724. mpfr_set_q(this->m_data, val.data(), GMP_RNDN);
  725. }
  726. mpfr_float_backend(const mpfr_t val)
  727. : detail::mpfr_float_imp<digits10, AllocationType>()
  728. {
  729. mpfr_set(this->m_data, val, GMP_RNDN);
  730. }
  731. mpfr_float_backend(const mpf_t val)
  732. : detail::mpfr_float_imp<digits10, AllocationType>()
  733. {
  734. mpfr_set_f(this->m_data, val, GMP_RNDN);
  735. }
  736. mpfr_float_backend(const mpz_t val)
  737. : detail::mpfr_float_imp<digits10, AllocationType>()
  738. {
  739. mpfr_set_z(this->m_data, val, GMP_RNDN);
  740. }
  741. mpfr_float_backend(const mpq_t val)
  742. : detail::mpfr_float_imp<digits10, AllocationType>()
  743. {
  744. mpfr_set_q(this->m_data, val, GMP_RNDN);
  745. }
  746. // Construction with precision: we ignore the precision here.
  747. template <class V>
  748. mpfr_float_backend(const V& o, unsigned)
  749. {
  750. *this = o;
  751. }
  752. mpfr_float_backend& operator=(const mpfr_float_backend& o)
  753. {
  754. *static_cast<detail::mpfr_float_imp<digits10, AllocationType>*>(this) = static_cast<detail::mpfr_float_imp<digits10, AllocationType> const&>(o);
  755. return *this;
  756. }
  757. // rvalue assign
  758. mpfr_float_backend& operator=(mpfr_float_backend&& o) noexcept
  759. {
  760. *static_cast<detail::mpfr_float_imp<digits10, AllocationType>*>(this) = static_cast<detail::mpfr_float_imp<digits10, AllocationType>&&>(o);
  761. return *this;
  762. }
  763. template <class V>
  764. mpfr_float_backend& operator=(const V& v)
  765. {
  766. *static_cast<detail::mpfr_float_imp<digits10, AllocationType>*>(this) = v;
  767. return *this;
  768. }
  769. mpfr_float_backend& operator=(const mpfr_t val)
  770. {
  771. if (this->m_data[0]._mpfr_d == 0)
  772. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  773. mpfr_set(this->m_data, val, GMP_RNDN);
  774. return *this;
  775. }
  776. mpfr_float_backend& operator=(const mpf_t val)
  777. {
  778. if (this->m_data[0]._mpfr_d == 0)
  779. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  780. mpfr_set_f(this->m_data, val, GMP_RNDN);
  781. return *this;
  782. }
  783. mpfr_float_backend& operator=(const mpz_t val)
  784. {
  785. if (this->m_data[0]._mpfr_d == 0)
  786. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  787. mpfr_set_z(this->m_data, val, GMP_RNDN);
  788. return *this;
  789. }
  790. mpfr_float_backend& operator=(const mpq_t val)
  791. {
  792. if (this->m_data[0]._mpfr_d == 0)
  793. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  794. mpfr_set_q(this->m_data, val, GMP_RNDN);
  795. return *this;
  796. }
  797. // We don't change our precision here, this is a fixed precision type:
  798. template <unsigned D, mpfr_allocation_type AT>
  799. mpfr_float_backend& operator=(const mpfr_float_backend<D, AT>& val)
  800. {
  801. if (this->m_data[0]._mpfr_d == 0)
  802. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  803. mpfr_set(this->m_data, val.data(), GMP_RNDN);
  804. return *this;
  805. }
  806. template <unsigned D>
  807. mpfr_float_backend& operator=(const gmp_float<D>& val)
  808. {
  809. if (this->m_data[0]._mpfr_d == 0)
  810. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  811. mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
  812. return *this;
  813. }
  814. mpfr_float_backend& operator=(const gmp_int& val)
  815. {
  816. if (this->m_data[0]._mpfr_d == 0)
  817. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  818. mpfr_set_z(this->m_data, val.data(), GMP_RNDN);
  819. return *this;
  820. }
  821. mpfr_float_backend& operator=(const gmp_rational& val)
  822. {
  823. if (this->m_data[0]._mpfr_d == 0)
  824. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  825. mpfr_set_q(this->m_data, val.data(), GMP_RNDN);
  826. return *this;
  827. }
  828. };
  829. template <>
  830. struct mpfr_float_backend<0, allocate_dynamic> : public detail::mpfr_float_imp<0, allocate_dynamic>
  831. {
  832. mpfr_float_backend() : detail::mpfr_float_imp<0, allocate_dynamic>() {}
  833. mpfr_float_backend(const mpfr_t val)
  834. : detail::mpfr_float_imp<0, allocate_dynamic>((unsigned)mpfr_get_prec(val))
  835. {
  836. mpfr_set(this->m_data, val, GMP_RNDN);
  837. }
  838. mpfr_float_backend(const mpf_t val)
  839. : detail::mpfr_float_imp<0, allocate_dynamic>((unsigned)mpf_get_prec(val))
  840. {
  841. mpfr_set_f(this->m_data, val, GMP_RNDN);
  842. }
  843. mpfr_float_backend(const mpz_t val)
  844. : detail::mpfr_float_imp<0, allocate_dynamic>()
  845. {
  846. mpfr_set_z(this->m_data, val, GMP_RNDN);
  847. }
  848. mpfr_float_backend(const mpq_t val)
  849. : detail::mpfr_float_imp<0, allocate_dynamic>()
  850. {
  851. mpfr_set_q(this->m_data, val, GMP_RNDN);
  852. }
  853. mpfr_float_backend(const mpfr_float_backend& o) : detail::mpfr_float_imp<0, allocate_dynamic>(o) {}
  854. // rvalue copy
  855. mpfr_float_backend(mpfr_float_backend&& o) noexcept : detail::mpfr_float_imp<0, allocate_dynamic>(static_cast<detail::mpfr_float_imp<0, allocate_dynamic>&&>(o))
  856. {}
  857. template <class V>
  858. mpfr_float_backend(const V& o, unsigned digits10)
  859. : detail::mpfr_float_imp<0, allocate_dynamic>(multiprecision::detail::digits10_2_2(digits10))
  860. {
  861. *this = o;
  862. }
  863. #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
  864. mpfr_float_backend(const std::string_view& o, unsigned digits10)
  865. : detail::mpfr_float_imp<0, allocate_dynamic>(multiprecision::detail::digits10_2_2(digits10))
  866. {
  867. std::string s(o);
  868. *this = s.c_str();
  869. }
  870. #endif
  871. template <unsigned D>
  872. mpfr_float_backend(const gmp_float<D>& val, unsigned digits10)
  873. : detail::mpfr_float_imp<0, allocate_dynamic>(multiprecision::detail::digits10_2_2(digits10))
  874. {
  875. mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
  876. }
  877. template <unsigned D>
  878. mpfr_float_backend(const mpfr_float_backend<D>& val, unsigned digits10)
  879. : detail::mpfr_float_imp<0, allocate_dynamic>(multiprecision::detail::digits10_2_2(digits10))
  880. {
  881. mpfr_set(this->m_data, val.data(), GMP_RNDN);
  882. }
  883. template <unsigned D>
  884. mpfr_float_backend(const mpfr_float_backend<D>& val)
  885. : detail::mpfr_float_imp<0, allocate_dynamic>(mpfr_get_prec(val.data()))
  886. {
  887. mpfr_set(this->m_data, val.data(), GMP_RNDN);
  888. }
  889. template <unsigned D>
  890. mpfr_float_backend(const gmp_float<D>& val)
  891. : detail::mpfr_float_imp<0, allocate_dynamic>(mpf_get_prec(val.data()))
  892. {
  893. mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
  894. }
  895. mpfr_float_backend(const gmp_int& val)
  896. : detail::mpfr_float_imp<0, allocate_dynamic>()
  897. {
  898. mpfr_set_z(this->m_data, val.data(), GMP_RNDN);
  899. }
  900. mpfr_float_backend(const gmp_rational& val)
  901. : detail::mpfr_float_imp<0, allocate_dynamic>()
  902. {
  903. mpfr_set_q(this->m_data, val.data(), GMP_RNDN);
  904. }
  905. mpfr_float_backend& operator=(const mpfr_float_backend& o)
  906. {
  907. if (this != &o)
  908. {
  909. if (this->m_data[0]._mpfr_d == 0)
  910. mpfr_init2(this->m_data, mpfr_get_prec(o.data()));
  911. else
  912. detail::mpfr_copy_precision(this->m_data, o.data());
  913. mpfr_set(this->m_data, o.data(), GMP_RNDN);
  914. }
  915. return *this;
  916. }
  917. // rvalue assign
  918. mpfr_float_backend& operator=(mpfr_float_backend&& o) noexcept
  919. {
  920. *static_cast<detail::mpfr_float_imp<0, allocate_dynamic>*>(this) = static_cast<detail::mpfr_float_imp<0, allocate_dynamic>&&>(o);
  921. return *this;
  922. }
  923. template <class V>
  924. mpfr_float_backend& operator=(const V& v)
  925. {
  926. *static_cast<detail::mpfr_float_imp<0, allocate_dynamic>*>(this) = v;
  927. return *this;
  928. }
  929. mpfr_float_backend& operator=(const mpfr_t val)
  930. {
  931. if (this->m_data[0]._mpfr_d == 0)
  932. mpfr_init2(this->m_data, mpfr_get_prec(val));
  933. else
  934. mpfr_set_prec(this->m_data, mpfr_get_prec(val));
  935. mpfr_set(this->m_data, val, GMP_RNDN);
  936. return *this;
  937. }
  938. mpfr_float_backend& operator=(const mpf_t val)
  939. {
  940. if (this->m_data[0]._mpfr_d == 0)
  941. mpfr_init2(this->m_data, (mpfr_prec_t)mpf_get_prec(val));
  942. else
  943. mpfr_set_prec(this->m_data, (unsigned)mpf_get_prec(val));
  944. mpfr_set_f(this->m_data, val, GMP_RNDN);
  945. return *this;
  946. }
  947. mpfr_float_backend& operator=(const mpz_t val)
  948. {
  949. if (this->m_data[0]._mpfr_d == 0)
  950. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(get_default_precision()));
  951. mpfr_set_z(this->m_data, val, GMP_RNDN);
  952. return *this;
  953. }
  954. mpfr_float_backend& operator=(const mpq_t val)
  955. {
  956. if (this->m_data[0]._mpfr_d == 0)
  957. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(get_default_precision()));
  958. mpfr_set_q(this->m_data, val, GMP_RNDN);
  959. return *this;
  960. }
  961. template <unsigned D>
  962. mpfr_float_backend& operator=(const mpfr_float_backend<D>& val)
  963. {
  964. if (this->m_data[0]._mpfr_d == 0)
  965. mpfr_init2(this->m_data, mpfr_get_prec(val.data()));
  966. else
  967. mpfr_set_prec(this->m_data, mpfr_get_prec(val.data()));
  968. mpfr_set(this->m_data, val.data(), GMP_RNDN);
  969. return *this;
  970. }
  971. template <unsigned D>
  972. mpfr_float_backend& operator=(const gmp_float<D>& val)
  973. {
  974. if (this->m_data[0]._mpfr_d == 0)
  975. mpfr_init2(this->m_data, mpf_get_prec(val.data()));
  976. else
  977. mpfr_set_prec(this->m_data, mpf_get_prec(val.data()));
  978. mpfr_set_f(this->m_data, val.data(), GMP_RNDN);
  979. return *this;
  980. }
  981. mpfr_float_backend& operator=(const gmp_int& val)
  982. {
  983. if (this->m_data[0]._mpfr_d == 0)
  984. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(get_default_precision()));
  985. mpfr_set_z(this->m_data, val.data(), GMP_RNDN);
  986. return *this;
  987. }
  988. mpfr_float_backend& operator=(const gmp_rational& val)
  989. {
  990. if (this->m_data[0]._mpfr_d == 0)
  991. mpfr_init2(this->m_data, multiprecision::detail::digits10_2_2(get_default_precision()));
  992. mpfr_set_q(this->m_data, val.data(), GMP_RNDN);
  993. return *this;
  994. }
  995. static unsigned default_precision() noexcept
  996. {
  997. return get_default_precision();
  998. }
  999. static void default_precision(unsigned v) noexcept
  1000. {
  1001. get_default_precision() = v;
  1002. }
  1003. unsigned precision() const noexcept
  1004. {
  1005. return multiprecision::detail::digits2_2_10(mpfr_get_prec(this->m_data));
  1006. }
  1007. void precision(unsigned digits10) noexcept
  1008. {
  1009. mpfr_prec_round(this->m_data, multiprecision::detail::digits10_2_2((digits10)), GMP_RNDN);
  1010. }
  1011. };
  1012. template <unsigned digits10, mpfr_allocation_type AllocationType, class T>
  1013. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const mpfr_float_backend<digits10, AllocationType>& a, const T& b)
  1014. {
  1015. return a.compare(b) == 0;
  1016. }
  1017. template <unsigned digits10, mpfr_allocation_type AllocationType, class T>
  1018. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const mpfr_float_backend<digits10, AllocationType>& a, const T& b)
  1019. {
  1020. return a.compare(b) < 0;
  1021. }
  1022. template <unsigned digits10, mpfr_allocation_type AllocationType, class T>
  1023. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const mpfr_float_backend<digits10, AllocationType>& a, const T& b)
  1024. {
  1025. return a.compare(b) > 0;
  1026. }
  1027. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1028. inline bool eval_eq(const mpfr_float_backend<digits10, AllocationType>& a, const mpfr_float_backend<digits10, AllocationType>& b)noexcept
  1029. {
  1030. return mpfr_equal_p(a.data(), b.data());
  1031. }
  1032. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1033. inline bool eval_lt(const mpfr_float_backend<digits10, AllocationType>& a, const mpfr_float_backend<digits10, AllocationType>& b) noexcept
  1034. {
  1035. return mpfr_less_p(a.data(), b.data());
  1036. }
  1037. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1038. inline bool eval_gt(const mpfr_float_backend<digits10, AllocationType>& a, const mpfr_float_backend<digits10, AllocationType>& b) noexcept
  1039. {
  1040. return mpfr_greater_p(a.data(), b.data());
  1041. }
  1042. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1043. inline void eval_add(mpfr_float_backend<D1, A1>& result, const mpfr_float_backend<D2, A2>& o)
  1044. {
  1045. mpfr_add(result.data(), result.data(), o.data(), GMP_RNDN);
  1046. }
  1047. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1048. inline void eval_subtract(mpfr_float_backend<D1, A1>& result, const mpfr_float_backend<D2, A2>& o)
  1049. {
  1050. mpfr_sub(result.data(), result.data(), o.data(), GMP_RNDN);
  1051. }
  1052. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1053. inline void eval_multiply(mpfr_float_backend<D1, A1>& result, const mpfr_float_backend<D2, A2>& o)
  1054. {
  1055. if ((void*)&o == (void*)&result)
  1056. mpfr_sqr(result.data(), o.data(), GMP_RNDN);
  1057. else
  1058. mpfr_mul(result.data(), result.data(), o.data(), GMP_RNDN);
  1059. }
  1060. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1061. inline void eval_divide(mpfr_float_backend<D1, A1>& result, const mpfr_float_backend<D2, A2>& o)
  1062. {
  1063. mpfr_div(result.data(), result.data(), o.data(), GMP_RNDN);
  1064. }
  1065. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1066. inline void eval_add(mpfr_float_backend<digits10, AllocationType>& result, unsigned long i)
  1067. {
  1068. mpfr_add_ui(result.data(), result.data(), i, GMP_RNDN);
  1069. }
  1070. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1071. inline void eval_subtract(mpfr_float_backend<digits10, AllocationType>& result, unsigned long i)
  1072. {
  1073. mpfr_sub_ui(result.data(), result.data(), i, GMP_RNDN);
  1074. }
  1075. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1076. inline void eval_multiply(mpfr_float_backend<digits10, AllocationType>& result, unsigned long i)
  1077. {
  1078. mpfr_mul_ui(result.data(), result.data(), i, GMP_RNDN);
  1079. }
  1080. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1081. inline void eval_divide(mpfr_float_backend<digits10, AllocationType>& result, unsigned long i)
  1082. {
  1083. mpfr_div_ui(result.data(), result.data(), i, GMP_RNDN);
  1084. }
  1085. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1086. inline void eval_add(mpfr_float_backend<digits10, AllocationType>& result, long i)
  1087. {
  1088. if (i > 0)
  1089. mpfr_add_ui(result.data(), result.data(), i, GMP_RNDN);
  1090. else
  1091. mpfr_sub_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  1092. }
  1093. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1094. inline void eval_subtract(mpfr_float_backend<digits10, AllocationType>& result, long i)
  1095. {
  1096. if (i > 0)
  1097. mpfr_sub_ui(result.data(), result.data(), i, GMP_RNDN);
  1098. else
  1099. mpfr_add_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  1100. }
  1101. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1102. inline void eval_multiply(mpfr_float_backend<digits10, AllocationType>& result, long i)
  1103. {
  1104. mpfr_mul_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  1105. if (i < 0)
  1106. mpfr_neg(result.data(), result.data(), GMP_RNDN);
  1107. }
  1108. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1109. inline void eval_divide(mpfr_float_backend<digits10, AllocationType>& result, long i)
  1110. {
  1111. mpfr_div_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i), GMP_RNDN);
  1112. if (i < 0)
  1113. mpfr_neg(result.data(), result.data(), GMP_RNDN);
  1114. }
  1115. //
  1116. // Specialised 3 arg versions of the basic operators:
  1117. //
  1118. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2, unsigned D3, mpfr_allocation_type A3>
  1119. inline void eval_add(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, const mpfr_float_backend<D3, A3>& y)
  1120. {
  1121. mpfr_add(a.data(), x.data(), y.data(), GMP_RNDN);
  1122. }
  1123. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1124. inline void eval_add(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, unsigned long y)
  1125. {
  1126. mpfr_add_ui(a.data(), x.data(), y, GMP_RNDN);
  1127. }
  1128. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1129. inline void eval_add(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, long y)
  1130. {
  1131. if (y < 0)
  1132. mpfr_sub_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1133. else
  1134. mpfr_add_ui(a.data(), x.data(), y, GMP_RNDN);
  1135. }
  1136. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1137. inline void eval_add(mpfr_float_backend<D1, A1>& a, unsigned long x, const mpfr_float_backend<D2, A2>& y)
  1138. {
  1139. mpfr_add_ui(a.data(), y.data(), x, GMP_RNDN);
  1140. }
  1141. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1142. inline void eval_add(mpfr_float_backend<D1, A1>& a, long x, const mpfr_float_backend<D2, A2>& y)
  1143. {
  1144. if (x < 0)
  1145. {
  1146. mpfr_ui_sub(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data(), GMP_RNDN);
  1147. mpfr_neg(a.data(), a.data(), GMP_RNDN);
  1148. }
  1149. else
  1150. mpfr_add_ui(a.data(), y.data(), x, GMP_RNDN);
  1151. }
  1152. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2, unsigned D3, mpfr_allocation_type A3>
  1153. inline void eval_subtract(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, const mpfr_float_backend<D3, A3>& y)
  1154. {
  1155. mpfr_sub(a.data(), x.data(), y.data(), GMP_RNDN);
  1156. }
  1157. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1158. inline void eval_subtract(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, unsigned long y)
  1159. {
  1160. mpfr_sub_ui(a.data(), x.data(), y, GMP_RNDN);
  1161. }
  1162. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1163. inline void eval_subtract(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, long y)
  1164. {
  1165. if (y < 0)
  1166. mpfr_add_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1167. else
  1168. mpfr_sub_ui(a.data(), x.data(), y, GMP_RNDN);
  1169. }
  1170. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1171. inline void eval_subtract(mpfr_float_backend<D1, A1>& a, unsigned long x, const mpfr_float_backend<D2, A2>& y)
  1172. {
  1173. mpfr_ui_sub(a.data(), x, y.data(), GMP_RNDN);
  1174. }
  1175. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1176. inline void eval_subtract(mpfr_float_backend<D1, A1>& a, long x, const mpfr_float_backend<D2, A2>& y)
  1177. {
  1178. if (x < 0)
  1179. {
  1180. mpfr_add_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x), GMP_RNDN);
  1181. mpfr_neg(a.data(), a.data(), GMP_RNDN);
  1182. }
  1183. else
  1184. mpfr_ui_sub(a.data(), x, y.data(), GMP_RNDN);
  1185. }
  1186. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2, unsigned D3, mpfr_allocation_type A3>
  1187. inline void eval_multiply(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, const mpfr_float_backend<D3, A3>& y)
  1188. {
  1189. if ((void*)&x == (void*)&y)
  1190. mpfr_sqr(a.data(), x.data(), GMP_RNDN);
  1191. else
  1192. mpfr_mul(a.data(), x.data(), y.data(), GMP_RNDN);
  1193. }
  1194. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1195. inline void eval_multiply(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, unsigned long y)
  1196. {
  1197. mpfr_mul_ui(a.data(), x.data(), y, GMP_RNDN);
  1198. }
  1199. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1200. inline void eval_multiply(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, long y)
  1201. {
  1202. if (y < 0)
  1203. {
  1204. mpfr_mul_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1205. a.negate();
  1206. }
  1207. else
  1208. mpfr_mul_ui(a.data(), x.data(), y, GMP_RNDN);
  1209. }
  1210. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1211. inline void eval_multiply(mpfr_float_backend<D1, A1>& a, unsigned long x, const mpfr_float_backend<D2, A2>& y)
  1212. {
  1213. mpfr_mul_ui(a.data(), y.data(), x, GMP_RNDN);
  1214. }
  1215. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1216. inline void eval_multiply(mpfr_float_backend<D1, A1>& a, long x, const mpfr_float_backend<D2, A2>& y)
  1217. {
  1218. if (x < 0)
  1219. {
  1220. mpfr_mul_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x), GMP_RNDN);
  1221. mpfr_neg(a.data(), a.data(), GMP_RNDN);
  1222. }
  1223. else
  1224. mpfr_mul_ui(a.data(), y.data(), x, GMP_RNDN);
  1225. }
  1226. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2, unsigned D3, mpfr_allocation_type A3>
  1227. inline void eval_divide(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, const mpfr_float_backend<D3, A3>& y)
  1228. {
  1229. mpfr_div(a.data(), x.data(), y.data(), GMP_RNDN);
  1230. }
  1231. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1232. inline void eval_divide(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, unsigned long y)
  1233. {
  1234. mpfr_div_ui(a.data(), x.data(), y, GMP_RNDN);
  1235. }
  1236. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1237. inline void eval_divide(mpfr_float_backend<D1, A1>& a, const mpfr_float_backend<D2, A2>& x, long y)
  1238. {
  1239. if (y < 0)
  1240. {
  1241. mpfr_div_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y), GMP_RNDN);
  1242. a.negate();
  1243. }
  1244. else
  1245. mpfr_div_ui(a.data(), x.data(), y, GMP_RNDN);
  1246. }
  1247. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1248. inline void eval_divide(mpfr_float_backend<D1, A1>& a, unsigned long x, const mpfr_float_backend<D2, A2>& y)
  1249. {
  1250. mpfr_ui_div(a.data(), x, y.data(), GMP_RNDN);
  1251. }
  1252. template <unsigned D1, unsigned D2, mpfr_allocation_type A1, mpfr_allocation_type A2>
  1253. inline void eval_divide(mpfr_float_backend<D1, A1>& a, long x, const mpfr_float_backend<D2, A2>& y)
  1254. {
  1255. if (x < 0)
  1256. {
  1257. mpfr_ui_div(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data(), GMP_RNDN);
  1258. mpfr_neg(a.data(), a.data(), GMP_RNDN);
  1259. }
  1260. else
  1261. mpfr_ui_div(a.data(), x, y.data(), GMP_RNDN);
  1262. }
  1263. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1264. inline bool eval_is_zero(const mpfr_float_backend<digits10, AllocationType>& val) noexcept
  1265. {
  1266. return 0 != mpfr_zero_p(val.data());
  1267. }
  1268. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1269. inline int eval_get_sign(const mpfr_float_backend<digits10, AllocationType>& val) noexcept
  1270. {
  1271. return mpfr_sgn(val.data());
  1272. }
  1273. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1274. inline void eval_convert_to(unsigned long* result, const mpfr_float_backend<digits10, AllocationType>& val)
  1275. {
  1276. if (mpfr_nan_p(val.data()))
  1277. {
  1278. BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
  1279. }
  1280. *result = mpfr_get_ui(val.data(), GMP_RNDZ);
  1281. }
  1282. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1283. inline void eval_convert_to(long* result, const mpfr_float_backend<digits10, AllocationType>& val)
  1284. {
  1285. if (mpfr_nan_p(val.data()))
  1286. {
  1287. BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
  1288. }
  1289. *result = mpfr_get_si(val.data(), GMP_RNDZ);
  1290. }
  1291. #ifdef _MPFR_H_HAVE_INTMAX_T
  1292. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1293. inline void eval_convert_to(boost::ulong_long_type* result, const mpfr_float_backend<digits10, AllocationType>& val)
  1294. {
  1295. if (mpfr_nan_p(val.data()))
  1296. {
  1297. BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
  1298. }
  1299. *result = mpfr_get_uj(val.data(), GMP_RNDZ);
  1300. }
  1301. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1302. inline void eval_convert_to(boost::long_long_type* result, const mpfr_float_backend<digits10, AllocationType>& val)
  1303. {
  1304. if (mpfr_nan_p(val.data()))
  1305. {
  1306. BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
  1307. }
  1308. *result = mpfr_get_sj(val.data(), GMP_RNDZ);
  1309. }
  1310. #endif
  1311. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1312. inline void eval_convert_to(float* result, const mpfr_float_backend<digits10, AllocationType>& val) noexcept
  1313. {
  1314. *result = mpfr_get_flt(val.data(), GMP_RNDN);
  1315. }
  1316. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1317. inline void eval_convert_to(double* result, const mpfr_float_backend<digits10, AllocationType>& val) noexcept
  1318. {
  1319. *result = mpfr_get_d(val.data(), GMP_RNDN);
  1320. }
  1321. template <unsigned digits10, mpfr_allocation_type AllocationType>
  1322. inline void eval_convert_to(long double* result, const mpfr_float_backend<digits10, AllocationType>& val) noexcept
  1323. {
  1324. *result = mpfr_get_ld(val.data(), GMP_RNDN);
  1325. }
  1326. //
  1327. // Native non-member operations:
  1328. //
  1329. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1330. inline void eval_sqrt(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val)
  1331. {
  1332. mpfr_sqrt(result.data(), val.data(), GMP_RNDN);
  1333. }
  1334. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1335. inline void eval_abs(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val)
  1336. {
  1337. mpfr_abs(result.data(), val.data(), GMP_RNDN);
  1338. }
  1339. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1340. inline void eval_fabs(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val)
  1341. {
  1342. mpfr_abs(result.data(), val.data(), GMP_RNDN);
  1343. }
  1344. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1345. inline void eval_ceil(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val)
  1346. {
  1347. mpfr_ceil(result.data(), val.data());
  1348. }
  1349. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1350. inline void eval_floor(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val)
  1351. {
  1352. mpfr_floor(result.data(), val.data());
  1353. }
  1354. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1355. inline void eval_trunc(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val)
  1356. {
  1357. mpfr_trunc(result.data(), val.data());
  1358. }
  1359. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1360. inline void eval_ldexp(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val, long e)
  1361. {
  1362. if (e > 0)
  1363. mpfr_mul_2exp(result.data(), val.data(), e, GMP_RNDN);
  1364. else if (e < 0)
  1365. mpfr_div_2exp(result.data(), val.data(), -e, GMP_RNDN);
  1366. else
  1367. result = val;
  1368. }
  1369. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1370. inline void eval_frexp(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val, int* e)
  1371. {
  1372. if (mpfr_zero_p(val.data()))
  1373. {
  1374. *e = 0;
  1375. result = val;
  1376. return;
  1377. }
  1378. mp_exp_t v = mpfr_get_exp(val.data());
  1379. *e = v;
  1380. if (v)
  1381. eval_ldexp(result, val, -v);
  1382. else
  1383. result = val;
  1384. }
  1385. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1386. inline void eval_frexp(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& val, long* e)
  1387. {
  1388. if (mpfr_zero_p(val.data()))
  1389. {
  1390. *e = 0;
  1391. result = val;
  1392. return;
  1393. }
  1394. mp_exp_t v = mpfr_get_exp(val.data());
  1395. *e = v;
  1396. if(v)
  1397. eval_ldexp(result, val, -v);
  1398. else
  1399. result = val;
  1400. }
  1401. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1402. inline int eval_fpclassify(const mpfr_float_backend<Digits10, AllocateType>& val) noexcept
  1403. {
  1404. return mpfr_inf_p(val.data()) ? FP_INFINITE : mpfr_nan_p(val.data()) ? FP_NAN : mpfr_zero_p(val.data()) ? FP_ZERO : FP_NORMAL;
  1405. }
  1406. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1407. inline void eval_pow(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& b, const mpfr_float_backend<Digits10, AllocateType>& e)
  1408. {
  1409. if (mpfr_zero_p(b.data()) && mpfr_integer_p(e.data()) && (mpfr_signbit(e.data()) == 0) && mpfr_fits_ulong_p(e.data(), GMP_RNDN) && (mpfr_get_ui(e.data(), GMP_RNDN) & 1))
  1410. {
  1411. mpfr_set(result.data(), b.data(), GMP_RNDN);
  1412. }
  1413. else
  1414. mpfr_pow(result.data(), b.data(), e.data(), GMP_RNDN);
  1415. }
  1416. #ifdef BOOST_MSVC
  1417. //
  1418. // The enable_if usage below doesn't work with msvc - but only when
  1419. // certain other enable_if usages are defined first. It's a capricious
  1420. // and rather annoying compiler bug in other words....
  1421. //
  1422. #define BOOST_MP_ENABLE_IF_WORKAROUND (Digits10 || !Digits10)&&
  1423. #else
  1424. #define BOOST_MP_ENABLE_IF_WORKAROUND
  1425. #endif
  1426. template <unsigned Digits10, mpfr_allocation_type AllocateType, class Integer>
  1427. inline typename std::enable_if<boost::multiprecision::detail::is_signed<Integer>::value && boost::multiprecision::detail::is_integral<Integer>::value && (BOOST_MP_ENABLE_IF_WORKAROUND(sizeof(Integer) <= sizeof(long)))>::type
  1428. eval_pow(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& b, const Integer& e)
  1429. {
  1430. mpfr_pow_si(result.data(), b.data(), e, GMP_RNDN);
  1431. }
  1432. template <unsigned Digits10, mpfr_allocation_type AllocateType, class Integer>
  1433. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<Integer>::value && (BOOST_MP_ENABLE_IF_WORKAROUND(sizeof(Integer) <= sizeof(long)))>::type
  1434. eval_pow(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& b, const Integer& e)
  1435. {
  1436. mpfr_pow_ui(result.data(), b.data(), e, GMP_RNDN);
  1437. }
  1438. #undef BOOST_MP_ENABLE_IF_WORKAROUND
  1439. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1440. inline void eval_exp(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1441. {
  1442. mpfr_exp(result.data(), arg.data(), GMP_RNDN);
  1443. }
  1444. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1445. inline void eval_exp2(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1446. {
  1447. mpfr_exp2(result.data(), arg.data(), GMP_RNDN);
  1448. }
  1449. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1450. inline void eval_log(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1451. {
  1452. mpfr_log(result.data(), arg.data(), GMP_RNDN);
  1453. }
  1454. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1455. inline void eval_log10(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1456. {
  1457. mpfr_log10(result.data(), arg.data(), GMP_RNDN);
  1458. }
  1459. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1460. inline void eval_sin(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1461. {
  1462. mpfr_sin(result.data(), arg.data(), GMP_RNDN);
  1463. }
  1464. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1465. inline void eval_cos(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1466. {
  1467. mpfr_cos(result.data(), arg.data(), GMP_RNDN);
  1468. }
  1469. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1470. inline void eval_tan(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1471. {
  1472. mpfr_tan(result.data(), arg.data(), GMP_RNDN);
  1473. }
  1474. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1475. inline void eval_asin(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1476. {
  1477. mpfr_asin(result.data(), arg.data(), GMP_RNDN);
  1478. }
  1479. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1480. inline void eval_acos(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1481. {
  1482. mpfr_acos(result.data(), arg.data(), GMP_RNDN);
  1483. }
  1484. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1485. inline void eval_atan(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1486. {
  1487. mpfr_atan(result.data(), arg.data(), GMP_RNDN);
  1488. }
  1489. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1490. inline void eval_atan2(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg1, const mpfr_float_backend<Digits10, AllocateType>& arg2)
  1491. {
  1492. mpfr_atan2(result.data(), arg1.data(), arg2.data(), GMP_RNDN);
  1493. }
  1494. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1495. inline void eval_sinh(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1496. {
  1497. mpfr_sinh(result.data(), arg.data(), GMP_RNDN);
  1498. }
  1499. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1500. inline void eval_cosh(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1501. {
  1502. mpfr_cosh(result.data(), arg.data(), GMP_RNDN);
  1503. }
  1504. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1505. inline void eval_tanh(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1506. {
  1507. mpfr_tanh(result.data(), arg.data(), GMP_RNDN);
  1508. }
  1509. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1510. inline void eval_log2(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg)
  1511. {
  1512. mpfr_log2(result.data(), arg.data(), GMP_RNDN);
  1513. }
  1514. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1515. inline void eval_modf(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& arg, mpfr_float_backend<Digits10, AllocateType>* pipart)
  1516. {
  1517. if (0 == pipart)
  1518. {
  1519. mpfr_float_backend<Digits10, AllocateType> ipart;
  1520. mpfr_modf(ipart.data(), result.data(), arg.data(), GMP_RNDN);
  1521. }
  1522. else
  1523. {
  1524. mpfr_modf(pipart->data(), result.data(), arg.data(), GMP_RNDN);
  1525. }
  1526. }
  1527. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1528. inline void eval_remainder(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& a, const mpfr_float_backend<Digits10, AllocateType>& b)
  1529. {
  1530. mpfr_remainder(result.data(), a.data(), b.data(), GMP_RNDN);
  1531. }
  1532. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1533. inline void eval_remquo(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& a, const mpfr_float_backend<Digits10, AllocateType>& b, int* pi)
  1534. {
  1535. long l;
  1536. mpfr_remquo(result.data(), &l, a.data(), b.data(), GMP_RNDN);
  1537. if (pi)
  1538. *pi = l;
  1539. }
  1540. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1541. inline void eval_fmod(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& a, const mpfr_float_backend<Digits10, AllocateType>& b)
  1542. {
  1543. mpfr_fmod(result.data(), a.data(), b.data(), GMP_RNDN);
  1544. }
  1545. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1546. inline void eval_multiply_add(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& a, const mpfr_float_backend<Digits10, AllocateType>& b)
  1547. {
  1548. mpfr_fma(result.data(), a.data(), b.data(), result.data(), GMP_RNDN);
  1549. }
  1550. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1551. inline void eval_multiply_add(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& a, const mpfr_float_backend<Digits10, AllocateType>& b, const mpfr_float_backend<Digits10, AllocateType>& c)
  1552. {
  1553. mpfr_fma(result.data(), a.data(), b.data(), c.data(), GMP_RNDN);
  1554. }
  1555. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1556. inline void eval_multiply_subtract(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& a, const mpfr_float_backend<Digits10, AllocateType>& b)
  1557. {
  1558. mpfr_fms(result.data(), a.data(), b.data(), result.data(), GMP_RNDN);
  1559. result.negate();
  1560. }
  1561. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1562. inline void eval_multiply_subtract(mpfr_float_backend<Digits10, AllocateType>& result, const mpfr_float_backend<Digits10, AllocateType>& a, const mpfr_float_backend<Digits10, AllocateType>& b, const mpfr_float_backend<Digits10, AllocateType>& c)
  1563. {
  1564. mpfr_fms(result.data(), a.data(), b.data(), c.data(), GMP_RNDN);
  1565. }
  1566. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1567. inline int eval_signbit BOOST_PREVENT_MACRO_SUBSTITUTION(const mpfr_float_backend<Digits10, AllocateType>& arg)
  1568. {
  1569. return (arg.data()[0]._mpfr_sign < 0) ? 1 : 0;
  1570. }
  1571. template <unsigned Digits10, mpfr_allocation_type AllocateType>
  1572. inline std::size_t hash_value(const mpfr_float_backend<Digits10, AllocateType>& val)
  1573. {
  1574. std::size_t result = 0;
  1575. std::size_t len = val.data()[0]._mpfr_prec / mp_bits_per_limb;
  1576. if (val.data()[0]._mpfr_prec % mp_bits_per_limb)
  1577. ++len;
  1578. for (std::size_t i = 0; i < len; ++i)
  1579. boost::hash_combine(result, val.data()[0]._mpfr_d[i]);
  1580. boost::hash_combine(result, val.data()[0]._mpfr_exp);
  1581. boost::hash_combine(result, val.data()[0]._mpfr_sign);
  1582. return result;
  1583. }
  1584. } // namespace backends
  1585. namespace detail {
  1586. template <>
  1587. struct is_variable_precision<backends::mpfr_float_backend<0> > : public std::integral_constant<bool, true>
  1588. {};
  1589. } // namespace detail
  1590. template <>
  1591. struct number_category<detail::canonical<mpfr_t, backends::mpfr_float_backend<0> >::type> : public std::integral_constant<int, number_kind_floating_point>
  1592. {};
  1593. template <unsigned D, boost::multiprecision::mpfr_allocation_type A1, boost::multiprecision::mpfr_allocation_type A2>
  1594. struct is_equivalent_number_type<backends::mpfr_float_backend<D, A1>, backends::mpfr_float_backend<D, A2> > : public std::integral_constant<bool, true> {};
  1595. using boost::multiprecision::backends::mpfr_float_backend;
  1596. using mpfr_float_50 = number<mpfr_float_backend<50> > ;
  1597. using mpfr_float_100 = number<mpfr_float_backend<100> > ;
  1598. using mpfr_float_500 = number<mpfr_float_backend<500> > ;
  1599. using mpfr_float_1000 = number<mpfr_float_backend<1000> >;
  1600. using mpfr_float = number<mpfr_float_backend<0> > ;
  1601. using static_mpfr_float_50 = number<mpfr_float_backend<50, allocate_stack> > ;
  1602. using static_mpfr_float_100 = number<mpfr_float_backend<100, allocate_stack> >;
  1603. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1604. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& a, const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& b)
  1605. {
  1606. return (boost::multiprecision::signbit)(a) != (boost::multiprecision::signbit)(b) ? boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>(-a) : a;
  1607. }
  1608. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1609. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates> copysign BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates>& a, const boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates>& b)
  1610. {
  1611. return (boost::multiprecision::signbit)(a) != (boost::multiprecision::signbit)(b) ? boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates>(-a) : a;
  1612. }
  1613. } // namespace multiprecision
  1614. namespace math {
  1615. using boost::multiprecision::copysign;
  1616. using boost::multiprecision::signbit;
  1617. namespace tools {
  1618. inline void set_output_precision(const boost::multiprecision::mpfr_float& val, std::ostream& os)
  1619. {
  1620. os << std::setprecision(val.precision());
  1621. }
  1622. template <>
  1623. inline int digits<boost::multiprecision::mpfr_float>()
  1624. #ifdef BOOST_MATH_NOEXCEPT
  1625. noexcept
  1626. #endif
  1627. {
  1628. return multiprecision::detail::digits10_2_2(boost::multiprecision::mpfr_float::default_precision());
  1629. }
  1630. template <>
  1631. inline int digits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, boost::multiprecision::et_off> >()
  1632. #ifdef BOOST_MATH_NOEXCEPT
  1633. noexcept
  1634. #endif
  1635. {
  1636. return multiprecision::detail::digits10_2_2(boost::multiprecision::mpfr_float::default_precision());
  1637. }
  1638. template <>
  1639. inline boost::multiprecision::mpfr_float
  1640. max_value<boost::multiprecision::mpfr_float>()
  1641. {
  1642. boost::multiprecision::mpfr_float result(0.5);
  1643. mpfr_mul_2exp(result.backend().data(), result.backend().data(), mpfr_get_emax(), GMP_RNDN);
  1644. BOOST_ASSERT(mpfr_number_p(result.backend().data()));
  1645. return result;
  1646. }
  1647. template <>
  1648. inline boost::multiprecision::mpfr_float
  1649. min_value<boost::multiprecision::mpfr_float>()
  1650. {
  1651. boost::multiprecision::mpfr_float result(0.5);
  1652. mpfr_div_2exp(result.backend().data(), result.backend().data(), -mpfr_get_emin(), GMP_RNDN);
  1653. BOOST_ASSERT(mpfr_number_p(result.backend().data()));
  1654. return result;
  1655. }
  1656. template <>
  1657. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, boost::multiprecision::et_off>
  1658. max_value<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, boost::multiprecision::et_off> >()
  1659. {
  1660. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, boost::multiprecision::et_off> result(0.5);
  1661. mpfr_mul_2exp(result.backend().data(), result.backend().data(), mpfr_get_emax(), GMP_RNDN);
  1662. BOOST_ASSERT(mpfr_number_p(result.backend().data()));
  1663. return result;
  1664. }
  1665. template <>
  1666. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, boost::multiprecision::et_off>
  1667. min_value<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, boost::multiprecision::et_off> >()
  1668. {
  1669. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, boost::multiprecision::et_off> result(0.5);
  1670. mpfr_div_2exp(result.backend().data(), result.backend().data(), -mpfr_get_emin(), GMP_RNDN);
  1671. BOOST_ASSERT(mpfr_number_p(result.backend().data()));
  1672. return result;
  1673. }
  1674. template <>
  1675. inline int digits<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float::backend_type> > >()
  1676. #ifdef BOOST_MATH_NOEXCEPT
  1677. noexcept
  1678. #endif
  1679. {
  1680. return multiprecision::detail::digits10_2_2(boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float::backend_type> >::default_precision());
  1681. }
  1682. template <>
  1683. inline int digits<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<0> >, boost::multiprecision::et_off> >()
  1684. #ifdef BOOST_MATH_NOEXCEPT
  1685. noexcept
  1686. #endif
  1687. {
  1688. return multiprecision::detail::digits10_2_2(boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float::backend_type> >::default_precision());
  1689. }
  1690. template <>
  1691. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float::backend_type> >
  1692. max_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float::backend_type> > >()
  1693. {
  1694. return max_value<boost::multiprecision::mpfr_float>().backend();
  1695. }
  1696. template <>
  1697. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float::backend_type> >
  1698. min_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float::backend_type> > >()
  1699. {
  1700. return min_value<boost::multiprecision::mpfr_float>().backend();
  1701. }
  1702. template <>
  1703. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<0> >, boost::multiprecision::et_off>
  1704. max_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<0> >, boost::multiprecision::et_off> >()
  1705. {
  1706. return max_value<boost::multiprecision::mpfr_float>().backend();
  1707. }
  1708. template <>
  1709. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<0> >, boost::multiprecision::et_off>
  1710. min_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<0> >, boost::multiprecision::et_off> >()
  1711. {
  1712. return min_value<boost::multiprecision::mpfr_float>().backend();
  1713. }
  1714. } // namespace tools
  1715. namespace constants { namespace detail {
  1716. template <class T>
  1717. struct constant_pi;
  1718. template <class T>
  1719. struct constant_ln_two;
  1720. template <class T>
  1721. struct constant_euler;
  1722. template <class T>
  1723. struct constant_catalan;
  1724. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1725. struct constant_pi<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >
  1726. {
  1727. using result_type = boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>;
  1728. template <int N>
  1729. static inline const result_type& get(const std::integral_constant<int, N>&)
  1730. {
  1731. static result_type result;
  1732. static bool has_init = false;
  1733. if (!has_init)
  1734. {
  1735. mpfr_const_pi(result.backend().data(), GMP_RNDN);
  1736. has_init = true;
  1737. }
  1738. return result;
  1739. }
  1740. static inline const result_type get(const std::integral_constant<int, 0>&)
  1741. {
  1742. result_type result;
  1743. mpfr_const_pi(result.backend().data(), GMP_RNDN);
  1744. return result;
  1745. }
  1746. };
  1747. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1748. struct constant_ln_two<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >
  1749. {
  1750. using result_type = boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>;
  1751. template <int N>
  1752. static inline const result_type& get(const std::integral_constant<int, N>&)
  1753. {
  1754. static result_type result;
  1755. static bool init = false;
  1756. if (!init)
  1757. {
  1758. mpfr_const_log2(result.backend().data(), GMP_RNDN);
  1759. init = true;
  1760. }
  1761. return result;
  1762. }
  1763. static inline const result_type get(const std::integral_constant<int, 0>&)
  1764. {
  1765. result_type result;
  1766. mpfr_const_log2(result.backend().data(), GMP_RNDN);
  1767. return result;
  1768. }
  1769. };
  1770. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1771. struct constant_euler<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >
  1772. {
  1773. using result_type = boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>;
  1774. template <int N>
  1775. static inline const result_type& get(const std::integral_constant<int, N>&)
  1776. {
  1777. static result_type result;
  1778. static bool init = false;
  1779. if (!init)
  1780. {
  1781. mpfr_const_euler(result.backend().data(), GMP_RNDN);
  1782. init = true;
  1783. }
  1784. return result;
  1785. }
  1786. static inline const result_type get(const std::integral_constant<int, 0>&)
  1787. {
  1788. result_type result;
  1789. mpfr_const_euler(result.backend().data(), GMP_RNDN);
  1790. return result;
  1791. }
  1792. };
  1793. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1794. struct constant_catalan<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >
  1795. {
  1796. using result_type = boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>;
  1797. template <int N>
  1798. static inline const result_type& get(const std::integral_constant<int, N>&)
  1799. {
  1800. static result_type result;
  1801. static bool init = false;
  1802. if (!init)
  1803. {
  1804. mpfr_const_catalan(result.backend().data(), GMP_RNDN);
  1805. init = true;
  1806. }
  1807. return result;
  1808. }
  1809. static inline const result_type get(const std::integral_constant<int, 0>&)
  1810. {
  1811. result_type result;
  1812. mpfr_const_catalan(result.backend().data(), GMP_RNDN);
  1813. return result;
  1814. }
  1815. };
  1816. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1817. struct constant_pi<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates> >
  1818. {
  1819. using result_type = boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates>;
  1820. template <int N>
  1821. static inline const result_type& get(const std::integral_constant<int, N>&)
  1822. {
  1823. static result_type result;
  1824. static bool has_init = false;
  1825. if (!has_init)
  1826. {
  1827. mpfr_const_pi(result.backend().value().data(), GMP_RNDN);
  1828. has_init = true;
  1829. }
  1830. return result;
  1831. }
  1832. static inline const result_type get(const std::integral_constant<int, 0>&)
  1833. {
  1834. result_type result;
  1835. mpfr_const_pi(result.backend().value().data(), GMP_RNDN);
  1836. return result;
  1837. }
  1838. };
  1839. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1840. struct constant_ln_two<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates> >
  1841. {
  1842. using result_type = boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates>;
  1843. template <int N>
  1844. static inline const result_type& get(const std::integral_constant<int, N>&)
  1845. {
  1846. static result_type result;
  1847. static bool init = false;
  1848. if (!init)
  1849. {
  1850. mpfr_const_log2(result.backend().value().data(), GMP_RNDN);
  1851. init = true;
  1852. }
  1853. return result;
  1854. }
  1855. static inline const result_type get(const std::integral_constant<int, 0>&)
  1856. {
  1857. result_type result;
  1858. mpfr_const_log2(result.backend().value().data(), GMP_RNDN);
  1859. return result;
  1860. }
  1861. };
  1862. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1863. struct constant_euler<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates> >
  1864. {
  1865. using result_type = boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates>;
  1866. template <int N>
  1867. static inline const result_type& get(const std::integral_constant<int, N>&)
  1868. {
  1869. static result_type result;
  1870. static bool init = false;
  1871. if (!init)
  1872. {
  1873. mpfr_const_euler(result.backend().value().data(), GMP_RNDN);
  1874. init = true;
  1875. }
  1876. return result;
  1877. }
  1878. static inline const result_type get(const std::integral_constant<int, 0>&)
  1879. {
  1880. result_type result;
  1881. mpfr_const_euler(result.backend().value().data(), GMP_RNDN);
  1882. return result;
  1883. }
  1884. };
  1885. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1886. struct constant_catalan<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates> >
  1887. {
  1888. using result_type = boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType> >, ExpressionTemplates>;
  1889. template <int N>
  1890. static inline const result_type& get(const std::integral_constant<int, N>&)
  1891. {
  1892. static result_type result;
  1893. static bool init = false;
  1894. if (!init)
  1895. {
  1896. mpfr_const_catalan(result.backend().value().data(), GMP_RNDN);
  1897. init = true;
  1898. }
  1899. return result;
  1900. }
  1901. static inline const result_type get(const std::integral_constant<int, 0>&)
  1902. {
  1903. result_type result;
  1904. mpfr_const_catalan(result.backend().value().data(), GMP_RNDN);
  1905. return result;
  1906. }
  1907. };
  1908. }} // namespace constants::detail
  1909. } // namespace math
  1910. namespace multiprecision {
  1911. //
  1912. // Overloaded special functions which call native mpfr routines:
  1913. //
  1914. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1915. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> asinh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1916. {
  1917. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1918. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1919. mpfr_asinh(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1920. return result;
  1921. }
  1922. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1923. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> acosh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1924. {
  1925. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1926. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1927. mpfr_acosh(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1928. return result;
  1929. }
  1930. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1931. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> atanh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1932. {
  1933. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1934. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1935. mpfr_atanh(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1936. return result;
  1937. }
  1938. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1939. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1940. {
  1941. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1942. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1943. mpfr_cbrt(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1944. return result;
  1945. }
  1946. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1947. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1948. {
  1949. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1950. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1951. mpfr_erf(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1952. return result;
  1953. }
  1954. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1955. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1956. {
  1957. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1958. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1959. mpfr_erfc(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1960. return result;
  1961. }
  1962. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1963. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1964. {
  1965. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1966. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1967. mpfr_expm1(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1968. return result;
  1969. }
  1970. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1971. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1972. {
  1973. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1974. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1975. mpfr_lngamma(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1976. return result;
  1977. }
  1978. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1979. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1980. {
  1981. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1982. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1983. mpfr_gamma(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1984. return result;
  1985. }
  1986. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  1987. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  1988. {
  1989. boost::multiprecision::detail::scoped_default_precision<number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  1990. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  1991. mpfr_log1p(result.backend().data(), arg.backend().data(), GMP_RNDN);
  1992. return result;
  1993. }
  1994. } // namespace multiprecision
  1995. namespace math {
  1996. //
  1997. // Overloaded special functions which call native mpfr routines:
  1998. //
  1999. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2000. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> asinh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy&)
  2001. {
  2002. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2003. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2004. mpfr_asinh(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2005. if (mpfr_inf_p(result.backend().data()))
  2006. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("asinh<%1%>(%1%)", 0, Policy());
  2007. if (mpfr_nan_p(result.backend().data()))
  2008. return policies::raise_evaluation_error("asinh<%1%>(%1%)", "Unknown error, result is a NaN", result, Policy());
  2009. return result;
  2010. }
  2011. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2012. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> asinh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2013. {
  2014. return asinh(arg, policies::policy<>());
  2015. }
  2016. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2017. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> acosh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy&)
  2018. {
  2019. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2020. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2021. mpfr_acosh(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2022. if (mpfr_inf_p(result.backend().data()))
  2023. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("acosh<%1%>(%1%)", 0, Policy());
  2024. if (mpfr_nan_p(result.backend().data()))
  2025. return policies::raise_evaluation_error("acosh<%1%>(%1%)", "Unknown error, result is a NaN", result, Policy());
  2026. return result;
  2027. }
  2028. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2029. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> acosh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2030. {
  2031. return acosh(arg, policies::policy<>());
  2032. }
  2033. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2034. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> atanh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy& )
  2035. {
  2036. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2037. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2038. mpfr_atanh(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2039. if (mpfr_inf_p(result.backend().data()))
  2040. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("atanh<%1%>(%1%)", 0, Policy());
  2041. if (mpfr_nan_p(result.backend().data()))
  2042. return policies::raise_evaluation_error("atanh<%1%>(%1%)", "Unknown error, result is a NaN", result, Policy());
  2043. return result;
  2044. }
  2045. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2046. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> atanh BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2047. {
  2048. return atanh(arg, policies::policy<>());
  2049. }
  2050. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2051. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy&)
  2052. {
  2053. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2054. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2055. mpfr_cbrt(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2056. if (mpfr_inf_p(result.backend().data()))
  2057. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("cbrt<%1%>(%1%)", 0, Policy());
  2058. if (mpfr_nan_p(result.backend().data()))
  2059. return policies::raise_evaluation_error("cbrt<%1%>(%1%)", "Unknown error, result is a NaN", result, Policy());
  2060. return result;
  2061. }
  2062. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2063. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> cbrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2064. {
  2065. return cbrt(arg, policies::policy<>());
  2066. }
  2067. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2068. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy& pol)
  2069. {
  2070. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2071. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2072. mpfr_erf(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2073. if (mpfr_inf_p(result.backend().data()))
  2074. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("erf<%1%>(%1%)", 0, pol);
  2075. if (mpfr_nan_p(result.backend().data()))
  2076. return policies::raise_evaluation_error("erf<%1%>(%1%)", "Unknown error, result is a NaN", result, pol);
  2077. return result;
  2078. }
  2079. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2080. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> erf BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2081. {
  2082. return erf(arg, policies::policy<>());
  2083. }
  2084. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2085. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy& pol)
  2086. {
  2087. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2088. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2089. mpfr_erfc(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2090. if (mpfr_inf_p(result.backend().data()))
  2091. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("erfc<%1%>(%1%)", 0, pol);
  2092. if (mpfr_nan_p(result.backend().data()))
  2093. return policies::raise_evaluation_error("erfc<%1%>(%1%)", "Unknown error, result is a NaN", result, pol);
  2094. return result;
  2095. }
  2096. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2097. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> erfc BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2098. {
  2099. return erfc(arg, policies::policy<>());
  2100. }
  2101. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2102. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> expm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy& pol)
  2103. {
  2104. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2105. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2106. mpfr_expm1(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2107. if (mpfr_inf_p(result.backend().data()))
  2108. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("expm1<%1%>(%1%)", 0, pol);
  2109. if (mpfr_nan_p(result.backend().data()))
  2110. return policies::raise_evaluation_error("expm1<%1%>(%1%)", "Unknown error, result is a NaN", result, pol);
  2111. return result;
  2112. }
  2113. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2114. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> exm1 BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2115. {
  2116. return expm1(arg, policies::policy<>());
  2117. }
  2118. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2119. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> arg, int* sign, const Policy& pol)
  2120. {
  2121. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2122. (void)precision_guard; // warning suppression
  2123. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2124. if (arg > 0)
  2125. {
  2126. mpfr_lngamma(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2127. if (sign)
  2128. *sign = 1;
  2129. }
  2130. else
  2131. {
  2132. if (floor(arg) == arg)
  2133. return policies::raise_pole_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >(
  2134. "lgamma<%1%>", "Evaluation of lgamma at a negative integer %1%.", arg, pol);
  2135. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> t = detail::sinpx(arg);
  2136. arg = -arg;
  2137. if (t < 0)
  2138. {
  2139. t = -t;
  2140. }
  2141. result = boost::multiprecision::log(boost::math::constants::pi<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >()) - lgamma(arg, 0, pol) - boost::multiprecision::log(t);
  2142. if (sign)
  2143. {
  2144. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> phase = 1 - arg;
  2145. phase = floor(phase) / 2;
  2146. if (floor(phase) == phase)
  2147. *sign = -1;
  2148. else
  2149. *sign = 1;
  2150. }
  2151. }
  2152. if (mpfr_inf_p(result.backend().data()))
  2153. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("lgamma<%1%>(%1%)", 0, pol);
  2154. if (mpfr_nan_p(result.backend().data()))
  2155. return policies::raise_evaluation_error("lgamma<%1%>(%1%)", "Unknown error, result is a NaN", result, pol);
  2156. return result;
  2157. }
  2158. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2159. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, int* sign)
  2160. {
  2161. return lgamma(arg, sign, policies::policy<>());
  2162. }
  2163. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2164. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy& pol)
  2165. {
  2166. return lgamma(arg, 0, pol);
  2167. }
  2168. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2169. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> lgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2170. {
  2171. return lgamma(arg, 0, policies::policy<>());
  2172. }
  2173. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2174. inline typename std::enable_if<boost::math::policies::is_policy<Policy>::value, boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::type tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy& pol)
  2175. {
  2176. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2177. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2178. mpfr_gamma(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2179. if (mpfr_inf_p(result.backend().data()))
  2180. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("tgamma<%1%>(%1%)", 0, pol);
  2181. if (mpfr_nan_p(result.backend().data()))
  2182. return policies::raise_evaluation_error("tgamma<%1%>(%1%)", "Unknown error, result is a NaN", result, pol);
  2183. return result;
  2184. }
  2185. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2186. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> tgamma BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2187. {
  2188. return tgamma(arg, policies::policy<>());
  2189. }
  2190. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2191. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy& pol)
  2192. {
  2193. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2194. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2195. mpfr_log1p(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2196. if (mpfr_inf_p(result.backend().data()))
  2197. return (arg == -1 ? -1 : 1) * policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("log1p<%1%>(%1%)", 0, pol);
  2198. if (mpfr_nan_p(result.backend().data()))
  2199. return policies::raise_evaluation_error("log1p<%1%>(%1%)", "Unknown error, result is a NaN", result, pol);
  2200. return result;
  2201. }
  2202. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2203. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> log1p BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2204. {
  2205. return log1p(arg, policies::policy<>());
  2206. }
  2207. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates, class Policy>
  2208. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> rsqrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg, const Policy& pol)
  2209. {
  2210. boost::multiprecision::detail::scoped_default_precision<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> > precision_guard(arg);
  2211. boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> result;
  2212. mpfr_rec_sqrt(result.backend().data(), arg.backend().data(), GMP_RNDN);
  2213. if (mpfr_inf_p(result.backend().data()))
  2214. return policies::raise_overflow_error<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >("rsqrt<%1%>(%1%)", 0, pol);
  2215. if (mpfr_nan_p(result.backend().data()))
  2216. return policies::raise_evaluation_error("rsqrt<%1%>(%1%)", "Negative argument, result is a NaN", result, pol);
  2217. return result;
  2218. }
  2219. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2220. inline boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> rsqrt BOOST_PREVENT_MACRO_SUBSTITUTION(const boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>& arg)
  2221. {
  2222. return rsqrt(arg, policies::policy<>());
  2223. }
  2224. } // namespace math
  2225. } // namespace boost
  2226. namespace std {
  2227. //
  2228. // numeric_limits [partial] specializations for the types declared in this header:
  2229. //
  2230. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2231. class numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >
  2232. {
  2233. using number_type = boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates>;
  2234. public:
  2235. static constexpr bool is_specialized = true;
  2236. static number_type(min)()
  2237. {
  2238. static std::pair<bool, number_type> value;
  2239. if (!value.first)
  2240. {
  2241. value.first = true;
  2242. value.second = 0.5;
  2243. mpfr_div_2exp(value.second.backend().data(), value.second.backend().data(), -mpfr_get_emin(), GMP_RNDN);
  2244. }
  2245. return value.second;
  2246. }
  2247. static number_type(max)()
  2248. {
  2249. static std::pair<bool, number_type> value;
  2250. if (!value.first)
  2251. {
  2252. value.first = true;
  2253. value.second = 0.5;
  2254. mpfr_mul_2exp(value.second.backend().data(), value.second.backend().data(), mpfr_get_emax(), GMP_RNDN);
  2255. }
  2256. return value.second;
  2257. }
  2258. static constexpr number_type lowest()
  2259. {
  2260. return -(max)();
  2261. }
  2262. static constexpr int digits = static_cast<int>((Digits10 * 1000L) / 301L + ((Digits10 * 1000L) % 301 ? 2 : 1));
  2263. static constexpr int digits10 = Digits10;
  2264. // Is this really correct???
  2265. static constexpr int max_digits10 = boost::multiprecision::detail::calc_max_digits10<digits>::value;
  2266. static constexpr bool is_signed = true;
  2267. static constexpr bool is_integer = false;
  2268. static constexpr bool is_exact = false;
  2269. static constexpr int radix = 2;
  2270. static number_type epsilon()
  2271. {
  2272. static std::pair<bool, number_type> value;
  2273. if (!value.first)
  2274. {
  2275. value.first = true;
  2276. value.second = 1;
  2277. mpfr_div_2exp(value.second.backend().data(), value.second.backend().data(), std::numeric_limits<number_type>::digits - 1, GMP_RNDN);
  2278. }
  2279. return value.second;
  2280. }
  2281. // What value should this be????
  2282. static number_type round_error()
  2283. {
  2284. // returns epsilon/2
  2285. static std::pair<bool, number_type> value;
  2286. if (!value.first)
  2287. {
  2288. value.first = true;
  2289. value.second = 1;
  2290. mpfr_div_2exp(value.second.backend().data(), value.second.backend().data(), 1, GMP_RNDN);
  2291. }
  2292. return value.second;
  2293. }
  2294. static constexpr long min_exponent = MPFR_EMIN_DEFAULT;
  2295. static constexpr long min_exponent10 = (MPFR_EMIN_DEFAULT / 1000) * 301L;
  2296. static constexpr long max_exponent = MPFR_EMAX_DEFAULT;
  2297. static constexpr long max_exponent10 = (MPFR_EMAX_DEFAULT / 1000) * 301L;
  2298. static constexpr bool has_infinity = true;
  2299. static constexpr bool has_quiet_NaN = true;
  2300. static constexpr bool has_signaling_NaN = false;
  2301. static constexpr float_denorm_style has_denorm = denorm_absent;
  2302. static constexpr bool has_denorm_loss = false;
  2303. static number_type infinity()
  2304. {
  2305. // returns epsilon/2
  2306. static std::pair<bool, number_type> value;
  2307. if (!value.first)
  2308. {
  2309. value.first = true;
  2310. value.second = 1;
  2311. mpfr_set_inf(value.second.backend().data(), 1);
  2312. }
  2313. return value.second;
  2314. }
  2315. static number_type quiet_NaN()
  2316. {
  2317. // returns epsilon/2
  2318. static std::pair<bool, number_type> value;
  2319. if (!value.first)
  2320. {
  2321. value.first = true;
  2322. value.second = 1;
  2323. mpfr_set_nan(value.second.backend().data());
  2324. }
  2325. return value.second;
  2326. }
  2327. static constexpr number_type signaling_NaN()
  2328. {
  2329. return number_type(0);
  2330. }
  2331. static constexpr number_type denorm_min() { return number_type(0); }
  2332. static constexpr bool is_iec559 = false;
  2333. static constexpr bool is_bounded = true;
  2334. static constexpr bool is_modulo = false;
  2335. static constexpr bool traps = true;
  2336. static constexpr bool tinyness_before = false;
  2337. static constexpr float_round_style round_style = round_to_nearest;
  2338. };
  2339. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2340. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::digits;
  2341. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2342. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::digits10;
  2343. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2344. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::max_digits10;
  2345. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2346. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::is_signed;
  2347. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2348. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::is_integer;
  2349. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2350. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::is_exact;
  2351. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2352. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::radix;
  2353. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2354. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::min_exponent;
  2355. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2356. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::min_exponent10;
  2357. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2358. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::max_exponent;
  2359. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2360. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::max_exponent10;
  2361. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2362. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::has_infinity;
  2363. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2364. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::has_quiet_NaN;
  2365. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2366. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::has_signaling_NaN;
  2367. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2368. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::has_denorm;
  2369. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2370. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::has_denorm_loss;
  2371. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2372. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::is_iec559;
  2373. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2374. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::is_bounded;
  2375. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2376. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::is_modulo;
  2377. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2378. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::traps;
  2379. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2380. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::tinyness_before;
  2381. template <unsigned Digits10, boost::multiprecision::mpfr_allocation_type AllocateType, boost::multiprecision::expression_template_option ExpressionTemplates>
  2382. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<Digits10, AllocateType>, ExpressionTemplates> >::round_style;
  2383. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2384. class numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >
  2385. {
  2386. using number_type = boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates>;
  2387. public:
  2388. static constexpr bool is_specialized = false;
  2389. static number_type(min)()
  2390. {
  2391. number_type value(0.5);
  2392. mpfr_div_2exp(value.backend().data(), value.backend().data(), -mpfr_get_emin(), GMP_RNDN);
  2393. return value;
  2394. }
  2395. static number_type(max)()
  2396. {
  2397. number_type value(0.5);
  2398. mpfr_mul_2exp(value.backend().data(), value.backend().data(), mpfr_get_emax(), GMP_RNDN);
  2399. return value;
  2400. }
  2401. static number_type lowest()
  2402. {
  2403. return -(max)();
  2404. }
  2405. static constexpr int digits = INT_MAX;
  2406. static constexpr int digits10 = INT_MAX;
  2407. static constexpr int max_digits10 = INT_MAX;
  2408. static constexpr bool is_signed = true;
  2409. static constexpr bool is_integer = false;
  2410. static constexpr bool is_exact = false;
  2411. static constexpr int radix = 2;
  2412. static number_type epsilon()
  2413. {
  2414. number_type value(1);
  2415. mpfr_div_2exp(value.backend().data(), value.backend().data(), boost::multiprecision::detail::digits10_2_2(number_type::default_precision()) - 1, GMP_RNDN);
  2416. return value;
  2417. }
  2418. static number_type round_error()
  2419. {
  2420. return epsilon() / 2;
  2421. }
  2422. static constexpr long min_exponent = MPFR_EMIN_DEFAULT;
  2423. static constexpr long min_exponent10 = (MPFR_EMIN_DEFAULT / 1000) * 301L;
  2424. static constexpr long max_exponent = MPFR_EMAX_DEFAULT;
  2425. static constexpr long max_exponent10 = (MPFR_EMAX_DEFAULT / 1000) * 301L;
  2426. static constexpr bool has_infinity = true;
  2427. static constexpr bool has_quiet_NaN = true;
  2428. static constexpr bool has_signaling_NaN = false;
  2429. static constexpr float_denorm_style has_denorm = denorm_absent;
  2430. static constexpr bool has_denorm_loss = false;
  2431. static number_type infinity()
  2432. {
  2433. number_type value;
  2434. mpfr_set_inf(value.backend().data(), 1);
  2435. return value;
  2436. }
  2437. static number_type quiet_NaN()
  2438. {
  2439. number_type value;
  2440. mpfr_set_nan(value.backend().data());
  2441. return value;
  2442. }
  2443. static number_type signaling_NaN() { return number_type(0); }
  2444. static number_type denorm_min() { return number_type(0); }
  2445. static constexpr bool is_iec559 = false;
  2446. static constexpr bool is_bounded = true;
  2447. static constexpr bool is_modulo = false;
  2448. static constexpr bool traps = false;
  2449. static constexpr bool tinyness_before = false;
  2450. static constexpr float_round_style round_style = round_toward_zero;
  2451. };
  2452. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2453. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::digits;
  2454. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2455. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::digits10;
  2456. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2457. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::max_digits10;
  2458. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2459. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::is_signed;
  2460. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2461. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::is_integer;
  2462. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2463. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::is_exact;
  2464. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2465. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::radix;
  2466. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2467. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::min_exponent;
  2468. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2469. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::min_exponent10;
  2470. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2471. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::max_exponent;
  2472. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2473. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::max_exponent10;
  2474. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2475. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::has_infinity;
  2476. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2477. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::has_quiet_NaN;
  2478. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2479. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::has_signaling_NaN;
  2480. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2481. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::has_denorm;
  2482. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2483. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::has_denorm_loss;
  2484. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2485. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::is_iec559;
  2486. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2487. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::is_bounded;
  2488. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2489. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::is_modulo;
  2490. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2491. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::traps;
  2492. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2493. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::tinyness_before;
  2494. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2495. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::mpfr_float_backend<0>, ExpressionTemplates> >::round_style;
  2496. } // namespace std
  2497. #endif