gmp.hpp 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2011 John Maddock.
  3. // Copyright 2021 Matt Borland. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MP_GMP_HPP
  7. #define BOOST_MP_GMP_HPP
  8. #include <boost/multiprecision/detail/standalone_config.hpp>
  9. #include <boost/multiprecision/number.hpp>
  10. #include <boost/multiprecision/debug_adaptor.hpp>
  11. #include <boost/multiprecision/detail/integer_ops.hpp>
  12. #include <boost/multiprecision/detail/float128_functions.hpp>
  13. #include <boost/multiprecision/detail/digits.hpp>
  14. #include <boost/multiprecision/detail/atomic.hpp>
  15. #include <boost/multiprecision/detail/hash.hpp>
  16. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  17. #include <boost/multiprecision/detail/assert.hpp>
  18. #include <boost/multiprecision/detail/fpclassify.hpp>
  19. #include <boost/multiprecision/detail/string_helpers.hpp>
  20. #include <algorithm>
  21. #include <cctype>
  22. #include <cfloat>
  23. #include <climits>
  24. #include <clocale>
  25. #include <cmath>
  26. #include <cstdint>
  27. #include <cstdlib>
  28. #include <cstring>
  29. #include <iomanip>
  30. #include <iostream>
  31. #include <limits>
  32. #include <memory>
  33. #include <type_traits>
  34. #include <utility>
  35. //
  36. // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
  37. //
  38. #ifdef BOOST_MP_MATH_AVAILABLE
  39. #include <boost/math/special_functions/asinh.hpp>
  40. #include <boost/math/special_functions/acosh.hpp>
  41. #include <boost/math/special_functions/atanh.hpp>
  42. #include <boost/math/special_functions/cbrt.hpp>
  43. #include <boost/math/special_functions/expm1.hpp>
  44. #include <boost/math/special_functions/gamma.hpp>
  45. #endif
  46. #ifdef BOOST_MSVC
  47. #pragma warning(push)
  48. #pragma warning(disable : 4127)
  49. #endif
  50. #include <gmp.h>
  51. #ifdef BOOST_MSVC
  52. #pragma warning(pop)
  53. #endif
  54. #if defined(__MPIR_VERSION) && defined(__MPIR_VERSION_MINOR) && defined(__MPIR_VERSION_PATCHLEVEL)
  55. #define BOOST_MP_MPIR_VERSION (__MPIR_VERSION * 10000 + __MPIR_VERSION_MINOR * 100 + __MPIR_VERSION_PATCHLEVEL)
  56. #else
  57. #define BOOST_MP_MPIR_VERSION 0
  58. #endif
  59. namespace boost {
  60. namespace multiprecision {
  61. namespace backends {
  62. #ifdef BOOST_MSVC
  63. // warning C4127: conditional expression is constant
  64. #pragma warning(push)
  65. //#pragma warning(disable : 4127)
  66. #endif
  67. template <unsigned digits10>
  68. struct gmp_float;
  69. struct gmp_int;
  70. struct gmp_rational;
  71. } // namespace backends
  72. template <>
  73. struct number_category<backends::gmp_int> : public std::integral_constant<int, number_kind_integer>
  74. {};
  75. template <>
  76. struct number_category<backends::gmp_rational> : public std::integral_constant<int, number_kind_rational>
  77. {};
  78. template <unsigned digits10>
  79. struct number_category<backends::gmp_float<digits10> > : public std::integral_constant<int, number_kind_floating_point>
  80. {};
  81. namespace backends {
  82. //
  83. // Within this file, the only functions we mark as noexcept are those that manipulate
  84. // (but don't create) an mpf_t. All other types may allocate at pretty much any time
  85. // via a user-supplied allocator, and therefore throw.
  86. //
  87. namespace detail {
  88. template <unsigned digits10>
  89. struct gmp_float_imp
  90. {
  91. #ifdef BOOST_HAS_LONG_LONG
  92. using signed_types = std::tuple<long, long long> ;
  93. using unsigned_types = std::tuple<unsigned long, unsigned long long>;
  94. #else
  95. using signed_types = std::tuple<long> ;
  96. using unsigned_types = std::tuple<unsigned long>;
  97. #endif
  98. using float_types = std::tuple<double, long double>;
  99. using exponent_type = long ;
  100. gmp_float_imp() noexcept
  101. {
  102. m_data[0]._mp_d = nullptr; // uninitialized m_data
  103. m_data[0]._mp_prec = 1;
  104. }
  105. gmp_float_imp(const gmp_float_imp& o)
  106. {
  107. //
  108. // We have to do an init followed by a set here, otherwise *this may be at
  109. // a lower precision than o: seems like mpf_init_set copies just enough bits
  110. // to get the right value, but if it's then used in further calculations
  111. // things go badly wrong!!
  112. //
  113. mpf_init2(m_data, preserve_source_precision() ? mpf_get_prec(o.data()) : boost::multiprecision::detail::digits10_2_2(get_default_precision()));
  114. if (o.m_data[0]._mp_d)
  115. mpf_set(m_data, o.m_data);
  116. }
  117. // rvalue copy
  118. gmp_float_imp(gmp_float_imp&& o) noexcept
  119. {
  120. if ((this->get_default_options() == variable_precision_options::preserve_target_precision) && (mpf_get_prec(o.data()) != boost::multiprecision::detail::digits10_2_2(get_default_precision())))
  121. {
  122. mpf_init2(m_data, boost::multiprecision::detail::digits10_2_2(get_default_precision()));
  123. *this = static_cast<const gmp_float_imp&>(o);
  124. }
  125. else
  126. {
  127. m_data[0] = o.m_data[0];
  128. o.m_data[0]._mp_d = nullptr;
  129. }
  130. }
  131. gmp_float_imp& operator=(const gmp_float_imp& o)
  132. {
  133. if (m_data[0]._mp_d == nullptr)
  134. {
  135. mpf_init2(m_data, preserve_source_precision() ? mpf_get_prec(o.data()) : boost::multiprecision::detail::digits10_2_2(get_default_precision()));
  136. mpf_set(m_data, o.m_data);
  137. }
  138. else if (preserve_source_precision() && (mpf_get_prec(data()) != mpf_get_prec(o.data())))
  139. {
  140. mpf_t t;
  141. mpf_init2(t, mpf_get_prec(o.data()));
  142. mpf_set(t, o.data());
  143. mpf_swap(data(), t);
  144. mpf_clear(t);
  145. }
  146. else
  147. {
  148. mpf_set(m_data, o.m_data);
  149. }
  150. return *this;
  151. }
  152. // rvalue assign
  153. gmp_float_imp& operator=(gmp_float_imp&& o) noexcept
  154. {
  155. if ((this->get_default_options() == variable_precision_options::preserve_target_precision) && (mpf_get_prec(o.data()) != mpf_get_prec(data())))
  156. *this = static_cast<const gmp_float_imp&>(o);
  157. else
  158. {
  159. mpf_swap(m_data, o.m_data);
  160. }
  161. return *this;
  162. }
  163. #ifdef BOOST_HAS_LONG_LONG
  164. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  165. gmp_float_imp& operator=(unsigned long long i)
  166. {
  167. *this = static_cast<unsigned long>(i);
  168. return *this;
  169. }
  170. #else
  171. gmp_float_imp& operator=(unsigned long long i)
  172. {
  173. if (m_data[0]._mp_d == nullptr)
  174. {
  175. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  176. }
  177. unsigned long long mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
  178. unsigned shift = 0;
  179. mpf_t t;
  180. mpf_init2(t, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  181. mpf_set_ui(m_data, 0);
  182. while (i)
  183. {
  184. mpf_set_ui(t, static_cast<unsigned long>(i & mask));
  185. if (shift)
  186. mpf_mul_2exp(t, t, shift);
  187. mpf_add(m_data, m_data, t);
  188. shift += std::numeric_limits<unsigned long>::digits;
  189. i >>= std::numeric_limits<unsigned long>::digits;
  190. }
  191. mpf_clear(t);
  192. return *this;
  193. }
  194. #endif
  195. gmp_float_imp& operator=(long long i)
  196. {
  197. if (m_data[0]._mp_d == nullptr)
  198. {
  199. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  200. }
  201. bool neg = i < 0;
  202. *this = static_cast<unsigned long long>(boost::multiprecision::detail::unsigned_abs(i));
  203. if (neg)
  204. mpf_neg(m_data, m_data);
  205. return *this;
  206. }
  207. #endif
  208. gmp_float_imp& operator=(unsigned long i)
  209. {
  210. if (m_data[0]._mp_d == nullptr)
  211. {
  212. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  213. }
  214. mpf_set_ui(m_data, i);
  215. return *this;
  216. }
  217. gmp_float_imp& operator=(long i)
  218. {
  219. if (m_data[0]._mp_d == nullptr)
  220. {
  221. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  222. }
  223. mpf_set_si(m_data, i);
  224. return *this;
  225. }
  226. #ifdef BOOST_HAS_INT128
  227. gmp_float_imp& operator=(uint128_type i)
  228. {
  229. if (m_data[0]._mp_d == nullptr)
  230. {
  231. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  232. }
  233. unsigned long mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
  234. unsigned shift = 0;
  235. mpf_t t;
  236. mpf_init2(t, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  237. mpf_set_ui(m_data, 0);
  238. while (i)
  239. {
  240. mpf_set_ui(t, static_cast<unsigned long>(i & mask));
  241. if (shift)
  242. mpf_mul_2exp(t, t, shift);
  243. mpf_add(m_data, m_data, t);
  244. shift += std::numeric_limits<unsigned long>::digits;
  245. i >>= std::numeric_limits<unsigned long>::digits;
  246. }
  247. mpf_clear(t);
  248. return *this;
  249. }
  250. gmp_float_imp& operator=(int128_type i)
  251. {
  252. if (m_data[0]._mp_d == nullptr)
  253. {
  254. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  255. }
  256. bool neg = i < 0;
  257. *this = static_cast<uint128_type>(boost::multiprecision::detail::unsigned_abs(i));
  258. if (neg)
  259. mpf_neg(m_data, m_data);
  260. return *this;
  261. }
  262. #endif
  263. gmp_float_imp& operator=(double d)
  264. {
  265. if (m_data[0]._mp_d == nullptr)
  266. {
  267. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  268. }
  269. mpf_set_d(m_data, d);
  270. return *this;
  271. }
  272. template <class F>
  273. gmp_float_imp& assign_float(F a)
  274. {
  275. BOOST_MP_FLOAT128_USING using std::floor; using std::frexp; using std::ldexp;
  276. if (m_data[0]._mp_d == nullptr)
  277. {
  278. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  279. }
  280. if (a == 0)
  281. {
  282. mpf_set_si(m_data, 0);
  283. return *this;
  284. }
  285. if (a == 1)
  286. {
  287. mpf_set_si(m_data, 1);
  288. return *this;
  289. }
  290. BOOST_MP_ASSERT(!BOOST_MP_ISINF(a));
  291. BOOST_MP_ASSERT(!BOOST_MP_ISNAN(a));
  292. int e;
  293. F f, term;
  294. mpf_set_ui(m_data, 0u);
  295. f = frexp(a, &e);
  296. constexpr int shift = std::numeric_limits<int>::digits - 1;
  297. while (f)
  298. {
  299. // extract int sized bits from f:
  300. f = ldexp(f, shift);
  301. term = floor(f);
  302. e -= shift;
  303. mpf_mul_2exp(m_data, m_data, shift);
  304. if (term > 0)
  305. mpf_add_ui(m_data, m_data, static_cast<unsigned>(term));
  306. else
  307. mpf_sub_ui(m_data, m_data, static_cast<unsigned>(-term));
  308. f -= term;
  309. }
  310. if (e > 0)
  311. mpf_mul_2exp(m_data, m_data, e);
  312. else if (e < 0)
  313. mpf_div_2exp(m_data, m_data, -e);
  314. return *this;
  315. }
  316. gmp_float_imp& operator=(long double a)
  317. {
  318. return assign_float(a);
  319. }
  320. #ifdef BOOST_HAS_FLOAT128
  321. gmp_float_imp& operator= (float128_type a)
  322. {
  323. return assign_float(a);
  324. }
  325. #endif
  326. gmp_float_imp& operator=(const char* s)
  327. {
  328. if (m_data[0]._mp_d == nullptr)
  329. {
  330. mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
  331. }
  332. if (s && (*s == '+'))
  333. ++s; // Leading "+" sign not supported by mpf_set_str:
  334. //
  335. // Validate the string as mpf_set_str does a poor job of this:
  336. //
  337. static const char* digits = "0123456789";
  338. const char* p = s;
  339. if (*s == '-')
  340. ++s;
  341. s += boost::multiprecision::detail::find_first_not_of(s, s + std::strlen(s), digits);
  342. std::lconv const* l = std::localeconv();
  343. std::size_t len = strlen(l->decimal_point);
  344. if (std::find(l->decimal_point, l->decimal_point + len, *s) != l->decimal_point + len)
  345. {
  346. ++s;
  347. s += boost::multiprecision::detail::find_first_not_of(s, s + std::strlen(s), digits);
  348. }
  349. if ((*s == 'e') || (*s == 'E'))
  350. {
  351. ++s;
  352. if ((*s == '+') || (*s == '-'))
  353. ++s;
  354. s += boost::multiprecision::detail::find_first_not_of(s, s + std::strlen(s), digits);
  355. }
  356. if(*s)
  357. BOOST_MP_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid floating point number.")));
  358. s = p;
  359. if (0 != mpf_set_str(m_data, s, 10))
  360. BOOST_MP_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid floating point number.")));
  361. return *this;
  362. }
  363. void swap(gmp_float_imp& o) noexcept
  364. {
  365. mpf_swap(m_data, o.m_data);
  366. }
  367. std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
  368. {
  369. BOOST_MP_ASSERT(m_data[0]._mp_d);
  370. bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
  371. bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
  372. std::streamsize org_digits(digits);
  373. if (scientific && digits)
  374. ++digits;
  375. std::string result;
  376. mp_exp_t e;
  377. void* (*alloc_func_ptr)(size_t);
  378. void* (*realloc_func_ptr)(void*, size_t, size_t);
  379. void (*free_func_ptr)(void*, size_t);
  380. mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
  381. if (mpf_sgn(m_data) == 0)
  382. {
  383. e = 0;
  384. result = "0";
  385. if (fixed && digits)
  386. ++digits;
  387. }
  388. else
  389. {
  390. char* ps = mpf_get_str(nullptr, &e, 10, static_cast<std::size_t>(digits), m_data);
  391. --e; // To match with what our formatter expects.
  392. if (fixed)
  393. {
  394. // Oops we actually need a different number of digits to what we asked for:
  395. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  396. digits += e + 1;
  397. if (digits == 0)
  398. {
  399. // We need to get *all* the digits and then possibly round up,
  400. // we end up with either "0" or "1" as the result.
  401. ps = mpf_get_str(nullptr, &e, 10, 0, m_data);
  402. --e;
  403. unsigned offset = *ps == '-' ? 1 : 0;
  404. if (ps[offset] > '5')
  405. {
  406. ++e;
  407. ps[offset] = '1';
  408. ps[offset + 1] = 0;
  409. }
  410. else if (ps[offset] == '5')
  411. {
  412. unsigned i = offset + 1;
  413. bool round_up = false;
  414. while (ps[i] != 0)
  415. {
  416. if (ps[i] != '0')
  417. {
  418. round_up = true;
  419. break;
  420. }
  421. ++i;
  422. }
  423. if (round_up)
  424. {
  425. ++e;
  426. ps[offset] = '1';
  427. ps[offset + 1] = 0;
  428. }
  429. else
  430. {
  431. ps[offset] = '0';
  432. ps[offset + 1] = 0;
  433. }
  434. }
  435. else
  436. {
  437. ps[offset] = '0';
  438. ps[offset + 1] = 0;
  439. }
  440. }
  441. else if (digits > 0)
  442. {
  443. mp_exp_t old_e = e;
  444. ps = mpf_get_str(nullptr, &e, 10, static_cast<std::size_t>(digits), m_data);
  445. --e; // To match with what our formatter expects.
  446. if (old_e > e)
  447. {
  448. // in some cases, when we ask for more digits of precision, it will
  449. // change the number of digits to the left of the decimal, if that
  450. // happens, account for it here.
  451. // example: cout << fixed << setprecision(3) << mpf_float_50("99.9809")
  452. digits -= old_e - e;
  453. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  454. ps = mpf_get_str(nullptr, &e, 10, static_cast<std::size_t>(digits), m_data);
  455. --e; // To match with what our formatter expects.
  456. }
  457. }
  458. else
  459. {
  460. ps = mpf_get_str(nullptr, &e, 10, 1, m_data);
  461. --e;
  462. unsigned offset = *ps == '-' ? 1 : 0;
  463. ps[offset] = '0';
  464. ps[offset + 1] = 0;
  465. }
  466. }
  467. result = ps;
  468. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  469. }
  470. boost::multiprecision::detail::format_float_string(result, e, org_digits, f, mpf_sgn(m_data) == 0);
  471. return result;
  472. }
  473. ~gmp_float_imp() noexcept
  474. {
  475. if (m_data[0]._mp_d)
  476. {
  477. mpf_clear(m_data);
  478. }
  479. }
  480. void negate() noexcept
  481. {
  482. BOOST_MP_ASSERT(m_data[0]._mp_d);
  483. mpf_neg(m_data, m_data);
  484. }
  485. int compare(const gmp_float<digits10>& o) const noexcept
  486. {
  487. BOOST_MP_ASSERT(m_data[0]._mp_d && o.m_data[0]._mp_d);
  488. return mpf_cmp(m_data, o.m_data);
  489. }
  490. int compare(long i) const noexcept
  491. {
  492. BOOST_MP_ASSERT(m_data[0]._mp_d);
  493. return mpf_cmp_si(m_data, i);
  494. }
  495. int compare(unsigned long i) const noexcept
  496. {
  497. BOOST_MP_ASSERT(m_data[0]._mp_d);
  498. return mpf_cmp_ui(m_data, i);
  499. }
  500. template <class V>
  501. typename std::enable_if<boost::multiprecision::detail::is_arithmetic<V>::value, int>::type compare(V v) const
  502. {
  503. gmp_float<digits10> d;
  504. d = v;
  505. return compare(d);
  506. }
  507. mpf_t& data() noexcept
  508. {
  509. BOOST_MP_ASSERT(m_data[0]._mp_d);
  510. return m_data;
  511. }
  512. const mpf_t& data() const noexcept
  513. {
  514. BOOST_MP_ASSERT(m_data[0]._mp_d);
  515. return m_data;
  516. }
  517. protected:
  518. mpf_t m_data;
  519. static unsigned& get_default_precision() noexcept
  520. {
  521. static BOOST_MP_THREAD_LOCAL unsigned val(get_global_default_precision());
  522. return val;
  523. }
  524. static boost::multiprecision::detail::precision_type& get_global_default_precision() noexcept
  525. {
  526. static boost::multiprecision::detail::precision_type val(50);
  527. return val;
  528. }
  529. #ifndef BOOST_MT_NO_ATOMIC_INT
  530. static std::atomic<variable_precision_options>& get_global_default_options() noexcept
  531. #else
  532. static variable_precision_options& get_global_default_options() noexcept
  533. #endif
  534. {
  535. #ifndef BOOST_MT_NO_ATOMIC_INT
  536. static std::atomic<variable_precision_options> val{variable_precision_options::preserve_related_precision};
  537. #else
  538. static variable_precision_options val{variable_precision_options::preserve_related_precision};
  539. #endif
  540. return val;
  541. }
  542. static variable_precision_options& get_default_options()noexcept
  543. {
  544. static BOOST_MP_THREAD_LOCAL variable_precision_options val(get_global_default_options());
  545. return val;
  546. }
  547. static bool preserve_source_precision() noexcept
  548. {
  549. return get_default_options() >= variable_precision_options::preserve_source_precision;
  550. }
  551. };
  552. class gmp_char_ptr
  553. {
  554. private:
  555. char* ptr_val;
  556. void* (*alloc_func_ptr)(size_t);
  557. void* (*realloc_func_ptr)(void*, size_t, size_t);
  558. void (*free_func_ptr)(void*, size_t);
  559. public:
  560. gmp_char_ptr() = delete;
  561. explicit gmp_char_ptr(char* val_) : ptr_val {val_}
  562. {
  563. mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
  564. }
  565. ~gmp_char_ptr() noexcept
  566. {
  567. (*free_func_ptr)((void*)ptr_val, sizeof(*ptr_val));
  568. ptr_val = nullptr;
  569. }
  570. inline char* get() noexcept { return ptr_val; }
  571. };
  572. } // namespace detail
  573. template <unsigned digits10>
  574. struct gmp_float : public detail::gmp_float_imp<digits10>
  575. {
  576. gmp_float()
  577. {
  578. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  579. }
  580. gmp_float(const gmp_float& o) : detail::gmp_float_imp<digits10>(o) {}
  581. template <unsigned D>
  582. gmp_float(const gmp_float<D>& o, typename std::enable_if<D <= digits10>::type* = nullptr);
  583. template <unsigned D>
  584. explicit gmp_float(const gmp_float<D>& o, typename std::enable_if<!(D <= digits10)>::type* = nullptr);
  585. gmp_float(const gmp_int& o);
  586. gmp_float(const gmp_rational& o);
  587. gmp_float(const mpf_t val)
  588. {
  589. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  590. mpf_set(this->m_data, val);
  591. }
  592. gmp_float(const mpz_t val)
  593. {
  594. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  595. mpf_set_z(this->m_data, val);
  596. }
  597. gmp_float(const mpq_t val)
  598. {
  599. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  600. mpf_set_q(this->m_data, val);
  601. }
  602. // rvalue copy
  603. gmp_float(gmp_float&& o) noexcept : detail::gmp_float_imp<digits10>(static_cast<detail::gmp_float_imp<digits10>&&>(o))
  604. {}
  605. gmp_float& operator=(const gmp_float& o)
  606. {
  607. *static_cast<detail::gmp_float_imp<digits10>*>(this) = static_cast<detail::gmp_float_imp<digits10> const&>(o);
  608. return *this;
  609. }
  610. gmp_float& operator=(gmp_float&& o) noexcept
  611. {
  612. *static_cast<detail::gmp_float_imp<digits10>*>(this) = static_cast<detail::gmp_float_imp<digits10>&&>(o);
  613. return *this;
  614. }
  615. template <unsigned D>
  616. gmp_float& operator=(const gmp_float<D>& o);
  617. gmp_float& operator=(const gmp_int& o);
  618. gmp_float& operator=(const gmp_rational& o);
  619. gmp_float& operator=(const mpf_t val)
  620. {
  621. if (this->m_data[0]._mp_d == nullptr)
  622. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  623. mpf_set(this->m_data, val);
  624. return *this;
  625. }
  626. gmp_float& operator=(const mpz_t val)
  627. {
  628. if (this->m_data[0]._mp_d == nullptr)
  629. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  630. mpf_set_z(this->m_data, val);
  631. return *this;
  632. }
  633. gmp_float& operator=(const mpq_t val)
  634. {
  635. if (this->m_data[0]._mp_d == nullptr)
  636. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  637. mpf_set_q(this->m_data, val);
  638. return *this;
  639. }
  640. template <class V>
  641. typename std::enable_if<std::is_assignable<detail::gmp_float_imp<digits10>, V>::value, gmp_float&>::type operator=(const V& v)
  642. {
  643. *static_cast<detail::gmp_float_imp<digits10>*>(this) = v;
  644. return *this;
  645. }
  646. };
  647. template <>
  648. struct gmp_float<0> : public detail::gmp_float_imp<0>
  649. {
  650. //
  651. // We have a problem with mpf_t in that the precision we request isn't what we get.
  652. // As a result the front end can end up chasing it's tail trying to create a variable
  653. // with the the correct precision to hold the result of an expression.
  654. // See: https://github.com/boostorg/multiprecision/issues/164
  655. // The problem is made worse by the fact that our conversions from base10 to 2 and
  656. // vice-versa do not exactly round trip (and probably never will).
  657. // The workaround is to keep track of the precision requested, and always return
  658. // that as the current actual precision.
  659. //
  660. private:
  661. unsigned requested_precision;
  662. public:
  663. gmp_float() : requested_precision(get_default_precision())
  664. {
  665. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  666. }
  667. gmp_float(const mpf_t val) : requested_precision(get_default_precision())
  668. {
  669. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  670. mpf_set(this->m_data, val);
  671. }
  672. gmp_float(const mpz_t val) : requested_precision(get_default_precision())
  673. {
  674. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  675. mpf_set_z(this->m_data, val);
  676. }
  677. gmp_float(const mpq_t val) : requested_precision(get_default_precision())
  678. {
  679. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  680. mpf_set_q(this->m_data, val);
  681. }
  682. gmp_float(const gmp_float& o) : detail::gmp_float_imp<0>(o), requested_precision(preserve_source_precision() ? o.requested_precision : get_default_precision()) {}
  683. template <unsigned D>
  684. gmp_float(const gmp_float<D>& o)
  685. {
  686. mpf_init2(this->m_data, preserve_related_precision() ? mpf_get_prec(o.data()) : multiprecision::detail::digits10_2_2(get_default_precision()));
  687. mpf_set(this->m_data, o.data());
  688. requested_precision = preserve_related_precision() ? D : get_default_precision();
  689. }
  690. // rvalue copy
  691. gmp_float(gmp_float&& o) noexcept : detail::gmp_float_imp<0>(static_cast<detail::gmp_float_imp<0>&&>(o)), requested_precision((this->get_default_options() != variable_precision_options::preserve_target_precision) ? o.requested_precision : get_default_precision())
  692. {}
  693. gmp_float(const gmp_int& o);
  694. gmp_float(const gmp_rational& o);
  695. gmp_float(const gmp_float& o, unsigned digits10) : requested_precision(digits10)
  696. {
  697. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  698. mpf_set(this->m_data, o.data());
  699. }
  700. template <class V>
  701. gmp_float(const V& o, unsigned digits10) : requested_precision(digits10)
  702. {
  703. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  704. *this = o;
  705. }
  706. #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
  707. //
  708. // Support for new types in C++17
  709. //
  710. template <class Traits>
  711. gmp_float(const std::basic_string_view<char, Traits>& o, unsigned digits10) : requested_precision(digits10)
  712. {
  713. using default_ops::assign_from_string_view;
  714. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
  715. assign_from_string_view(*this, o);
  716. }
  717. #endif
  718. gmp_float& operator=(const gmp_float& o)
  719. {
  720. *static_cast<detail::gmp_float_imp<0>*>(this) = static_cast<detail::gmp_float_imp<0> const&>(o);
  721. if(preserve_source_precision())
  722. requested_precision = o.requested_precision;
  723. return *this;
  724. }
  725. // rvalue copy
  726. gmp_float& operator=(gmp_float&& o) noexcept
  727. {
  728. *static_cast<detail::gmp_float_imp<0>*>(this) = static_cast<detail::gmp_float_imp<0>&&>(o);
  729. if ((this->get_default_options() != variable_precision_options::preserve_target_precision))
  730. requested_precision = o.requested_precision;
  731. return *this;
  732. }
  733. template <unsigned D>
  734. gmp_float& operator=(const gmp_float<D>& o)
  735. {
  736. if (this->m_data[0]._mp_d == nullptr)
  737. {
  738. mpf_init2(this->m_data, preserve_related_precision() ? mpf_get_prec(o.data()) : multiprecision::detail::digits10_2_2(get_default_precision()));
  739. }
  740. else if(preserve_related_precision())
  741. {
  742. mpf_set_prec(this->m_data, mpf_get_prec(o.data()));
  743. }
  744. mpf_set(this->m_data, o.data());
  745. if (preserve_related_precision())
  746. requested_precision = D;
  747. return *this;
  748. }
  749. gmp_float& operator=(const gmp_int& o);
  750. gmp_float& operator=(const gmp_rational& o);
  751. gmp_float& operator=(const mpf_t val)
  752. {
  753. if (this->m_data[0]._mp_d == nullptr)
  754. {
  755. requested_precision = get_default_precision();
  756. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  757. }
  758. mpf_set(this->m_data, val);
  759. return *this;
  760. }
  761. gmp_float& operator=(const mpz_t val)
  762. {
  763. if (this->m_data[0]._mp_d == nullptr)
  764. {
  765. requested_precision = get_default_precision();
  766. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  767. }
  768. mpf_set_z(this->m_data, val);
  769. return *this;
  770. }
  771. gmp_float& operator=(const mpq_t val)
  772. {
  773. if (this->m_data[0]._mp_d == nullptr)
  774. {
  775. requested_precision = get_default_precision();
  776. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  777. }
  778. mpf_set_q(this->m_data, val);
  779. return *this;
  780. }
  781. template <class V>
  782. typename std::enable_if<std::is_assignable<detail::gmp_float_imp<0>, V>::value, gmp_float&>::type operator=(const V& v)
  783. {
  784. constexpr unsigned d10 = std::is_floating_point<V>::value ?
  785. std::numeric_limits<V>::digits10 :
  786. std::numeric_limits<V>::digits10 ? 1 + std::numeric_limits<V>::digits10 :
  787. 1 + boost::multiprecision::detail::digits2_2_10(std::numeric_limits<V>::digits);
  788. if((thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision) && (precision() < d10))
  789. this->precision(d10);
  790. *static_cast<detail::gmp_float_imp<0>*>(this) = v;
  791. return *this;
  792. }
  793. static unsigned default_precision() noexcept
  794. {
  795. return get_global_default_precision();
  796. }
  797. static void default_precision(unsigned v) noexcept
  798. {
  799. get_global_default_precision() = v;
  800. }
  801. static unsigned thread_default_precision() noexcept
  802. {
  803. return get_default_precision();
  804. }
  805. static void thread_default_precision(unsigned v) noexcept
  806. {
  807. get_default_precision() = v;
  808. }
  809. unsigned precision() const noexcept
  810. {
  811. return requested_precision;
  812. }
  813. void precision(unsigned digits10) noexcept
  814. {
  815. requested_precision = digits10;
  816. mpf_set_prec(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  817. }
  818. //
  819. // Variable precision options:
  820. //
  821. static variable_precision_options default_variable_precision_options()noexcept
  822. {
  823. return get_global_default_options();
  824. }
  825. static variable_precision_options thread_default_variable_precision_options()noexcept
  826. {
  827. return get_default_options();
  828. }
  829. static void default_variable_precision_options(variable_precision_options opts)
  830. {
  831. get_global_default_options() = opts;
  832. }
  833. static void thread_default_variable_precision_options(variable_precision_options opts)
  834. {
  835. get_default_options() = opts;
  836. }
  837. static bool preserve_source_precision()
  838. {
  839. return get_default_options() >= variable_precision_options::preserve_source_precision;
  840. }
  841. static bool preserve_related_precision()
  842. {
  843. return get_default_options() >= variable_precision_options::preserve_related_precision;
  844. }
  845. static bool preserve_all_precision()
  846. {
  847. return get_default_options() >= variable_precision_options::preserve_all_precision;
  848. }
  849. //
  850. // swap:
  851. //
  852. void swap(gmp_float& o)
  853. {
  854. std::swap(requested_precision, o.requested_precision);
  855. gmp_float_imp<0>::swap(o);
  856. }
  857. };
  858. template <unsigned digits10, class T>
  859. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const gmp_float<digits10>& a, const T& b) noexcept
  860. {
  861. return a.compare(b) == 0;
  862. }
  863. template <unsigned digits10, class T>
  864. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const gmp_float<digits10>& a, const T& b) noexcept
  865. {
  866. return a.compare(b) < 0;
  867. }
  868. template <unsigned digits10, class T>
  869. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const gmp_float<digits10>& a, const T& b) noexcept
  870. {
  871. return a.compare(b) > 0;
  872. }
  873. template <unsigned D1, unsigned D2>
  874. inline void eval_add(gmp_float<D1>& result, const gmp_float<D2>& o)
  875. {
  876. mpf_add(result.data(), result.data(), o.data());
  877. }
  878. template <unsigned D1, unsigned D2>
  879. inline void eval_subtract(gmp_float<D1>& result, const gmp_float<D2>& o)
  880. {
  881. mpf_sub(result.data(), result.data(), o.data());
  882. }
  883. template <unsigned D1, unsigned D2>
  884. inline void eval_multiply(gmp_float<D1>& result, const gmp_float<D2>& o)
  885. {
  886. mpf_mul(result.data(), result.data(), o.data());
  887. }
  888. template <unsigned digits10>
  889. inline bool eval_is_zero(const gmp_float<digits10>& val) noexcept
  890. {
  891. return mpf_sgn(val.data()) == 0;
  892. }
  893. template <unsigned D1, unsigned D2>
  894. inline void eval_divide(gmp_float<D1>& result, const gmp_float<D2>& o)
  895. {
  896. if (eval_is_zero(o))
  897. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  898. mpf_div(result.data(), result.data(), o.data());
  899. }
  900. template <unsigned digits10>
  901. inline void eval_add(gmp_float<digits10>& result, unsigned long i)
  902. {
  903. mpf_add_ui(result.data(), result.data(), i);
  904. }
  905. template <unsigned digits10>
  906. inline void eval_subtract(gmp_float<digits10>& result, unsigned long i)
  907. {
  908. mpf_sub_ui(result.data(), result.data(), i);
  909. }
  910. template <unsigned digits10>
  911. inline void eval_multiply(gmp_float<digits10>& result, unsigned long i)
  912. {
  913. mpf_mul_ui(result.data(), result.data(), i);
  914. }
  915. template <unsigned digits10>
  916. inline void eval_divide(gmp_float<digits10>& result, unsigned long i)
  917. {
  918. if (i == 0)
  919. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  920. mpf_div_ui(result.data(), result.data(), i);
  921. }
  922. template <unsigned digits10>
  923. inline void eval_add(gmp_float<digits10>& result, long i)
  924. {
  925. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  926. if (i > 0)
  927. mpf_add_ui(result.data(), result.data(), static_cast<local_uint_type>(i));
  928. else if (i < 0)
  929. mpf_sub_ui(result.data(), result.data(), static_cast<local_uint_type>(-i));
  930. }
  931. template <unsigned digits10>
  932. inline void eval_subtract(gmp_float<digits10>& result, long i)
  933. {
  934. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  935. if (i > 0)
  936. mpf_sub_ui(result.data(), result.data(), static_cast<local_uint_type>(i));
  937. else if (i < 0)
  938. mpf_add_ui(result.data(), result.data(), static_cast<local_uint_type>(-i));
  939. }
  940. template <unsigned digits10>
  941. inline void eval_multiply(gmp_float<digits10>& result, long i)
  942. {
  943. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  944. mpf_mul_ui(result.data(), result.data(), static_cast<local_uint_type>(boost::multiprecision::detail::unsigned_abs(i)));
  945. if (i < 0)
  946. mpf_neg(result.data(), result.data());
  947. }
  948. template <unsigned digits10>
  949. inline void eval_divide(gmp_float<digits10>& result, long i)
  950. {
  951. if (i == 0)
  952. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  953. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  954. mpf_div_ui(result.data(), result.data(), static_cast<local_uint_type>(boost::multiprecision::detail::unsigned_abs(i)));
  955. if (i < 0)
  956. mpf_neg(result.data(), result.data());
  957. }
  958. //
  959. // Specialised 3 arg versions of the basic operators:
  960. //
  961. template <unsigned D1, unsigned D2, unsigned D3>
  962. inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
  963. {
  964. mpf_add(a.data(), x.data(), y.data());
  965. }
  966. template <unsigned D1, unsigned D2>
  967. inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
  968. {
  969. mpf_add_ui(a.data(), x.data(), y);
  970. }
  971. template <unsigned D1, unsigned D2>
  972. inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
  973. {
  974. if (y < 0)
  975. mpf_sub_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
  976. else
  977. mpf_add_ui(a.data(), x.data(), y);
  978. }
  979. template <unsigned D1, unsigned D2>
  980. inline void eval_add(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
  981. {
  982. mpf_add_ui(a.data(), y.data(), x);
  983. }
  984. template <unsigned D1, unsigned D2>
  985. inline void eval_add(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
  986. {
  987. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  988. if (x < 0)
  989. {
  990. mpf_ui_sub(a.data(), static_cast<local_uint_type>(-x), y.data());
  991. mpf_neg(a.data(), a.data());
  992. }
  993. else
  994. mpf_add_ui(a.data(), y.data(), static_cast<local_uint_type>(x));
  995. }
  996. template <unsigned D1, unsigned D2, unsigned D3>
  997. inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
  998. {
  999. mpf_sub(a.data(), x.data(), y.data());
  1000. }
  1001. template <unsigned D1, unsigned D2>
  1002. inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
  1003. {
  1004. mpf_sub_ui(a.data(), x.data(), y);
  1005. }
  1006. template <unsigned D1, unsigned D2>
  1007. inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
  1008. {
  1009. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1010. if (y < 0)
  1011. mpf_add_ui(a.data(), x.data(), static_cast<local_uint_type>(-y));
  1012. else
  1013. mpf_sub_ui(a.data(), x.data(), static_cast<local_uint_type>(y));
  1014. }
  1015. template <unsigned D1, unsigned D2>
  1016. inline void eval_subtract(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
  1017. {
  1018. mpf_ui_sub(a.data(), x, y.data());
  1019. }
  1020. template <unsigned D1, unsigned D2>
  1021. inline void eval_subtract(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
  1022. {
  1023. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1024. if (x < 0)
  1025. {
  1026. mpf_add_ui(a.data(), y.data(), static_cast<local_uint_type>(-x));
  1027. mpf_neg(a.data(), a.data());
  1028. }
  1029. else
  1030. mpf_ui_sub(a.data(), static_cast<local_uint_type>(x), y.data());
  1031. }
  1032. template <unsigned D1, unsigned D2, unsigned D3>
  1033. inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
  1034. {
  1035. mpf_mul(a.data(), x.data(), y.data());
  1036. }
  1037. template <unsigned D1, unsigned D2>
  1038. inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
  1039. {
  1040. mpf_mul_ui(a.data(), x.data(), y);
  1041. }
  1042. template <unsigned D1, unsigned D2>
  1043. inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
  1044. {
  1045. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1046. if (y < 0)
  1047. {
  1048. mpf_mul_ui(a.data(), x.data(), static_cast<local_uint_type>(-y));
  1049. a.negate();
  1050. }
  1051. else
  1052. mpf_mul_ui(a.data(), x.data(), static_cast<local_uint_type>(y));
  1053. }
  1054. template <unsigned D1, unsigned D2>
  1055. inline void eval_multiply(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
  1056. {
  1057. mpf_mul_ui(a.data(), y.data(), x);
  1058. }
  1059. template <unsigned D1, unsigned D2>
  1060. inline void eval_multiply(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
  1061. {
  1062. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1063. if (x < 0)
  1064. {
  1065. mpf_mul_ui(a.data(), y.data(), static_cast<local_uint_type>(-x));
  1066. mpf_neg(a.data(), a.data());
  1067. }
  1068. else
  1069. mpf_mul_ui(a.data(), y.data(), static_cast<local_uint_type>(x));
  1070. }
  1071. template <unsigned D1, unsigned D2, unsigned D3>
  1072. inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
  1073. {
  1074. if (eval_is_zero(y))
  1075. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1076. mpf_div(a.data(), x.data(), y.data());
  1077. }
  1078. template <unsigned D1, unsigned D2>
  1079. inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
  1080. {
  1081. if (y == 0)
  1082. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1083. mpf_div_ui(a.data(), x.data(), y);
  1084. }
  1085. template <unsigned D1, unsigned D2>
  1086. inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
  1087. {
  1088. if (y == 0)
  1089. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1090. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1091. if (y < 0)
  1092. {
  1093. mpf_div_ui(a.data(), x.data(), static_cast<local_uint_type>(-y));
  1094. a.negate();
  1095. }
  1096. else
  1097. mpf_div_ui(a.data(), x.data(), static_cast<local_uint_type>(y));
  1098. }
  1099. template <unsigned D1, unsigned D2>
  1100. inline void eval_divide(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
  1101. {
  1102. if (eval_is_zero(y))
  1103. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1104. mpf_ui_div(a.data(), x, y.data());
  1105. }
  1106. template <unsigned D1, unsigned D2>
  1107. inline void eval_divide(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
  1108. {
  1109. if (eval_is_zero(y))
  1110. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1111. if (x < 0)
  1112. {
  1113. mpf_ui_div(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data());
  1114. mpf_neg(a.data(), a.data());
  1115. }
  1116. else
  1117. {
  1118. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1119. mpf_ui_div(a.data(), static_cast<local_uint_type>(x), y.data());
  1120. }
  1121. }
  1122. template <unsigned digits10>
  1123. inline int eval_get_sign(const gmp_float<digits10>& val) noexcept
  1124. {
  1125. return mpf_sgn(val.data());
  1126. }
  1127. template <unsigned digits10>
  1128. inline void eval_convert_to(unsigned long* result, const gmp_float<digits10>& val) noexcept
  1129. {
  1130. if (0 == mpf_fits_ulong_p(val.data()))
  1131. *result = (std::numeric_limits<unsigned long>::max)();
  1132. else
  1133. *result = static_cast<unsigned long>(mpf_get_ui(val.data()));
  1134. }
  1135. template <unsigned digits10>
  1136. inline void eval_convert_to(long* result, const gmp_float<digits10>& val) noexcept
  1137. {
  1138. if (0 == mpf_fits_slong_p(val.data()))
  1139. {
  1140. *result = (std::numeric_limits<long>::max)();
  1141. *result *= mpf_sgn(val.data());
  1142. }
  1143. else
  1144. *result = static_cast<long>(mpf_get_si(val.data()));
  1145. }
  1146. #ifdef BOOST_MP_STANDALONE
  1147. template <unsigned digits10>
  1148. inline void eval_convert_to(long double* result, const gmp_float<digits10>& val) noexcept
  1149. {
  1150. mp_exp_t exp = 0;
  1151. detail::gmp_char_ptr val_char_ptr {mpf_get_str(nullptr, &exp, 10, LDBL_DIG, val.data())};
  1152. auto temp_string = std::string(val_char_ptr.get());
  1153. if(exp > 0 && static_cast<std::size_t>(exp) < temp_string.size())
  1154. {
  1155. if(temp_string.front() == '-')
  1156. {
  1157. ++exp;
  1158. }
  1159. temp_string.insert(static_cast<std::size_t>(exp), static_cast<std::size_t>(1u), '.');
  1160. }
  1161. *result = std::strtold(temp_string.c_str(), nullptr);
  1162. if((temp_string.size() == 2ul && *result < 0.0l) ||
  1163. (static_cast<std::size_t>(exp) > temp_string.size()))
  1164. {
  1165. *result *= std::pow(10l, exp-1);
  1166. }
  1167. }
  1168. #endif // BOOST_MP_STANDALONE
  1169. template <unsigned digits10>
  1170. inline void eval_convert_to(double* result, const gmp_float<digits10>& val) noexcept
  1171. {
  1172. *result = mpf_get_d(val.data());
  1173. }
  1174. #ifdef BOOST_HAS_LONG_LONG
  1175. template <unsigned digits10>
  1176. inline void eval_convert_to(long long* result, const gmp_float<digits10>& val)
  1177. {
  1178. gmp_float<digits10> t(val);
  1179. if (eval_get_sign(t) < 0)
  1180. t.negate();
  1181. long digits = std::numeric_limits<long long>::digits - std::numeric_limits<long>::digits;
  1182. if (digits > 0)
  1183. mpf_div_2exp(t.data(), t.data(), digits);
  1184. if (!mpf_fits_slong_p(t.data()))
  1185. {
  1186. if (eval_get_sign(val) < 0)
  1187. *result = (std::numeric_limits<long long>::min)();
  1188. else
  1189. *result = (std::numeric_limits<long long>::max)();
  1190. return;
  1191. };
  1192. *result = mpf_get_si(t.data());
  1193. while (digits > 0)
  1194. {
  1195. *result <<= digits;
  1196. digits -= std::numeric_limits<unsigned long>::digits;
  1197. mpf_mul_2exp(t.data(), t.data(), digits >= 0 ? std::numeric_limits<unsigned long>::digits : std::numeric_limits<unsigned long>::digits + digits);
  1198. unsigned long l = static_cast<unsigned long>(mpf_get_ui(t.data()));
  1199. if (digits < 0)
  1200. l >>= -digits;
  1201. *result |= l;
  1202. }
  1203. if (eval_get_sign(val) < 0)
  1204. *result = -*result;
  1205. }
  1206. template <unsigned digits10>
  1207. inline void eval_convert_to(unsigned long long* result, const gmp_float<digits10>& val)
  1208. {
  1209. gmp_float<digits10> t(val);
  1210. long digits = std::numeric_limits<long long>::digits - std::numeric_limits<long>::digits;
  1211. if (digits > 0)
  1212. mpf_div_2exp(t.data(), t.data(), digits);
  1213. if (!mpf_fits_ulong_p(t.data()))
  1214. {
  1215. *result = (std::numeric_limits<long long>::max)();
  1216. return;
  1217. }
  1218. *result = mpf_get_ui(t.data());
  1219. while (digits > 0)
  1220. {
  1221. *result <<= digits;
  1222. digits -= std::numeric_limits<unsigned long>::digits;
  1223. mpf_mul_2exp(t.data(), t.data(), digits >= 0 ? std::numeric_limits<unsigned long>::digits : std::numeric_limits<unsigned long>::digits + digits);
  1224. unsigned long l = static_cast<unsigned long>(mpf_get_ui(t.data()));
  1225. if (digits < 0)
  1226. l >>= -digits;
  1227. *result |= l;
  1228. }
  1229. }
  1230. #endif
  1231. #ifdef BOOST_HAS_FLOAT128
  1232. template <unsigned digits10>
  1233. inline void eval_convert_to(float128_type* result, const gmp_float<digits10>& val)
  1234. {
  1235. *result = float128_procs::strtoflt128(val.str(0, std::ios_base::scientific).c_str(), nullptr);
  1236. }
  1237. #endif
  1238. //
  1239. // Native non-member operations:
  1240. //
  1241. template <unsigned Digits10>
  1242. inline void eval_sqrt(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  1243. {
  1244. mpf_sqrt(result.data(), val.data());
  1245. }
  1246. template <unsigned Digits10>
  1247. inline void eval_abs(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  1248. {
  1249. mpf_abs(result.data(), val.data());
  1250. }
  1251. template <unsigned Digits10>
  1252. inline void eval_fabs(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  1253. {
  1254. mpf_abs(result.data(), val.data());
  1255. }
  1256. template <unsigned Digits10>
  1257. inline void eval_ceil(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  1258. {
  1259. mpf_ceil(result.data(), val.data());
  1260. }
  1261. template <unsigned Digits10>
  1262. inline void eval_floor(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  1263. {
  1264. mpf_floor(result.data(), val.data());
  1265. }
  1266. template <unsigned Digits10>
  1267. inline void eval_trunc(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
  1268. {
  1269. mpf_trunc(result.data(), val.data());
  1270. }
  1271. template <unsigned Digits10>
  1272. inline void eval_ldexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, long e)
  1273. {
  1274. if (e > 0)
  1275. mpf_mul_2exp(result.data(), val.data(), static_cast<mp_bitcnt_t>(e));
  1276. else if (e < 0)
  1277. mpf_div_2exp(result.data(), val.data(), static_cast<mp_bitcnt_t>(-e));
  1278. else
  1279. result = val;
  1280. }
  1281. template <unsigned Digits10>
  1282. inline void eval_frexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, int* e)
  1283. {
  1284. #if (BOOST_MP_MPIR_VERSION >= 20600) && (BOOST_MP_MPIR_VERSION < 30000)
  1285. mpir_si v;
  1286. mpf_get_d_2exp(&v, val.data());
  1287. #else
  1288. long v;
  1289. mpf_get_d_2exp(&v, val.data());
  1290. #endif
  1291. *e = static_cast<int>(v);
  1292. eval_ldexp(result, val, -v);
  1293. }
  1294. template <unsigned Digits10>
  1295. inline void eval_frexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, long* e)
  1296. {
  1297. #if (BOOST_MP_MPIR_VERSION >= 20600) && (BOOST_MP_MPIR_VERSION < 30000)
  1298. mpir_si v;
  1299. mpf_get_d_2exp(&v, val.data());
  1300. *e = v;
  1301. eval_ldexp(result, val, -v);
  1302. #else
  1303. mpf_get_d_2exp(e, val.data());
  1304. eval_ldexp(result, val, -*e);
  1305. #endif
  1306. }
  1307. template <unsigned Digits10>
  1308. inline std::size_t hash_value(const gmp_float<Digits10>& val)
  1309. {
  1310. std::size_t result = 0;
  1311. for (int i = 0; i < std::abs(val.data()[0]._mp_size); ++i)
  1312. boost::multiprecision::detail::hash_combine(result, val.data()[0]._mp_d[i]);
  1313. boost::multiprecision::detail::hash_combine(result, val.data()[0]._mp_exp, val.data()[0]._mp_size);
  1314. return result;
  1315. }
  1316. struct gmp_int
  1317. {
  1318. #ifdef BOOST_HAS_LONG_LONG
  1319. using signed_types = std::tuple<long, long long> ;
  1320. using unsigned_types = std::tuple<unsigned long, unsigned long long>;
  1321. #else
  1322. using signed_types = std::tuple<long> ;
  1323. using unsigned_types = std::tuple<unsigned long>;
  1324. #endif
  1325. using float_types = std::tuple<double, long double>;
  1326. gmp_int()
  1327. {
  1328. mpz_init(this->m_data);
  1329. }
  1330. gmp_int(const gmp_int& o)
  1331. {
  1332. if (o.m_data[0]._mp_d)
  1333. mpz_init_set(m_data, o.m_data);
  1334. else
  1335. mpz_init(this->m_data);
  1336. }
  1337. // rvalue
  1338. gmp_int(gmp_int&& o) noexcept
  1339. {
  1340. m_data[0] = o.m_data[0];
  1341. o.m_data[0]._mp_d = nullptr;
  1342. }
  1343. explicit gmp_int(const mpf_t val)
  1344. {
  1345. mpz_init(this->m_data);
  1346. mpz_set_f(this->m_data, val);
  1347. }
  1348. gmp_int(const mpz_t val)
  1349. {
  1350. mpz_init_set(this->m_data, val);
  1351. }
  1352. gmp_int(long i)
  1353. {
  1354. mpz_init_set_si(this->m_data, i);
  1355. }
  1356. gmp_int(unsigned long i)
  1357. {
  1358. mpz_init_set_ui(this->m_data, i);
  1359. }
  1360. explicit gmp_int(const mpq_t val)
  1361. {
  1362. mpz_init(this->m_data);
  1363. mpz_set_q(this->m_data, val);
  1364. }
  1365. template <unsigned Digits10>
  1366. explicit gmp_int(const gmp_float<Digits10>& o)
  1367. {
  1368. mpz_init(this->m_data);
  1369. mpz_set_f(this->m_data, o.data());
  1370. }
  1371. explicit gmp_int(const gmp_rational& o);
  1372. gmp_int& operator=(const gmp_int& o)
  1373. {
  1374. if (m_data[0]._mp_d == nullptr)
  1375. mpz_init(this->m_data);
  1376. mpz_set(m_data, o.m_data);
  1377. return *this;
  1378. }
  1379. // rvalue copy
  1380. gmp_int& operator=(gmp_int&& o) noexcept
  1381. {
  1382. mpz_swap(m_data, o.m_data);
  1383. return *this;
  1384. }
  1385. #ifdef BOOST_HAS_LONG_LONG
  1386. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  1387. gmp_int& operator=(unsigned long long i)
  1388. {
  1389. *this = static_cast<unsigned long>(i);
  1390. return *this;
  1391. }
  1392. #else
  1393. gmp_int& operator=(unsigned long long i)
  1394. {
  1395. if (m_data[0]._mp_d == nullptr)
  1396. mpz_init(this->m_data);
  1397. unsigned long long mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
  1398. unsigned shift = 0;
  1399. mpz_t t;
  1400. mpz_set_ui(m_data, 0);
  1401. mpz_init_set_ui(t, 0);
  1402. while (i)
  1403. {
  1404. mpz_set_ui(t, static_cast<unsigned long>(i & mask));
  1405. if (shift)
  1406. mpz_mul_2exp(t, t, shift);
  1407. mpz_add(m_data, m_data, t);
  1408. shift += std::numeric_limits<unsigned long>::digits;
  1409. i >>= std::numeric_limits<unsigned long>::digits;
  1410. }
  1411. mpz_clear(t);
  1412. return *this;
  1413. }
  1414. #endif
  1415. gmp_int& operator=(long long i)
  1416. {
  1417. if (m_data[0]._mp_d == nullptr)
  1418. mpz_init(this->m_data);
  1419. bool neg = i < 0;
  1420. *this = boost::multiprecision::detail::unsigned_abs(i);
  1421. if (neg)
  1422. mpz_neg(m_data, m_data);
  1423. return *this;
  1424. }
  1425. #endif
  1426. #ifdef BOOST_HAS_INT128
  1427. gmp_int& operator=(uint128_type i)
  1428. {
  1429. if (m_data[0]._mp_d == nullptr)
  1430. mpz_init(this->m_data);
  1431. uint128_type mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
  1432. unsigned shift = 0;
  1433. mpz_t t;
  1434. mpz_set_ui(m_data, 0);
  1435. mpz_init_set_ui(t, 0);
  1436. while (i)
  1437. {
  1438. mpz_set_ui(t, static_cast<unsigned long>(i & mask));
  1439. if (shift)
  1440. mpz_mul_2exp(t, t, shift);
  1441. mpz_add(m_data, m_data, t);
  1442. shift += std::numeric_limits<unsigned long>::digits;
  1443. i >>= std::numeric_limits<unsigned long>::digits;
  1444. }
  1445. mpz_clear(t);
  1446. return *this;
  1447. }
  1448. gmp_int& operator=(int128_type i)
  1449. {
  1450. if (m_data[0]._mp_d == nullptr)
  1451. mpz_init(this->m_data);
  1452. bool neg = i < 0;
  1453. *this = boost::multiprecision::detail::unsigned_abs(i);
  1454. if (neg)
  1455. mpz_neg(m_data, m_data);
  1456. return *this;
  1457. }
  1458. #endif
  1459. gmp_int& operator=(unsigned long i)
  1460. {
  1461. if (m_data[0]._mp_d == nullptr)
  1462. mpz_init(this->m_data);
  1463. mpz_set_ui(m_data, i);
  1464. return *this;
  1465. }
  1466. gmp_int& operator=(long i)
  1467. {
  1468. if (m_data[0]._mp_d == nullptr)
  1469. mpz_init(this->m_data);
  1470. mpz_set_si(m_data, i);
  1471. return *this;
  1472. }
  1473. gmp_int& operator=(double d)
  1474. {
  1475. if (m_data[0]._mp_d == nullptr)
  1476. mpz_init(this->m_data);
  1477. mpz_set_d(m_data, d);
  1478. return *this;
  1479. }
  1480. template <class F>
  1481. gmp_int& assign_float(F a)
  1482. {
  1483. BOOST_MP_FLOAT128_USING using std::floor; using std::frexp; using std::ldexp;
  1484. if (m_data[0]._mp_d == nullptr)
  1485. mpz_init(this->m_data);
  1486. if (a == 0)
  1487. {
  1488. mpz_set_si(m_data, 0);
  1489. return *this;
  1490. }
  1491. if (a == 1)
  1492. {
  1493. mpz_set_si(m_data, 1);
  1494. return *this;
  1495. }
  1496. BOOST_MP_ASSERT(!BOOST_MP_ISINF(a));
  1497. BOOST_MP_ASSERT(!BOOST_MP_ISNAN(a));
  1498. int e;
  1499. F f, term;
  1500. mpz_set_ui(m_data, 0u);
  1501. f = frexp(a, &e);
  1502. constexpr int shift = std::numeric_limits<int>::digits - 1;
  1503. while (f != static_cast<F>(0.0f))
  1504. {
  1505. // extract int sized bits from f:
  1506. f = ldexp(f, shift);
  1507. term = floor(f);
  1508. e -= shift;
  1509. mpz_mul_2exp(m_data, m_data, shift);
  1510. if (term > 0)
  1511. mpz_add_ui(m_data, m_data, static_cast<unsigned>(term));
  1512. else
  1513. mpz_sub_ui(m_data, m_data, static_cast<unsigned>(-term));
  1514. f -= term;
  1515. }
  1516. if (e > 0)
  1517. mpz_mul_2exp(m_data, m_data, static_cast<mp_bitcnt_t>(e));
  1518. else if (e < 0)
  1519. mpz_div_2exp(m_data, m_data, static_cast<mp_bitcnt_t>(-e));
  1520. return *this;
  1521. }
  1522. gmp_int& operator=(long double a)
  1523. {
  1524. return assign_float(a);
  1525. }
  1526. gmp_int& operator=(const char* s)
  1527. {
  1528. if (m_data[0]._mp_d == nullptr)
  1529. mpz_init(this->m_data);
  1530. std::size_t n = s ? std::strlen(s) : 0;
  1531. int radix = 10;
  1532. if (n && (*s == '0'))
  1533. {
  1534. if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
  1535. {
  1536. radix = 16;
  1537. s += 2;
  1538. n -= 2;
  1539. }
  1540. else
  1541. {
  1542. radix = 8;
  1543. n -= 1;
  1544. }
  1545. }
  1546. if (n)
  1547. {
  1548. if (0 != mpz_set_str(m_data, s, radix))
  1549. BOOST_MP_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid integer.")));
  1550. }
  1551. else
  1552. mpz_set_ui(m_data, 0);
  1553. return *this;
  1554. }
  1555. #ifdef BOOST_HAS_FLOAT128
  1556. gmp_int& operator=(float128_type a)
  1557. {
  1558. return assign_float(a);
  1559. }
  1560. #endif
  1561. gmp_int& operator=(const mpf_t val)
  1562. {
  1563. if (m_data[0]._mp_d == nullptr)
  1564. mpz_init(this->m_data);
  1565. mpz_set_f(this->m_data, val);
  1566. return *this;
  1567. }
  1568. gmp_int& operator=(const mpz_t val)
  1569. {
  1570. if (m_data[0]._mp_d == nullptr)
  1571. mpz_init(this->m_data);
  1572. mpz_set(this->m_data, val);
  1573. return *this;
  1574. }
  1575. gmp_int& operator=(const mpq_t val)
  1576. {
  1577. if (m_data[0]._mp_d == nullptr)
  1578. mpz_init(this->m_data);
  1579. mpz_set_q(this->m_data, val);
  1580. return *this;
  1581. }
  1582. template <unsigned Digits10>
  1583. gmp_int& operator=(const gmp_float<Digits10>& o)
  1584. {
  1585. if (m_data[0]._mp_d == nullptr)
  1586. mpz_init(this->m_data);
  1587. mpz_set_f(this->m_data, o.data());
  1588. return *this;
  1589. }
  1590. gmp_int& operator=(const gmp_rational& o);
  1591. void swap(gmp_int& o)
  1592. {
  1593. mpz_swap(m_data, o.m_data);
  1594. }
  1595. std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
  1596. {
  1597. BOOST_MP_ASSERT(m_data[0]._mp_d);
  1598. int base = 10;
  1599. if ((f & std::ios_base::oct) == std::ios_base::oct)
  1600. base = 8;
  1601. else if ((f & std::ios_base::hex) == std::ios_base::hex)
  1602. base = 16;
  1603. //
  1604. // sanity check, bases 8 and 16 are only available for positive numbers:
  1605. //
  1606. if ((base != 10) && (mpz_sgn(m_data) < 0))
  1607. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Formatted output in bases 8 or 16 is only available for positive numbers"));
  1608. void* (*alloc_func_ptr)(size_t);
  1609. void* (*realloc_func_ptr)(void*, size_t, size_t);
  1610. void (*free_func_ptr)(void*, size_t);
  1611. const char* ps = mpz_get_str(nullptr, base, m_data);
  1612. std::string s = ps;
  1613. mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
  1614. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  1615. if (f & std::ios_base::uppercase)
  1616. for (size_t i = 0; i < s.length(); ++i)
  1617. s[i] = static_cast<char>(std::toupper(s[i]));
  1618. if ((base != 10) && (f & std::ios_base::showbase))
  1619. {
  1620. int pos = s[0] == '-' ? 1 : 0;
  1621. const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
  1622. s.insert(static_cast<std::string::size_type>(pos), pp);
  1623. }
  1624. if ((f & std::ios_base::showpos) && (s[0] != '-'))
  1625. s.insert(static_cast<std::string::size_type>(0), 1, '+');
  1626. return s;
  1627. }
  1628. ~gmp_int() noexcept
  1629. {
  1630. if (m_data[0]._mp_d)
  1631. mpz_clear(m_data);
  1632. }
  1633. void negate() noexcept
  1634. {
  1635. BOOST_MP_ASSERT(m_data[0]._mp_d);
  1636. mpz_neg(m_data, m_data);
  1637. }
  1638. int compare(const gmp_int& o) const noexcept
  1639. {
  1640. BOOST_MP_ASSERT(m_data[0]._mp_d && o.m_data[0]._mp_d);
  1641. return mpz_cmp(m_data, o.m_data);
  1642. }
  1643. int compare(long i) const noexcept
  1644. {
  1645. BOOST_MP_ASSERT(m_data[0]._mp_d);
  1646. return mpz_cmp_si(m_data, i);
  1647. }
  1648. int compare(unsigned long i) const noexcept
  1649. {
  1650. BOOST_MP_ASSERT(m_data[0]._mp_d);
  1651. return mpz_cmp_ui(m_data, i);
  1652. }
  1653. template <class V>
  1654. int compare(V v) const
  1655. {
  1656. gmp_int d;
  1657. d = v;
  1658. return compare(d);
  1659. }
  1660. mpz_t& data() noexcept
  1661. {
  1662. BOOST_MP_ASSERT(m_data[0]._mp_d);
  1663. return m_data;
  1664. }
  1665. const mpz_t& data() const noexcept
  1666. {
  1667. BOOST_MP_ASSERT(m_data[0]._mp_d);
  1668. return m_data;
  1669. }
  1670. protected:
  1671. mpz_t m_data;
  1672. };
  1673. template <class T>
  1674. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const gmp_int& a, const T& b)
  1675. {
  1676. return a.compare(b) == 0;
  1677. }
  1678. template <class T>
  1679. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const gmp_int& a, const T& b)
  1680. {
  1681. return a.compare(b) < 0;
  1682. }
  1683. template <class T>
  1684. inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const gmp_int& a, const T& b)
  1685. {
  1686. return a.compare(b) > 0;
  1687. }
  1688. inline bool eval_is_zero(const gmp_int& val)
  1689. {
  1690. return mpz_sgn(val.data()) == 0;
  1691. }
  1692. inline void eval_add(gmp_int& t, const gmp_int& o)
  1693. {
  1694. mpz_add(t.data(), t.data(), o.data());
  1695. }
  1696. inline void eval_multiply_add(gmp_int& t, const gmp_int& a, const gmp_int& b)
  1697. {
  1698. mpz_addmul(t.data(), a.data(), b.data());
  1699. }
  1700. inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, const gmp_int& b)
  1701. {
  1702. mpz_submul(t.data(), a.data(), b.data());
  1703. }
  1704. inline void eval_subtract(gmp_int& t, const gmp_int& o)
  1705. {
  1706. mpz_sub(t.data(), t.data(), o.data());
  1707. }
  1708. inline void eval_multiply(gmp_int& t, const gmp_int& o)
  1709. {
  1710. mpz_mul(t.data(), t.data(), o.data());
  1711. }
  1712. inline void eval_divide(gmp_int& t, const gmp_int& o)
  1713. {
  1714. if (eval_is_zero(o))
  1715. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1716. mpz_tdiv_q(t.data(), t.data(), o.data());
  1717. }
  1718. inline void eval_modulus(gmp_int& t, const gmp_int& o)
  1719. {
  1720. mpz_tdiv_r(t.data(), t.data(), o.data());
  1721. }
  1722. inline void eval_add(gmp_int& t, unsigned long i)
  1723. {
  1724. mpz_add_ui(t.data(), t.data(), i);
  1725. }
  1726. inline void eval_multiply_add(gmp_int& t, const gmp_int& a, unsigned long i)
  1727. {
  1728. mpz_addmul_ui(t.data(), a.data(), i);
  1729. }
  1730. inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, unsigned long i)
  1731. {
  1732. mpz_submul_ui(t.data(), a.data(), i);
  1733. }
  1734. inline void eval_subtract(gmp_int& t, unsigned long i)
  1735. {
  1736. mpz_sub_ui(t.data(), t.data(), i);
  1737. }
  1738. inline void eval_multiply(gmp_int& t, unsigned long i)
  1739. {
  1740. mpz_mul_ui(t.data(), t.data(), i);
  1741. }
  1742. inline void eval_modulus(gmp_int& t, unsigned long i)
  1743. {
  1744. mpz_tdiv_r_ui(t.data(), t.data(), i);
  1745. }
  1746. inline void eval_divide(gmp_int& t, unsigned long i)
  1747. {
  1748. if (i == 0)
  1749. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1750. mpz_tdiv_q_ui(t.data(), t.data(), i);
  1751. }
  1752. inline void eval_add(gmp_int& t, long i)
  1753. {
  1754. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1755. if (i > 0)
  1756. mpz_add_ui(t.data(), t.data(), static_cast<local_uint_type>(i));
  1757. else if (i < 0)
  1758. mpz_sub_ui(t.data(), t.data(), static_cast<local_uint_type>(-i));
  1759. }
  1760. inline void eval_multiply_add(gmp_int& t, const gmp_int& a, long i)
  1761. {
  1762. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1763. if (i > 0)
  1764. mpz_addmul_ui(t.data(), a.data(), static_cast<local_uint_type>(i));
  1765. else
  1766. mpz_submul_ui(t.data(), a.data(), static_cast<local_uint_type>(-i));
  1767. }
  1768. inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, long i)
  1769. {
  1770. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1771. if (i > 0)
  1772. mpz_submul_ui(t.data(), a.data(), static_cast<local_uint_type>(i));
  1773. else
  1774. mpz_addmul_ui(t.data(), a.data(), static_cast<local_uint_type>(-i));
  1775. }
  1776. inline void eval_subtract(gmp_int& t, long i)
  1777. {
  1778. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1779. if (i > 0)
  1780. mpz_sub_ui(t.data(), t.data(), static_cast<local_uint_type>(i));
  1781. else if (i < 0)
  1782. mpz_add_ui(t.data(), t.data(), static_cast<local_uint_type>(-i));
  1783. }
  1784. inline void eval_multiply(gmp_int& t, long i)
  1785. {
  1786. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1787. mpz_mul_ui(t.data(), t.data(), static_cast<local_uint_type>(boost::multiprecision::detail::unsigned_abs(i)));
  1788. if (i < 0)
  1789. mpz_neg(t.data(), t.data());
  1790. }
  1791. inline void eval_modulus(gmp_int& t, long i)
  1792. {
  1793. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1794. mpz_tdiv_r_ui(t.data(), t.data(), static_cast<local_uint_type>(boost::multiprecision::detail::unsigned_abs(i)));
  1795. }
  1796. inline void eval_divide(gmp_int& t, long i)
  1797. {
  1798. if (i == 0)
  1799. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1800. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1801. mpz_tdiv_q_ui(t.data(), t.data(), static_cast<local_uint_type>(boost::multiprecision::detail::unsigned_abs(i)));
  1802. if (i < 0)
  1803. mpz_neg(t.data(), t.data());
  1804. }
  1805. template <class UI>
  1806. inline void eval_left_shift(gmp_int& t, UI i)
  1807. {
  1808. mpz_mul_2exp(t.data(), t.data(), static_cast<unsigned long>(i));
  1809. }
  1810. template <class UI>
  1811. inline void eval_right_shift(gmp_int& t, UI i)
  1812. {
  1813. mpz_fdiv_q_2exp(t.data(), t.data(), static_cast<unsigned long>(i));
  1814. }
  1815. template <class UI>
  1816. inline void eval_left_shift(gmp_int& t, const gmp_int& v, UI i)
  1817. {
  1818. mpz_mul_2exp(t.data(), v.data(), static_cast<unsigned long>(i));
  1819. }
  1820. template <class UI>
  1821. inline void eval_right_shift(gmp_int& t, const gmp_int& v, UI i)
  1822. {
  1823. mpz_fdiv_q_2exp(t.data(), v.data(), static_cast<unsigned long>(i));
  1824. }
  1825. inline void eval_bitwise_and(gmp_int& result, const gmp_int& v)
  1826. {
  1827. mpz_and(result.data(), result.data(), v.data());
  1828. }
  1829. inline void eval_bitwise_or(gmp_int& result, const gmp_int& v)
  1830. {
  1831. mpz_ior(result.data(), result.data(), v.data());
  1832. }
  1833. inline void eval_bitwise_xor(gmp_int& result, const gmp_int& v)
  1834. {
  1835. mpz_xor(result.data(), result.data(), v.data());
  1836. }
  1837. inline void eval_add(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1838. {
  1839. mpz_add(t.data(), p.data(), o.data());
  1840. }
  1841. inline void eval_subtract(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1842. {
  1843. mpz_sub(t.data(), p.data(), o.data());
  1844. }
  1845. inline void eval_multiply(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1846. {
  1847. mpz_mul(t.data(), p.data(), o.data());
  1848. }
  1849. inline void eval_divide(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1850. {
  1851. if (eval_is_zero(o))
  1852. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1853. mpz_tdiv_q(t.data(), p.data(), o.data());
  1854. }
  1855. inline void eval_modulus(gmp_int& t, const gmp_int& p, const gmp_int& o)
  1856. {
  1857. mpz_tdiv_r(t.data(), p.data(), o.data());
  1858. }
  1859. inline void eval_add(gmp_int& t, const gmp_int& p, unsigned long i)
  1860. {
  1861. mpz_add_ui(t.data(), p.data(), i);
  1862. }
  1863. inline void eval_subtract(gmp_int& t, const gmp_int& p, unsigned long i)
  1864. {
  1865. mpz_sub_ui(t.data(), p.data(), i);
  1866. }
  1867. inline void eval_multiply(gmp_int& t, const gmp_int& p, unsigned long i)
  1868. {
  1869. mpz_mul_ui(t.data(), p.data(), i);
  1870. }
  1871. inline void eval_modulus(gmp_int& t, const gmp_int& p, unsigned long i)
  1872. {
  1873. mpz_tdiv_r_ui(t.data(), p.data(), i);
  1874. }
  1875. inline void eval_divide(gmp_int& t, const gmp_int& p, unsigned long i)
  1876. {
  1877. if (i == 0)
  1878. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1879. mpz_tdiv_q_ui(t.data(), p.data(), i);
  1880. }
  1881. inline void eval_add(gmp_int& t, const gmp_int& p, long i)
  1882. {
  1883. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1884. if (i > 0)
  1885. mpz_add_ui(t.data(), p.data(), static_cast<local_uint_type>(i));
  1886. else
  1887. mpz_sub_ui(t.data(), p.data(), static_cast<local_uint_type>(-i));
  1888. }
  1889. inline void eval_subtract(gmp_int& t, const gmp_int& p, long i)
  1890. {
  1891. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  1892. if (i > 0)
  1893. mpz_sub_ui(t.data(), p.data(), static_cast<local_uint_type>(i));
  1894. else
  1895. mpz_add_ui(t.data(), p.data(), static_cast<local_uint_type>(-i));
  1896. }
  1897. inline void eval_multiply(gmp_int& t, const gmp_int& p, long i)
  1898. {
  1899. mpz_mul_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
  1900. if (i < 0)
  1901. mpz_neg(t.data(), t.data());
  1902. }
  1903. inline void eval_modulus(gmp_int& t, const gmp_int& p, long i)
  1904. {
  1905. mpz_tdiv_r_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
  1906. }
  1907. inline void eval_divide(gmp_int& t, const gmp_int& p, long i)
  1908. {
  1909. if (i == 0)
  1910. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  1911. mpz_tdiv_q_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
  1912. if (i < 0)
  1913. mpz_neg(t.data(), t.data());
  1914. }
  1915. inline void eval_bitwise_and(gmp_int& result, const gmp_int& u, const gmp_int& v)
  1916. {
  1917. mpz_and(result.data(), u.data(), v.data());
  1918. }
  1919. inline void eval_bitwise_or(gmp_int& result, const gmp_int& u, const gmp_int& v)
  1920. {
  1921. mpz_ior(result.data(), u.data(), v.data());
  1922. }
  1923. inline void eval_bitwise_xor(gmp_int& result, const gmp_int& u, const gmp_int& v)
  1924. {
  1925. mpz_xor(result.data(), u.data(), v.data());
  1926. }
  1927. inline void eval_complement(gmp_int& result, const gmp_int& u)
  1928. {
  1929. mpz_com(result.data(), u.data());
  1930. }
  1931. inline int eval_get_sign(const gmp_int& val)
  1932. {
  1933. return mpz_sgn(val.data());
  1934. }
  1935. inline void eval_convert_to(unsigned long* result, const gmp_int& val)
  1936. {
  1937. if (mpz_sgn(val.data()) < 0)
  1938. {
  1939. BOOST_MP_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
  1940. }
  1941. else
  1942. *result = static_cast<unsigned long>(mpz_get_ui(val.data()));
  1943. }
  1944. inline void eval_convert_to(long* result, const gmp_int& val)
  1945. {
  1946. if (0 == mpz_fits_slong_p(val.data()))
  1947. {
  1948. *result = mpz_sgn(val.data()) < 0 ? (std::numeric_limits<long>::min)() : (std::numeric_limits<long>::max)();
  1949. }
  1950. else
  1951. *result = static_cast<long>(mpz_get_si(val.data()));
  1952. }
  1953. inline void eval_convert_to(long double* result, const gmp_int& val)
  1954. {
  1955. detail::gmp_char_ptr val_char_ptr {mpz_get_str(nullptr, 10, val.data())};
  1956. *result = std::strtold(val_char_ptr.get(), nullptr);
  1957. }
  1958. inline void eval_convert_to(double* result, const gmp_int& val)
  1959. {
  1960. *result = mpz_get_d(val.data());
  1961. }
  1962. #ifdef BOOST_HAS_LONG_LONG
  1963. inline void eval_convert_to(unsigned long long* result, const gmp_int& val)
  1964. {
  1965. if (mpz_sgn(val.data()) < 0)
  1966. {
  1967. BOOST_MP_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
  1968. }
  1969. *result = 0;
  1970. gmp_int t(val);
  1971. unsigned parts = sizeof(unsigned long long) / sizeof(unsigned long);
  1972. for (unsigned i = 0; i < parts; ++i)
  1973. {
  1974. unsigned long long part = mpz_get_ui(t.data());
  1975. if (i)
  1976. *result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
  1977. else
  1978. *result = part;
  1979. mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
  1980. }
  1981. }
  1982. inline void eval_convert_to(long long* result, const gmp_int& val)
  1983. {
  1984. int s = mpz_sgn(val.data());
  1985. *result = 0;
  1986. gmp_int t(val);
  1987. unsigned parts = sizeof(unsigned long long) / sizeof(unsigned long);
  1988. unsigned long long unsigned_result = 0;
  1989. for (unsigned i = 0; i < parts; ++i)
  1990. {
  1991. unsigned long long part = mpz_get_ui(t.data());
  1992. if (i)
  1993. unsigned_result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
  1994. else
  1995. unsigned_result = part;
  1996. mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
  1997. }
  1998. //
  1999. // Overflow check:
  2000. //
  2001. bool overflow = false;
  2002. if (mpz_sgn(t.data()))
  2003. {
  2004. overflow = true;
  2005. }
  2006. if ((s > 0) && (unsigned_result > static_cast<unsigned long long>((std::numeric_limits<long long>::max)())))
  2007. overflow = true;
  2008. if((s < 0) && (unsigned_result > 1u - static_cast<unsigned long long>((std::numeric_limits<long long>::min)() + 1)))
  2009. overflow = true;
  2010. if(overflow)
  2011. *result = s < 0 ? (std::numeric_limits<long long>::min)() : (std::numeric_limits<long long>::max)();
  2012. else
  2013. *result = s < 0 ? -static_cast<long long>(unsigned_result - 1u) - 1 : static_cast<long long>(unsigned_result);
  2014. }
  2015. #endif
  2016. #ifdef BOOST_HAS_INT128
  2017. inline void eval_convert_to(uint128_type* result, const gmp_int& val)
  2018. {
  2019. if (mpz_sgn(val.data()) < 0)
  2020. {
  2021. BOOST_MP_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
  2022. }
  2023. *result = 0;
  2024. gmp_int t(val);
  2025. unsigned parts = sizeof(uint128_type) / sizeof(unsigned long);
  2026. for (unsigned i = 0; i < parts; ++i)
  2027. {
  2028. uint128_type part = mpz_get_ui(t.data());
  2029. if (i)
  2030. *result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
  2031. else
  2032. *result = part;
  2033. mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
  2034. }
  2035. }
  2036. inline void eval_convert_to(int128_type* result, const gmp_int& val)
  2037. {
  2038. int s = mpz_sgn(val.data());
  2039. *result = 0;
  2040. gmp_int t(val);
  2041. unsigned parts = sizeof(uint128_type) / sizeof(unsigned long);
  2042. uint128_type unsigned_result = 0;
  2043. for (unsigned i = 0; i < parts; ++i)
  2044. {
  2045. uint128_type part = mpz_get_ui(t.data());
  2046. if (i)
  2047. unsigned_result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
  2048. else
  2049. unsigned_result = part;
  2050. mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
  2051. }
  2052. //
  2053. // Overflow check:
  2054. //
  2055. constexpr int128_type int128_max = static_cast<int128_type>((static_cast<uint128_type>(1u) << 127) - 1);
  2056. constexpr int128_type int128_min = static_cast<int128_type>(static_cast<int128_type>(-int128_max) -1);
  2057. bool overflow = false;
  2058. if (mpz_sgn(t.data()))
  2059. {
  2060. overflow = true;
  2061. }
  2062. if ((s > 0) && (unsigned_result > static_cast<uint128_type>(int128_max)))
  2063. overflow = true;
  2064. if ((s < 0) && (unsigned_result > 1u - static_cast<uint128_type>(int128_min + 1)))
  2065. overflow = true;
  2066. if (overflow)
  2067. *result = s < 0 ? int128_min : int128_max;
  2068. else
  2069. *result = s < 0 ? -static_cast<int128_type>(unsigned_result - 1u) - 1 : static_cast<int128_type>(unsigned_result);
  2070. }
  2071. template <unsigned digits10>
  2072. inline void eval_convert_to(int128_type* result, const gmp_float<digits10>& val)
  2073. {
  2074. gmp_int i;
  2075. mpz_set_f(i.data(), val.data());
  2076. eval_convert_to(result, i);
  2077. }
  2078. template <unsigned digits10>
  2079. inline void eval_convert_to(uint128_type* result, const gmp_float<digits10>& val)
  2080. {
  2081. gmp_int i;
  2082. mpz_set_f(i.data(), val.data());
  2083. eval_convert_to(result, i);
  2084. }
  2085. #endif
  2086. #ifdef BOOST_HAS_FLOAT128
  2087. inline void eval_convert_to(float128_type* result, const gmp_int& val)
  2088. {
  2089. *result = float128_procs::strtoflt128(val.str(0, std::ios_base::fixed).c_str(), nullptr);
  2090. }
  2091. #endif
  2092. inline void eval_abs(gmp_int& result, const gmp_int& val)
  2093. {
  2094. mpz_abs(result.data(), val.data());
  2095. }
  2096. inline void eval_gcd(gmp_int& result, const gmp_int& a, const gmp_int& b)
  2097. {
  2098. mpz_gcd(result.data(), a.data(), b.data());
  2099. }
  2100. inline void eval_lcm(gmp_int& result, const gmp_int& a, const gmp_int& b)
  2101. {
  2102. mpz_lcm(result.data(), a.data(), b.data());
  2103. }
  2104. template <class I>
  2105. inline typename std::enable_if<(boost::multiprecision::detail::is_unsigned<I>::value && (sizeof(I) <= sizeof(unsigned long)))>::type eval_gcd(gmp_int& result, const gmp_int& a, const I b)
  2106. {
  2107. mpz_gcd_ui(result.data(), a.data(), b);
  2108. }
  2109. template <class I>
  2110. inline typename std::enable_if<(boost::multiprecision::detail::is_unsigned<I>::value && (sizeof(I) <= sizeof(unsigned long)))>::type eval_lcm(gmp_int& result, const gmp_int& a, const I b)
  2111. {
  2112. mpz_lcm_ui(result.data(), a.data(), b);
  2113. }
  2114. template <class I>
  2115. inline typename std::enable_if<(boost::multiprecision::detail::is_signed<I>::value && boost::multiprecision::detail::is_integral<I>::value && (sizeof(I) <= sizeof(long)))>::type eval_gcd(gmp_int& result, const gmp_int& a, const I b)
  2116. {
  2117. mpz_gcd_ui(result.data(), a.data(), boost::multiprecision::detail::unsigned_abs(b));
  2118. }
  2119. template <class I>
  2120. inline typename std::enable_if<boost::multiprecision::detail::is_signed<I>::value && boost::multiprecision::detail::is_integral<I>::value && ((sizeof(I) <= sizeof(long)))>::type eval_lcm(gmp_int& result, const gmp_int& a, const I b)
  2121. {
  2122. mpz_lcm_ui(result.data(), a.data(), boost::multiprecision::detail::unsigned_abs(b));
  2123. }
  2124. inline void eval_integer_sqrt(gmp_int& s, gmp_int& r, const gmp_int& x)
  2125. {
  2126. mpz_sqrtrem(s.data(), r.data(), x.data());
  2127. }
  2128. inline std::size_t eval_lsb(const gmp_int& val)
  2129. {
  2130. int c = eval_get_sign(val);
  2131. if (c == 0)
  2132. {
  2133. BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
  2134. }
  2135. if (c < 0)
  2136. {
  2137. BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
  2138. }
  2139. return static_cast<unsigned>(mpz_scan1(val.data(), 0));
  2140. }
  2141. inline std::size_t eval_msb(const gmp_int& val)
  2142. {
  2143. int c = eval_get_sign(val);
  2144. if (c == 0)
  2145. {
  2146. BOOST_MP_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
  2147. }
  2148. if (c < 0)
  2149. {
  2150. BOOST_MP_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
  2151. }
  2152. return static_cast<unsigned>(mpz_sizeinbase(val.data(), 2) - 1);
  2153. }
  2154. inline bool eval_bit_test(const gmp_int& val, std::size_t index)
  2155. {
  2156. return mpz_tstbit(val.data(), index) ? true : false;
  2157. }
  2158. inline void eval_bit_set(gmp_int& val, std::size_t index)
  2159. {
  2160. mpz_setbit(val.data(), index);
  2161. }
  2162. inline void eval_bit_unset(gmp_int& val, std::size_t index)
  2163. {
  2164. mpz_clrbit(val.data(), index);
  2165. }
  2166. inline void eval_bit_flip(gmp_int& val, std::size_t index)
  2167. {
  2168. mpz_combit(val.data(), index);
  2169. }
  2170. inline void eval_qr(const gmp_int& x, const gmp_int& y,
  2171. gmp_int& q, gmp_int& r)
  2172. {
  2173. mpz_tdiv_qr(q.data(), r.data(), x.data(), y.data());
  2174. }
  2175. template <class Integer>
  2176. inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<Integer>::value, Integer>::type eval_integer_modulus(const gmp_int& x, Integer val)
  2177. {
  2178. #if defined(__MPIR_VERSION) && (__MPIR_VERSION >= 3)
  2179. if ((sizeof(Integer) <= sizeof(mpir_ui)) || (val <= (std::numeric_limits<mpir_ui>::max)()))
  2180. #else
  2181. if ((sizeof(Integer) <= sizeof(long)) || (val <= (std::numeric_limits<unsigned long>::max)()))
  2182. #endif
  2183. {
  2184. return static_cast<Integer>(mpz_tdiv_ui(x.data(), val));
  2185. }
  2186. else
  2187. {
  2188. return default_ops::eval_integer_modulus(x, val);
  2189. }
  2190. }
  2191. template <class Integer>
  2192. inline typename std::enable_if<boost::multiprecision::detail::is_signed<Integer>::value && boost::multiprecision::detail::is_integral<Integer>::value, Integer>::type eval_integer_modulus(const gmp_int& x, Integer val)
  2193. {
  2194. return eval_integer_modulus(x, boost::multiprecision::detail::unsigned_abs(val));
  2195. }
  2196. inline void eval_powm(gmp_int& result, const gmp_int& base, const gmp_int& p, const gmp_int& m)
  2197. {
  2198. if (eval_get_sign(p) < 0)
  2199. {
  2200. BOOST_MP_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
  2201. }
  2202. mpz_powm(result.data(), base.data(), p.data(), m.data());
  2203. }
  2204. template <class Integer>
  2205. inline typename std::enable_if<
  2206. boost::multiprecision::detail::is_unsigned<Integer>::value && (sizeof(Integer) <= sizeof(unsigned long))>::type
  2207. eval_powm(gmp_int& result, const gmp_int& base, Integer p, const gmp_int& m)
  2208. {
  2209. mpz_powm_ui(result.data(), base.data(), p, m.data());
  2210. }
  2211. template <class Integer>
  2212. inline typename std::enable_if<boost::multiprecision::detail::is_signed<Integer>::value && boost::multiprecision::detail::is_integral<Integer>::value && (sizeof(Integer) <= sizeof(unsigned long))>::type
  2213. eval_powm(gmp_int& result, const gmp_int& base, Integer p, const gmp_int& m)
  2214. {
  2215. if (p < 0)
  2216. {
  2217. BOOST_MP_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
  2218. }
  2219. mpz_powm_ui(result.data(), base.data(), p, m.data());
  2220. }
  2221. inline std::size_t hash_value(const gmp_int& val)
  2222. {
  2223. // We should really use mpz_limbs_read here, but that's unsupported on older versions:
  2224. std::size_t result = 0;
  2225. for (int i = 0; i < std::abs(val.data()[0]._mp_size); ++i)
  2226. boost::multiprecision::detail::hash_combine(result, val.data()[0]._mp_d[i]);
  2227. boost::multiprecision::detail::hash_combine(result, val.data()[0]._mp_size);
  2228. return result;
  2229. }
  2230. struct gmp_rational;
  2231. void eval_add(gmp_rational& t, const gmp_rational& o);
  2232. struct gmp_rational
  2233. {
  2234. #ifdef BOOST_HAS_LONG_LONG
  2235. using signed_types = std::tuple<long, long long> ;
  2236. using unsigned_types = std::tuple<unsigned long, unsigned long long>;
  2237. #else
  2238. using signed_types = std::tuple<long> ;
  2239. using unsigned_types = std::tuple<unsigned long>;
  2240. #endif
  2241. using float_types = std::tuple<double, long double>;
  2242. gmp_rational()
  2243. {
  2244. mpq_init(this->m_data);
  2245. }
  2246. gmp_rational(const gmp_rational& o)
  2247. {
  2248. mpq_init(m_data);
  2249. if (o.m_data[0]._mp_num._mp_d)
  2250. mpq_set(m_data, o.m_data);
  2251. }
  2252. gmp_rational(const gmp_int& o)
  2253. {
  2254. mpz_init_set(&m_data[0]._mp_num, o.data());
  2255. mpz_init_set_ui(&m_data[0]._mp_den, 1u);
  2256. }
  2257. gmp_rational(long i)
  2258. {
  2259. mpz_init_set_si(&m_data[0]._mp_num, i);
  2260. mpz_init_set_ui(&m_data[0]._mp_den, 1u);
  2261. }
  2262. gmp_rational(unsigned long ui)
  2263. {
  2264. mpz_init_set_ui(&m_data[0]._mp_num, ui);
  2265. mpz_init_set_ui(&m_data[0]._mp_den, 1u);
  2266. }
  2267. // 2-arg constructors:
  2268. template <class T, class U>
  2269. gmp_rational(const T& a, const U& b, typename std::enable_if<std::is_constructible<gmp_int, T>::value && std::is_constructible<gmp_int, U>::value>::type* = nullptr)
  2270. {
  2271. gmp_int i(a), j(b);
  2272. if (eval_is_zero(j))
  2273. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2274. m_data[0]._mp_num = i.data()[0];
  2275. m_data[0]._mp_den = j.data()[0];
  2276. mpq_canonicalize(m_data);
  2277. i.data()[0]._mp_d = nullptr;
  2278. j.data()[0]._mp_d = nullptr;
  2279. }
  2280. template <class U>
  2281. gmp_rational(const gmp_int& a, const U& b, typename std::enable_if<std::is_constructible<gmp_int, U>::value>::type* = nullptr)
  2282. {
  2283. gmp_int j(b);
  2284. if (eval_is_zero(j))
  2285. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2286. mpz_init_set(&m_data[0]._mp_num, a.data());
  2287. m_data[0]._mp_den = j.data()[0];
  2288. if (boost::multiprecision::detail::unsigned_abs(b) > 1)
  2289. mpq_canonicalize(m_data);
  2290. j.data()[0]._mp_d = nullptr;
  2291. }
  2292. template <class U>
  2293. gmp_rational(gmp_int&& a, const U& b, typename std::enable_if<std::is_constructible<gmp_int, U>::value>::type* = nullptr)
  2294. {
  2295. gmp_int j(b);
  2296. if (eval_is_zero(j))
  2297. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2298. m_data[0]._mp_num = a.data()[0];
  2299. m_data[0]._mp_den = j.data()[0];
  2300. if (boost::multiprecision::detail::unsigned_abs(b) > 1)
  2301. mpq_canonicalize(m_data);
  2302. a.data()[0]._mp_d = nullptr;
  2303. j.data()[0]._mp_d = nullptr;
  2304. }
  2305. template <class T>
  2306. gmp_rational(const T& a, const gmp_int& b, typename std::enable_if<std::is_constructible<gmp_int, T>::value>::type* = nullptr)
  2307. {
  2308. if (eval_is_zero(b))
  2309. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2310. gmp_int i(a);
  2311. m_data[0]._mp_num = i.data()[0];
  2312. mpz_init_set(&m_data[0]._mp_den, b.data());
  2313. if(boost::multiprecision::detail::unsigned_abs(a) > 1)
  2314. mpq_canonicalize(m_data);
  2315. i.data()[0]._mp_d = nullptr;
  2316. }
  2317. template <class T>
  2318. gmp_rational(const T& a, gmp_int&& b, typename std::enable_if<std::is_constructible<gmp_int, T>::value>::type* = nullptr)
  2319. {
  2320. if (eval_is_zero(static_cast<gmp_int&&>(b)))
  2321. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2322. gmp_int i(a);
  2323. m_data[0]._mp_num = i.data()[0];
  2324. m_data[0]._mp_den = b.data()[0];
  2325. if(boost::multiprecision::detail::unsigned_abs(a) > 1)
  2326. mpq_canonicalize(m_data);
  2327. i.data()[0]._mp_d = nullptr;
  2328. b.data()[0]._mp_d = nullptr;
  2329. }
  2330. gmp_rational(const gmp_int& a, const gmp_int& b)
  2331. {
  2332. if (eval_is_zero(b))
  2333. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2334. mpz_init_set(&m_data[0]._mp_num, a.data());
  2335. mpz_init_set(&m_data[0]._mp_den, b.data());
  2336. mpq_canonicalize(m_data);
  2337. }
  2338. gmp_rational(const gmp_int& a, gmp_int&& b)
  2339. {
  2340. if (eval_is_zero(static_cast<gmp_int&&>(b)))
  2341. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2342. mpz_init_set(&m_data[0]._mp_num, a.data());
  2343. m_data[0]._mp_den = b.data()[0];
  2344. mpq_canonicalize(m_data);
  2345. b.data()[0]._mp_d = nullptr;
  2346. }
  2347. gmp_rational(gmp_int&& a, const gmp_int& b)
  2348. {
  2349. if (eval_is_zero(b))
  2350. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2351. m_data[0]._mp_num = a.data()[0];
  2352. mpz_init_set(&m_data[0]._mp_den, b.data());
  2353. mpq_canonicalize(m_data);
  2354. a.data()[0]._mp_d = nullptr;
  2355. }
  2356. gmp_rational(gmp_int&& a, gmp_int&& b)
  2357. {
  2358. if (eval_is_zero(static_cast<gmp_int&&>(b)))
  2359. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2360. m_data[0]._mp_num = a.data()[0];
  2361. m_data[0]._mp_den = b.data()[0];
  2362. mpq_canonicalize(m_data);
  2363. a.data()[0]._mp_d = nullptr;
  2364. b.data()[0]._mp_d = nullptr;
  2365. }
  2366. // rvalue copy
  2367. gmp_rational(gmp_rational&& o) noexcept
  2368. {
  2369. m_data[0] = o.m_data[0];
  2370. o.m_data[0]._mp_num._mp_d = nullptr;
  2371. o.m_data[0]._mp_den._mp_d = nullptr;
  2372. }
  2373. gmp_rational(const mpq_t o)
  2374. {
  2375. mpq_init(m_data);
  2376. mpq_set(m_data, o);
  2377. }
  2378. gmp_rational(const mpz_t o)
  2379. {
  2380. mpq_init(m_data);
  2381. mpq_set_z(m_data, o);
  2382. }
  2383. gmp_rational& operator=(const gmp_rational& o)
  2384. {
  2385. if (m_data[0]._mp_den._mp_d == nullptr)
  2386. mpq_init(m_data);
  2387. mpq_set(m_data, o.m_data);
  2388. return *this;
  2389. }
  2390. // rvalue assign
  2391. gmp_rational& operator=(gmp_rational&& o) noexcept
  2392. {
  2393. mpq_swap(m_data, o.m_data);
  2394. return *this;
  2395. }
  2396. #ifdef BOOST_HAS_LONG_LONG
  2397. #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
  2398. gmp_rational& operator=(unsigned long long i)
  2399. {
  2400. *this = static_cast<unsigned long>(i);
  2401. return *this;
  2402. }
  2403. #else
  2404. gmp_rational& operator=(unsigned long long i)
  2405. {
  2406. if (m_data[0]._mp_den._mp_d == nullptr)
  2407. mpq_init(m_data);
  2408. gmp_int zi;
  2409. zi = i;
  2410. mpq_set_z(m_data, zi.data());
  2411. return *this;
  2412. }
  2413. gmp_rational& operator=(long long i)
  2414. {
  2415. if (m_data[0]._mp_den._mp_d == nullptr)
  2416. mpq_init(m_data);
  2417. bool neg = i < 0;
  2418. *this = boost::multiprecision::detail::unsigned_abs(i);
  2419. if (neg)
  2420. mpq_neg(m_data, m_data);
  2421. return *this;
  2422. }
  2423. #endif
  2424. #endif
  2425. gmp_rational& operator=(unsigned long i)
  2426. {
  2427. if (m_data[0]._mp_den._mp_d == nullptr)
  2428. mpq_init(m_data);
  2429. mpq_set_ui(m_data, i, 1);
  2430. return *this;
  2431. }
  2432. gmp_rational& operator=(long i)
  2433. {
  2434. if (m_data[0]._mp_den._mp_d == nullptr)
  2435. mpq_init(m_data);
  2436. mpq_set_si(m_data, i, 1);
  2437. return *this;
  2438. }
  2439. gmp_rational& operator=(double d)
  2440. {
  2441. if (m_data[0]._mp_den._mp_d == nullptr)
  2442. mpq_init(m_data);
  2443. mpq_set_d(m_data, d);
  2444. return *this;
  2445. }
  2446. template <class F>
  2447. gmp_rational& assign_float(F a)
  2448. {
  2449. using default_ops::eval_add;
  2450. using default_ops::eval_subtract;
  2451. BOOST_MP_FLOAT128_USING using std::floor; using std::frexp; using std::ldexp;
  2452. if (m_data[0]._mp_den._mp_d == nullptr)
  2453. mpq_init(m_data);
  2454. if (a == 0)
  2455. {
  2456. mpq_set_si(m_data, 0, 1);
  2457. return *this;
  2458. }
  2459. if (a == 1)
  2460. {
  2461. mpq_set_si(m_data, 1, 1);
  2462. return *this;
  2463. }
  2464. BOOST_MP_ASSERT(!BOOST_MP_ISINF(a));
  2465. BOOST_MP_ASSERT(!BOOST_MP_ISNAN(a));
  2466. int e;
  2467. F f, term;
  2468. mpq_set_ui(m_data, 0, 1);
  2469. mpq_set_ui(m_data, 0u, 1);
  2470. gmp_rational t;
  2471. f = frexp(a, &e);
  2472. constexpr int shift = std::numeric_limits<int>::digits - 1;
  2473. while (f != static_cast<F>(0.0f))
  2474. {
  2475. // extract int sized bits from f:
  2476. f = ldexp(f, shift);
  2477. term = floor(f);
  2478. e -= shift;
  2479. mpq_mul_2exp(m_data, m_data, shift);
  2480. t = static_cast<long>(term);
  2481. eval_add(*this, t);
  2482. f -= term;
  2483. }
  2484. if (e > 0)
  2485. mpq_mul_2exp(m_data, m_data, static_cast<mp_bitcnt_t>(e));
  2486. else if (e < 0)
  2487. mpq_div_2exp(m_data, m_data, static_cast<mp_bitcnt_t>(-e));
  2488. return *this;
  2489. }
  2490. gmp_rational& operator=(long double a)
  2491. {
  2492. return assign_float(a);
  2493. }
  2494. #ifdef BOOST_HAS_FLOAT128
  2495. gmp_rational& operator=(float128_type a)
  2496. {
  2497. return assign_float(a);
  2498. }
  2499. #endif
  2500. #ifdef BOOST_HAS_INT128
  2501. gmp_rational& operator=(uint128_type i)
  2502. {
  2503. gmp_int gi;
  2504. gi = i;
  2505. return *this = gi;
  2506. }
  2507. gmp_rational& operator=(int128_type i)
  2508. {
  2509. gmp_int gi;
  2510. gi = i;
  2511. return *this = gi;
  2512. }
  2513. #endif
  2514. gmp_rational& operator=(const char* s)
  2515. {
  2516. if (m_data[0]._mp_den._mp_d == nullptr)
  2517. mpq_init(m_data);
  2518. if (0 != mpq_set_str(m_data, s, 10))
  2519. BOOST_MP_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid rational number.")));
  2520. return *this;
  2521. }
  2522. gmp_rational& operator=(const gmp_int& o)
  2523. {
  2524. if (m_data[0]._mp_den._mp_d == nullptr)
  2525. mpq_init(m_data);
  2526. mpq_set_z(m_data, o.data());
  2527. return *this;
  2528. }
  2529. gmp_rational& operator=(const mpq_t o)
  2530. {
  2531. if (m_data[0]._mp_den._mp_d == nullptr)
  2532. mpq_init(m_data);
  2533. mpq_set(m_data, o);
  2534. return *this;
  2535. }
  2536. gmp_rational& operator=(const mpz_t o)
  2537. {
  2538. if (m_data[0]._mp_den._mp_d == nullptr)
  2539. mpq_init(m_data);
  2540. mpq_set_z(m_data, o);
  2541. return *this;
  2542. }
  2543. void swap(gmp_rational& o)
  2544. {
  2545. mpq_swap(m_data, o.m_data);
  2546. }
  2547. std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags /*f*/) const
  2548. {
  2549. BOOST_MP_ASSERT(m_data[0]._mp_num._mp_d);
  2550. // TODO make a better job of this including handling of f!!
  2551. void* (*alloc_func_ptr)(size_t);
  2552. void* (*realloc_func_ptr)(void*, size_t, size_t);
  2553. void (*free_func_ptr)(void*, size_t);
  2554. const char* ps = mpq_get_str(nullptr, 10, m_data);
  2555. std::string s = ps;
  2556. mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
  2557. (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
  2558. return s;
  2559. }
  2560. ~gmp_rational()
  2561. {
  2562. if (m_data[0]._mp_num._mp_d || m_data[0]._mp_den._mp_d)
  2563. mpq_clear(m_data);
  2564. }
  2565. void negate()
  2566. {
  2567. BOOST_MP_ASSERT(m_data[0]._mp_num._mp_d);
  2568. mpq_neg(m_data, m_data);
  2569. }
  2570. int compare(const gmp_rational& o) const
  2571. {
  2572. BOOST_MP_ASSERT(m_data[0]._mp_num._mp_d && o.m_data[0]._mp_num._mp_d);
  2573. return mpq_cmp(m_data, o.m_data);
  2574. }
  2575. template <class V>
  2576. int compare(V v) const
  2577. {
  2578. gmp_rational d;
  2579. d = v;
  2580. return compare(d);
  2581. }
  2582. int compare(unsigned long v) const
  2583. {
  2584. BOOST_MP_ASSERT(m_data[0]._mp_num._mp_d);
  2585. return mpq_cmp_ui(m_data, v, 1);
  2586. }
  2587. int compare(long v) const
  2588. {
  2589. BOOST_MP_ASSERT(m_data[0]._mp_num._mp_d);
  2590. return mpq_cmp_si(m_data, v, 1);
  2591. }
  2592. mpq_t& data()
  2593. {
  2594. BOOST_MP_ASSERT(m_data[0]._mp_num._mp_d);
  2595. return m_data;
  2596. }
  2597. const mpq_t& data() const
  2598. {
  2599. BOOST_MP_ASSERT(m_data[0]._mp_num._mp_d);
  2600. return m_data;
  2601. }
  2602. protected:
  2603. mpq_t m_data;
  2604. };
  2605. inline bool eval_is_zero(const gmp_rational& val)
  2606. {
  2607. return mpq_sgn(val.data()) == 0;
  2608. }
  2609. template <class T>
  2610. inline bool eval_eq(gmp_rational& a, const T& b)
  2611. {
  2612. return a.compare(b) == 0;
  2613. }
  2614. template <class T>
  2615. inline bool eval_lt(gmp_rational& a, const T& b)
  2616. {
  2617. return a.compare(b) < 0;
  2618. }
  2619. template <class T>
  2620. inline bool eval_gt(gmp_rational& a, const T& b)
  2621. {
  2622. return a.compare(b) > 0;
  2623. }
  2624. inline void eval_add(gmp_rational& t, const gmp_rational& o)
  2625. {
  2626. mpq_add(t.data(), t.data(), o.data());
  2627. }
  2628. inline void eval_subtract(gmp_rational& t, const gmp_rational& o)
  2629. {
  2630. mpq_sub(t.data(), t.data(), o.data());
  2631. }
  2632. inline void eval_multiply(gmp_rational& t, const gmp_rational& o)
  2633. {
  2634. mpq_mul(t.data(), t.data(), o.data());
  2635. }
  2636. inline void eval_divide(gmp_rational& t, const gmp_rational& o)
  2637. {
  2638. if (eval_is_zero(o))
  2639. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2640. mpq_div(t.data(), t.data(), o.data());
  2641. }
  2642. inline void eval_add(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
  2643. {
  2644. mpq_add(t.data(), p.data(), o.data());
  2645. }
  2646. inline void eval_subtract(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
  2647. {
  2648. mpq_sub(t.data(), p.data(), o.data());
  2649. }
  2650. inline void eval_multiply(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
  2651. {
  2652. mpq_mul(t.data(), p.data(), o.data());
  2653. }
  2654. inline void eval_divide(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
  2655. {
  2656. if (eval_is_zero(o))
  2657. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2658. mpq_div(t.data(), p.data(), o.data());
  2659. }
  2660. //
  2661. // operator with scalars:
  2662. //
  2663. inline void eval_add(gmp_rational& result, gmp_rational const& a, gmp_int const& b)
  2664. {
  2665. // we allow result and a to be the same object here:
  2666. if (&a != &result)
  2667. {
  2668. mpz_set(mpq_numref(result.data()), mpq_numref(a.data()));
  2669. mpz_set(mpq_denref(result.data()), mpq_denref(a.data()));
  2670. }
  2671. mpz_addmul(mpq_numref(result.data()), mpq_denref(a.data()), b.data());
  2672. // no need to normalize, there can be no common divisor as long as a is already normalized.
  2673. }
  2674. inline void eval_add(gmp_rational& result, gmp_rational const& a, unsigned long b)
  2675. {
  2676. // we allow result and a to be the same object here:
  2677. if (&a != &result)
  2678. {
  2679. mpz_set(mpq_numref(result.data()), mpq_numref(a.data()));
  2680. mpz_set(mpq_denref(result.data()), mpq_denref(a.data()));
  2681. }
  2682. mpz_addmul_ui(mpq_numref(result.data()), mpq_denref(a.data()), b);
  2683. // no need to normalize, there can be no common divisor as long as a is already normalized.
  2684. }
  2685. inline void eval_add(gmp_rational& result, gmp_rational const& a, long b)
  2686. {
  2687. // we allow result and a to be the same object here:
  2688. if (&a != &result)
  2689. {
  2690. mpz_set(mpq_numref(result.data()), mpq_numref(a.data()));
  2691. mpz_set(mpq_denref(result.data()), mpq_denref(a.data()));
  2692. }
  2693. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  2694. if(b > 0)
  2695. mpz_addmul_ui(mpq_numref(result.data()), mpq_denref(a.data()), static_cast<local_uint_type>(b));
  2696. else
  2697. mpz_submul_ui(mpq_numref(result.data()), mpq_denref(a.data()), static_cast<local_uint_type>(-b));
  2698. // no need to normalize, there can be no common divisor as long as a is already normalized.
  2699. }
  2700. template <class T>
  2701. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_add(gmp_rational& result, gmp_rational const& a, const T& b)
  2702. {
  2703. gmp_int t;
  2704. t = b;
  2705. eval_add(result, a, t);
  2706. }
  2707. template <class T>
  2708. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_add(gmp_rational& result, const T& b, gmp_rational const& a)
  2709. {
  2710. eval_add(result, a, b);
  2711. }
  2712. template <class T>
  2713. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_add(gmp_rational& result, const T& b)
  2714. {
  2715. eval_add(result, result, b);
  2716. }
  2717. inline void eval_subtract(gmp_rational& result, gmp_rational const& a, gmp_int const& b)
  2718. {
  2719. // we allow result and a to be the same object here:
  2720. if (&a != &result)
  2721. {
  2722. mpz_set(mpq_numref(result.data()), mpq_numref(a.data()));
  2723. mpz_set(mpq_denref(result.data()), mpq_denref(a.data()));
  2724. }
  2725. mpz_submul(mpq_numref(result.data()), mpq_denref(a.data()), b.data());
  2726. // no need to normalize, there can be no common divisor as long as a is already normalized.
  2727. }
  2728. inline void eval_subtract(gmp_rational& result, gmp_rational const& a, unsigned long b)
  2729. {
  2730. // we allow result and a to be the same object here:
  2731. if (&a != &result)
  2732. {
  2733. mpz_set(mpq_numref(result.data()), mpq_numref(a.data()));
  2734. mpz_set(mpq_denref(result.data()), mpq_denref(a.data()));
  2735. }
  2736. mpz_submul_ui(mpq_numref(result.data()), mpq_denref(a.data()), b);
  2737. // no need to normalize, there can be no common divisor as long as a is already normalized.
  2738. }
  2739. inline void eval_subtract(gmp_rational& result, gmp_rational const& a, long b)
  2740. {
  2741. // we allow result and a to be the same object here:
  2742. if (&a != &result)
  2743. {
  2744. mpz_set(mpq_numref(result.data()), mpq_numref(a.data()));
  2745. mpz_set(mpq_denref(result.data()), mpq_denref(a.data()));
  2746. }
  2747. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  2748. if(b > 0)
  2749. mpz_submul_ui(mpq_numref(result.data()), mpq_denref(a.data()), static_cast<local_uint_type>(b));
  2750. else
  2751. mpz_addmul_ui(mpq_numref(result.data()), mpq_denref(a.data()), static_cast<local_uint_type>(-b));
  2752. // no need to normalize, there can be no common divisor as long as a is already normalized.
  2753. }
  2754. template <class T>
  2755. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_subtract(gmp_rational& result, gmp_rational const& a, const T& b)
  2756. {
  2757. gmp_int t;
  2758. t = b;
  2759. eval_subtract(result, a, t);
  2760. }
  2761. template <class T>
  2762. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_subtract(gmp_rational& result, const T& b, gmp_rational const& a)
  2763. {
  2764. eval_subtract(result, a, b);
  2765. result.negate();
  2766. }
  2767. template <class T>
  2768. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_subtract(gmp_rational& result, const T& b)
  2769. {
  2770. eval_subtract(result, result, b);
  2771. }
  2772. inline void eval_multiply(gmp_rational& result, gmp_rational const& a, gmp_int const& b)
  2773. {
  2774. gmp_int g, t;
  2775. mpz_gcd(g.data(), mpq_denref(a.data()), b.data());
  2776. if (!mpz_fits_uint_p(g.data()) || (mpz_get_ui(g.data()) != 1))
  2777. {
  2778. // We get here if the gcd is not unity, this is true if the number is
  2779. // too large for an unsigned long, or if we get an unsigned long and check against 1.
  2780. eval_divide(t, b, g);
  2781. mpz_mul(mpq_numref(result.data()), t.data(), mpq_numref(a.data()));
  2782. mpz_divexact(mpq_denref(result.data()), mpq_denref(a.data()), g.data());
  2783. }
  2784. else
  2785. {
  2786. // gcd is 1.
  2787. mpz_mul(mpq_numref(result.data()), mpq_numref(a.data()), b.data());
  2788. if (&result != &a)
  2789. mpz_set(mpq_denref(result.data()), mpq_denref(a.data()));
  2790. }
  2791. }
  2792. inline void eval_multiply(gmp_rational& result, gmp_rational const& a, unsigned long b)
  2793. {
  2794. if (b == 0)
  2795. {
  2796. mpq_set_ui(result.data(), b, 1);
  2797. return;
  2798. }
  2799. if (mpz_sgn(mpq_numref(a.data())) == 0)
  2800. {
  2801. result = a;
  2802. return;
  2803. }
  2804. unsigned long g = static_cast<unsigned long>(mpz_gcd_ui(nullptr, mpq_denref(a.data()), b));
  2805. if (g != 1)
  2806. {
  2807. BOOST_MP_ASSERT(g);
  2808. b /= g;
  2809. mpz_mul_ui(mpq_numref(result.data()), mpq_numref(a.data()), b);
  2810. mpz_divexact_ui(mpq_denref(result.data()), mpq_denref(a.data()), g);
  2811. }
  2812. else
  2813. {
  2814. mpz_mul_ui(mpq_numref(result.data()), mpq_numref(a.data()), b);
  2815. if (&result != &a)
  2816. mpz_set(mpq_denref(result.data()), mpq_denref(a.data()));
  2817. }
  2818. }
  2819. inline void eval_multiply(gmp_rational& result, gmp_rational const& a, long b)
  2820. {
  2821. eval_multiply(result, a, boost::multiprecision::detail::unsigned_abs(b));
  2822. if (b < 0)
  2823. result.negate();
  2824. }
  2825. template <class T>
  2826. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_multiply(gmp_rational& result, gmp_rational const& a, const T& b)
  2827. {
  2828. gmp_int t;
  2829. t = b;
  2830. eval_multiply(result, a, t);
  2831. }
  2832. template <class T>
  2833. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_multiply(gmp_rational& result, const T& b, gmp_rational const& a)
  2834. {
  2835. eval_multiply(result, a, b);
  2836. }
  2837. template <class T>
  2838. inline typename std::enable_if<boost::multiprecision::detail::is_integral<T>::value>::type eval_multiply(gmp_rational& result, const T& b)
  2839. {
  2840. eval_multiply(result, result, b);
  2841. }
  2842. inline int eval_get_sign(const gmp_rational& val)
  2843. {
  2844. return mpq_sgn(val.data());
  2845. }
  2846. template <class R>
  2847. inline typename std::enable_if<number_category<R>::value == number_kind_floating_point>::type eval_convert_to(R* result, const gmp_rational& backend)
  2848. {
  2849. //
  2850. // The generic conversion is as good as anything we can write here:
  2851. //
  2852. // This does not round correctly:
  2853. //
  2854. //*result = mpq_get_d(val.data());
  2855. //
  2856. // This does:
  2857. //
  2858. ::boost::multiprecision::detail::generic_convert_rational_to_float(*result, backend);
  2859. }
  2860. #ifdef BOOST_HAS_FLOAT128
  2861. inline void eval_convert_to(float128_type* result, const gmp_rational& val)
  2862. {
  2863. using default_ops::eval_convert_to;
  2864. gmp_int n, d;
  2865. float128_type fn, fd;
  2866. mpz_set(n.data(), mpq_numref(val.data()));
  2867. mpz_set(d.data(), mpq_denref(val.data()));
  2868. eval_convert_to(&fn, n);
  2869. eval_convert_to(&fd, d);
  2870. *result = fn / fd;
  2871. }
  2872. #endif
  2873. template <class R>
  2874. inline typename std::enable_if<number_category<R>::value == number_kind_integer>::type eval_convert_to(R* result, const gmp_rational& backend)
  2875. {
  2876. gmp_int n(mpq_numref(backend.data()));
  2877. gmp_int d(mpq_denref(backend.data()));
  2878. using default_ops::eval_divide;
  2879. eval_divide(n, d);
  2880. using default_ops::eval_convert_to;
  2881. eval_convert_to(result, n);
  2882. }
  2883. inline void eval_abs(gmp_rational& result, const gmp_rational& val)
  2884. {
  2885. mpq_abs(result.data(), val.data());
  2886. }
  2887. inline void assign_components(gmp_rational& result, unsigned long v1, unsigned long v2)
  2888. {
  2889. if (v2 == 0u)
  2890. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2891. mpq_set_ui(result.data(), v1, v2);
  2892. mpq_canonicalize(result.data());
  2893. }
  2894. inline void assign_components(gmp_rational& result, long v1, long v2)
  2895. {
  2896. if (v2 == 0)
  2897. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2898. using local_uint_type = typename boost::multiprecision::detail::make_unsigned<long>::type;
  2899. if (v2 < 0)
  2900. mpq_set_si(result.data(), -v1, static_cast<local_uint_type>(-v2));
  2901. else
  2902. mpq_set_si(result.data(), v1, static_cast<local_uint_type>(v2));
  2903. mpq_canonicalize(result.data());
  2904. }
  2905. inline void assign_components(gmp_rational& result, gmp_int const& v1, gmp_int const& v2)
  2906. {
  2907. if (eval_is_zero(v2))
  2908. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2909. mpz_set(mpq_numref(result.data()), v1.data());
  2910. mpz_set(mpq_denref(result.data()), v2.data());
  2911. mpq_canonicalize(result.data());
  2912. }
  2913. template <class T, class U>
  2914. void assign_components(gmp_rational& result, const T& a, const U& b)
  2915. {
  2916. gmp_int x, y;
  2917. x = a;
  2918. y = b;
  2919. if (eval_is_zero(y))
  2920. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2921. std::swap(result.data()[0]._mp_num, x.data()[0]);
  2922. std::swap(result.data()[0]._mp_den, y.data()[0]);
  2923. mpq_canonicalize(result.data());
  2924. }
  2925. template <class U>
  2926. void assign_components(gmp_rational& result, const gmp_int& a, const U& b)
  2927. {
  2928. gmp_int y;
  2929. y = b;
  2930. if (eval_is_zero(y))
  2931. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2932. mpz_set(&result.data()[0]._mp_num, a.data());
  2933. std::swap(result.data()[0]._mp_den, y.data()[0]);
  2934. mpq_canonicalize(result.data());
  2935. }
  2936. template <class T>
  2937. void assign_components(gmp_rational& result, const T& a, const gmp_int& b)
  2938. {
  2939. if (eval_is_zero(b))
  2940. BOOST_MP_THROW_EXCEPTION(std::overflow_error("Division by zero."));
  2941. gmp_int x;
  2942. x = a;
  2943. std::swap(result.data()[0]._mp_num, x.data()[0]);
  2944. mpz_set(&result.data()[0]._mp_den, b.data());
  2945. mpq_canonicalize(result.data());
  2946. }
  2947. inline std::size_t hash_value(const gmp_rational& val)
  2948. {
  2949. std::size_t result = 0;
  2950. for (int i = 0; i < std::abs(val.data()[0]._mp_num._mp_size); ++i)
  2951. boost::multiprecision::detail::hash_combine(result, val.data()[0]._mp_num._mp_d[i]);
  2952. for (int i = 0; i < std::abs(val.data()[0]._mp_den._mp_size); ++i)
  2953. boost::multiprecision::detail::hash_combine(result, val.data()[0]._mp_den._mp_d[i]);
  2954. boost::multiprecision::detail::hash_combine(result, val.data()[0]._mp_num._mp_size);
  2955. return result;
  2956. }
  2957. //
  2958. // Some useful helpers:
  2959. //
  2960. inline std::size_t used_gmp_int_bits(const gmp_int& val)
  2961. {
  2962. return eval_msb(val) - eval_lsb(val) + 1;
  2963. }
  2964. inline std::size_t used_gmp_rational_bits(const gmp_rational& val)
  2965. {
  2966. unsigned d2_d = static_cast<unsigned>(mpz_sizeinbase(mpq_denref(val.data()), 2) - mpz_scan1(mpq_denref(val.data()), 0));
  2967. unsigned d2_n = static_cast<unsigned>(mpz_sizeinbase(mpq_numref(val.data()), 2) - mpz_scan1(mpq_numref(val.data()), 0));
  2968. return (std::max)(d2_d, d2_n);
  2969. }
  2970. //
  2971. // Some member functions that are dependent upon previous code go here:
  2972. //
  2973. template <unsigned Digits10>
  2974. template <unsigned D>
  2975. inline gmp_float<Digits10>::gmp_float(const gmp_float<D>& o, typename std::enable_if<D <= Digits10>::type*)
  2976. {
  2977. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2978. mpf_set(this->m_data, o.data());
  2979. }
  2980. template <unsigned Digits10>
  2981. template <unsigned D>
  2982. inline gmp_float<Digits10>::gmp_float(const gmp_float<D>& o, typename std::enable_if< !(D <= Digits10)>::type*)
  2983. {
  2984. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2985. mpf_set(this->m_data, o.data());
  2986. }
  2987. template <unsigned Digits10>
  2988. inline gmp_float<Digits10>::gmp_float(const gmp_int& o)
  2989. {
  2990. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2991. mpf_set_z(this->data(), o.data());
  2992. }
  2993. template <unsigned Digits10>
  2994. inline gmp_float<Digits10>::gmp_float(const gmp_rational& o)
  2995. {
  2996. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  2997. mpf_set_q(this->data(), o.data());
  2998. }
  2999. template <unsigned Digits10>
  3000. template <unsigned D>
  3001. inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_float<D>& o)
  3002. {
  3003. if (this->m_data[0]._mp_d == nullptr)
  3004. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  3005. mpf_set(this->m_data, o.data());
  3006. return *this;
  3007. }
  3008. template <unsigned Digits10>
  3009. inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_int& o)
  3010. {
  3011. if (this->m_data[0]._mp_d == nullptr)
  3012. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  3013. mpf_set_z(this->data(), o.data());
  3014. return *this;
  3015. }
  3016. template <unsigned Digits10>
  3017. inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_rational& o)
  3018. {
  3019. if (this->m_data[0]._mp_d == nullptr)
  3020. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
  3021. mpf_set_q(this->data(), o.data());
  3022. return *this;
  3023. }
  3024. inline gmp_float<0>::gmp_float(const gmp_int& o) : requested_precision(get_default_precision())
  3025. {
  3026. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  3027. {
  3028. std::size_t d2 = used_gmp_int_bits(o);
  3029. std::size_t d10 = 1 + multiprecision::detail::digits2_2_10(d2);
  3030. if (d10 > requested_precision)
  3031. requested_precision = static_cast<unsigned>(d10);
  3032. }
  3033. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  3034. mpf_set_z(this->data(), o.data());
  3035. }
  3036. inline gmp_float<0>::gmp_float(const gmp_rational& o) : requested_precision(get_default_precision())
  3037. {
  3038. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  3039. {
  3040. std::size_t d10 = 1 + multiprecision::detail::digits2_2_10(used_gmp_rational_bits(o));
  3041. if (d10 > requested_precision)
  3042. requested_precision = static_cast<unsigned>(d10);
  3043. }
  3044. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  3045. mpf_set_q(this->data(), o.data());
  3046. }
  3047. inline gmp_float<0>& gmp_float<0>::operator=(const gmp_int& o)
  3048. {
  3049. if (this->m_data[0]._mp_d == nullptr)
  3050. {
  3051. requested_precision = this->get_default_precision();
  3052. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  3053. {
  3054. std::size_t d2 = used_gmp_int_bits(o);
  3055. std::size_t d10 = 1 + multiprecision::detail::digits2_2_10(d2);
  3056. if (d10 > requested_precision)
  3057. requested_precision = static_cast<unsigned>(d10);
  3058. }
  3059. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  3060. }
  3061. else if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  3062. {
  3063. std::size_t d2 = used_gmp_int_bits(o);
  3064. std::size_t d10 = 1 + multiprecision::detail::digits2_2_10(d2);
  3065. if (d10 > requested_precision)
  3066. this->precision(static_cast<unsigned>(d10));
  3067. }
  3068. mpf_set_z(this->data(), o.data());
  3069. return *this;
  3070. }
  3071. inline gmp_float<0>& gmp_float<0>::operator=(const gmp_rational& o)
  3072. {
  3073. if (this->m_data[0]._mp_d == nullptr)
  3074. {
  3075. requested_precision = this->get_default_precision();
  3076. if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  3077. {
  3078. std::size_t d10 = 1 + multiprecision::detail::digits2_2_10(used_gmp_rational_bits(o));
  3079. if (d10 > requested_precision)
  3080. requested_precision = static_cast<unsigned>(d10);
  3081. }
  3082. mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
  3083. }
  3084. else if (thread_default_variable_precision_options() >= variable_precision_options::preserve_all_precision)
  3085. {
  3086. std::size_t d10 = 1 + multiprecision::detail::digits2_2_10(used_gmp_rational_bits(o));
  3087. if (d10 > requested_precision)
  3088. this->precision(static_cast<unsigned>(d10));
  3089. }
  3090. mpf_set_q(this->data(), o.data());
  3091. return *this;
  3092. }
  3093. inline gmp_int::gmp_int(const gmp_rational& o)
  3094. {
  3095. mpz_init(this->m_data);
  3096. mpz_set_q(this->m_data, o.data());
  3097. }
  3098. inline gmp_int& gmp_int::operator=(const gmp_rational& o)
  3099. {
  3100. if (this->m_data[0]._mp_d == nullptr)
  3101. mpz_init(this->m_data);
  3102. mpz_set_q(this->m_data, o.data());
  3103. return *this;
  3104. }
  3105. } //namespace backends
  3106. template <expression_template_option ExpressionTemplates>
  3107. struct component_type<number<gmp_rational, ExpressionTemplates> >
  3108. {
  3109. using type = number<gmp_int, ExpressionTemplates>;
  3110. };
  3111. template <expression_template_option ET>
  3112. inline number<gmp_int, ET> numerator(const number<gmp_rational, ET>& val)
  3113. {
  3114. number<gmp_int, ET> result;
  3115. mpz_set(result.backend().data(), (mpq_numref(val.backend().data())));
  3116. return result;
  3117. }
  3118. template <expression_template_option ET>
  3119. inline number<gmp_int, ET> denominator(const number<gmp_rational, ET>& val)
  3120. {
  3121. number<gmp_int, ET> result;
  3122. mpz_set(result.backend().data(), (mpq_denref(val.backend().data())));
  3123. return result;
  3124. }
  3125. namespace detail {
  3126. template <>
  3127. struct digits2<number<gmp_float<0>, et_on> >
  3128. {
  3129. static long value()
  3130. {
  3131. return static_cast<long>(multiprecision::detail::digits10_2_2(gmp_float<0>::thread_default_precision()));
  3132. }
  3133. };
  3134. template <>
  3135. struct digits2<number<gmp_float<0>, et_off> >
  3136. {
  3137. static long value()
  3138. {
  3139. return static_cast<long>(multiprecision::detail::digits10_2_2(gmp_float<0>::thread_default_precision()));
  3140. }
  3141. };
  3142. template <>
  3143. struct digits2<number<debug_adaptor<gmp_float<0> >, et_on> >
  3144. {
  3145. static long value()
  3146. {
  3147. return static_cast<long>(multiprecision::detail::digits10_2_2(gmp_float<0>::thread_default_precision()));
  3148. }
  3149. };
  3150. template <>
  3151. struct digits2<number<debug_adaptor<gmp_float<0> >, et_off> >
  3152. {
  3153. static long value()
  3154. {
  3155. return static_cast<long>(multiprecision::detail::digits10_2_2(gmp_float<0>::thread_default_precision()));
  3156. }
  3157. };
  3158. template <unsigned Digits10>
  3159. struct transcendental_reduction_type<boost::multiprecision::backends::gmp_float<Digits10> >
  3160. {
  3161. //
  3162. // The type used for trigonometric reduction needs 3 times the precision of the base type.
  3163. // This is double the precision of the original type, plus the largest exponent supported.
  3164. // As a practical measure the largest argument supported is 1/eps, as supporting larger
  3165. // arguments requires the division of argument by PI/2 to also be done at higher precision,
  3166. // otherwise the result (an integer) can not be represented exactly.
  3167. //
  3168. // See ARGUMENT REDUCTION FOR HUGE ARGUMENTS. K C Ng.
  3169. //
  3170. using type = boost::multiprecision::backends::gmp_float<Digits10 * 3>;
  3171. };
  3172. } // namespace detail
  3173. template <>
  3174. struct number_category<detail::canonical<mpz_t, gmp_int>::type> : public std::integral_constant<int, number_kind_integer>
  3175. {};
  3176. template <>
  3177. struct number_category<detail::canonical<mpq_t, gmp_rational>::type> : public std::integral_constant<int, number_kind_rational>
  3178. {};
  3179. template <>
  3180. struct number_category<detail::canonical<mpf_t, gmp_float<0> >::type> : public std::integral_constant<int, number_kind_floating_point>
  3181. {};
  3182. namespace detail {
  3183. template <>
  3184. struct is_variable_precision<backends::gmp_float<0> > : public std::integral_constant<bool, true>
  3185. {};
  3186. } // namespace detail
  3187. } // namespace multiprecision
  3188. namespace math { namespace tools {
  3189. #ifndef BOOST_MP_MATH_AVAILABLE
  3190. template <typename T>
  3191. inline int digits();
  3192. template <typename T>
  3193. inline T max_value();
  3194. template <typename T>
  3195. inline T min_value();
  3196. #endif // BOOST_MP_MATH_AVAILABLE
  3197. inline void set_output_precision(const boost::multiprecision::mpf_float& val, std::ostream& os)
  3198. {
  3199. const int sz_prec = static_cast<int>(val.precision());
  3200. os << std::setprecision(sz_prec);
  3201. }
  3202. template <>
  3203. inline int digits<boost::multiprecision::mpf_float>()
  3204. #ifdef BOOST_MATH_NOEXCEPT
  3205. noexcept
  3206. #endif
  3207. {
  3208. return static_cast<int>(multiprecision::detail::digits10_2_2(boost::multiprecision::mpf_float::thread_default_precision()));
  3209. }
  3210. template <>
  3211. inline int digits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
  3212. #ifdef BOOST_MATH_NOEXCEPT
  3213. noexcept
  3214. #endif
  3215. {
  3216. return static_cast<int>(multiprecision::detail::digits10_2_2(boost::multiprecision::mpf_float::thread_default_precision()));
  3217. }
  3218. template <>
  3219. inline boost::multiprecision::mpf_float
  3220. max_value<boost::multiprecision::mpf_float>()
  3221. {
  3222. boost::multiprecision::mpf_float result(0.5);
  3223. mpf_mul_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  3224. return result;
  3225. }
  3226. template <>
  3227. inline boost::multiprecision::mpf_float
  3228. min_value<boost::multiprecision::mpf_float>()
  3229. {
  3230. boost::multiprecision::mpf_float result(0.5);
  3231. mpf_div_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  3232. return result;
  3233. }
  3234. template <>
  3235. inline boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off>
  3236. max_value<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
  3237. {
  3238. boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> result(0.5);
  3239. mpf_mul_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  3240. return result;
  3241. }
  3242. template <>
  3243. inline boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off>
  3244. min_value<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
  3245. {
  3246. boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> result(0.5);
  3247. mpf_div_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  3248. return result;
  3249. }
  3250. template <>
  3251. inline int digits<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
  3252. #ifdef BOOST_MATH_NOEXCEPT
  3253. noexcept
  3254. #endif
  3255. {
  3256. return static_cast<int>(multiprecision::detail::digits10_2_2(boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >::thread_default_precision()));
  3257. }
  3258. template <>
  3259. inline int digits<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
  3260. #ifdef BOOST_MATH_NOEXCEPT
  3261. noexcept
  3262. #endif
  3263. {
  3264. return static_cast<int>(multiprecision::detail::digits10_2_2(boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >::thread_default_precision()));
  3265. }
  3266. template <>
  3267. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >
  3268. max_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
  3269. {
  3270. return max_value<boost::multiprecision::mpf_float>().backend();
  3271. }
  3272. template <>
  3273. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >
  3274. min_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
  3275. {
  3276. return min_value<boost::multiprecision::mpf_float>().backend();
  3277. }
  3278. template <>
  3279. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off>
  3280. max_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
  3281. {
  3282. return max_value<boost::multiprecision::mpf_float>().backend();
  3283. }
  3284. template <>
  3285. inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off>
  3286. min_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
  3287. {
  3288. return min_value<boost::multiprecision::mpf_float>().backend();
  3289. }
  3290. }} // namespace math::tools
  3291. } // namespace boost
  3292. namespace std {
  3293. //
  3294. // numeric_limits [partial] specializations for the types declared in this header:
  3295. //
  3296. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3297. class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >
  3298. {
  3299. using number_type = boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates>;
  3300. //
  3301. // min and max values chosen so as to not cause segfaults when calling
  3302. // mpf_get_str on 64-bit Linux builds. Possibly we could use larger
  3303. // exponent values elsewhere.
  3304. //
  3305. static number_type calc_min()
  3306. {
  3307. number_type result(1);
  3308. mpf_div_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  3309. return result;
  3310. }
  3311. static number_type calc_max()
  3312. {
  3313. number_type result(1);
  3314. mpf_mul_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
  3315. return result;
  3316. }
  3317. static number_type calc_epsilon()
  3318. {
  3319. number_type result(1);
  3320. mpf_div_2exp(result.backend().data(), result.backend().data(), std::numeric_limits<number_type>::digits - 1);
  3321. return result;
  3322. }
  3323. public:
  3324. static constexpr bool is_specialized = true;
  3325. static number_type(min)()
  3326. {
  3327. // rely on C++11 thread safe initialization of statics:
  3328. static const number_type value{calc_min()};
  3329. return value;
  3330. }
  3331. static number_type(max)()
  3332. {
  3333. static number_type value{calc_max()};
  3334. return value;
  3335. }
  3336. static constexpr number_type lowest()
  3337. {
  3338. return -(max)();
  3339. }
  3340. static constexpr int digits = static_cast<int>((Digits10 * 1000L) / 301L + ((Digits10 * 1000L) % 301L ? 2 : 1));
  3341. static constexpr int digits10 = Digits10;
  3342. // Have to allow for a possible extra limb inside the gmp data structure:
  3343. static constexpr int max_digits10 = Digits10 + 3 + ((GMP_LIMB_BITS * 301L) / 1000L);
  3344. static constexpr bool is_signed = true;
  3345. static constexpr bool is_integer = false;
  3346. static constexpr bool is_exact = false;
  3347. static constexpr int radix = 2;
  3348. static number_type epsilon()
  3349. {
  3350. static const number_type value{calc_epsilon()};
  3351. return value;
  3352. }
  3353. // What value should this be????
  3354. static number_type round_error()
  3355. {
  3356. return 1;
  3357. }
  3358. static constexpr long min_exponent = LONG_MIN;
  3359. static constexpr long min_exponent10 = (LONG_MIN / 1000) * 301L;
  3360. static constexpr long max_exponent = LONG_MAX;
  3361. static constexpr long max_exponent10 = (LONG_MAX / 1000) * 301L;
  3362. static constexpr bool has_infinity = false;
  3363. static constexpr bool has_quiet_NaN = false;
  3364. static constexpr bool has_signaling_NaN = false;
  3365. #ifdef _MSC_VER
  3366. #pragma warning(push)
  3367. #pragma warning(disable : 4996)
  3368. #endif
  3369. static constexpr float_denorm_style has_denorm = denorm_absent;
  3370. #ifdef _MSC_VER
  3371. #pragma warning(pop)
  3372. #endif
  3373. static constexpr bool has_denorm_loss = false;
  3374. static constexpr number_type infinity() { return number_type(); }
  3375. static constexpr number_type quiet_NaN() { return number_type(); }
  3376. static constexpr number_type signaling_NaN() { return number_type(); }
  3377. static constexpr number_type denorm_min() { return (min)(); }
  3378. static constexpr bool is_iec559 = false;
  3379. static constexpr bool is_bounded = true;
  3380. static constexpr bool is_modulo = false;
  3381. static constexpr bool traps = true;
  3382. static constexpr bool tinyness_before = false;
  3383. static constexpr float_round_style round_style = round_indeterminate;
  3384. };
  3385. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3386. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::digits;
  3387. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3388. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::digits10;
  3389. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3390. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_digits10;
  3391. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3392. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_signed;
  3393. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3394. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_integer;
  3395. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3396. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_exact;
  3397. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3398. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::radix;
  3399. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3400. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::min_exponent;
  3401. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3402. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::min_exponent10;
  3403. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3404. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_exponent;
  3405. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3406. constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_exponent10;
  3407. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3408. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_infinity;
  3409. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3410. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_quiet_NaN;
  3411. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3412. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_signaling_NaN;
  3413. #ifdef _MSC_VER
  3414. #pragma warning(push)
  3415. #pragma warning(disable : 4996)
  3416. #endif
  3417. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3418. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_denorm;
  3419. #ifdef _MSC_VER
  3420. #pragma warning(pop)
  3421. #endif
  3422. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3423. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_denorm_loss;
  3424. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3425. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_iec559;
  3426. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3427. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_bounded;
  3428. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3429. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_modulo;
  3430. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3431. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::traps;
  3432. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3433. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::tinyness_before;
  3434. template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
  3435. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::round_style;
  3436. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3437. class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >
  3438. {
  3439. using number_type = boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates>;
  3440. public:
  3441. static constexpr bool is_specialized = false;
  3442. static number_type(min)() { return number_type(); }
  3443. static number_type(max)() { return number_type(); }
  3444. static number_type lowest() { return number_type(); }
  3445. static constexpr int digits = 0;
  3446. static constexpr int digits10 = 0;
  3447. static constexpr int max_digits10 = 0;
  3448. static constexpr bool is_signed = false;
  3449. static constexpr bool is_integer = false;
  3450. static constexpr bool is_exact = false;
  3451. static constexpr int radix = 0;
  3452. static number_type epsilon() { return number_type(); }
  3453. static number_type round_error() { return number_type(); }
  3454. static constexpr int min_exponent = 0;
  3455. static constexpr int min_exponent10 = 0;
  3456. static constexpr int max_exponent = 0;
  3457. static constexpr int max_exponent10 = 0;
  3458. static constexpr bool has_infinity = false;
  3459. static constexpr bool has_quiet_NaN = false;
  3460. static constexpr bool has_signaling_NaN = false;
  3461. #ifdef _MSC_VER
  3462. #pragma warning(push)
  3463. #pragma warning(disable : 4996)
  3464. #endif
  3465. static constexpr float_denorm_style has_denorm = denorm_absent;
  3466. #ifdef _MSC_VER
  3467. #pragma warning(pop)
  3468. #endif
  3469. static constexpr bool has_denorm_loss = false;
  3470. static number_type infinity() { return number_type(); }
  3471. static number_type quiet_NaN() { return number_type(); }
  3472. static number_type signaling_NaN() { return number_type(); }
  3473. static number_type denorm_min() { return number_type(); }
  3474. static constexpr bool is_iec559 = false;
  3475. static constexpr bool is_bounded = false;
  3476. static constexpr bool is_modulo = false;
  3477. static constexpr bool traps = false;
  3478. static constexpr bool tinyness_before = false;
  3479. static constexpr float_round_style round_style = round_indeterminate;
  3480. };
  3481. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3482. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::digits;
  3483. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3484. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::digits10;
  3485. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3486. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_digits10;
  3487. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3488. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_signed;
  3489. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3490. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_integer;
  3491. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3492. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_exact;
  3493. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3494. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::radix;
  3495. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3496. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::min_exponent;
  3497. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3498. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::min_exponent10;
  3499. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3500. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_exponent;
  3501. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3502. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_exponent10;
  3503. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3504. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_infinity;
  3505. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3506. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_quiet_NaN;
  3507. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3508. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_signaling_NaN;
  3509. #ifdef _MSC_VER
  3510. #pragma warning(push)
  3511. #pragma warning(disable : 4996)
  3512. #endif
  3513. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3514. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_denorm;
  3515. #ifdef _MSC_VER
  3516. #pragma warning(pop)
  3517. #endif
  3518. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3519. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_denorm_loss;
  3520. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3521. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_iec559;
  3522. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3523. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_bounded;
  3524. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3525. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_modulo;
  3526. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3527. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::traps;
  3528. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3529. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::tinyness_before;
  3530. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3531. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::round_style;
  3532. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3533. class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >
  3534. {
  3535. using number_type = boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates>;
  3536. public:
  3537. static constexpr bool is_specialized = true;
  3538. //
  3539. // Largest and smallest numbers are bounded only by available memory, set
  3540. // to zero:
  3541. //
  3542. static number_type(min)()
  3543. {
  3544. return number_type();
  3545. }
  3546. static number_type(max)()
  3547. {
  3548. return number_type();
  3549. }
  3550. static number_type lowest() { return (min)(); }
  3551. static constexpr int digits = INT_MAX;
  3552. static constexpr int digits10 = (INT_MAX / 1000) * 301L;
  3553. static constexpr int max_digits10 = digits10 + 3;
  3554. static constexpr bool is_signed = true;
  3555. static constexpr bool is_integer = true;
  3556. static constexpr bool is_exact = true;
  3557. static constexpr int radix = 2;
  3558. static number_type epsilon() { return number_type(); }
  3559. static number_type round_error() { return number_type(); }
  3560. static constexpr int min_exponent = 0;
  3561. static constexpr int min_exponent10 = 0;
  3562. static constexpr int max_exponent = 0;
  3563. static constexpr int max_exponent10 = 0;
  3564. static constexpr bool has_infinity = false;
  3565. static constexpr bool has_quiet_NaN = false;
  3566. static constexpr bool has_signaling_NaN = false;
  3567. #ifdef _MSC_VER
  3568. #pragma warning(push)
  3569. #pragma warning(disable : 4996)
  3570. #endif
  3571. static constexpr float_denorm_style has_denorm = denorm_absent;
  3572. #ifdef _MSC_VER
  3573. #pragma warning(pop)
  3574. #endif
  3575. static constexpr bool has_denorm_loss = false;
  3576. static number_type infinity() { return number_type(); }
  3577. static number_type quiet_NaN() { return number_type(); }
  3578. static number_type signaling_NaN() { return number_type(); }
  3579. static number_type denorm_min() { return number_type(); }
  3580. static constexpr bool is_iec559 = false;
  3581. static constexpr bool is_bounded = false;
  3582. static constexpr bool is_modulo = false;
  3583. static constexpr bool traps = false;
  3584. static constexpr bool tinyness_before = false;
  3585. static constexpr float_round_style round_style = round_toward_zero;
  3586. };
  3587. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3588. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::digits;
  3589. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3590. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::digits10;
  3591. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3592. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_digits10;
  3593. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3594. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_signed;
  3595. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3596. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_integer;
  3597. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3598. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_exact;
  3599. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3600. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::radix;
  3601. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3602. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::min_exponent;
  3603. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3604. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::min_exponent10;
  3605. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3606. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_exponent;
  3607. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3608. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_exponent10;
  3609. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3610. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_infinity;
  3611. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3612. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_quiet_NaN;
  3613. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3614. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_signaling_NaN;
  3615. #ifdef _MSC_VER
  3616. #pragma warning(push)
  3617. #pragma warning(disable : 4996)
  3618. #endif
  3619. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3620. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_denorm;
  3621. #ifdef _MSC_VER
  3622. #pragma warning(pop)
  3623. #endif
  3624. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3625. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_denorm_loss;
  3626. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3627. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_iec559;
  3628. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3629. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_bounded;
  3630. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3631. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_modulo;
  3632. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3633. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::traps;
  3634. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3635. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::tinyness_before;
  3636. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3637. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::round_style;
  3638. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3639. class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >
  3640. {
  3641. using number_type = boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates>;
  3642. public:
  3643. static constexpr bool is_specialized = true;
  3644. //
  3645. // Largest and smallest numbers are bounded only by available memory, set
  3646. // to zero:
  3647. //
  3648. static number_type(min)()
  3649. {
  3650. return number_type();
  3651. }
  3652. static number_type(max)()
  3653. {
  3654. return number_type();
  3655. }
  3656. static number_type lowest() { return (min)(); }
  3657. // Digits are unbounded, use zero for now:
  3658. static constexpr int digits = INT_MAX;
  3659. static constexpr int digits10 = (INT_MAX / 1000) * 301L;
  3660. static constexpr int max_digits10 = digits10 + 3;
  3661. static constexpr bool is_signed = true;
  3662. static constexpr bool is_integer = false;
  3663. static constexpr bool is_exact = true;
  3664. static constexpr int radix = 2;
  3665. static number_type epsilon() { return number_type(); }
  3666. static number_type round_error() { return number_type(); }
  3667. static constexpr int min_exponent = 0;
  3668. static constexpr int min_exponent10 = 0;
  3669. static constexpr int max_exponent = 0;
  3670. static constexpr int max_exponent10 = 0;
  3671. static constexpr bool has_infinity = false;
  3672. static constexpr bool has_quiet_NaN = false;
  3673. static constexpr bool has_signaling_NaN = false;
  3674. #ifdef _MSC_VER
  3675. #pragma warning(push)
  3676. #pragma warning(disable : 4996)
  3677. #endif
  3678. static constexpr float_denorm_style has_denorm = denorm_absent;
  3679. #ifdef _MSC_VER
  3680. #pragma warning(pop)
  3681. #endif
  3682. static constexpr bool has_denorm_loss = false;
  3683. static number_type infinity() { return number_type(); }
  3684. static number_type quiet_NaN() { return number_type(); }
  3685. static number_type signaling_NaN() { return number_type(); }
  3686. static number_type denorm_min() { return (min)(); }
  3687. static constexpr bool is_iec559 = false;
  3688. static constexpr bool is_bounded = false;
  3689. static constexpr bool is_modulo = false;
  3690. static constexpr bool traps = false;
  3691. static constexpr bool tinyness_before = false;
  3692. static constexpr float_round_style round_style = round_toward_zero;
  3693. };
  3694. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3695. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::digits;
  3696. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3697. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::digits10;
  3698. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3699. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_digits10;
  3700. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3701. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_signed;
  3702. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3703. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_integer;
  3704. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3705. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_exact;
  3706. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3707. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::radix;
  3708. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3709. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::min_exponent;
  3710. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3711. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::min_exponent10;
  3712. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3713. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_exponent;
  3714. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3715. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_exponent10;
  3716. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3717. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_infinity;
  3718. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3719. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_quiet_NaN;
  3720. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3721. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_signaling_NaN;
  3722. #ifdef _MSC_VER
  3723. #pragma warning(push)
  3724. #pragma warning(disable : 4996)
  3725. #endif
  3726. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3727. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_denorm;
  3728. #ifdef _MSC_VER
  3729. #pragma warning(pop)
  3730. #endif
  3731. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3732. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_denorm_loss;
  3733. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3734. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_iec559;
  3735. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3736. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_bounded;
  3737. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3738. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_modulo;
  3739. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3740. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::traps;
  3741. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3742. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::tinyness_before;
  3743. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3744. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::round_style;
  3745. #ifdef BOOST_MSVC
  3746. #pragma warning(pop)
  3747. #endif
  3748. } // namespace std
  3749. namespace Eigen
  3750. {
  3751. template <class B1, class B2>
  3752. struct NumTraitsImp;
  3753. template <boost::multiprecision::expression_template_option ExpressionTemplates>
  3754. struct NumTraitsImp<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates>, boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >
  3755. {
  3756. using self_type = boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates>;
  3757. using Real = typename boost::multiprecision::scalar_result_from_possible_complex<self_type>::type;
  3758. using NonInteger = self_type; // Not correct but we can't do much better??
  3759. using Literal = double;
  3760. using Nested = self_type;
  3761. enum
  3762. {
  3763. IsComplex = boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_complex,
  3764. IsInteger = boost::multiprecision::number_category<self_type>::value == boost::multiprecision::number_kind_integer,
  3765. ReadCost = 1,
  3766. AddCost = 4,
  3767. MulCost = 8,
  3768. IsSigned = std::numeric_limits<self_type>::is_specialized ? std::numeric_limits<self_type>::is_signed : true,
  3769. RequireInitialization = 1,
  3770. };
  3771. static Real highest() noexcept
  3772. {
  3773. return boost::math::tools::max_value<Real>();
  3774. }
  3775. static Real lowest() noexcept
  3776. {
  3777. return boost::math::tools::min_value<Real>();
  3778. }
  3779. static int digits() noexcept
  3780. {
  3781. return boost::math::tools::digits<Real>();
  3782. }
  3783. static int digits10()
  3784. {
  3785. return Real::thread_default_precision();
  3786. }
  3787. static Real epsilon()
  3788. {
  3789. return ldexp(Real(1), 1 - digits());
  3790. }
  3791. static Real dummy_precision()
  3792. {
  3793. return 1000 * epsilon();
  3794. }
  3795. static constexpr long min_exponent() noexcept
  3796. {
  3797. return LONG_MIN;
  3798. }
  3799. static constexpr long max_exponent() noexcept
  3800. {
  3801. return LONG_MAX;
  3802. }
  3803. static Real infinity()
  3804. {
  3805. return Real();
  3806. }
  3807. static Real quiet_NaN()
  3808. {
  3809. return Real();
  3810. }
  3811. };
  3812. }
  3813. #endif