gmp.hpp 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146
  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_ER_GMP_BACKEND_HPP
  6. #define BOOST_MATH_ER_GMP_BACKEND_HPP
  7. #include <boost/multiprecision/number.hpp>
  8. #include <boost/multiprecision/debug_adaptor.hpp>
  9. #include <boost/multiprecision/detail/integer_ops.hpp>
  10. #include <boost/multiprecision/detail/big_lanczos.hpp>
  11. #include <boost/multiprecision/detail/digits.hpp>
  12. #include <boost/multiprecision/detail/atomic.hpp>
  13. #include <boost/math/special_functions/fpclassify.hpp>
  14. #include <cstdint>
  15. #include <boost/functional/hash_fwd.hpp>
  16. //
  17. // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
  18. //
  19. #include <boost/math/special_functions/asinh.hpp>
  20. #include <boost/math/special_functions/acosh.hpp>
  21. #include <boost/math/special_functions/atanh.hpp>
  22. #include <boost/math/special_functions/cbrt.hpp>
  23. #include <boost/math/special_functions/expm1.hpp>
  24. #include <boost/math/special_functions/gamma.hpp>
  25. #ifdef BOOST_MSVC
  26. #pragma warning(push)
  27. #pragma warning(disable : 4127)
  28. #endif
  29. #include <gmp.h>
  30. #ifdef BOOST_MSVC
  31. #pragma warning(pop)
  32. #endif
  33. #if defined(__MPIR_VERSION) && defined(__MPIR_VERSION_MINOR) && defined(__MPIR_VERSION_PATCHLEVEL)
  34. #define BOOST_MP_MPIR_VERSION (__MPIR_VERSION * 10000 + __MPIR_VERSION_MINOR * 100 + __MPIR_VERSION_PATCHLEVEL)
  35. #else
  36. #define BOOST_MP_MPIR_VERSION 0
  37. #endif
  38. #include <cctype>
  39. #include <cmath>
  40. #include <limits>
  41. #include <climits>
  42. namespace boost {
  43. namespace multiprecision {
  44. namespace backends {
  45. #ifdef BOOST_MSVC
  46. // warning C4127: conditional expression is constant
  47. #pragma warning(push)
  48. //#pragma warning(disable : 4127)
  49. #endif
  50. template <unsigned digits10>
  51. struct gmp_float;
  52. struct gmp_int;
  53. struct gmp_rational;
  54. } // namespace backends
  55. template <>
  56. struct number_category<backends::gmp_int> : public std::integral_constant<int, number_kind_integer>
  57. {};
  58. template <>
  59. struct number_category<backends::gmp_rational> : public std::integral_constant<int, number_kind_rational>
  60. {};
  61. template <unsigned digits10>
  62. struct number_category<backends::gmp_float<digits10> > : public std::integral_constant<int, number_kind_floating_point>
  63. {};
  64. namespace backends {
  65. //
  66. // Within this file, the only functions we mark as noexcept are those that manipulate
  67. // (but don't create) an mpf_t. All other types may allocate at pretty much any time
  68. // via a user-supplied allocator, and therefore throw.
  69. //
  70. namespace detail {
  71. template <unsigned digits10>
  72. struct gmp_float_imp
  73. {
  74. #ifdef BOOST_HAS_LONG_LONG
  75. using signed_types = std::tuple<long, boost::long_long_type> ;
  76. using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
  77. #else
  78. using signed_types = std::tuple<long> ;
  79. using unsigned_types = std::tuple<unsigned long>;
  80. #endif
  81. using float_types = std::tuple<double, long double>;
  82. using exponent_type = long ;
  83. gmp_float_imp() noexcept
  84. {
  85. m_data[0]._mp_d = 0; // uninitialized m_data
  86. }
  87. gmp_float_imp(const gmp_float_imp& o)
  88. {
  89. //
  90. // We have to do an init followed by a set here, otherwise *this may be at
  91. // a lower precision than o: seems like mpf_init_set copies just enough bits
  92. // to get the right value, but if it's then used in further calculations
  93. // things go badly wrong!!
  94. //
  95. mpf_init2(m_data, mpf_get_prec(o.data()));
  96. if (o.m_data[0]._mp_d)
  97. mpf_set(m_data, o.m_data);
  98. }
  99. // rvalue copy
  100. gmp_float_imp(gmp_float_imp&& o) noexcept
  101. {
  102. m_data[0] = o.m_data[0];
  103. o.m_data[0]._mp_d = 0;
  104. }
  105. gmp_float_imp& operator=(const gmp_float_imp& o)
  106. {
  107. if (m_data[0]._mp_d == 0)
  108. mpf_init2(m_data, mpf_get_prec(o.data()));
  109. if (mpf_get_prec(data()) != mpf_get_prec(o.data()))
  110. {
  111. mpf_t t;
  112. mpf_init2(t, mpf_get_prec(o.data()));
  113. mpf_set(t, o.data());
  114. mpf_swap(data(), t);
  115. mpf_clear(t);
  116. }
  117. else
  118. {
  119. if (o.m_data[0]._mp_d)
  120. mpf_set(m_data, o.m_data);
  121. }
  122. return *this;
  123. }
  124. // rvalue assign
  125. gmp_float_imp& operator=(gmp_float_imp&& o) noexcept
  126. {
  127. mpf_swap(m_data, o.m_data);
  128. return *this;
  129. }
  130. #ifdef BOOST_HAS_LONG_LONG
  131. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  132. gmp_float_imp& operator=(boost::ulong_long_type i)
  133. {
  134. *this = static_cast<unsigned long>(i);
  135. return *this;
  136. }
  137. #else
  138. gmp_float_imp& operator=(boost::ulong_long_type i)
  139. {
  140. if (m_data[0]._mp_d == 0)
  141. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  142. boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
  143. unsigned shift = 0;
  144. mpf_t t;
  145. mpf_init2(t, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  146. mpf_set_ui(m_data, 0);
  147. while (i)
  148. {
  149. mpf_set_ui(t, static_cast<unsigned long>(i & mask));
  150. if (shift)
  151. mpf_mul_2exp(t, t, shift);
  152. mpf_add(m_data, m_data, t);
  153. shift += std::numeric_limits<unsigned long>::digits;
  154. i >>= std::numeric_limits<unsigned long>::digits;
  155. }
  156. mpf_clear(t);
  157. return *this;
  158. }
  159. #endif
  160. gmp_float_imp& operator=(boost::long_long_type i)
  161. {
  162. if (m_data[0]._mp_d == 0)
  163. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  164. bool neg = i < 0;
  165. *this = static_cast<boost::ulong_long_type>(boost::multiprecision::detail::unsigned_abs(i));
  166. if (neg)
  167. mpf_neg(m_data, m_data);
  168. return *this;
  169. }
  170. #endif
  171. gmp_float_imp& operator=(unsigned long i)
  172. {
  173. if (m_data[0]._mp_d == 0)
  174. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  175. mpf_set_ui(m_data, i);
  176. return *this;
  177. }
  178. gmp_float_imp& operator=(long i)
  179. {
  180. if (m_data[0]._mp_d == 0)
  181. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  182. mpf_set_si(m_data, i);
  183. return *this;
  184. }
  185. gmp_float_imp& operator=(double d)
  186. {
  187. if (m_data[0]._mp_d == 0)
  188. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  189. mpf_set_d(m_data, d);
  190. return *this;
  191. }
  192. gmp_float_imp& operator=(long double a)
  193. {
  194. using std::floor;
  195. using std::frexp;
  196. using std::ldexp;
  197. if (m_data[0]._mp_d == 0)
  198. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  199. if (a == 0)
  200. {
  201. mpf_set_si(m_data, 0);
  202. return *this;
  203. }
  204. if (a == 1)
  205. {
  206. mpf_set_si(m_data, 1);
  207. return *this;
  208. }
  209. BOOST_ASSERT(!(boost::math::isinf)(a));
  210. BOOST_ASSERT(!(boost::math::isnan)(a));
  211. int e;
  212. long double f, term;
  213. mpf_set_ui(m_data, 0u);
  214. f = frexp(a, &e);
  215. constexpr const int shift = std::numeric_limits<int>::digits - 1;
  216. while (f)
  217. {
  218. // extract int sized bits from f:
  219. f = ldexp(f, shift);
  220. term = floor(f);
  221. e -= shift;
  222. mpf_mul_2exp(m_data, m_data, shift);
  223. if (term > 0)
  224. mpf_add_ui(m_data, m_data, static_cast<unsigned>(term));
  225. else
  226. mpf_sub_ui(m_data, m_data, static_cast<unsigned>(-term));
  227. f -= term;
  228. }
  229. if (e > 0)
  230. mpf_mul_2exp(m_data, m_data, e);
  231. else if (e < 0)
  232. mpf_div_2exp(m_data, m_data, -e);
  233. return *this;
  234. }
  235. gmp_float_imp& operator=(const char* s)
  236. {
  237. if (m_data[0]._mp_d == 0)
  238. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  239. if (s && (*s == '+'))
  240. ++s; // Leading "+" sign not supported by mpf_set_str:
  241. if (0 != mpf_set_str(m_data, s, 10))
  242. BOOST_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid floating point number.")));
  243. return *this;
  244. }
  245. void swap(gmp_float_imp& o) noexcept
  246. {
  247. mpf_swap(m_data, o.m_data);
  248. }
  249. std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
  250. {
  251. BOOST_ASSERT(m_data[0]._mp_d);
  252. bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
  253. bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
  254. std::streamsize org_digits(digits);
  255. if (scientific && digits)
  256. ++digits;
  257. std::string result;
  258. mp_exp_t e;
  259. void* (*alloc_func_ptr)(size_t);
  260. void* (*realloc_func_ptr)(void*, size_t, size_t);
  261. void (*free_func_ptr)(void*, size_t);
  262. mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
  263. if (mpf_sgn(m_data) == 0)
  264. {
  265. e = 0;
  266. result = "0";
  267. if (fixed && digits)
  268. ++digits;
  269. }
  270. else
  271. {
  272. char* ps = mpf_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data);
  273. --e; // To match with what our formatter expects.
  274. if (fixed && e != -1)
  275. {
  276. // Oops we actually need a different number of digits to what we asked for:
  277. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  278. digits += e + 1;
  279. if (digits == 0)
  280. {
  281. // We need to get *all* the digits and then possibly round up,
  282. // we end up with either "0" or "1" as the result.
  283. ps = mpf_get_str(0, &e, 10, 0, m_data);
  284. --e;
  285. unsigned offset = *ps == '-' ? 1 : 0;
  286. if (ps[offset] > '5')
  287. {
  288. ++e;
  289. ps[offset] = '1';
  290. ps[offset + 1] = 0;
  291. }
  292. else if (ps[offset] == '5')
  293. {
  294. unsigned i = offset + 1;
  295. bool round_up = false;
  296. while (ps[i] != 0)
  297. {
  298. if (ps[i] != '0')
  299. {
  300. round_up = true;
  301. break;
  302. }
  303. ++i;
  304. }
  305. if (round_up)
  306. {
  307. ++e;
  308. ps[offset] = '1';
  309. ps[offset + 1] = 0;
  310. }
  311. else
  312. {
  313. ps[offset] = '0';
  314. ps[offset + 1] = 0;
  315. }
  316. }
  317. else
  318. {
  319. ps[offset] = '0';
  320. ps[offset + 1] = 0;
  321. }
  322. }
  323. else if (digits > 0)
  324. {
  325. mp_exp_t old_e = e;
  326. ps = mpf_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data);
  327. --e; // To match with what our formatter expects.
  328. if (old_e > e)
  329. {
  330. // in some cases, when we ask for more digits of precision, it will
  331. // change the number of digits to the left of the decimal, if that
  332. // happens, account for it here.
  333. // example: cout << fixed << setprecision(3) << mpf_float_50("99.9809")
  334. digits -= old_e - e;
  335. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  336. ps = mpf_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data);
  337. --e; // To match with what our formatter expects.
  338. }
  339. }
  340. else
  341. {
  342. ps = mpf_get_str(0, &e, 10, 1, m_data);
  343. --e;
  344. unsigned offset = *ps == '-' ? 1 : 0;
  345. ps[offset] = '0';
  346. ps[offset + 1] = 0;
  347. }
  348. }
  349. result = ps;
  350. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  351. }
  352. boost::multiprecision::detail::format_float_string(result, e, org_digits, f, mpf_sgn(m_data) == 0);
  353. return result;
  354. }
  355. ~gmp_float_imp() noexcept
  356. {
  357. if (m_data[0]._mp_d)
  358. mpf_clear(m_data);
  359. }
  360. void negate() noexcept
  361. {
  362. BOOST_ASSERT(m_data[0]._mp_d);
  363. mpf_neg(m_data, m_data);
  364. }
  365. int compare(const gmp_float<digits10>& o) const noexcept
  366. {
  367. BOOST_ASSERT(m_data[0]._mp_d && o.m_data[0]._mp_d);
  368. return mpf_cmp(m_data, o.m_data);
  369. }
  370. int compare(long i) const noexcept
  371. {
  372. BOOST_ASSERT(m_data[0]._mp_d);
  373. return mpf_cmp_si(m_data, i);
  374. }
  375. int compare(unsigned long i) const noexcept
  376. {
  377. BOOST_ASSERT(m_data[0]._mp_d);
  378. return mpf_cmp_ui(m_data, i);
  379. }
  380. template <class V>
  381. typename std::enable_if<boost::multiprecision::detail::is_arithmetic<V>::value, int>::type compare(V v) const
  382. {
  383. gmp_float<digits10> d;
  384. d = v;
  385. return compare(d);
  386. }
  387. mpf_t& data() noexcept
  388. {
  389. BOOST_ASSERT(m_data[0]._mp_d);
  390. return m_data;
  391. }
  392. const mpf_t& data() const noexcept
  393. {
  394. BOOST_ASSERT(m_data[0]._mp_d);
  395. return m_data;
  396. }
  397. protected:
  398. mpf_t m_data;
  399. static boost::multiprecision::detail::precision_type& get_default_precision() noexcept
  400. {
  401. static boost::multiprecision::detail::precision_type val(50);
  402. return val;
  403. }
  404. };
  405. } // namespace detail
  406. struct gmp_int;
  407. struct gmp_rational;
  408. template <unsigned digits10>
  409. struct gmp_float : public detail::gmp_float_imp<digits10>
  410. {
  411. gmp_float()
  412. {
  413. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  414. }
  415. gmp_float(const gmp_float& o) : detail::gmp_float_imp<digits10>(o) {}
  416. template <unsigned D>
  417. gmp_float(const gmp_float<D>& o, typename std::enable_if<D <= digits10>::type* = 0);
  418. template <unsigned D>
  419. explicit gmp_float(const gmp_float<D>& o, typename std::enable_if<!(D <= digits10)>::type* = 0);
  420. gmp_float(const gmp_int& o);
  421. gmp_float(const gmp_rational& o);
  422. gmp_float(const mpf_t val)
  423. {
  424. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  425. mpf_set(this->m_data, val);
  426. }
  427. gmp_float(const mpz_t val)
  428. {
  429. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  430. mpf_set_z(this->m_data, val);
  431. }
  432. gmp_float(const mpq_t val)
  433. {
  434. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  435. mpf_set_q(this->m_data, val);
  436. }
  437. // rvalue copy
  438. gmp_float(gmp_float&& o) noexcept : detail::gmp_float_imp<digits10>(static_cast<detail::gmp_float_imp<digits10>&&>(o))
  439. {}
  440. gmp_float& operator=(const gmp_float& o)
  441. {
  442. *static_cast<detail::gmp_float_imp<digits10>*>(this) = static_cast<detail::gmp_float_imp<digits10> const&>(o);
  443. return *this;
  444. }
  445. gmp_float& operator=(gmp_float&& o) noexcept
  446. {
  447. *static_cast<detail::gmp_float_imp<digits10>*>(this) = static_cast<detail::gmp_float_imp<digits10>&&>(o);
  448. return *this;
  449. }
  450. template <unsigned D>
  451. gmp_float& operator=(const gmp_float<D>& o);
  452. gmp_float& operator=(const gmp_int& o);
  453. gmp_float& operator=(const gmp_rational& o);
  454. gmp_float& operator=(const mpf_t val)
  455. {
  456. if (this->m_data[0]._mp_d == 0)
  457. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  458. mpf_set(this->m_data, val);
  459. return *this;
  460. }
  461. gmp_float& operator=(const mpz_t val)
  462. {
  463. if (this->m_data[0]._mp_d == 0)
  464. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  465. mpf_set_z(this->m_data, val);
  466. return *this;
  467. }
  468. gmp_float& operator=(const mpq_t val)
  469. {
  470. if (this->m_data[0]._mp_d == 0)
  471. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  472. mpf_set_q(this->m_data, val);
  473. return *this;
  474. }
  475. template <class V>
  476. gmp_float& operator=(const V& v)
  477. {
  478. *static_cast<detail::gmp_float_imp<digits10>*>(this) = v;
  479. return *this;
  480. }
  481. };
  482. template <>
  483. struct gmp_float<0> : public detail::gmp_float_imp<0>
  484. {
  485. //
  486. // We have a problem with mpf_t in that the precision we request isn't what we get.
  487. // As a result the front end can end up chasing it's tail trying to create a variable
  488. // with the the correct precision to hold the result of an expression.
  489. // See: https://github.com/boostorg/multiprecision/issues/164
  490. // The problem is made worse by the fact that our conversions from base10 to 2 and
  491. // vice-versa do not exactly round trip (and probably never will).
  492. // The workaround is to keep track of the precision requested, and always return
  493. // that as the current actual precision.
  494. //
  495. private:
  496. unsigned requested_precision;
  497. public:
  498. gmp_float() : requested_precision(get_default_precision())
  499. {
  500. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  501. }
  502. gmp_float(const mpf_t val) : requested_precision(get_default_precision())
  503. {
  504. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  505. mpf_set(this->m_data, val);
  506. }
  507. gmp_float(const mpz_t val) : requested_precision(get_default_precision())
  508. {
  509. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  510. mpf_set_z(this->m_data, val);
  511. }
  512. gmp_float(const mpq_t val) : requested_precision(get_default_precision())
  513. {
  514. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  515. mpf_set_q(this->m_data, val);
  516. }
  517. gmp_float(const gmp_float& o) : detail::gmp_float_imp<0>(o), requested_precision(o.requested_precision) {}
  518. template <unsigned D>
  519. gmp_float(const gmp_float<D>& o)
  520. {
  521. mpf_init2(this->m_data, mpf_get_prec(o.data()));
  522. mpf_set(this->m_data, o.data());
  523. requested_precision = D;
  524. }
  525. // rvalue copy
  526. gmp_float(gmp_float&& o) noexcept : detail::gmp_float_imp<0>(static_cast<detail::gmp_float_imp<0>&&>(o)), requested_precision(o.requested_precision)
  527. {}
  528. gmp_float(const gmp_int& o);
  529. gmp_float(const gmp_rational& o);
  530. gmp_float(const gmp_float& o, unsigned digits10) : requested_precision(digits10)
  531. {
  532. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  533. mpf_set(this->m_data, o.data());
  534. }
  535. template <class V>
  536. gmp_float(const V& o, unsigned digits10) : requested_precision(digits10)
  537. {
  538. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  539. *this = o;
  540. }
  541. #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
  542. //
  543. // Support for new types in C++17
  544. //
  545. template <class Traits>
  546. gmp_float(const std::basic_string_view<char, Traits>& o, unsigned digits10) : requested_precision(digits10)
  547. {
  548. using default_ops::assign_from_string_view;
  549. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  550. assign_from_string_view(*this, o);
  551. }
  552. #endif
  553. gmp_float& operator=(const gmp_float& o)
  554. {
  555. *static_cast<detail::gmp_float_imp<0>*>(this) = static_cast<detail::gmp_float_imp<0> const&>(o);
  556. requested_precision = o.requested_precision;
  557. return *this;
  558. }
  559. // rvalue copy
  560. gmp_float& operator=(gmp_float&& o) noexcept
  561. {
  562. *static_cast<detail::gmp_float_imp<0>*>(this) = static_cast<detail::gmp_float_imp<0>&&>(o);
  563. requested_precision = o.requested_precision;
  564. return *this;
  565. }
  566. template <unsigned D>
  567. gmp_float& operator=(const gmp_float<D>& o)
  568. {
  569. if (this->m_data[0]._mp_d == 0)
  570. {
  571. mpf_init2(this->m_data, mpf_get_prec(o.data()));
  572. }
  573. else
  574. {
  575. mpf_set_prec(this->m_data, mpf_get_prec(o.data()));
  576. }
  577. mpf_set(this->m_data, o.data());
  578. requested_precision = D;
  579. return *this;
  580. }
  581. gmp_float& operator=(const gmp_int& o);
  582. gmp_float& operator=(const gmp_rational& o);
  583. gmp_float& operator=(const mpf_t val)
  584. {
  585. if (this->m_data[0]._mp_d == 0)
  586. {
  587. requested_precision = get_default_precision();
  588. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  589. }
  590. mpf_set(this->m_data, val);
  591. return *this;
  592. }
  593. gmp_float& operator=(const mpz_t val)
  594. {
  595. if (this->m_data[0]._mp_d == 0)
  596. {
  597. requested_precision = get_default_precision();
  598. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  599. }
  600. mpf_set_z(this->m_data, val);
  601. return *this;
  602. }
  603. gmp_float& operator=(const mpq_t val)
  604. {
  605. if (this->m_data[0]._mp_d == 0)
  606. {
  607. requested_precision = get_default_precision();
  608. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  609. }
  610. mpf_set_q(this->m_data, val);
  611. return *this;
  612. }
  613. template <class V>
  614. gmp_float& operator=(const V& v)
  615. {
  616. *static_cast<detail::gmp_float_imp<0>*>(this) = v;
  617. return *this;
  618. }
  619. static unsigned default_precision() noexcept
  620. {
  621. return get_default_precision();
  622. }
  623. static void default_precision(unsigned v) noexcept
  624. {
  625. get_default_precision() = v;
  626. }
  627. unsigned precision() const noexcept
  628. {
  629. return requested_precision;
  630. }
  631. void precision(unsigned digits10) noexcept
  632. {
  633. requested_precision = digits10;
  634. mpf_set_prec(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  635. }
  636. void swap(gmp_float& o)
  637. {
  638. std::swap(requested_precision, o.requested_precision);
  639. gmp_float_imp<0>::swap(o);
  640. }
  641. };
  642. template <unsigned digits10, class T>
  643. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const gmp_float<digits10>& a, const T& b) noexcept
  644. {
  645. return a.compare(b) == 0;
  646. }
  647. template <unsigned digits10, class T>
  648. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const gmp_float<digits10>& a, const T& b) noexcept
  649. {
  650. return a.compare(b) < 0;
  651. }
  652. template <unsigned digits10, class T>
  653. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const gmp_float<digits10>& a, const T& b) noexcept
  654. {
  655. return a.compare(b) > 0;
  656. }
  657. template <unsigned D1, unsigned D2>
  658. inline void eval_add(gmp_float<D1>& result, const gmp_float<D2>& o)
  659. {
  660. mpf_add(result.data(), result.data(), o.data());
  661. }
  662. template <unsigned D1, unsigned D2>
  663. inline void eval_subtract(gmp_float<D1>& result, const gmp_float<D2>& o)
  664. {
  665. mpf_sub(result.data(), result.data(), o.data());
  666. }
  667. template <unsigned D1, unsigned D2>
  668. inline void eval_multiply(gmp_float<D1>& result, const gmp_float<D2>& o)
  669. {
  670. mpf_mul(result.data(), result.data(), o.data());
  671. }
  672. template <unsigned digits10>
  673. inline bool eval_is_zero(const gmp_float<digits10>& val) noexcept
  674. {
  675. return mpf_sgn(val.data()) == 0;
  676. }
  677. template <unsigned D1, unsigned D2>
  678. inline void eval_divide(gmp_float<D1>& result, const gmp_float<D2>& o)
  679. {
  680. if (eval_is_zero(o))
  681. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  682. mpf_div(result.data(), result.data(), o.data());
  683. }
  684. template <unsigned digits10>
  685. inline void eval_add(gmp_float<digits10>& result, unsigned long i)
  686. {
  687. mpf_add_ui(result.data(), result.data(), i);
  688. }
  689. template <unsigned digits10>
  690. inline void eval_subtract(gmp_float<digits10>& result, unsigned long i)
  691. {
  692. mpf_sub_ui(result.data(), result.data(), i);
  693. }
  694. template <unsigned digits10>
  695. inline void eval_multiply(gmp_float<digits10>& result, unsigned long i)
  696. {
  697. mpf_mul_ui(result.data(), result.data(), i);
  698. }
  699. template <unsigned digits10>
  700. inline void eval_divide(gmp_float<digits10>& result, unsigned long i)
  701. {
  702. if (i == 0)
  703. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  704. mpf_div_ui(result.data(), result.data(), i);
  705. }
  706. template <unsigned digits10>
  707. inline void eval_add(gmp_float<digits10>& result, long i)
  708. {
  709. if (i > 0)
  710. mpf_add_ui(result.data(), result.data(), i);
  711. else
  712. mpf_sub_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
  713. }
  714. template <unsigned digits10>
  715. inline void eval_subtract(gmp_float<digits10>& result, long i)
  716. {
  717. if (i > 0)
  718. mpf_sub_ui(result.data(), result.data(), i);
  719. else
  720. mpf_add_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
  721. }
  722. template <unsigned digits10>
  723. inline void eval_multiply(gmp_float<digits10>& result, long i)
  724. {
  725. mpf_mul_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
  726. if (i < 0)
  727. mpf_neg(result.data(), result.data());
  728. }
  729. template <unsigned digits10>
  730. inline void eval_divide(gmp_float<digits10>& result, long i)
  731. {
  732. if (i == 0)
  733. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  734. mpf_div_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
  735. if (i < 0)
  736. mpf_neg(result.data(), result.data());
  737. }
  738. //
  739. // Specialised 3 arg versions of the basic operators:
  740. //
  741. template <unsigned D1, unsigned D2, unsigned D3>
  742. inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
  743. {
  744. mpf_add(a.data(), x.data(), y.data());
  745. }
  746. template <unsigned D1, unsigned D2>
  747. inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
  748. {
  749. mpf_add_ui(a.data(), x.data(), y);
  750. }
  751. template <unsigned D1, unsigned D2>
  752. inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
  753. {
  754. if (y < 0)
  755. mpf_sub_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  756. else
  757. mpf_add_ui(a.data(), x.data(), y);
  758. }
  759. template <unsigned D1, unsigned D2>
  760. inline void eval_add(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
  761. {
  762. mpf_add_ui(a.data(), y.data(), x);
  763. }
  764. template <unsigned D1, unsigned D2>
  765. inline void eval_add(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
  766. {
  767. if (x < 0)
  768. {
  769. mpf_ui_sub(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data());
  770. mpf_neg(a.data(), a.data());
  771. }
  772. else
  773. mpf_add_ui(a.data(), y.data(), x);
  774. }
  775. template <unsigned D1, unsigned D2, unsigned D3>
  776. inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
  777. {
  778. mpf_sub(a.data(), x.data(), y.data());
  779. }
  780. template <unsigned D1, unsigned D2>
  781. inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
  782. {
  783. mpf_sub_ui(a.data(), x.data(), y);
  784. }
  785. template <unsigned D1, unsigned D2>
  786. inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
  787. {
  788. if (y < 0)
  789. mpf_add_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  790. else
  791. mpf_sub_ui(a.data(), x.data(), y);
  792. }
  793. template <unsigned D1, unsigned D2>
  794. inline void eval_subtract(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
  795. {
  796. mpf_ui_sub(a.data(), x, y.data());
  797. }
  798. template <unsigned D1, unsigned D2>
  799. inline void eval_subtract(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
  800. {
  801. if (x < 0)
  802. {
  803. mpf_add_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x));
  804. mpf_neg(a.data(), a.data());
  805. }
  806. else
  807. mpf_ui_sub(a.data(), x, y.data());
  808. }
  809. template <unsigned D1, unsigned D2, unsigned D3>
  810. inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
  811. {
  812. mpf_mul(a.data(), x.data(), y.data());
  813. }
  814. template <unsigned D1, unsigned D2>
  815. inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
  816. {
  817. mpf_mul_ui(a.data(), x.data(), y);
  818. }
  819. template <unsigned D1, unsigned D2>
  820. inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
  821. {
  822. if (y < 0)
  823. {
  824. mpf_mul_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  825. a.negate();
  826. }
  827. else
  828. mpf_mul_ui(a.data(), x.data(), y);
  829. }
  830. template <unsigned D1, unsigned D2>
  831. inline void eval_multiply(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
  832. {
  833. mpf_mul_ui(a.data(), y.data(), x);
  834. }
  835. template <unsigned D1, unsigned D2>
  836. inline void eval_multiply(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
  837. {
  838. if (x < 0)
  839. {
  840. mpf_mul_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x));
  841. mpf_neg(a.data(), a.data());
  842. }
  843. else
  844. mpf_mul_ui(a.data(), y.data(), x);
  845. }
  846. template <unsigned D1, unsigned D2, unsigned D3>
  847. inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
  848. {
  849. if (eval_is_zero(y))
  850. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  851. mpf_div(a.data(), x.data(), y.data());
  852. }
  853. template <unsigned D1, unsigned D2>
  854. inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
  855. {
  856. if (y == 0)
  857. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  858. mpf_div_ui(a.data(), x.data(), y);
  859. }
  860. template <unsigned D1, unsigned D2>
  861. inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
  862. {
  863. if (y == 0)
  864. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  865. if (y < 0)
  866. {
  867. mpf_div_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  868. a.negate();
  869. }
  870. else
  871. mpf_div_ui(a.data(), x.data(), y);
  872. }
  873. template <unsigned D1, unsigned D2>
  874. inline void eval_divide(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
  875. {
  876. if (eval_is_zero(y))
  877. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  878. mpf_ui_div(a.data(), x, y.data());
  879. }
  880. template <unsigned D1, unsigned D2>
  881. inline void eval_divide(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
  882. {
  883. if (eval_is_zero(y))
  884. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  885. if (x < 0)
  886. {
  887. mpf_ui_div(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data());
  888. mpf_neg(a.data(), a.data());
  889. }
  890. else
  891. mpf_ui_div(a.data(), x, y.data());
  892. }
  893. template <unsigned digits10>
  894. inline int eval_get_sign(const gmp_float<digits10>& val) noexcept
  895. {
  896. return mpf_sgn(val.data());
  897. }
  898. template <unsigned digits10>
  899. inline void eval_convert_to(unsigned long* result, const gmp_float<digits10>& val) noexcept
  900. {
  901. if (0 == mpf_fits_ulong_p(val.data()))
  902. *result = (std::numeric_limits<unsigned long>::max)();
  903. else
  904. *result = (unsigned long)mpf_get_ui(val.data());
  905. }
  906. template <unsigned digits10>
  907. inline void eval_convert_to(long* result, const gmp_float<digits10>& val) noexcept
  908. {
  909. if (0 == mpf_fits_slong_p(val.data()))
  910. {
  911. *result = (std::numeric_limits<long>::max)();
  912. *result *= mpf_sgn(val.data());
  913. }
  914. else
  915. *result = (long)mpf_get_si(val.data());
  916. }
  917. template <unsigned digits10>
  918. inline void eval_convert_to(double* result, const gmp_float<digits10>& val) noexcept
  919. {
  920. *result = mpf_get_d(val.data());
  921. }
  922. #ifdef BOOST_HAS_LONG_LONG
  923. template <unsigned digits10>
  924. inline void eval_convert_to(boost::long_long_type* result, const gmp_float<digits10>& val)
  925. {
  926. gmp_float<digits10> t(val);
  927. if (eval_get_sign(t) < 0)
  928. t.negate();
  929. long digits = std::numeric_limits<boost::long_long_type>::digits - std::numeric_limits<long>::digits;
  930. if (digits > 0)
  931. mpf_div_2exp(t.data(), t.data(), digits);
  932. if (!mpf_fits_slong_p(t.data()))
  933. {
  934. if (eval_get_sign(val) < 0)
  935. *result = (std::numeric_limits<boost::long_long_type>::min)();
  936. else
  937. *result = (std::numeric_limits<boost::long_long_type>::max)();
  938. return;
  939. };
  940. *result = mpf_get_si(t.data());
  941. while (digits > 0)
  942. {
  943. *result <<= digits;
  944. digits -= std::numeric_limits<unsigned long>::digits;
  945. mpf_mul_2exp(t.data(), t.data(), digits >= 0 ? std::numeric_limits<unsigned long>::digits : std::numeric_limits<unsigned long>::digits + digits);
  946. unsigned long l = (unsigned long)mpf_get_ui(t.data());
  947. if (digits < 0)
  948. l >>= -digits;
  949. *result |= l;
  950. }
  951. if (eval_get_sign(val) < 0)
  952. *result = -*result;
  953. }
  954. template <unsigned digits10>
  955. inline void eval_convert_to(boost::ulong_long_type* result, const gmp_float<digits10>& val)
  956. {
  957. gmp_float<digits10> t(val);
  958. long digits = std::numeric_limits<boost::long_long_type>::digits - std::numeric_limits<long>::digits;
  959. if (digits > 0)
  960. mpf_div_2exp(t.data(), t.data(), digits);
  961. if (!mpf_fits_ulong_p(t.data()))
  962. {
  963. *result = (std::numeric_limits<boost::long_long_type>::max)();
  964. return;
  965. }
  966. *result = mpf_get_ui(t.data());
  967. while (digits > 0)
  968. {
  969. *result <<= digits;
  970. digits -= std::numeric_limits<unsigned long>::digits;
  971. mpf_mul_2exp(t.data(), t.data(), digits >= 0 ? std::numeric_limits<unsigned long>::digits : std::numeric_limits<unsigned long>::digits + digits);
  972. unsigned long l = (unsigned long)mpf_get_ui(t.data());
  973. if (digits < 0)
  974. l >>= -digits;
  975. *result |= l;
  976. }
  977. }
  978. #endif
  979. //
  980. // Native non-member operations:
  981. //
  982. template <unsigned Digits10>
  983. inline void eval_sqrt(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  984. {
  985. mpf_sqrt(result.data(), val.data());
  986. }
  987. template <unsigned Digits10>
  988. inline void eval_abs(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  989. {
  990. mpf_abs(result.data(), val.data());
  991. }
  992. template <unsigned Digits10>
  993. inline void eval_fabs(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  994. {
  995. mpf_abs(result.data(), val.data());
  996. }
  997. template <unsigned Digits10>
  998. inline void eval_ceil(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  999. {
  1000. mpf_ceil(result.data(), val.data());
  1001. }
  1002. template <unsigned Digits10>
  1003. inline void eval_floor(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  1004. {
  1005. mpf_floor(result.data(), val.data());
  1006. }
  1007. template <unsigned Digits10>
  1008. inline void eval_trunc(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  1009. {
  1010. mpf_trunc(result.data(), val.data());
  1011. }
  1012. template <unsigned Digits10>
  1013. inline void eval_ldexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, long e)
  1014. {
  1015. if (e > 0)
  1016. mpf_mul_2exp(result.data(), val.data(), e);
  1017. else if (e < 0)
  1018. mpf_div_2exp(result.data(), val.data(), -e);
  1019. else
  1020. result = val;
  1021. }
  1022. template <unsigned Digits10>
  1023. inline void eval_frexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, int* e)
  1024. {
  1025. #if (BOOST_MP_MPIR_VERSION >= 20600) && (BOOST_MP_MPIR_VERSION < 30000)
  1026. mpir_si v;
  1027. mpf_get_d_2exp(&v, val.data());
  1028. #else
  1029. long v;
  1030. mpf_get_d_2exp(&v, val.data());
  1031. #endif
  1032. *e = v;
  1033. eval_ldexp(result, val, -v);
  1034. }
  1035. template <unsigned Digits10>
  1036. inline void eval_frexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, long* e)
  1037. {
  1038. #if (BOOST_MP_MPIR_VERSION >= 20600) && (BOOST_MP_MPIR_VERSION < 30000)
  1039. mpir_si v;
  1040. mpf_get_d_2exp(&v, val.data());
  1041. *e = v;
  1042. eval_ldexp(result, val, -v);
  1043. #else
  1044. mpf_get_d_2exp(e, val.data());
  1045. eval_ldexp(result, val, -*e);
  1046. #endif
  1047. }
  1048. template <unsigned Digits10>
  1049. inline std::size_t hash_value(const gmp_float<Digits10>& val)
  1050. {
  1051. std::size_t result = 0;
  1052. for (int i = 0; i < std::abs(val.data()[0]._mp_size); ++i)
  1053. boost::hash_combine(result, val.data()[0]._mp_d[i]);
  1054. boost::hash_combine(result, val.data()[0]._mp_exp);
  1055. boost::hash_combine(result, val.data()[0]._mp_size);
  1056. return result;
  1057. }
  1058. struct gmp_int
  1059. {
  1060. #ifdef BOOST_HAS_LONG_LONG
  1061. using signed_types = std::tuple<long, boost::long_long_type> ;
  1062. using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
  1063. #else
  1064. using signed_types = std::tuple<long> ;
  1065. using unsigned_types = std::tuple<unsigned long>;
  1066. #endif
  1067. using float_types = std::tuple<double, long double>;
  1068. gmp_int()
  1069. {
  1070. mpz_init(this->m_data);
  1071. }
  1072. gmp_int(const gmp_int& o)
  1073. {
  1074. if (o.m_data[0]._mp_d)
  1075. mpz_init_set(m_data, o.m_data);
  1076. else
  1077. mpz_init(this->m_data);
  1078. }
  1079. // rvalue
  1080. gmp_int(gmp_int&& o) noexcept
  1081. {
  1082. m_data[0] = o.m_data[0];
  1083. o.m_data[0]._mp_d = 0;
  1084. }
  1085. explicit gmp_int(const mpf_t val)
  1086. {
  1087. mpz_init(this->m_data);
  1088. mpz_set_f(this->m_data, val);
  1089. }
  1090. gmp_int(const mpz_t val)
  1091. {
  1092. mpz_init_set(this->m_data, val);
  1093. }
  1094. explicit gmp_int(const mpq_t val)
  1095. {
  1096. mpz_init(this->m_data);
  1097. mpz_set_q(this->m_data, val);
  1098. }
  1099. template <unsigned Digits10>
  1100. explicit gmp_int(const gmp_float<Digits10>& o)
  1101. {
  1102. mpz_init(this->m_data);
  1103. mpz_set_f(this->m_data, o.data());
  1104. }
  1105. explicit gmp_int(const gmp_rational& o);
  1106. gmp_int& operator=(const gmp_int& o)
  1107. {
  1108. if (m_data[0]._mp_d == 0)
  1109. mpz_init(this->m_data);
  1110. mpz_set(m_data, o.m_data);
  1111. return *this;
  1112. }
  1113. // rvalue copy
  1114. gmp_int& operator=(gmp_int&& o) noexcept
  1115. {
  1116. mpz_swap(m_data, o.m_data);
  1117. return *this;
  1118. }
  1119. #ifdef BOOST_HAS_LONG_LONG
  1120. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  1121. gmp_int& operator=(boost::ulong_long_type i)
  1122. {
  1123. *this = static_cast<unsigned long>(i);
  1124. return *this;
  1125. }
  1126. #else
  1127. gmp_int& operator=(boost::ulong_long_type i)
  1128. {
  1129. if (m_data[0]._mp_d == 0)
  1130. mpz_init(this->m_data);
  1131. boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
  1132. unsigned shift = 0;
  1133. mpz_t t;
  1134. mpz_set_ui(m_data, 0);
  1135. mpz_init_set_ui(t, 0);
  1136. while (i)
  1137. {
  1138. mpz_set_ui(t, static_cast<unsigned long>(i & mask));
  1139. if (shift)
  1140. mpz_mul_2exp(t, t, shift);
  1141. mpz_add(m_data, m_data, t);
  1142. shift += std::numeric_limits<unsigned long>::digits;
  1143. i >>= std::numeric_limits<unsigned long>::digits;
  1144. }
  1145. mpz_clear(t);
  1146. return *this;
  1147. }
  1148. #endif
  1149. gmp_int& operator=(boost::long_long_type i)
  1150. {
  1151. if (m_data[0]._mp_d == 0)
  1152. mpz_init(this->m_data);
  1153. bool neg = i < 0;
  1154. *this = boost::multiprecision::detail::unsigned_abs(i);
  1155. if (neg)
  1156. mpz_neg(m_data, m_data);
  1157. return *this;
  1158. }
  1159. #endif
  1160. #ifdef BOOST_HAS_INT128
  1161. gmp_int& operator=(unsigned __int128 i)
  1162. {
  1163. if (m_data[0]._mp_d == 0)
  1164. mpz_init(this->m_data);
  1165. unsigned __int128 mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
  1166. unsigned shift = 0;
  1167. mpz_t t;
  1168. mpz_set_ui(m_data, 0);
  1169. mpz_init_set_ui(t, 0);
  1170. while (i)
  1171. {
  1172. mpz_set_ui(t, static_cast<unsigned long>(i & mask));
  1173. if (shift)
  1174. mpz_mul_2exp(t, t, shift);
  1175. mpz_add(m_data, m_data, t);
  1176. shift += std::numeric_limits<unsigned long>::digits;
  1177. i >>= std::numeric_limits<unsigned long>::digits;
  1178. }
  1179. mpz_clear(t);
  1180. return *this;
  1181. }
  1182. gmp_int& operator=(__int128 i)
  1183. {
  1184. if (m_data[0]._mp_d == 0)
  1185. mpz_init(this->m_data);
  1186. bool neg = i < 0;
  1187. *this = boost::multiprecision::detail::unsigned_abs(i);
  1188. if (neg)
  1189. mpz_neg(m_data, m_data);
  1190. return *this;
  1191. }
  1192. #endif
  1193. gmp_int& operator=(unsigned long i)
  1194. {
  1195. if (m_data[0]._mp_d == 0)
  1196. mpz_init(this->m_data);
  1197. mpz_set_ui(m_data, i);
  1198. return *this;
  1199. }
  1200. gmp_int& operator=(long i)
  1201. {
  1202. if (m_data[0]._mp_d == 0)
  1203. mpz_init(this->m_data);
  1204. mpz_set_si(m_data, i);
  1205. return *this;
  1206. }
  1207. gmp_int& operator=(double d)
  1208. {
  1209. if (m_data[0]._mp_d == 0)
  1210. mpz_init(this->m_data);
  1211. mpz_set_d(m_data, d);
  1212. return *this;
  1213. }
  1214. gmp_int& operator=(long double a)
  1215. {
  1216. using std::floor;
  1217. using std::frexp;
  1218. using std::ldexp;
  1219. if (m_data[0]._mp_d == 0)
  1220. mpz_init(this->m_data);
  1221. if (a == 0)
  1222. {
  1223. mpz_set_si(m_data, 0);
  1224. return *this;
  1225. }
  1226. if (a == 1)
  1227. {
  1228. mpz_set_si(m_data, 1);
  1229. return *this;
  1230. }
  1231. BOOST_ASSERT(!(boost::math::isinf)(a));
  1232. BOOST_ASSERT(!(boost::math::isnan)(a));
  1233. int e;
  1234. long double f, term;
  1235. mpz_set_ui(m_data, 0u);
  1236. f = frexp(a, &e);
  1237. constexpr const int shift = std::numeric_limits<int>::digits - 1;
  1238. while (f)
  1239. {
  1240. // extract int sized bits from f:
  1241. f = ldexp(f, shift);
  1242. term = floor(f);
  1243. e -= shift;
  1244. mpz_mul_2exp(m_data, m_data, shift);
  1245. if (term > 0)
  1246. mpz_add_ui(m_data, m_data, static_cast<unsigned>(term));
  1247. else
  1248. mpz_sub_ui(m_data, m_data, static_cast<unsigned>(-term));
  1249. f -= term;
  1250. }
  1251. if (e > 0)
  1252. mpz_mul_2exp(m_data, m_data, e);
  1253. else if (e < 0)
  1254. mpz_div_2exp(m_data, m_data, -e);
  1255. return *this;
  1256. }
  1257. gmp_int& operator=(const char* s)
  1258. {
  1259. if (m_data[0]._mp_d == 0)
  1260. mpz_init(this->m_data);
  1261. std::size_t n = s ? std::strlen(s) : 0;
  1262. int radix = 10;
  1263. if (n && (*s == '0'))
  1264. {
  1265. if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
  1266. {
  1267. radix = 16;
  1268. s += 2;
  1269. n -= 2;
  1270. }
  1271. else
  1272. {
  1273. radix = 8;
  1274. n -= 1;
  1275. }
  1276. }
  1277. if (n)
  1278. {
  1279. if (0 != mpz_set_str(m_data, s, radix))
  1280. BOOST_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid integer.")));
  1281. }
  1282. else
  1283. mpz_set_ui(m_data, 0);
  1284. return *this;
  1285. }
  1286. gmp_int& operator=(const mpf_t val)
  1287. {
  1288. if (m_data[0]._mp_d == 0)
  1289. mpz_init(this->m_data);
  1290. mpz_set_f(this->m_data, val);
  1291. return *this;
  1292. }
  1293. gmp_int& operator=(const mpz_t val)
  1294. {
  1295. if (m_data[0]._mp_d == 0)
  1296. mpz_init(this->m_data);
  1297. mpz_set(this->m_data, val);
  1298. return *this;
  1299. }
  1300. gmp_int& operator=(const mpq_t val)
  1301. {
  1302. if (m_data[0]._mp_d == 0)
  1303. mpz_init(this->m_data);
  1304. mpz_set_q(this->m_data, val);
  1305. return *this;
  1306. }
  1307. template <unsigned Digits10>
  1308. gmp_int& operator=(const gmp_float<Digits10>& o)
  1309. {
  1310. if (m_data[0]._mp_d == 0)
  1311. mpz_init(this->m_data);
  1312. mpz_set_f(this->m_data, o.data());
  1313. return *this;
  1314. }
  1315. gmp_int& operator=(const gmp_rational& o);
  1316. void swap(gmp_int& o)
  1317. {
  1318. mpz_swap(m_data, o.m_data);
  1319. }
  1320. std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
  1321. {
  1322. BOOST_ASSERT(m_data[0]._mp_d);
  1323. int base = 10;
  1324. if ((f & std::ios_base::oct) == std::ios_base::oct)
  1325. base = 8;
  1326. else if ((f & std::ios_base::hex) == std::ios_base::hex)
  1327. base = 16;
  1328. //
  1329. // sanity check, bases 8 and 16 are only available for positive numbers:
  1330. //
  1331. if ((base != 10) && (mpz_sgn(m_data) < 0))
  1332. BOOST_THROW_EXCEPTION(std::runtime_error("Formatted output in bases 8 or 16 is only available for positive numbers"));
  1333. void* (*alloc_func_ptr)(size_t);
  1334. void* (*realloc_func_ptr)(void*, size_t, size_t);
  1335. void (*free_func_ptr)(void*, size_t);
  1336. const char* ps = mpz_get_str(0, base, m_data);
  1337. std::string s = ps;
  1338. mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
  1339. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  1340. if (f & std::ios_base::uppercase)
  1341. for (size_t i = 0; i < s.length(); ++i)
  1342. s[i] = static_cast<char>(std::toupper(s[i]));
  1343. if ((base != 10) && (f & std::ios_base::showbase))
  1344. {
  1345. int pos = s[0] == '-' ? 1 : 0;
  1346. const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
  1347. s.insert(static_cast<std::string::size_type>(pos), pp);
  1348. }
  1349. if ((f & std::ios_base::showpos) && (s[0] != '-'))
  1350. s.insert(static_cast<std::string::size_type>(0), 1, '+');
  1351. return s;
  1352. }
  1353. ~gmp_int() noexcept
  1354. {
  1355. if (m_data[0]._mp_d)
  1356. mpz_clear(m_data);
  1357. }
  1358. void negate() noexcept
  1359. {
  1360. BOOST_ASSERT(m_data[0]._mp_d);
  1361. mpz_neg(m_data, m_data);
  1362. }
  1363. int compare(const gmp_int& o) const noexcept
  1364. {
  1365. BOOST_ASSERT(m_data[0]._mp_d && o.m_data[0]._mp_d);
  1366. return mpz_cmp(m_data, o.m_data);
  1367. }
  1368. int compare(long i) const noexcept
  1369. {
  1370. BOOST_ASSERT(m_data[0]._mp_d);
  1371. return mpz_cmp_si(m_data, i);
  1372. }
  1373. int compare(unsigned long i) const noexcept
  1374. {
  1375. BOOST_ASSERT(m_data[0]._mp_d);
  1376. return mpz_cmp_ui(m_data, i);
  1377. }
  1378. template <class V>
  1379. int compare(V v) const
  1380. {
  1381. gmp_int d;
  1382. d = v;
  1383. return compare(d);
  1384. }
  1385. mpz_t& data() noexcept
  1386. {
  1387. BOOST_ASSERT(m_data[0]._mp_d);
  1388. return m_data;
  1389. }
  1390. const mpz_t& data() const noexcept
  1391. {
  1392. BOOST_ASSERT(m_data[0]._mp_d);
  1393. return m_data;
  1394. }
  1395. protected:
  1396. mpz_t m_data;
  1397. };
  1398. template <class T>
  1399. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const gmp_int& a, const T& b)
  1400. {
  1401. return a.compare(b) == 0;
  1402. }
  1403. template <class T>
  1404. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const gmp_int& a, const T& b)
  1405. {
  1406. return a.compare(b) < 0;
  1407. }
  1408. template <class T>
  1409. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const gmp_int& a, const T& b)
  1410. {
  1411. return a.compare(b) > 0;
  1412. }
  1413. inline bool eval_is_zero(const gmp_int& val)
  1414. {
  1415. return mpz_sgn(val.data()) == 0;
  1416. }
  1417. inline void eval_add(gmp_int& t, const gmp_int& o)
  1418. {
  1419. mpz_add(t.data(), t.data(), o.data());
  1420. }
  1421. inline void eval_multiply_add(gmp_int& t, const gmp_int& a, const gmp_int& b)
  1422. {
  1423. mpz_addmul(t.data(), a.data(), b.data());
  1424. }
  1425. inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, const gmp_int& b)
  1426. {
  1427. mpz_submul(t.data(), a.data(), b.data());
  1428. }
  1429. inline void eval_subtract(gmp_int& t, const gmp_int& o)
  1430. {
  1431. mpz_sub(t.data(), t.data(), o.data());
  1432. }
  1433. inline void eval_multiply(gmp_int& t, const gmp_int& o)
  1434. {
  1435. mpz_mul(t.data(), t.data(), o.data());
  1436. }
  1437. inline void eval_divide(gmp_int& t, const gmp_int& o)
  1438. {
  1439. if (eval_is_zero(o))
  1440. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1441. mpz_tdiv_q(t.data(), t.data(), o.data());
  1442. }
  1443. inline void eval_modulus(gmp_int& t, const gmp_int& o)
  1444. {
  1445. mpz_tdiv_r(t.data(), t.data(), o.data());
  1446. }
  1447. inline void eval_add(gmp_int& t, unsigned long i)
  1448. {
  1449. mpz_add_ui(t.data(), t.data(), i);
  1450. }
  1451. inline void eval_multiply_add(gmp_int& t, const gmp_int& a, unsigned long i)
  1452. {
  1453. mpz_addmul_ui(t.data(), a.data(), i);
  1454. }
  1455. inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, unsigned long i)
  1456. {
  1457. mpz_submul_ui(t.data(), a.data(), i);
  1458. }
  1459. inline void eval_subtract(gmp_int& t, unsigned long i)
  1460. {
  1461. mpz_sub_ui(t.data(), t.data(), i);
  1462. }
  1463. inline void eval_multiply(gmp_int& t, unsigned long i)
  1464. {
  1465. mpz_mul_ui(t.data(), t.data(), i);
  1466. }
  1467. inline void eval_modulus(gmp_int& t, unsigned long i)
  1468. {
  1469. mpz_tdiv_r_ui(t.data(), t.data(), i);
  1470. }
  1471. inline void eval_divide(gmp_int& t, unsigned long i)
  1472. {
  1473. if (i == 0)
  1474. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1475. mpz_tdiv_q_ui(t.data(), t.data(), i);
  1476. }
  1477. inline void eval_add(gmp_int& t, long i)
  1478. {
  1479. if (i > 0)
  1480. mpz_add_ui(t.data(), t.data(), i);
  1481. else
  1482. mpz_sub_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
  1483. }
  1484. inline void eval_multiply_add(gmp_int& t, const gmp_int& a, long i)
  1485. {
  1486. if (i > 0)
  1487. mpz_addmul_ui(t.data(), a.data(), i);
  1488. else
  1489. mpz_submul_ui(t.data(), a.data(), boost::multiprecision::detail::unsigned_abs(i));
  1490. }
  1491. inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, long i)
  1492. {
  1493. if (i > 0)
  1494. mpz_submul_ui(t.data(), a.data(), i);
  1495. else
  1496. mpz_addmul_ui(t.data(), a.data(), boost::multiprecision::detail::unsigned_abs(i));
  1497. }
  1498. inline void eval_subtract(gmp_int& t, long i)
  1499. {
  1500. if (i > 0)
  1501. mpz_sub_ui(t.data(), t.data(), i);
  1502. else
  1503. mpz_add_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
  1504. }
  1505. inline void eval_multiply(gmp_int& t, long i)
  1506. {
  1507. mpz_mul_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
  1508. if (i < 0)
  1509. mpz_neg(t.data(), t.data());
  1510. }
  1511. inline void eval_modulus(gmp_int& t, long i)
  1512. {
  1513. mpz_tdiv_r_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
  1514. }
  1515. inline void eval_divide(gmp_int& t, long i)
  1516. {
  1517. if (i == 0)
  1518. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1519. mpz_tdiv_q_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
  1520. if (i < 0)
  1521. mpz_neg(t.data(), t.data());
  1522. }
  1523. template <class UI>
  1524. inline void eval_left_shift(gmp_int& t, UI i)
  1525. {
  1526. mpz_mul_2exp(t.data(), t.data(), static_cast<unsigned long>(i));
  1527. }
  1528. template <class UI>
  1529. inline void eval_right_shift(gmp_int& t, UI i)
  1530. {
  1531. mpz_fdiv_q_2exp(t.data(), t.data(), static_cast<unsigned long>(i));
  1532. }
  1533. template <class UI>
  1534. inline void eval_left_shift(gmp_int& t, const gmp_int& v, UI i)
  1535. {
  1536. mpz_mul_2exp(t.data(), v.data(), static_cast<unsigned long>(i));
  1537. }
  1538. template <class UI>
  1539. inline void eval_right_shift(gmp_int& t, const gmp_int& v, UI i)
  1540. {
  1541. mpz_fdiv_q_2exp(t.data(), v.data(), static_cast<unsigned long>(i));
  1542. }
  1543. inline void eval_bitwise_and(gmp_int& result, const gmp_int& v)
  1544. {
  1545. mpz_and(result.data(), result.data(), v.data());
  1546. }
  1547. inline void eval_bitwise_or(gmp_int& result, const gmp_int& v)
  1548. {
  1549. mpz_ior(result.data(), result.data(), v.data());
  1550. }
  1551. inline void eval_bitwise_xor(gmp_int& result, const gmp_int& v)
  1552. {
  1553. mpz_xor(result.data(), result.data(), v.data());
  1554. }
  1555. inline void eval_add(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1556. {
  1557. mpz_add(t.data(), p.data(), o.data());
  1558. }
  1559. inline void eval_subtract(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1560. {
  1561. mpz_sub(t.data(), p.data(), o.data());
  1562. }
  1563. inline void eval_multiply(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1564. {
  1565. mpz_mul(t.data(), p.data(), o.data());
  1566. }
  1567. inline void eval_divide(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1568. {
  1569. if (eval_is_zero(o))
  1570. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1571. mpz_tdiv_q(t.data(), p.data(), o.data());
  1572. }
  1573. inline void eval_modulus(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1574. {
  1575. mpz_tdiv_r(t.data(), p.data(), o.data());
  1576. }
  1577. inline void eval_add(gmp_int& t, const gmp_int& p, unsigned long i)
  1578. {
  1579. mpz_add_ui(t.data(), p.data(), i);
  1580. }
  1581. inline void eval_subtract(gmp_int& t, const gmp_int& p, unsigned long i)
  1582. {
  1583. mpz_sub_ui(t.data(), p.data(), i);
  1584. }
  1585. inline void eval_multiply(gmp_int& t, const gmp_int& p, unsigned long i)
  1586. {
  1587. mpz_mul_ui(t.data(), p.data(), i);
  1588. }
  1589. inline void eval_modulus(gmp_int& t, const gmp_int& p, unsigned long i)
  1590. {
  1591. mpz_tdiv_r_ui(t.data(), p.data(), i);
  1592. }
  1593. inline void eval_divide(gmp_int& t, const gmp_int& p, unsigned long i)
  1594. {
  1595. if (i == 0)
  1596. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1597. mpz_tdiv_q_ui(t.data(), p.data(), i);
  1598. }
  1599. inline void eval_add(gmp_int& t, const gmp_int& p, long i)
  1600. {
  1601. if (i > 0)
  1602. mpz_add_ui(t.data(), p.data(), i);
  1603. else
  1604. mpz_sub_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
  1605. }
  1606. inline void eval_subtract(gmp_int& t, const gmp_int& p, long i)
  1607. {
  1608. if (i > 0)
  1609. mpz_sub_ui(t.data(), p.data(), i);
  1610. else
  1611. mpz_add_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
  1612. }
  1613. inline void eval_multiply(gmp_int& t, const gmp_int& p, long i)
  1614. {
  1615. mpz_mul_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
  1616. if (i < 0)
  1617. mpz_neg(t.data(), t.data());
  1618. }
  1619. inline void eval_modulus(gmp_int& t, const gmp_int& p, long i)
  1620. {
  1621. mpz_tdiv_r_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
  1622. }
  1623. inline void eval_divide(gmp_int& t, const gmp_int& p, long i)
  1624. {
  1625. if (i == 0)
  1626. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1627. mpz_tdiv_q_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
  1628. if (i < 0)
  1629. mpz_neg(t.data(), t.data());
  1630. }
  1631. inline void eval_bitwise_and(gmp_int& result, const gmp_int& u, const gmp_int& v)
  1632. {
  1633. mpz_and(result.data(), u.data(), v.data());
  1634. }
  1635. inline void eval_bitwise_or(gmp_int& result, const gmp_int& u, const gmp_int& v)
  1636. {
  1637. mpz_ior(result.data(), u.data(), v.data());
  1638. }
  1639. inline void eval_bitwise_xor(gmp_int& result, const gmp_int& u, const gmp_int& v)
  1640. {
  1641. mpz_xor(result.data(), u.data(), v.data());
  1642. }
  1643. inline void eval_complement(gmp_int& result, const gmp_int& u)
  1644. {
  1645. mpz_com(result.data(), u.data());
  1646. }
  1647. inline int eval_get_sign(const gmp_int& val)
  1648. {
  1649. return mpz_sgn(val.data());
  1650. }
  1651. inline void eval_convert_to(unsigned long* result, const gmp_int& val)
  1652. {
  1653. if (mpz_sgn(val.data()) < 0)
  1654. {
  1655. BOOST_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
  1656. }
  1657. else
  1658. *result = (unsigned long)mpz_get_ui(val.data());
  1659. }
  1660. inline void eval_convert_to(long* result, const gmp_int& val)
  1661. {
  1662. if (0 == mpz_fits_slong_p(val.data()))
  1663. {
  1664. *result = mpz_sgn(val.data()) < 0 ? (std::numeric_limits<long>::min)() : (std::numeric_limits<long>::max)();
  1665. }
  1666. else
  1667. *result = (signed long)mpz_get_si(val.data());
  1668. }
  1669. inline void eval_convert_to(double* result, const gmp_int& val)
  1670. {
  1671. *result = mpz_get_d(val.data());
  1672. }
  1673. #ifdef BOOST_HAS_LONG_LONG
  1674. inline void eval_convert_to(boost::ulong_long_type* result, const gmp_int& val)
  1675. {
  1676. if (mpz_sgn(val.data()) < 0)
  1677. {
  1678. BOOST_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
  1679. }
  1680. *result = 0;
  1681. gmp_int t(val);
  1682. unsigned parts = sizeof(boost::ulong_long_type) / sizeof(unsigned long);
  1683. for (unsigned i = 0; i < parts; ++i)
  1684. {
  1685. boost::ulong_long_type part = mpz_get_ui(t.data());
  1686. if (i)
  1687. *result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
  1688. else
  1689. *result = part;
  1690. mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
  1691. }
  1692. }
  1693. inline void eval_convert_to(boost::long_long_type* result, const gmp_int& val)
  1694. {
  1695. int s = mpz_sgn(val.data());
  1696. *result = 0;
  1697. gmp_int t(val);
  1698. unsigned parts = sizeof(boost::ulong_long_type) / sizeof(unsigned long);
  1699. boost::ulong_long_type unsigned_result = 0;
  1700. for (unsigned i = 0; i < parts; ++i)
  1701. {
  1702. boost::ulong_long_type part = mpz_get_ui(t.data());
  1703. if (i)
  1704. unsigned_result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
  1705. else
  1706. unsigned_result = part;
  1707. mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
  1708. }
  1709. //
  1710. // Overflow check:
  1711. //
  1712. bool overflow = false;
  1713. if (mpz_sgn(t.data()))
  1714. {
  1715. overflow = true;
  1716. }
  1717. if ((s > 0) && (unsigned_result > static_cast<boost::ulong_long_type>((std::numeric_limits<boost::long_long_type>::max)())))
  1718. overflow = true;
  1719. if((s < 0) && (unsigned_result > 1u - static_cast<boost::ulong_long_type>((std::numeric_limits<boost::long_long_type>::min)() + 1)))
  1720. overflow = true;
  1721. if(overflow)
  1722. *result = s < 0 ? (std::numeric_limits<boost::long_long_type>::min)() : (std::numeric_limits<boost::long_long_type>::max)();
  1723. else
  1724. *result = s < 0 ? -boost::long_long_type(unsigned_result - 1) - 1 : unsigned_result;
  1725. }
  1726. #endif
  1727. #ifdef BOOST_HAS_INT128
  1728. inline void eval_convert_to(unsigned __int128* result, const gmp_int& val)
  1729. {
  1730. if (mpz_sgn(val.data()) < 0)
  1731. {
  1732. BOOST_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
  1733. }
  1734. *result = 0;
  1735. gmp_int t(val);
  1736. unsigned parts = sizeof(unsigned __int128) / sizeof(unsigned long);
  1737. for (unsigned i = 0; i < parts; ++i)
  1738. {
  1739. unsigned __int128 part = mpz_get_ui(t.data());
  1740. if (i)
  1741. *result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
  1742. else
  1743. *result = part;
  1744. mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
  1745. }
  1746. }
  1747. inline void eval_convert_to(__int128* result, const gmp_int& val)
  1748. {
  1749. int s = mpz_sgn(val.data());
  1750. *result = 0;
  1751. gmp_int t(val);
  1752. unsigned parts = sizeof(unsigned __int128) / sizeof(unsigned long);
  1753. unsigned __int128 unsigned_result = 0;
  1754. for (unsigned i = 0; i < parts; ++i)
  1755. {
  1756. unsigned __int128 part = mpz_get_ui(t.data());
  1757. if (i)
  1758. unsigned_result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
  1759. else
  1760. unsigned_result = part;
  1761. mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
  1762. }
  1763. //
  1764. // Overflow check:
  1765. //
  1766. constexpr const __int128 int128_max = static_cast<__int128>((static_cast<unsigned __int128>(1u) << 127) - 1);
  1767. constexpr const __int128 int128_min = (static_cast<unsigned __int128>(1u) << 127);
  1768. bool overflow = false;
  1769. if (mpz_sgn(t.data()))
  1770. {
  1771. overflow = true;
  1772. }
  1773. if ((s > 0) && (unsigned_result > static_cast<unsigned __int128>(int128_max)))
  1774. overflow = true;
  1775. if ((s < 0) && (unsigned_result > 1u - static_cast<unsigned __int128>(int128_min + 1)))
  1776. overflow = true;
  1777. if (overflow)
  1778. *result = s < 0 ? int128_min : int128_max;
  1779. else
  1780. *result = s < 0 ? -__int128(unsigned_result - 1) - 1 : unsigned_result;
  1781. }
  1782. #endif
  1783. inline void eval_abs(gmp_int& result, const gmp_int& val)
  1784. {
  1785. mpz_abs(result.data(), val.data());
  1786. }
  1787. inline void eval_gcd(gmp_int& result, const gmp_int& a, const gmp_int& b)
  1788. {
  1789. mpz_gcd(result.data(), a.data(), b.data());
  1790. }
  1791. inline void eval_lcm(gmp_int& result, const gmp_int& a, const gmp_int& b)
  1792. {
  1793. mpz_lcm(result.data(), a.data(), b.data());
  1794. }
  1795. template <class I>
  1796. inline typename std::enable_if<(boost::multiprecision::detail::is_unsigned<I>::value && (sizeof(I) <= sizeof(unsigned long)))>::type eval_gcd(gmp_int& result, const gmp_int& a, const I b)
  1797. {
  1798. mpz_gcd_ui(result.data(), a.data(), b);
  1799. }
  1800. template <class I>
  1801. inline typename std::enable_if<(boost::multiprecision::detail::is_unsigned<I>::value && (sizeof(I) <= sizeof(unsigned long)))>::type eval_lcm(gmp_int& result, const gmp_int& a, const I b)
  1802. {
  1803. mpz_lcm_ui(result.data(), a.data(), b);
  1804. }
  1805. template <class I>
  1806. inline typename std::enable_if<(boost::multiprecision::detail::is_signed<I>::value && boost::multiprecision::detail::is_integral<I>::value && (sizeof(I) <= sizeof(long)))>::type eval_gcd(gmp_int& result, const gmp_int& a, const I b)
  1807. {
  1808. mpz_gcd_ui(result.data(), a.data(), boost::multiprecision::detail::unsigned_abs(b));
  1809. }
  1810. template <class I>
  1811. inline typename std::enable_if<boost::multiprecision::detail::is_signed<I>::value && boost::multiprecision::detail::is_integral<I>::value && ((sizeof(I) <= sizeof(long)))>::type eval_lcm(gmp_int& result, const gmp_int& a, const I b)
  1812. {
  1813. mpz_lcm_ui(result.data(), a.data(), boost::multiprecision::detail::unsigned_abs(b));
  1814. }
  1815. inline void eval_integer_sqrt(gmp_int& s, gmp_int& r, const gmp_int& x)
  1816. {
  1817. mpz_sqrtrem(s.data(), r.data(), x.data());
  1818. }
  1819. inline unsigned eval_lsb(const gmp_int& val)
  1820. {
  1821. int c = eval_get_sign(val);
  1822. if (c == 0)
  1823. {
  1824. BOOST_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
  1825. }
  1826. if (c < 0)
  1827. {
  1828. BOOST_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
  1829. }
  1830. return static_cast<unsigned>(mpz_scan1(val.data(), 0));
  1831. }
  1832. inline unsigned eval_msb(const gmp_int& val)
  1833. {
  1834. int c = eval_get_sign(val);
  1835. if (c == 0)
  1836. {
  1837. BOOST_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
  1838. }
  1839. if (c < 0)
  1840. {
  1841. BOOST_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
  1842. }
  1843. return static_cast<unsigned>(mpz_sizeinbase(val.data(), 2) - 1);
  1844. }
  1845. inline bool eval_bit_test(const gmp_int& val, unsigned index)
  1846. {
  1847. return mpz_tstbit(val.data(), index) ? true : false;
  1848. }
  1849. inline void eval_bit_set(gmp_int& val, unsigned index)
  1850. {
  1851. mpz_setbit(val.data(), index);
  1852. }
  1853. inline void eval_bit_unset(gmp_int& val, unsigned index)
  1854. {
  1855. mpz_clrbit(val.data(), index);
  1856. }
  1857. inline void eval_bit_flip(gmp_int& val, unsigned index)
  1858. {
  1859. mpz_combit(val.data(), index);
  1860. }
  1861. inline void eval_qr(const gmp_int& x, const gmp_int& y,
  1862. gmp_int& q, gmp_int& r)
  1863. {
  1864. mpz_tdiv_qr(q.data(), r.data(), x.data(), y.data());
  1865. }
  1866. template <class Integer>
  1867. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<Integer>::value, Integer>::type eval_integer_modulus(const gmp_int& x, Integer val)
  1868. {
  1869. #if defined(__MPIR_VERSION) && (__MPIR_VERSION >= 3)
  1870. if ((sizeof(Integer) <= sizeof(mpir_ui)) || (val <= (std::numeric_limits<mpir_ui>::max)()))
  1871. #else
  1872. if ((sizeof(Integer) <= sizeof(long)) || (val <= (std::numeric_limits<unsigned long>::max)()))
  1873. #endif
  1874. {
  1875. return static_cast<Integer>(mpz_tdiv_ui(x.data(), val));
  1876. }
  1877. else
  1878. {
  1879. return default_ops::eval_integer_modulus(x, val);
  1880. }
  1881. }
  1882. template <class Integer>
  1883. inline typename std::enable_if<boost::multiprecision::detail::is_signed<Integer>::value && boost::multiprecision::detail::is_integral<Integer>::value, Integer>::type eval_integer_modulus(const gmp_int& x, Integer val)
  1884. {
  1885. return eval_integer_modulus(x, boost::multiprecision::detail::unsigned_abs(val));
  1886. }
  1887. inline void eval_powm(gmp_int& result, const gmp_int& base, const gmp_int& p, const gmp_int& m)
  1888. {
  1889. if (eval_get_sign(p) < 0)
  1890. {
  1891. BOOST_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
  1892. }
  1893. mpz_powm(result.data(), base.data(), p.data(), m.data());
  1894. }
  1895. template <class Integer>
  1896. inline typename std::enable_if<
  1897. boost::multiprecision::detail::is_unsigned<Integer>::value && (sizeof(Integer) <= sizeof(unsigned long))>::type
  1898. eval_powm(gmp_int& result, const gmp_int& base, Integer p, const gmp_int& m)
  1899. {
  1900. mpz_powm_ui(result.data(), base.data(), p, m.data());
  1901. }
  1902. template <class Integer>
  1903. inline typename std::enable_if<boost::multiprecision::detail::is_signed<Integer>::value && boost::multiprecision::detail::is_integral<Integer>::value && (sizeof(Integer) <= sizeof(unsigned long))>::type
  1904. eval_powm(gmp_int& result, const gmp_int& base, Integer p, const gmp_int& m)
  1905. {
  1906. if (p < 0)
  1907. {
  1908. BOOST_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
  1909. }
  1910. mpz_powm_ui(result.data(), base.data(), p, m.data());
  1911. }
  1912. inline std::size_t hash_value(const gmp_int& val)
  1913. {
  1914. // We should really use mpz_limbs_read here, but that's unsupported on older versions:
  1915. std::size_t result = 0;
  1916. for (int i = 0; i < std::abs(val.data()[0]._mp_size); ++i)
  1917. boost::hash_combine(result, val.data()[0]._mp_d[i]);
  1918. boost::hash_combine(result, val.data()[0]._mp_size);
  1919. return result;
  1920. }
  1921. struct gmp_rational;
  1922. void eval_add(gmp_rational& t, const gmp_rational& o);
  1923. struct gmp_rational
  1924. {
  1925. #ifdef BOOST_HAS_LONG_LONG
  1926. using signed_types = std::tuple<long, boost::long_long_type> ;
  1927. using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
  1928. #else
  1929. using signed_types = std::tuple<long> ;
  1930. using unsigned_types = std::tuple<unsigned long>;
  1931. #endif
  1932. using float_types = std::tuple<double, long double>;
  1933. gmp_rational()
  1934. {
  1935. mpq_init(this->m_data);
  1936. }
  1937. gmp_rational(const gmp_rational& o)
  1938. {
  1939. mpq_init(m_data);
  1940. if (o.m_data[0]._mp_num._mp_d)
  1941. mpq_set(m_data, o.m_data);
  1942. }
  1943. gmp_rational(const gmp_int& o)
  1944. {
  1945. mpq_init(m_data);
  1946. mpq_set_z(m_data, o.data());
  1947. }
  1948. // rvalue copy
  1949. gmp_rational(gmp_rational&& o) noexcept
  1950. {
  1951. m_data[0] = o.m_data[0];
  1952. o.m_data[0]._mp_num._mp_d = 0;
  1953. o.m_data[0]._mp_den._mp_d = 0;
  1954. }
  1955. gmp_rational(const mpq_t o)
  1956. {
  1957. mpq_init(m_data);
  1958. mpq_set(m_data, o);
  1959. }
  1960. gmp_rational(const mpz_t o)
  1961. {
  1962. mpq_init(m_data);
  1963. mpq_set_z(m_data, o);
  1964. }
  1965. gmp_rational& operator=(const gmp_rational& o)
  1966. {
  1967. if (m_data[0]._mp_den._mp_d == 0)
  1968. mpq_init(m_data);
  1969. mpq_set(m_data, o.m_data);
  1970. return *this;
  1971. }
  1972. // rvalue assign
  1973. gmp_rational& operator=(gmp_rational&& o) noexcept
  1974. {
  1975. mpq_swap(m_data, o.m_data);
  1976. return *this;
  1977. }
  1978. #ifdef BOOST_HAS_LONG_LONG
  1979. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  1980. gmp_rational& operator=(boost::ulong_long_type i)
  1981. {
  1982. *this = static_cast<unsigned long>(i);
  1983. return *this;
  1984. }
  1985. #else
  1986. gmp_rational& operator=(boost::ulong_long_type i)
  1987. {
  1988. if (m_data[0]._mp_den._mp_d == 0)
  1989. mpq_init(m_data);
  1990. gmp_int zi;
  1991. zi = i;
  1992. mpq_set_z(m_data, zi.data());
  1993. return *this;
  1994. }
  1995. gmp_rational& operator=(boost::long_long_type i)
  1996. {
  1997. if (m_data[0]._mp_den._mp_d == 0)
  1998. mpq_init(m_data);
  1999. bool neg = i < 0;
  2000. *this = boost::multiprecision::detail::unsigned_abs(i);
  2001. if (neg)
  2002. mpq_neg(m_data, m_data);
  2003. return *this;
  2004. }
  2005. #endif
  2006. #endif
  2007. gmp_rational& operator=(unsigned long i)
  2008. {
  2009. if (m_data[0]._mp_den._mp_d == 0)
  2010. mpq_init(m_data);
  2011. mpq_set_ui(m_data, i, 1);
  2012. return *this;
  2013. }
  2014. gmp_rational& operator=(long i)
  2015. {
  2016. if (m_data[0]._mp_den._mp_d == 0)
  2017. mpq_init(m_data);
  2018. mpq_set_si(m_data, i, 1);
  2019. return *this;
  2020. }
  2021. gmp_rational& operator=(double d)
  2022. {
  2023. if (m_data[0]._mp_den._mp_d == 0)
  2024. mpq_init(m_data);
  2025. mpq_set_d(m_data, d);
  2026. return *this;
  2027. }
  2028. gmp_rational& operator=(long double a)
  2029. {
  2030. using default_ops::eval_add;
  2031. using default_ops::eval_subtract;
  2032. using std::floor;
  2033. using std::frexp;
  2034. using std::ldexp;
  2035. if (m_data[0]._mp_den._mp_d == 0)
  2036. mpq_init(m_data);
  2037. if (a == 0)
  2038. {
  2039. mpq_set_si(m_data, 0, 1);
  2040. return *this;
  2041. }
  2042. if (a == 1)
  2043. {
  2044. mpq_set_si(m_data, 1, 1);
  2045. return *this;
  2046. }
  2047. BOOST_ASSERT(!(boost::math::isinf)(a));
  2048. BOOST_ASSERT(!(boost::math::isnan)(a));
  2049. int e;
  2050. long double f, term;
  2051. mpq_set_ui(m_data, 0, 1);
  2052. mpq_set_ui(m_data, 0u, 1);
  2053. gmp_rational t;
  2054. f = frexp(a, &e);
  2055. constexpr const int shift = std::numeric_limits<int>::digits - 1;
  2056. while (f)
  2057. {
  2058. // extract int sized bits from f:
  2059. f = ldexp(f, shift);
  2060. term = floor(f);
  2061. e -= shift;
  2062. mpq_mul_2exp(m_data, m_data, shift);
  2063. t = static_cast<long>(term);
  2064. eval_add(*this, t);
  2065. f -= term;
  2066. }
  2067. if (e > 0)
  2068. mpq_mul_2exp(m_data, m_data, e);
  2069. else if (e < 0)
  2070. mpq_div_2exp(m_data, m_data, -e);
  2071. return *this;
  2072. }
  2073. gmp_rational& operator=(const char* s)
  2074. {
  2075. if (m_data[0]._mp_den._mp_d == 0)
  2076. mpq_init(m_data);
  2077. if (0 != mpq_set_str(m_data, s, 10))
  2078. BOOST_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid rational number.")));
  2079. return *this;
  2080. }
  2081. gmp_rational& operator=(const gmp_int& o)
  2082. {
  2083. if (m_data[0]._mp_den._mp_d == 0)
  2084. mpq_init(m_data);
  2085. mpq_set_z(m_data, o.data());
  2086. return *this;
  2087. }
  2088. gmp_rational& operator=(const mpq_t o)
  2089. {
  2090. if (m_data[0]._mp_den._mp_d == 0)
  2091. mpq_init(m_data);
  2092. mpq_set(m_data, o);
  2093. return *this;
  2094. }
  2095. gmp_rational& operator=(const mpz_t o)
  2096. {
  2097. if (m_data[0]._mp_den._mp_d == 0)
  2098. mpq_init(m_data);
  2099. mpq_set_z(m_data, o);
  2100. return *this;
  2101. }
  2102. void swap(gmp_rational& o)
  2103. {
  2104. mpq_swap(m_data, o.m_data);
  2105. }
  2106. std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags /*f*/) const
  2107. {
  2108. BOOST_ASSERT(m_data[0]._mp_num._mp_d);
  2109. // TODO make a better job of this including handling of f!!
  2110. void* (*alloc_func_ptr)(size_t);
  2111. void* (*realloc_func_ptr)(void*, size_t, size_t);
  2112. void (*free_func_ptr)(void*, size_t);
  2113. const char* ps = mpq_get_str(0, 10, m_data);
  2114. std::string s = ps;
  2115. mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
  2116. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  2117. return s;
  2118. }
  2119. ~gmp_rational()
  2120. {
  2121. if (m_data[0]._mp_num._mp_d || m_data[0]._mp_den._mp_d)
  2122. mpq_clear(m_data);
  2123. }
  2124. void negate()
  2125. {
  2126. BOOST_ASSERT(m_data[0]._mp_num._mp_d);
  2127. mpq_neg(m_data, m_data);
  2128. }
  2129. int compare(const gmp_rational& o) const
  2130. {
  2131. BOOST_ASSERT(m_data[0]._mp_num._mp_d && o.m_data[0]._mp_num._mp_d);
  2132. return mpq_cmp(m_data, o.m_data);
  2133. }
  2134. template <class V>
  2135. int compare(V v) const
  2136. {
  2137. gmp_rational d;
  2138. d = v;
  2139. return compare(d);
  2140. }
  2141. int compare(unsigned long v) const
  2142. {
  2143. BOOST_ASSERT(m_data[0]._mp_num._mp_d);
  2144. return mpq_cmp_ui(m_data, v, 1);
  2145. }
  2146. int compare(long v) const
  2147. {
  2148. BOOST_ASSERT(m_data[0]._mp_num._mp_d);
  2149. return mpq_cmp_si(m_data, v, 1);
  2150. }
  2151. mpq_t& data()
  2152. {
  2153. BOOST_ASSERT(m_data[0]._mp_num._mp_d);
  2154. return m_data;
  2155. }
  2156. const mpq_t& data() const
  2157. {
  2158. BOOST_ASSERT(m_data[0]._mp_num._mp_d);
  2159. return m_data;
  2160. }
  2161. protected:
  2162. mpq_t m_data;
  2163. };
  2164. inline bool eval_is_zero(const gmp_rational& val)
  2165. {
  2166. return mpq_sgn(val.data()) == 0;
  2167. }
  2168. template <class T>
  2169. inline bool eval_eq(gmp_rational& a, const T& b)
  2170. {
  2171. return a.compare(b) == 0;
  2172. }
  2173. template <class T>
  2174. inline bool eval_lt(gmp_rational& a, const T& b)
  2175. {
  2176. return a.compare(b) < 0;
  2177. }
  2178. template <class T>
  2179. inline bool eval_gt(gmp_rational& a, const T& b)
  2180. {
  2181. return a.compare(b) > 0;
  2182. }
  2183. inline void eval_add(gmp_rational& t, const gmp_rational& o)
  2184. {
  2185. mpq_add(t.data(), t.data(), o.data());
  2186. }
  2187. inline void eval_subtract(gmp_rational& t, const gmp_rational& o)
  2188. {
  2189. mpq_sub(t.data(), t.data(), o.data());
  2190. }
  2191. inline void eval_multiply(gmp_rational& t, const gmp_rational& o)
  2192. {
  2193. mpq_mul(t.data(), t.data(), o.data());
  2194. }
  2195. inline void eval_divide(gmp_rational& t, const gmp_rational& o)
  2196. {
  2197. if (eval_is_zero(o))
  2198. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2199. mpq_div(t.data(), t.data(), o.data());
  2200. }
  2201. inline void eval_add(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
  2202. {
  2203. mpq_add(t.data(), p.data(), o.data());
  2204. }
  2205. inline void eval_subtract(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
  2206. {
  2207. mpq_sub(t.data(), p.data(), o.data());
  2208. }
  2209. inline void eval_multiply(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
  2210. {
  2211. mpq_mul(t.data(), p.data(), o.data());
  2212. }
  2213. inline void eval_divide(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
  2214. {
  2215. if (eval_is_zero(o))
  2216. BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2217. mpq_div(t.data(), p.data(), o.data());
  2218. }
  2219. inline int eval_get_sign(const gmp_rational& val)
  2220. {
  2221. return mpq_sgn(val.data());
  2222. }
  2223. template <class R>
  2224. inline typename std::enable_if<number_category<R>::value == number_kind_floating_point>::type eval_convert_to(R* result, const gmp_rational& backend)
  2225. {
  2226. //
  2227. // The generic conversion is as good as anything we can write here:
  2228. //
  2229. // This does not round correctly:
  2230. //
  2231. //*result = mpq_get_d(val.data());
  2232. //
  2233. // This does:
  2234. //
  2235. ::boost::multiprecision::detail::generic_convert_rational_to_float(*result, backend);
  2236. }
  2237. #ifdef BOOST_HAS_FLOAT128
  2238. inline void eval_convert_to(__float128* result, const gmp_rational& val)
  2239. {
  2240. using default_ops::eval_convert_to;
  2241. gmp_int n, d;
  2242. __float128 fn, fd;
  2243. mpz_set(n.data(), mpq_numref(val.data()));
  2244. mpz_set(d.data(), mpq_denref(val.data()));
  2245. eval_convert_to(&fn, n);
  2246. eval_convert_to(&fd, d);
  2247. *result = fn / fd;
  2248. }
  2249. #endif
  2250. inline void eval_convert_to(long* result, const gmp_rational& val)
  2251. {
  2252. double r;
  2253. eval_convert_to(&r, val);
  2254. *result = static_cast<long>(r);
  2255. }
  2256. inline void eval_convert_to(unsigned long* result, const gmp_rational& val)
  2257. {
  2258. double r;
  2259. eval_convert_to(&r, val);
  2260. *result = static_cast<long>(r);
  2261. }
  2262. inline void eval_abs(gmp_rational& result, const gmp_rational& val)
  2263. {
  2264. mpq_abs(result.data(), val.data());
  2265. }
  2266. inline void assign_components(gmp_rational& result, unsigned long v1, unsigned long v2)
  2267. {
  2268. mpq_set_ui(result.data(), v1, v2);
  2269. mpq_canonicalize(result.data());
  2270. }
  2271. inline void assign_components(gmp_rational& result, long v1, long v2)
  2272. {
  2273. mpq_set_si(result.data(), v1, v2);
  2274. mpq_canonicalize(result.data());
  2275. }
  2276. inline void assign_components(gmp_rational& result, gmp_int const& v1, gmp_int const& v2)
  2277. {
  2278. mpz_set(mpq_numref(result.data()), v1.data());
  2279. mpz_set(mpq_denref(result.data()), v2.data());
  2280. mpq_canonicalize(result.data());
  2281. }
  2282. inline std::size_t hash_value(const gmp_rational& val)
  2283. {
  2284. std::size_t result = 0;
  2285. for (int i = 0; i < std::abs(val.data()[0]._mp_num._mp_size); ++i)
  2286. boost::hash_combine(result, val.data()[0]._mp_num._mp_d[i]);
  2287. for (int i = 0; i < std::abs(val.data()[0]._mp_den._mp_size); ++i)
  2288. boost::hash_combine(result, val.data()[0]._mp_den._mp_d[i]);
  2289. boost::hash_combine(result, val.data()[0]._mp_num._mp_size);
  2290. return result;
  2291. }
  2292. //
  2293. // Some member functions that are dependent upon previous code go here:
  2294. //
  2295. template <unsigned Digits10>
  2296. template <unsigned D>
  2297. inline gmp_float<Digits10>::gmp_float(const gmp_float<D>& o, typename std::enable_if<D <= Digits10>::type*)
  2298. {
  2299. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2300. mpf_set(this->m_data, o.data());
  2301. }
  2302. template <unsigned Digits10>
  2303. template <unsigned D>
  2304. inline gmp_float<Digits10>::gmp_float(const gmp_float<D>& o, typename std::enable_if< !(D <= Digits10)>::type*)
  2305. {
  2306. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2307. mpf_set(this->m_data, o.data());
  2308. }
  2309. template <unsigned Digits10>
  2310. inline gmp_float<Digits10>::gmp_float(const gmp_int& o)
  2311. {
  2312. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2313. mpf_set_z(this->data(), o.data());
  2314. }
  2315. template <unsigned Digits10>
  2316. inline gmp_float<Digits10>::gmp_float(const gmp_rational& o)
  2317. {
  2318. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2319. mpf_set_q(this->data(), o.data());
  2320. }
  2321. template <unsigned Digits10>
  2322. template <unsigned D>
  2323. inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_float<D>& o)
  2324. {
  2325. if (this->m_data[0]._mp_d == 0)
  2326. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2327. mpf_set(this->m_data, o.data());
  2328. return *this;
  2329. }
  2330. template <unsigned Digits10>
  2331. inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_int& o)
  2332. {
  2333. if (this->m_data[0]._mp_d == 0)
  2334. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2335. mpf_set_z(this->data(), o.data());
  2336. return *this;
  2337. }
  2338. template <unsigned Digits10>
  2339. inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_rational& o)
  2340. {
  2341. if (this->m_data[0]._mp_d == 0)
  2342. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2343. mpf_set_q(this->data(), o.data());
  2344. return *this;
  2345. }
  2346. inline gmp_float<0>::gmp_float(const gmp_int& o) : requested_precision(get_default_precision())
  2347. {
  2348. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  2349. mpf_set_z(this->data(), o.data());
  2350. }
  2351. inline gmp_float<0>::gmp_float(const gmp_rational& o) : requested_precision(get_default_precision())
  2352. {
  2353. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  2354. mpf_set_q(this->data(), o.data());
  2355. }
  2356. inline gmp_float<0>& gmp_float<0>::operator=(const gmp_int& o)
  2357. {
  2358. if (this->m_data[0]._mp_d == 0)
  2359. {
  2360. requested_precision = this->get_default_precision();
  2361. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  2362. }
  2363. mpf_set_z(this->data(), o.data());
  2364. return *this;
  2365. }
  2366. inline gmp_float<0>& gmp_float<0>::operator=(const gmp_rational& o)
  2367. {
  2368. if (this->m_data[0]._mp_d == 0)
  2369. {
  2370. requested_precision = this->get_default_precision();
  2371. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  2372. }
  2373. mpf_set_q(this->data(), o.data());
  2374. return *this;
  2375. }
  2376. inline gmp_int::gmp_int(const gmp_rational& o)
  2377. {
  2378. mpz_init(this->m_data);
  2379. mpz_set_q(this->m_data, o.data());
  2380. }
  2381. inline gmp_int& gmp_int::operator=(const gmp_rational& o)
  2382. {
  2383. if (this->m_data[0]._mp_d == 0)
  2384. mpz_init(this->m_data);
  2385. mpz_set_q(this->m_data, o.data());
  2386. return *this;
  2387. }
  2388. } //namespace backends
  2389. using boost::multiprecision::backends::gmp_float;
  2390. using boost::multiprecision::backends::gmp_int;
  2391. using boost::multiprecision::backends::gmp_rational;
  2392. template <expression_template_option ExpressionTemplates>
  2393. struct component_type<number<gmp_rational, ExpressionTemplates> >
  2394. {
  2395. using type = number<gmp_int, ExpressionTemplates>;
  2396. };
  2397. template <expression_template_option ET>
  2398. inline number<gmp_int, ET> numerator(const number<gmp_rational, ET>& val)
  2399. {
  2400. number<gmp_int, ET> result;
  2401. mpz_set(result.backend().data(), (mpq_numref(val.backend().data())));
  2402. return result;
  2403. }
  2404. template <expression_template_option ET>
  2405. inline number<gmp_int, ET> denominator(const number<gmp_rational, ET>& val)
  2406. {
  2407. number<gmp_int, ET> result;
  2408. mpz_set(result.backend().data(), (mpq_denref(val.backend().data())));
  2409. return result;
  2410. }
  2411. namespace detail {
  2412. template <>
  2413. struct digits2<number<gmp_float<0>, et_on> >
  2414. {
  2415. static long value()
  2416. {
  2417. return multiprecision::detail::digits10_2_2(gmp_float<0>::default_precision());
  2418. }
  2419. };
  2420. template <>
  2421. struct digits2<number<gmp_float<0>, et_off> >
  2422. {
  2423. static long value()
  2424. {
  2425. return multiprecision::detail::digits10_2_2(gmp_float<0>::default_precision());
  2426. }
  2427. };
  2428. template <>
  2429. struct digits2<number<debug_adaptor<gmp_float<0> >, et_on> >
  2430. {
  2431. static long value()
  2432. {
  2433. return multiprecision::detail::digits10_2_2(gmp_float<0>::default_precision());
  2434. }
  2435. };
  2436. template <>
  2437. struct digits2<number<debug_adaptor<gmp_float<0> >, et_off> >
  2438. {
  2439. static long value()
  2440. {
  2441. return multiprecision::detail::digits10_2_2(gmp_float<0>::default_precision());
  2442. }
  2443. };
  2444. template <unsigned Digits10>
  2445. struct transcendental_reduction_type<boost::multiprecision::backends::gmp_float<Digits10> >
  2446. {
  2447. //
  2448. // The type used for trigonometric reduction needs 3 times the precision of the base type.
  2449. // This is double the precision of the original type, plus the largest exponent supported.
  2450. // As a practical measure the largest argument supported is 1/eps, as supporting larger
  2451. // arguments requires the division of argument by PI/2 to also be done at higher precision,
  2452. // otherwise the result (an integer) can not be represented exactly.
  2453. //
  2454. // See ARGUMENT REDUCTION FOR HUGE ARGUMENTS. K C Ng.
  2455. //
  2456. using type = boost::multiprecision::backends::gmp_float<Digits10 * 3>;
  2457. };
  2458. } // namespace detail
  2459. template <>
  2460. struct number_category<detail::canonical<mpz_t, gmp_int>::type> : public std::integral_constant<int, number_kind_integer>
  2461. {};
  2462. template <>
  2463. struct number_category<detail::canonical<mpq_t, gmp_rational>::type> : public std::integral_constant<int, number_kind_rational>
  2464. {};
  2465. template <>
  2466. struct number_category<detail::canonical<mpf_t, gmp_float<0> >::type> : public std::integral_constant<int, number_kind_floating_point>
  2467. {};
  2468. namespace detail {
  2469. template <>
  2470. struct is_variable_precision<backends::gmp_float<0> > : public std::integral_constant<bool, true>
  2471. {};
  2472. } // namespace detail
  2473. using mpf_float_50 = number<gmp_float<50> > ;
  2474. using mpf_float_100 = number<gmp_float<100> > ;
  2475. using mpf_float_500 = number<gmp_float<500> > ;
  2476. using mpf_float_1000 = number<gmp_float<1000> >;
  2477. using mpf_float = number<gmp_float<0> > ;
  2478. using mpz_int = number<gmp_int> ;
  2479. using mpq_rational = number<gmp_rational> ;
  2480. } // namespace multiprecision
  2481. namespace math { namespace tools {
  2482. inline void set_output_precision(const boost::multiprecision::mpf_float& val, std::ostream& os)
  2483. {
  2484. os << std::setprecision(val.precision());
  2485. }
  2486. template <>
  2487. inline int digits<boost::multiprecision::mpf_float>()
  2488. #ifdef BOOST_MATH_NOEXCEPT
  2489. noexcept
  2490. #endif
  2491. {
  2492. return multiprecision::detail::digits10_2_2(boost::multiprecision::mpf_float::default_precision());
  2493. }
  2494. template <>
  2495. inline int digits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
  2496. #ifdef BOOST_MATH_NOEXCEPT
  2497. noexcept
  2498. #endif
  2499. {
  2500. return multiprecision::detail::digits10_2_2(boost::multiprecision::mpf_float::default_precision());
  2501. }
  2502. template <>
  2503. inline boost::multiprecision::mpf_float
  2504. max_value<boost::multiprecision::mpf_float>()
  2505. {
  2506. boost::multiprecision::mpf_float result(0.5);
  2507. mpf_mul_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  2508. return result;
  2509. }
  2510. template <>
  2511. inline boost::multiprecision::mpf_float
  2512. min_value<boost::multiprecision::mpf_float>()
  2513. {
  2514. boost::multiprecision::mpf_float result(0.5);
  2515. mpf_div_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  2516. return result;
  2517. }
  2518. template <>
  2519. inline boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off>
  2520. max_value<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
  2521. {
  2522. boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> result(0.5);
  2523. mpf_mul_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  2524. return result;
  2525. }
  2526. template <>
  2527. inline boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off>
  2528. min_value<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
  2529. {
  2530. boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> result(0.5);
  2531. mpf_div_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  2532. return result;
  2533. }
  2534. template <>
  2535. inline int digits<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
  2536. #ifdef BOOST_MATH_NOEXCEPT
  2537. noexcept
  2538. #endif
  2539. {
  2540. return multiprecision::detail::digits10_2_2(boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >::default_precision());
  2541. }
  2542. template <>
  2543. inline int digits<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
  2544. #ifdef BOOST_MATH_NOEXCEPT
  2545. noexcept
  2546. #endif
  2547. {
  2548. return multiprecision::detail::digits10_2_2(boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >::default_precision());
  2549. }
  2550. template <>
  2551. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >
  2552. max_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
  2553. {
  2554. return max_value<boost::multiprecision::mpf_float>().backend();
  2555. }
  2556. template <>
  2557. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >
  2558. min_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
  2559. {
  2560. return min_value<boost::multiprecision::mpf_float>().backend();
  2561. }
  2562. template <>
  2563. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off>
  2564. max_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
  2565. {
  2566. return max_value<boost::multiprecision::mpf_float>().backend();
  2567. }
  2568. template <>
  2569. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off>
  2570. min_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
  2571. {
  2572. return min_value<boost::multiprecision::mpf_float>().backend();
  2573. }
  2574. }} // namespace math::tools
  2575. } // namespace boost
  2576. namespace std {
  2577. //
  2578. // numeric_limits [partial] specializations for the types declared in this header:
  2579. //
  2580. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2581. class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >
  2582. {
  2583. using number_type = boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates>;
  2584. public:
  2585. static constexpr bool is_specialized = true;
  2586. //
  2587. // min and max values chosen so as to not cause segfaults when calling
  2588. // mpf_get_str on 64-bit Linux builds. Possibly we could use larger
  2589. // exponent values elsewhere.
  2590. //
  2591. static number_type(min)()
  2592. {
  2593. static std::pair<bool, number_type> value;
  2594. if (!value.first)
  2595. {
  2596. value.first = true;
  2597. value.second = 1;
  2598. mpf_div_2exp(value.second.backend().data(), value.second.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  2599. }
  2600. return value.second;
  2601. }
  2602. static number_type(max)()
  2603. {
  2604. static std::pair<bool, number_type> value;
  2605. if (!value.first)
  2606. {
  2607. value.first = true;
  2608. value.second = 1;
  2609. mpf_mul_2exp(value.second.backend().data(), value.second.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  2610. }
  2611. return value.second;
  2612. }
  2613. static constexpr number_type lowest()
  2614. {
  2615. return -(max)();
  2616. }
  2617. static constexpr int digits = static_cast<int>((Digits10 * 1000L) / 301L + ((Digits10 * 1000L) % 301L ? 2 : 1));
  2618. static constexpr int digits10 = Digits10;
  2619. // Have to allow for a possible extra limb inside the gmp data structure:
  2620. static constexpr int max_digits10 = Digits10 + 3 + ((GMP_LIMB_BITS * 301L) / 1000L);
  2621. static constexpr bool is_signed = true;
  2622. static constexpr bool is_integer = false;
  2623. static constexpr bool is_exact = false;
  2624. static constexpr int radix = 2;
  2625. static number_type epsilon()
  2626. {
  2627. static std::pair<bool, number_type> value;
  2628. if (!value.first)
  2629. {
  2630. value.first = true;
  2631. value.second = 1;
  2632. mpf_div_2exp(value.second.backend().data(), value.second.backend().data(), std::numeric_limits<number_type>::digits - 1);
  2633. }
  2634. return value.second;
  2635. }
  2636. // What value should this be????
  2637. static number_type round_error()
  2638. {
  2639. // returns epsilon/2
  2640. static std::pair<bool, number_type> value;
  2641. if (!value.first)
  2642. {
  2643. value.first = true;
  2644. value.second = 1;
  2645. }
  2646. return value.second;
  2647. }
  2648. static constexpr long min_exponent = LONG_MIN;
  2649. static constexpr long min_exponent10 = (LONG_MIN / 1000) * 301L;
  2650. static constexpr long max_exponent = LONG_MAX;
  2651. static constexpr long max_exponent10 = (LONG_MAX / 1000) * 301L;
  2652. static constexpr bool has_infinity = false;
  2653. static constexpr bool has_quiet_NaN = false;
  2654. static constexpr bool has_signaling_NaN = false;
  2655. static constexpr float_denorm_style has_denorm = denorm_absent;
  2656. static constexpr bool has_denorm_loss = false;
  2657. static constexpr number_type infinity() { return number_type(); }
  2658. static constexpr number_type quiet_NaN() { return number_type(); }
  2659. static constexpr number_type signaling_NaN() { return number_type(); }
  2660. static constexpr number_type denorm_min() { return number_type(); }
  2661. static constexpr bool is_iec559 = false;
  2662. static constexpr bool is_bounded = true;
  2663. static constexpr bool is_modulo = false;
  2664. static constexpr bool traps = true;
  2665. static constexpr bool tinyness_before = false;
  2666. static constexpr float_round_style round_style = round_indeterminate;
  2667. };
  2668. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2669. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::digits;
  2670. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2671. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::digits10;
  2672. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2673. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_digits10;
  2674. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2675. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_signed;
  2676. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2677. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_integer;
  2678. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2679. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_exact;
  2680. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2681. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::radix;
  2682. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2683. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::min_exponent;
  2684. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2685. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::min_exponent10;
  2686. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2687. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_exponent;
  2688. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2689. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_exponent10;
  2690. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2691. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_infinity;
  2692. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2693. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_quiet_NaN;
  2694. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2695. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_signaling_NaN;
  2696. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2697. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_denorm;
  2698. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2699. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_denorm_loss;
  2700. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2701. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_iec559;
  2702. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2703. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_bounded;
  2704. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2705. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_modulo;
  2706. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2707. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::traps;
  2708. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2709. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::tinyness_before;
  2710. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  2711. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::round_style;
  2712. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2713. class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >
  2714. {
  2715. using number_type = boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates>;
  2716. public:
  2717. static constexpr bool is_specialized = false;
  2718. static number_type(min)() { return number_type(); }
  2719. static number_type(max)() { return number_type(); }
  2720. static number_type lowest() { return number_type(); }
  2721. static constexpr int digits = 0;
  2722. static constexpr int digits10 = 0;
  2723. static constexpr int max_digits10 = 0;
  2724. static constexpr bool is_signed = false;
  2725. static constexpr bool is_integer = false;
  2726. static constexpr bool is_exact = false;
  2727. static constexpr int radix = 0;
  2728. static number_type epsilon() { return number_type(); }
  2729. static number_type round_error() { return number_type(); }
  2730. static constexpr int min_exponent = 0;
  2731. static constexpr int min_exponent10 = 0;
  2732. static constexpr int max_exponent = 0;
  2733. static constexpr int max_exponent10 = 0;
  2734. static constexpr bool has_infinity = false;
  2735. static constexpr bool has_quiet_NaN = false;
  2736. static constexpr bool has_signaling_NaN = false;
  2737. static constexpr float_denorm_style has_denorm = denorm_absent;
  2738. static constexpr bool has_denorm_loss = false;
  2739. static number_type infinity() { return number_type(); }
  2740. static number_type quiet_NaN() { return number_type(); }
  2741. static number_type signaling_NaN() { return number_type(); }
  2742. static number_type denorm_min() { return number_type(); }
  2743. static constexpr bool is_iec559 = false;
  2744. static constexpr bool is_bounded = false;
  2745. static constexpr bool is_modulo = false;
  2746. static constexpr bool traps = false;
  2747. static constexpr bool tinyness_before = false;
  2748. static constexpr float_round_style round_style = round_indeterminate;
  2749. };
  2750. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2751. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::digits;
  2752. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2753. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::digits10;
  2754. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2755. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_digits10;
  2756. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2757. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_signed;
  2758. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2759. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_integer;
  2760. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2761. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_exact;
  2762. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2763. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::radix;
  2764. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2765. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::min_exponent;
  2766. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2767. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::min_exponent10;
  2768. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2769. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_exponent;
  2770. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2771. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_exponent10;
  2772. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2773. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_infinity;
  2774. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2775. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_quiet_NaN;
  2776. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2777. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_signaling_NaN;
  2778. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2779. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_denorm;
  2780. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2781. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_denorm_loss;
  2782. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2783. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_iec559;
  2784. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2785. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_bounded;
  2786. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2787. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_modulo;
  2788. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2789. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::traps;
  2790. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2791. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::tinyness_before;
  2792. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2793. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::round_style;
  2794. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2795. class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >
  2796. {
  2797. using number_type = boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates>;
  2798. public:
  2799. static constexpr bool is_specialized = true;
  2800. //
  2801. // Largest and smallest numbers are bounded only by available memory, set
  2802. // to zero:
  2803. //
  2804. static number_type(min)()
  2805. {
  2806. return number_type();
  2807. }
  2808. static number_type(max)()
  2809. {
  2810. return number_type();
  2811. }
  2812. static number_type lowest() { return (min)(); }
  2813. static constexpr int digits = INT_MAX;
  2814. static constexpr int digits10 = (INT_MAX / 1000) * 301L;
  2815. static constexpr int max_digits10 = digits10 + 3;
  2816. static constexpr bool is_signed = true;
  2817. static constexpr bool is_integer = true;
  2818. static constexpr bool is_exact = true;
  2819. static constexpr int radix = 2;
  2820. static number_type epsilon() { return number_type(); }
  2821. static number_type round_error() { return number_type(); }
  2822. static constexpr int min_exponent = 0;
  2823. static constexpr int min_exponent10 = 0;
  2824. static constexpr int max_exponent = 0;
  2825. static constexpr int max_exponent10 = 0;
  2826. static constexpr bool has_infinity = false;
  2827. static constexpr bool has_quiet_NaN = false;
  2828. static constexpr bool has_signaling_NaN = false;
  2829. static constexpr float_denorm_style has_denorm = denorm_absent;
  2830. static constexpr bool has_denorm_loss = false;
  2831. static number_type infinity() { return number_type(); }
  2832. static number_type quiet_NaN() { return number_type(); }
  2833. static number_type signaling_NaN() { return number_type(); }
  2834. static number_type denorm_min() { return number_type(); }
  2835. static constexpr bool is_iec559 = false;
  2836. static constexpr bool is_bounded = false;
  2837. static constexpr bool is_modulo = false;
  2838. static constexpr bool traps = false;
  2839. static constexpr bool tinyness_before = false;
  2840. static constexpr float_round_style round_style = round_toward_zero;
  2841. };
  2842. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2843. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::digits;
  2844. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2845. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::digits10;
  2846. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2847. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_digits10;
  2848. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2849. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_signed;
  2850. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2851. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_integer;
  2852. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2853. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_exact;
  2854. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2855. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::radix;
  2856. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2857. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::min_exponent;
  2858. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2859. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::min_exponent10;
  2860. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2861. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_exponent;
  2862. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2863. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_exponent10;
  2864. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2865. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_infinity;
  2866. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2867. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_quiet_NaN;
  2868. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2869. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_signaling_NaN;
  2870. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2871. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_denorm;
  2872. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2873. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_denorm_loss;
  2874. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2875. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_iec559;
  2876. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2877. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_bounded;
  2878. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2879. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_modulo;
  2880. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2881. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::traps;
  2882. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2883. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::tinyness_before;
  2884. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2885. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::round_style;
  2886. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2887. class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >
  2888. {
  2889. using number_type = boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates>;
  2890. public:
  2891. static constexpr bool is_specialized = true;
  2892. //
  2893. // Largest and smallest numbers are bounded only by available memory, set
  2894. // to zero:
  2895. //
  2896. static number_type(min)()
  2897. {
  2898. return number_type();
  2899. }
  2900. static number_type(max)()
  2901. {
  2902. return number_type();
  2903. }
  2904. static number_type lowest() { return (min)(); }
  2905. // Digits are unbounded, use zero for now:
  2906. static constexpr int digits = INT_MAX;
  2907. static constexpr int digits10 = (INT_MAX / 1000) * 301L;
  2908. static constexpr int max_digits10 = digits10 + 3;
  2909. static constexpr bool is_signed = true;
  2910. static constexpr bool is_integer = false;
  2911. static constexpr bool is_exact = true;
  2912. static constexpr int radix = 2;
  2913. static number_type epsilon() { return number_type(); }
  2914. static number_type round_error() { return number_type(); }
  2915. static constexpr int min_exponent = 0;
  2916. static constexpr int min_exponent10 = 0;
  2917. static constexpr int max_exponent = 0;
  2918. static constexpr int max_exponent10 = 0;
  2919. static constexpr bool has_infinity = false;
  2920. static constexpr bool has_quiet_NaN = false;
  2921. static constexpr bool has_signaling_NaN = false;
  2922. static constexpr float_denorm_style has_denorm = denorm_absent;
  2923. static constexpr bool has_denorm_loss = false;
  2924. static number_type infinity() { return number_type(); }
  2925. static number_type quiet_NaN() { return number_type(); }
  2926. static number_type signaling_NaN() { return number_type(); }
  2927. static number_type denorm_min() { return number_type(); }
  2928. static constexpr bool is_iec559 = false;
  2929. static constexpr bool is_bounded = false;
  2930. static constexpr bool is_modulo = false;
  2931. static constexpr bool traps = false;
  2932. static constexpr bool tinyness_before = false;
  2933. static constexpr float_round_style round_style = round_toward_zero;
  2934. };
  2935. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2936. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::digits;
  2937. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2938. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::digits10;
  2939. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2940. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_digits10;
  2941. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2942. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_signed;
  2943. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2944. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_integer;
  2945. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2946. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_exact;
  2947. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2948. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::radix;
  2949. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2950. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::min_exponent;
  2951. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2952. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::min_exponent10;
  2953. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2954. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_exponent;
  2955. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2956. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_exponent10;
  2957. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2958. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_infinity;
  2959. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2960. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_quiet_NaN;
  2961. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2962. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_signaling_NaN;
  2963. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2964. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_denorm;
  2965. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2966. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_denorm_loss;
  2967. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2968. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_iec559;
  2969. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2970. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_bounded;
  2971. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2972. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_modulo;
  2973. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2974. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::traps;
  2975. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2976. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::tinyness_before;
  2977. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  2978. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::round_style;
  2979. #ifdef BOOST_MSVC
  2980. #pragma warning(pop)
  2981. #endif
  2982. } // namespace std
  2983. #endif