gmp.hpp 108 KB

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