12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146 |
- ///////////////////////////////////////////////////////////////////////////////
- // Copyright 2011 John Maddock. Distributed under the Boost
- // Software License, Version 1.0. (See accompanying file
- // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
- #ifndef BOOST_MATH_ER_GMP_BACKEND_HPP
- #define BOOST_MATH_ER_GMP_BACKEND_HPP
- #include <boost/multiprecision/number.hpp>
- #include <boost/multiprecision/debug_adaptor.hpp>
- #include <boost/multiprecision/detail/integer_ops.hpp>
- #include <boost/multiprecision/detail/big_lanczos.hpp>
- #include <boost/multiprecision/detail/digits.hpp>
- #include <boost/multiprecision/detail/atomic.hpp>
- #include <boost/math/special_functions/fpclassify.hpp>
- #include <cstdint>
- #include <boost/functional/hash_fwd.hpp>
- //
- // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
- //
- #include <boost/math/special_functions/asinh.hpp>
- #include <boost/math/special_functions/acosh.hpp>
- #include <boost/math/special_functions/atanh.hpp>
- #include <boost/math/special_functions/cbrt.hpp>
- #include <boost/math/special_functions/expm1.hpp>
- #include <boost/math/special_functions/gamma.hpp>
- #ifdef BOOST_MSVC
- #pragma warning(push)
- #pragma warning(disable : 4127)
- #endif
- #include <gmp.h>
- #ifdef BOOST_MSVC
- #pragma warning(pop)
- #endif
- #if defined(__MPIR_VERSION) && defined(__MPIR_VERSION_MINOR) && defined(__MPIR_VERSION_PATCHLEVEL)
- #define BOOST_MP_MPIR_VERSION (__MPIR_VERSION * 10000 + __MPIR_VERSION_MINOR * 100 + __MPIR_VERSION_PATCHLEVEL)
- #else
- #define BOOST_MP_MPIR_VERSION 0
- #endif
- #include <cctype>
- #include <cmath>
- #include <limits>
- #include <climits>
- namespace boost {
- namespace multiprecision {
- namespace backends {
- #ifdef BOOST_MSVC
- // warning C4127: conditional expression is constant
- #pragma warning(push)
- //#pragma warning(disable : 4127)
- #endif
- template <unsigned digits10>
- struct gmp_float;
- struct gmp_int;
- struct gmp_rational;
- } // namespace backends
- template <>
- struct number_category<backends::gmp_int> : public std::integral_constant<int, number_kind_integer>
- {};
- template <>
- struct number_category<backends::gmp_rational> : public std::integral_constant<int, number_kind_rational>
- {};
- template <unsigned digits10>
- struct number_category<backends::gmp_float<digits10> > : public std::integral_constant<int, number_kind_floating_point>
- {};
- namespace backends {
- //
- // Within this file, the only functions we mark as noexcept are those that manipulate
- // (but don't create) an mpf_t. All other types may allocate at pretty much any time
- // via a user-supplied allocator, and therefore throw.
- //
- namespace detail {
- template <unsigned digits10>
- struct gmp_float_imp
- {
- #ifdef BOOST_HAS_LONG_LONG
- using signed_types = std::tuple<long, boost::long_long_type> ;
- using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
- #else
- using signed_types = std::tuple<long> ;
- using unsigned_types = std::tuple<unsigned long>;
- #endif
- using float_types = std::tuple<double, long double>;
- using exponent_type = long ;
- gmp_float_imp() noexcept
- {
- m_data[0]._mp_d = 0; // uninitialized m_data
- }
- gmp_float_imp(const gmp_float_imp& o)
- {
- //
- // We have to do an init followed by a set here, otherwise *this may be at
- // a lower precision than o: seems like mpf_init_set copies just enough bits
- // to get the right value, but if it's then used in further calculations
- // things go badly wrong!!
- //
- mpf_init2(m_data, mpf_get_prec(o.data()));
- if (o.m_data[0]._mp_d)
- mpf_set(m_data, o.m_data);
- }
- // rvalue copy
- gmp_float_imp(gmp_float_imp&& o) noexcept
- {
- m_data[0] = o.m_data[0];
- o.m_data[0]._mp_d = 0;
- }
- gmp_float_imp& operator=(const gmp_float_imp& o)
- {
- if (m_data[0]._mp_d == 0)
- mpf_init2(m_data, mpf_get_prec(o.data()));
- if (mpf_get_prec(data()) != mpf_get_prec(o.data()))
- {
- mpf_t t;
- mpf_init2(t, mpf_get_prec(o.data()));
- mpf_set(t, o.data());
- mpf_swap(data(), t);
- mpf_clear(t);
- }
- else
- {
- if (o.m_data[0]._mp_d)
- mpf_set(m_data, o.m_data);
- }
- return *this;
- }
- // rvalue assign
- gmp_float_imp& operator=(gmp_float_imp&& o) noexcept
- {
- mpf_swap(m_data, o.m_data);
- return *this;
- }
- #ifdef BOOST_HAS_LONG_LONG
- #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
- gmp_float_imp& operator=(boost::ulong_long_type i)
- {
- *this = static_cast<unsigned long>(i);
- return *this;
- }
- #else
- gmp_float_imp& operator=(boost::ulong_long_type i)
- {
- if (m_data[0]._mp_d == 0)
- mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
- boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
- unsigned shift = 0;
- mpf_t t;
- mpf_init2(t, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
- mpf_set_ui(m_data, 0);
- while (i)
- {
- mpf_set_ui(t, static_cast<unsigned long>(i & mask));
- if (shift)
- mpf_mul_2exp(t, t, shift);
- mpf_add(m_data, m_data, t);
- shift += std::numeric_limits<unsigned long>::digits;
- i >>= std::numeric_limits<unsigned long>::digits;
- }
- mpf_clear(t);
- return *this;
- }
- #endif
- gmp_float_imp& operator=(boost::long_long_type i)
- {
- if (m_data[0]._mp_d == 0)
- mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
- bool neg = i < 0;
- *this = static_cast<boost::ulong_long_type>(boost::multiprecision::detail::unsigned_abs(i));
- if (neg)
- mpf_neg(m_data, m_data);
- return *this;
- }
- #endif
- gmp_float_imp& operator=(unsigned long i)
- {
- if (m_data[0]._mp_d == 0)
- mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
- mpf_set_ui(m_data, i);
- return *this;
- }
- gmp_float_imp& operator=(long i)
- {
- if (m_data[0]._mp_d == 0)
- mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
- mpf_set_si(m_data, i);
- return *this;
- }
- gmp_float_imp& operator=(double d)
- {
- if (m_data[0]._mp_d == 0)
- mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
- mpf_set_d(m_data, d);
- return *this;
- }
- gmp_float_imp& operator=(long double a)
- {
- using std::floor;
- using std::frexp;
- using std::ldexp;
- if (m_data[0]._mp_d == 0)
- mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
- if (a == 0)
- {
- mpf_set_si(m_data, 0);
- return *this;
- }
- if (a == 1)
- {
- mpf_set_si(m_data, 1);
- return *this;
- }
- BOOST_ASSERT(!(boost::math::isinf)(a));
- BOOST_ASSERT(!(boost::math::isnan)(a));
- int e;
- long double f, term;
- mpf_set_ui(m_data, 0u);
- f = frexp(a, &e);
- constexpr const int shift = std::numeric_limits<int>::digits - 1;
- while (f)
- {
- // extract int sized bits from f:
- f = ldexp(f, shift);
- term = floor(f);
- e -= shift;
- mpf_mul_2exp(m_data, m_data, shift);
- if (term > 0)
- mpf_add_ui(m_data, m_data, static_cast<unsigned>(term));
- else
- mpf_sub_ui(m_data, m_data, static_cast<unsigned>(-term));
- f -= term;
- }
- if (e > 0)
- mpf_mul_2exp(m_data, m_data, e);
- else if (e < 0)
- mpf_div_2exp(m_data, m_data, -e);
- return *this;
- }
- gmp_float_imp& operator=(const char* s)
- {
- if (m_data[0]._mp_d == 0)
- mpf_init2(m_data, multiprecision::detail::digits10_2_2(digits10 ? digits10 : (unsigned)get_default_precision()));
- if (s && (*s == '+'))
- ++s; // Leading "+" sign not supported by mpf_set_str:
- if (0 != mpf_set_str(m_data, s, 10))
- BOOST_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid floating point number.")));
- return *this;
- }
- void swap(gmp_float_imp& o) noexcept
- {
- mpf_swap(m_data, o.m_data);
- }
- std::string str(std::streamsize digits, std::ios_base::fmtflags f) const
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- bool scientific = (f & std::ios_base::scientific) == std::ios_base::scientific;
- bool fixed = (f & std::ios_base::fixed) == std::ios_base::fixed;
- std::streamsize org_digits(digits);
- if (scientific && digits)
- ++digits;
- std::string result;
- mp_exp_t e;
- void* (*alloc_func_ptr)(size_t);
- void* (*realloc_func_ptr)(void*, size_t, size_t);
- void (*free_func_ptr)(void*, size_t);
- mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
- if (mpf_sgn(m_data) == 0)
- {
- e = 0;
- result = "0";
- if (fixed && digits)
- ++digits;
- }
- else
- {
- char* ps = mpf_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data);
- --e; // To match with what our formatter expects.
- if (fixed && e != -1)
- {
- // Oops we actually need a different number of digits to what we asked for:
- (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
- digits += e + 1;
- if (digits == 0)
- {
- // We need to get *all* the digits and then possibly round up,
- // we end up with either "0" or "1" as the result.
- ps = mpf_get_str(0, &e, 10, 0, m_data);
- --e;
- unsigned offset = *ps == '-' ? 1 : 0;
- if (ps[offset] > '5')
- {
- ++e;
- ps[offset] = '1';
- ps[offset + 1] = 0;
- }
- else if (ps[offset] == '5')
- {
- unsigned i = offset + 1;
- bool round_up = false;
- while (ps[i] != 0)
- {
- if (ps[i] != '0')
- {
- round_up = true;
- break;
- }
- ++i;
- }
- if (round_up)
- {
- ++e;
- ps[offset] = '1';
- ps[offset + 1] = 0;
- }
- else
- {
- ps[offset] = '0';
- ps[offset + 1] = 0;
- }
- }
- else
- {
- ps[offset] = '0';
- ps[offset + 1] = 0;
- }
- }
- else if (digits > 0)
- {
- mp_exp_t old_e = e;
- ps = mpf_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data);
- --e; // To match with what our formatter expects.
- if (old_e > e)
- {
- // in some cases, when we ask for more digits of precision, it will
- // change the number of digits to the left of the decimal, if that
- // happens, account for it here.
- // example: cout << fixed << setprecision(3) << mpf_float_50("99.9809")
- digits -= old_e - e;
- (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
- ps = mpf_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data);
- --e; // To match with what our formatter expects.
- }
- }
- else
- {
- ps = mpf_get_str(0, &e, 10, 1, m_data);
- --e;
- unsigned offset = *ps == '-' ? 1 : 0;
- ps[offset] = '0';
- ps[offset + 1] = 0;
- }
- }
- result = ps;
- (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
- }
- boost::multiprecision::detail::format_float_string(result, e, org_digits, f, mpf_sgn(m_data) == 0);
- return result;
- }
- ~gmp_float_imp() noexcept
- {
- if (m_data[0]._mp_d)
- mpf_clear(m_data);
- }
- void negate() noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- mpf_neg(m_data, m_data);
- }
- int compare(const gmp_float<digits10>& o) const noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d && o.m_data[0]._mp_d);
- return mpf_cmp(m_data, o.m_data);
- }
- int compare(long i) const noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- return mpf_cmp_si(m_data, i);
- }
- int compare(unsigned long i) const noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- return mpf_cmp_ui(m_data, i);
- }
- template <class V>
- typename std::enable_if<boost::multiprecision::detail::is_arithmetic<V>::value, int>::type compare(V v) const
- {
- gmp_float<digits10> d;
- d = v;
- return compare(d);
- }
- mpf_t& data() noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- return m_data;
- }
- const mpf_t& data() const noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- return m_data;
- }
- protected:
- mpf_t m_data;
- static boost::multiprecision::detail::precision_type& get_default_precision() noexcept
- {
- static boost::multiprecision::detail::precision_type val(50);
- return val;
- }
- };
- } // namespace detail
- struct gmp_int;
- struct gmp_rational;
- template <unsigned digits10>
- struct gmp_float : public detail::gmp_float_imp<digits10>
- {
- gmp_float()
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- }
- gmp_float(const gmp_float& o) : detail::gmp_float_imp<digits10>(o) {}
- template <unsigned D>
- gmp_float(const gmp_float<D>& o, typename std::enable_if<D <= digits10>::type* = 0);
- template <unsigned D>
- explicit gmp_float(const gmp_float<D>& o, typename std::enable_if<!(D <= digits10)>::type* = 0);
- gmp_float(const gmp_int& o);
- gmp_float(const gmp_rational& o);
- gmp_float(const mpf_t val)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- mpf_set(this->m_data, val);
- }
- gmp_float(const mpz_t val)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- mpf_set_z(this->m_data, val);
- }
- gmp_float(const mpq_t val)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- mpf_set_q(this->m_data, val);
- }
- // rvalue copy
- gmp_float(gmp_float&& o) noexcept : detail::gmp_float_imp<digits10>(static_cast<detail::gmp_float_imp<digits10>&&>(o))
- {}
- gmp_float& operator=(const gmp_float& o)
- {
- *static_cast<detail::gmp_float_imp<digits10>*>(this) = static_cast<detail::gmp_float_imp<digits10> const&>(o);
- return *this;
- }
- gmp_float& operator=(gmp_float&& o) noexcept
- {
- *static_cast<detail::gmp_float_imp<digits10>*>(this) = static_cast<detail::gmp_float_imp<digits10>&&>(o);
- return *this;
- }
- template <unsigned D>
- gmp_float& operator=(const gmp_float<D>& o);
- gmp_float& operator=(const gmp_int& o);
- gmp_float& operator=(const gmp_rational& o);
- gmp_float& operator=(const mpf_t val)
- {
- if (this->m_data[0]._mp_d == 0)
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- mpf_set(this->m_data, val);
- return *this;
- }
- gmp_float& operator=(const mpz_t val)
- {
- if (this->m_data[0]._mp_d == 0)
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- mpf_set_z(this->m_data, val);
- return *this;
- }
- gmp_float& operator=(const mpq_t val)
- {
- if (this->m_data[0]._mp_d == 0)
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- mpf_set_q(this->m_data, val);
- return *this;
- }
- template <class V>
- gmp_float& operator=(const V& v)
- {
- *static_cast<detail::gmp_float_imp<digits10>*>(this) = v;
- return *this;
- }
- };
- template <>
- struct gmp_float<0> : public detail::gmp_float_imp<0>
- {
- //
- // We have a problem with mpf_t in that the precision we request isn't what we get.
- // As a result the front end can end up chasing it's tail trying to create a variable
- // with the the correct precision to hold the result of an expression.
- // See: https://github.com/boostorg/multiprecision/issues/164
- // The problem is made worse by the fact that our conversions from base10 to 2 and
- // vice-versa do not exactly round trip (and probably never will).
- // The workaround is to keep track of the precision requested, and always return
- // that as the current actual precision.
- //
- private:
- unsigned requested_precision;
- public:
- gmp_float() : requested_precision(get_default_precision())
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- }
- gmp_float(const mpf_t val) : requested_precision(get_default_precision())
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- mpf_set(this->m_data, val);
- }
- gmp_float(const mpz_t val) : requested_precision(get_default_precision())
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- mpf_set_z(this->m_data, val);
- }
- gmp_float(const mpq_t val) : requested_precision(get_default_precision())
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- mpf_set_q(this->m_data, val);
- }
- gmp_float(const gmp_float& o) : detail::gmp_float_imp<0>(o), requested_precision(o.requested_precision) {}
- template <unsigned D>
- gmp_float(const gmp_float<D>& o)
- {
- mpf_init2(this->m_data, mpf_get_prec(o.data()));
- mpf_set(this->m_data, o.data());
- requested_precision = D;
- }
- // rvalue copy
- gmp_float(gmp_float&& o) noexcept : detail::gmp_float_imp<0>(static_cast<detail::gmp_float_imp<0>&&>(o)), requested_precision(o.requested_precision)
- {}
- gmp_float(const gmp_int& o);
- gmp_float(const gmp_rational& o);
- gmp_float(const gmp_float& o, unsigned digits10) : requested_precision(digits10)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- mpf_set(this->m_data, o.data());
- }
- template <class V>
- gmp_float(const V& o, unsigned digits10) : requested_precision(digits10)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- *this = o;
- }
- #ifndef BOOST_NO_CXX17_HDR_STRING_VIEW
- //
- // Support for new types in C++17
- //
- template <class Traits>
- gmp_float(const std::basic_string_view<char, Traits>& o, unsigned digits10) : requested_precision(digits10)
- {
- using default_ops::assign_from_string_view;
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(digits10));
- assign_from_string_view(*this, o);
- }
- #endif
- gmp_float& operator=(const gmp_float& o)
- {
- *static_cast<detail::gmp_float_imp<0>*>(this) = static_cast<detail::gmp_float_imp<0> const&>(o);
- requested_precision = o.requested_precision;
- return *this;
- }
- // rvalue copy
- gmp_float& operator=(gmp_float&& o) noexcept
- {
- *static_cast<detail::gmp_float_imp<0>*>(this) = static_cast<detail::gmp_float_imp<0>&&>(o);
- requested_precision = o.requested_precision;
- return *this;
- }
- template <unsigned D>
- gmp_float& operator=(const gmp_float<D>& o)
- {
- if (this->m_data[0]._mp_d == 0)
- {
- mpf_init2(this->m_data, mpf_get_prec(o.data()));
- }
- else
- {
- mpf_set_prec(this->m_data, mpf_get_prec(o.data()));
- }
- mpf_set(this->m_data, o.data());
- requested_precision = D;
- return *this;
- }
- gmp_float& operator=(const gmp_int& o);
- gmp_float& operator=(const gmp_rational& o);
- gmp_float& operator=(const mpf_t val)
- {
- if (this->m_data[0]._mp_d == 0)
- {
- requested_precision = get_default_precision();
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- }
- mpf_set(this->m_data, val);
- return *this;
- }
- gmp_float& operator=(const mpz_t val)
- {
- if (this->m_data[0]._mp_d == 0)
- {
- requested_precision = get_default_precision();
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- }
- mpf_set_z(this->m_data, val);
- return *this;
- }
- gmp_float& operator=(const mpq_t val)
- {
- if (this->m_data[0]._mp_d == 0)
- {
- requested_precision = get_default_precision();
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- }
- mpf_set_q(this->m_data, val);
- return *this;
- }
- template <class V>
- gmp_float& operator=(const V& v)
- {
- *static_cast<detail::gmp_float_imp<0>*>(this) = v;
- return *this;
- }
- static unsigned default_precision() noexcept
- {
- return get_default_precision();
- }
- static void default_precision(unsigned v) noexcept
- {
- get_default_precision() = v;
- }
- unsigned precision() const noexcept
- {
- return requested_precision;
- }
- void precision(unsigned digits10) noexcept
- {
- requested_precision = digits10;
- mpf_set_prec(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- }
- void swap(gmp_float& o)
- {
- std::swap(requested_precision, o.requested_precision);
- gmp_float_imp<0>::swap(o);
- }
- };
- template <unsigned digits10, class T>
- 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
- {
- return a.compare(b) == 0;
- }
- template <unsigned digits10, class T>
- 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
- {
- return a.compare(b) < 0;
- }
- template <unsigned digits10, class T>
- 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
- {
- return a.compare(b) > 0;
- }
- template <unsigned D1, unsigned D2>
- inline void eval_add(gmp_float<D1>& result, const gmp_float<D2>& o)
- {
- mpf_add(result.data(), result.data(), o.data());
- }
- template <unsigned D1, unsigned D2>
- inline void eval_subtract(gmp_float<D1>& result, const gmp_float<D2>& o)
- {
- mpf_sub(result.data(), result.data(), o.data());
- }
- template <unsigned D1, unsigned D2>
- inline void eval_multiply(gmp_float<D1>& result, const gmp_float<D2>& o)
- {
- mpf_mul(result.data(), result.data(), o.data());
- }
- template <unsigned digits10>
- inline bool eval_is_zero(const gmp_float<digits10>& val) noexcept
- {
- return mpf_sgn(val.data()) == 0;
- }
- template <unsigned D1, unsigned D2>
- inline void eval_divide(gmp_float<D1>& result, const gmp_float<D2>& o)
- {
- if (eval_is_zero(o))
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpf_div(result.data(), result.data(), o.data());
- }
- template <unsigned digits10>
- inline void eval_add(gmp_float<digits10>& result, unsigned long i)
- {
- mpf_add_ui(result.data(), result.data(), i);
- }
- template <unsigned digits10>
- inline void eval_subtract(gmp_float<digits10>& result, unsigned long i)
- {
- mpf_sub_ui(result.data(), result.data(), i);
- }
- template <unsigned digits10>
- inline void eval_multiply(gmp_float<digits10>& result, unsigned long i)
- {
- mpf_mul_ui(result.data(), result.data(), i);
- }
- template <unsigned digits10>
- inline void eval_divide(gmp_float<digits10>& result, unsigned long i)
- {
- if (i == 0)
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpf_div_ui(result.data(), result.data(), i);
- }
- template <unsigned digits10>
- inline void eval_add(gmp_float<digits10>& result, long i)
- {
- if (i > 0)
- mpf_add_ui(result.data(), result.data(), i);
- else
- mpf_sub_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- template <unsigned digits10>
- inline void eval_subtract(gmp_float<digits10>& result, long i)
- {
- if (i > 0)
- mpf_sub_ui(result.data(), result.data(), i);
- else
- mpf_add_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- template <unsigned digits10>
- inline void eval_multiply(gmp_float<digits10>& result, long i)
- {
- mpf_mul_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
- if (i < 0)
- mpf_neg(result.data(), result.data());
- }
- template <unsigned digits10>
- inline void eval_divide(gmp_float<digits10>& result, long i)
- {
- if (i == 0)
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpf_div_ui(result.data(), result.data(), boost::multiprecision::detail::unsigned_abs(i));
- if (i < 0)
- mpf_neg(result.data(), result.data());
- }
- //
- // Specialised 3 arg versions of the basic operators:
- //
- template <unsigned D1, unsigned D2, unsigned D3>
- inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
- {
- mpf_add(a.data(), x.data(), y.data());
- }
- template <unsigned D1, unsigned D2>
- inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
- {
- mpf_add_ui(a.data(), x.data(), y);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_add(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
- {
- if (y < 0)
- mpf_sub_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
- else
- mpf_add_ui(a.data(), x.data(), y);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_add(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
- {
- mpf_add_ui(a.data(), y.data(), x);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_add(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
- {
- if (x < 0)
- {
- mpf_ui_sub(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data());
- mpf_neg(a.data(), a.data());
- }
- else
- mpf_add_ui(a.data(), y.data(), x);
- }
- template <unsigned D1, unsigned D2, unsigned D3>
- inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
- {
- mpf_sub(a.data(), x.data(), y.data());
- }
- template <unsigned D1, unsigned D2>
- inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
- {
- mpf_sub_ui(a.data(), x.data(), y);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_subtract(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
- {
- if (y < 0)
- mpf_add_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
- else
- mpf_sub_ui(a.data(), x.data(), y);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_subtract(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
- {
- mpf_ui_sub(a.data(), x, y.data());
- }
- template <unsigned D1, unsigned D2>
- inline void eval_subtract(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
- {
- if (x < 0)
- {
- mpf_add_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x));
- mpf_neg(a.data(), a.data());
- }
- else
- mpf_ui_sub(a.data(), x, y.data());
- }
- template <unsigned D1, unsigned D2, unsigned D3>
- inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
- {
- mpf_mul(a.data(), x.data(), y.data());
- }
- template <unsigned D1, unsigned D2>
- inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
- {
- mpf_mul_ui(a.data(), x.data(), y);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_multiply(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
- {
- if (y < 0)
- {
- mpf_mul_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
- a.negate();
- }
- else
- mpf_mul_ui(a.data(), x.data(), y);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_multiply(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
- {
- mpf_mul_ui(a.data(), y.data(), x);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_multiply(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
- {
- if (x < 0)
- {
- mpf_mul_ui(a.data(), y.data(), boost::multiprecision::detail::unsigned_abs(x));
- mpf_neg(a.data(), a.data());
- }
- else
- mpf_mul_ui(a.data(), y.data(), x);
- }
- template <unsigned D1, unsigned D2, unsigned D3>
- inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, const gmp_float<D3>& y)
- {
- if (eval_is_zero(y))
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpf_div(a.data(), x.data(), y.data());
- }
- template <unsigned D1, unsigned D2>
- inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, unsigned long y)
- {
- if (y == 0)
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpf_div_ui(a.data(), x.data(), y);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_divide(gmp_float<D1>& a, const gmp_float<D2>& x, long y)
- {
- if (y == 0)
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- if (y < 0)
- {
- mpf_div_ui(a.data(), x.data(), boost::multiprecision::detail::unsigned_abs(y));
- a.negate();
- }
- else
- mpf_div_ui(a.data(), x.data(), y);
- }
- template <unsigned D1, unsigned D2>
- inline void eval_divide(gmp_float<D1>& a, unsigned long x, const gmp_float<D2>& y)
- {
- if (eval_is_zero(y))
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpf_ui_div(a.data(), x, y.data());
- }
- template <unsigned D1, unsigned D2>
- inline void eval_divide(gmp_float<D1>& a, long x, const gmp_float<D2>& y)
- {
- if (eval_is_zero(y))
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- if (x < 0)
- {
- mpf_ui_div(a.data(), boost::multiprecision::detail::unsigned_abs(x), y.data());
- mpf_neg(a.data(), a.data());
- }
- else
- mpf_ui_div(a.data(), x, y.data());
- }
- template <unsigned digits10>
- inline int eval_get_sign(const gmp_float<digits10>& val) noexcept
- {
- return mpf_sgn(val.data());
- }
- template <unsigned digits10>
- inline void eval_convert_to(unsigned long* result, const gmp_float<digits10>& val) noexcept
- {
- if (0 == mpf_fits_ulong_p(val.data()))
- *result = (std::numeric_limits<unsigned long>::max)();
- else
- *result = (unsigned long)mpf_get_ui(val.data());
- }
- template <unsigned digits10>
- inline void eval_convert_to(long* result, const gmp_float<digits10>& val) noexcept
- {
- if (0 == mpf_fits_slong_p(val.data()))
- {
- *result = (std::numeric_limits<long>::max)();
- *result *= mpf_sgn(val.data());
- }
- else
- *result = (long)mpf_get_si(val.data());
- }
- template <unsigned digits10>
- inline void eval_convert_to(double* result, const gmp_float<digits10>& val) noexcept
- {
- *result = mpf_get_d(val.data());
- }
- #ifdef BOOST_HAS_LONG_LONG
- template <unsigned digits10>
- inline void eval_convert_to(boost::long_long_type* result, const gmp_float<digits10>& val)
- {
- gmp_float<digits10> t(val);
- if (eval_get_sign(t) < 0)
- t.negate();
- long digits = std::numeric_limits<boost::long_long_type>::digits - std::numeric_limits<long>::digits;
- if (digits > 0)
- mpf_div_2exp(t.data(), t.data(), digits);
- if (!mpf_fits_slong_p(t.data()))
- {
- if (eval_get_sign(val) < 0)
- *result = (std::numeric_limits<boost::long_long_type>::min)();
- else
- *result = (std::numeric_limits<boost::long_long_type>::max)();
- return;
- };
- *result = mpf_get_si(t.data());
- while (digits > 0)
- {
- *result <<= digits;
- digits -= std::numeric_limits<unsigned long>::digits;
- mpf_mul_2exp(t.data(), t.data(), digits >= 0 ? std::numeric_limits<unsigned long>::digits : std::numeric_limits<unsigned long>::digits + digits);
- unsigned long l = (unsigned long)mpf_get_ui(t.data());
- if (digits < 0)
- l >>= -digits;
- *result |= l;
- }
- if (eval_get_sign(val) < 0)
- *result = -*result;
- }
- template <unsigned digits10>
- inline void eval_convert_to(boost::ulong_long_type* result, const gmp_float<digits10>& val)
- {
- gmp_float<digits10> t(val);
- long digits = std::numeric_limits<boost::long_long_type>::digits - std::numeric_limits<long>::digits;
- if (digits > 0)
- mpf_div_2exp(t.data(), t.data(), digits);
- if (!mpf_fits_ulong_p(t.data()))
- {
- *result = (std::numeric_limits<boost::long_long_type>::max)();
- return;
- }
- *result = mpf_get_ui(t.data());
- while (digits > 0)
- {
- *result <<= digits;
- digits -= std::numeric_limits<unsigned long>::digits;
- mpf_mul_2exp(t.data(), t.data(), digits >= 0 ? std::numeric_limits<unsigned long>::digits : std::numeric_limits<unsigned long>::digits + digits);
- unsigned long l = (unsigned long)mpf_get_ui(t.data());
- if (digits < 0)
- l >>= -digits;
- *result |= l;
- }
- }
- #endif
- //
- // Native non-member operations:
- //
- template <unsigned Digits10>
- inline void eval_sqrt(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
- {
- mpf_sqrt(result.data(), val.data());
- }
- template <unsigned Digits10>
- inline void eval_abs(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
- {
- mpf_abs(result.data(), val.data());
- }
- template <unsigned Digits10>
- inline void eval_fabs(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
- {
- mpf_abs(result.data(), val.data());
- }
- template <unsigned Digits10>
- inline void eval_ceil(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
- {
- mpf_ceil(result.data(), val.data());
- }
- template <unsigned Digits10>
- inline void eval_floor(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
- {
- mpf_floor(result.data(), val.data());
- }
- template <unsigned Digits10>
- inline void eval_trunc(gmp_float<Digits10>& result, const gmp_float<Digits10>& val)
- {
- mpf_trunc(result.data(), val.data());
- }
- template <unsigned Digits10>
- inline void eval_ldexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, long e)
- {
- if (e > 0)
- mpf_mul_2exp(result.data(), val.data(), e);
- else if (e < 0)
- mpf_div_2exp(result.data(), val.data(), -e);
- else
- result = val;
- }
- template <unsigned Digits10>
- inline void eval_frexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, int* e)
- {
- #if (BOOST_MP_MPIR_VERSION >= 20600) && (BOOST_MP_MPIR_VERSION < 30000)
- mpir_si v;
- mpf_get_d_2exp(&v, val.data());
- #else
- long v;
- mpf_get_d_2exp(&v, val.data());
- #endif
- *e = v;
- eval_ldexp(result, val, -v);
- }
- template <unsigned Digits10>
- inline void eval_frexp(gmp_float<Digits10>& result, const gmp_float<Digits10>& val, long* e)
- {
- #if (BOOST_MP_MPIR_VERSION >= 20600) && (BOOST_MP_MPIR_VERSION < 30000)
- mpir_si v;
- mpf_get_d_2exp(&v, val.data());
- *e = v;
- eval_ldexp(result, val, -v);
- #else
- mpf_get_d_2exp(e, val.data());
- eval_ldexp(result, val, -*e);
- #endif
- }
- template <unsigned Digits10>
- inline std::size_t hash_value(const gmp_float<Digits10>& val)
- {
- std::size_t result = 0;
- for (int i = 0; i < std::abs(val.data()[0]._mp_size); ++i)
- boost::hash_combine(result, val.data()[0]._mp_d[i]);
- boost::hash_combine(result, val.data()[0]._mp_exp);
- boost::hash_combine(result, val.data()[0]._mp_size);
- return result;
- }
- struct gmp_int
- {
- #ifdef BOOST_HAS_LONG_LONG
- using signed_types = std::tuple<long, boost::long_long_type> ;
- using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
- #else
- using signed_types = std::tuple<long> ;
- using unsigned_types = std::tuple<unsigned long>;
- #endif
- using float_types = std::tuple<double, long double>;
- gmp_int()
- {
- mpz_init(this->m_data);
- }
- gmp_int(const gmp_int& o)
- {
- if (o.m_data[0]._mp_d)
- mpz_init_set(m_data, o.m_data);
- else
- mpz_init(this->m_data);
- }
- // rvalue
- gmp_int(gmp_int&& o) noexcept
- {
- m_data[0] = o.m_data[0];
- o.m_data[0]._mp_d = 0;
- }
- explicit gmp_int(const mpf_t val)
- {
- mpz_init(this->m_data);
- mpz_set_f(this->m_data, val);
- }
- gmp_int(const mpz_t val)
- {
- mpz_init_set(this->m_data, val);
- }
- explicit gmp_int(const mpq_t val)
- {
- mpz_init(this->m_data);
- mpz_set_q(this->m_data, val);
- }
- template <unsigned Digits10>
- explicit gmp_int(const gmp_float<Digits10>& o)
- {
- mpz_init(this->m_data);
- mpz_set_f(this->m_data, o.data());
- }
- explicit gmp_int(const gmp_rational& o);
- gmp_int& operator=(const gmp_int& o)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set(m_data, o.m_data);
- return *this;
- }
- // rvalue copy
- gmp_int& operator=(gmp_int&& o) noexcept
- {
- mpz_swap(m_data, o.m_data);
- return *this;
- }
- #ifdef BOOST_HAS_LONG_LONG
- #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
- gmp_int& operator=(boost::ulong_long_type i)
- {
- *this = static_cast<unsigned long>(i);
- return *this;
- }
- #else
- gmp_int& operator=(boost::ulong_long_type i)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- boost::ulong_long_type mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
- unsigned shift = 0;
- mpz_t t;
- mpz_set_ui(m_data, 0);
- mpz_init_set_ui(t, 0);
- while (i)
- {
- mpz_set_ui(t, static_cast<unsigned long>(i & mask));
- if (shift)
- mpz_mul_2exp(t, t, shift);
- mpz_add(m_data, m_data, t);
- shift += std::numeric_limits<unsigned long>::digits;
- i >>= std::numeric_limits<unsigned long>::digits;
- }
- mpz_clear(t);
- return *this;
- }
- #endif
- gmp_int& operator=(boost::long_long_type i)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- bool neg = i < 0;
- *this = boost::multiprecision::detail::unsigned_abs(i);
- if (neg)
- mpz_neg(m_data, m_data);
- return *this;
- }
- #endif
- #ifdef BOOST_HAS_INT128
- gmp_int& operator=(unsigned __int128 i)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- unsigned __int128 mask = ((((1uLL << (std::numeric_limits<unsigned long>::digits - 1)) - 1) << 1) | 1uLL);
- unsigned shift = 0;
- mpz_t t;
- mpz_set_ui(m_data, 0);
- mpz_init_set_ui(t, 0);
- while (i)
- {
- mpz_set_ui(t, static_cast<unsigned long>(i & mask));
- if (shift)
- mpz_mul_2exp(t, t, shift);
- mpz_add(m_data, m_data, t);
- shift += std::numeric_limits<unsigned long>::digits;
- i >>= std::numeric_limits<unsigned long>::digits;
- }
- mpz_clear(t);
- return *this;
- }
- gmp_int& operator=(__int128 i)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- bool neg = i < 0;
- *this = boost::multiprecision::detail::unsigned_abs(i);
- if (neg)
- mpz_neg(m_data, m_data);
- return *this;
- }
- #endif
- gmp_int& operator=(unsigned long i)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set_ui(m_data, i);
- return *this;
- }
- gmp_int& operator=(long i)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set_si(m_data, i);
- return *this;
- }
- gmp_int& operator=(double d)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set_d(m_data, d);
- return *this;
- }
- gmp_int& operator=(long double a)
- {
- using std::floor;
- using std::frexp;
- using std::ldexp;
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- if (a == 0)
- {
- mpz_set_si(m_data, 0);
- return *this;
- }
- if (a == 1)
- {
- mpz_set_si(m_data, 1);
- return *this;
- }
- BOOST_ASSERT(!(boost::math::isinf)(a));
- BOOST_ASSERT(!(boost::math::isnan)(a));
- int e;
- long double f, term;
- mpz_set_ui(m_data, 0u);
- f = frexp(a, &e);
- constexpr const int shift = std::numeric_limits<int>::digits - 1;
- while (f)
- {
- // extract int sized bits from f:
- f = ldexp(f, shift);
- term = floor(f);
- e -= shift;
- mpz_mul_2exp(m_data, m_data, shift);
- if (term > 0)
- mpz_add_ui(m_data, m_data, static_cast<unsigned>(term));
- else
- mpz_sub_ui(m_data, m_data, static_cast<unsigned>(-term));
- f -= term;
- }
- if (e > 0)
- mpz_mul_2exp(m_data, m_data, e);
- else if (e < 0)
- mpz_div_2exp(m_data, m_data, -e);
- return *this;
- }
- gmp_int& operator=(const char* s)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- std::size_t n = s ? std::strlen(s) : 0;
- int radix = 10;
- if (n && (*s == '0'))
- {
- if ((n > 1) && ((s[1] == 'x') || (s[1] == 'X')))
- {
- radix = 16;
- s += 2;
- n -= 2;
- }
- else
- {
- radix = 8;
- n -= 1;
- }
- }
- if (n)
- {
- if (0 != mpz_set_str(m_data, s, radix))
- BOOST_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid integer.")));
- }
- else
- mpz_set_ui(m_data, 0);
- return *this;
- }
- gmp_int& operator=(const mpf_t val)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set_f(this->m_data, val);
- return *this;
- }
- gmp_int& operator=(const mpz_t val)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set(this->m_data, val);
- return *this;
- }
- gmp_int& operator=(const mpq_t val)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set_q(this->m_data, val);
- return *this;
- }
- template <unsigned Digits10>
- gmp_int& operator=(const gmp_float<Digits10>& o)
- {
- if (m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set_f(this->m_data, o.data());
- return *this;
- }
- gmp_int& operator=(const gmp_rational& o);
- void swap(gmp_int& o)
- {
- mpz_swap(m_data, o.m_data);
- }
- std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags f) const
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- int base = 10;
- if ((f & std::ios_base::oct) == std::ios_base::oct)
- base = 8;
- else if ((f & std::ios_base::hex) == std::ios_base::hex)
- base = 16;
- //
- // sanity check, bases 8 and 16 are only available for positive numbers:
- //
- if ((base != 10) && (mpz_sgn(m_data) < 0))
- BOOST_THROW_EXCEPTION(std::runtime_error("Formatted output in bases 8 or 16 is only available for positive numbers"));
- void* (*alloc_func_ptr)(size_t);
- void* (*realloc_func_ptr)(void*, size_t, size_t);
- void (*free_func_ptr)(void*, size_t);
- const char* ps = mpz_get_str(0, base, m_data);
- std::string s = ps;
- mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
- (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
- if (f & std::ios_base::uppercase)
- for (size_t i = 0; i < s.length(); ++i)
- s[i] = static_cast<char>(std::toupper(s[i]));
- if ((base != 10) && (f & std::ios_base::showbase))
- {
- int pos = s[0] == '-' ? 1 : 0;
- const char* pp = base == 8 ? "0" : (f & std::ios_base::uppercase) ? "0X" : "0x";
- s.insert(static_cast<std::string::size_type>(pos), pp);
- }
- if ((f & std::ios_base::showpos) && (s[0] != '-'))
- s.insert(static_cast<std::string::size_type>(0), 1, '+');
- return s;
- }
- ~gmp_int() noexcept
- {
- if (m_data[0]._mp_d)
- mpz_clear(m_data);
- }
- void negate() noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- mpz_neg(m_data, m_data);
- }
- int compare(const gmp_int& o) const noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d && o.m_data[0]._mp_d);
- return mpz_cmp(m_data, o.m_data);
- }
- int compare(long i) const noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- return mpz_cmp_si(m_data, i);
- }
- int compare(unsigned long i) const noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- return mpz_cmp_ui(m_data, i);
- }
- template <class V>
- int compare(V v) const
- {
- gmp_int d;
- d = v;
- return compare(d);
- }
- mpz_t& data() noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- return m_data;
- }
- const mpz_t& data() const noexcept
- {
- BOOST_ASSERT(m_data[0]._mp_d);
- return m_data;
- }
- protected:
- mpz_t m_data;
- };
- template <class T>
- inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_eq(const gmp_int& a, const T& b)
- {
- return a.compare(b) == 0;
- }
- template <class T>
- inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_lt(const gmp_int& a, const T& b)
- {
- return a.compare(b) < 0;
- }
- template <class T>
- inline typename std::enable_if<boost::multiprecision::detail::is_arithmetic<T>::value, bool>::type eval_gt(const gmp_int& a, const T& b)
- {
- return a.compare(b) > 0;
- }
- inline bool eval_is_zero(const gmp_int& val)
- {
- return mpz_sgn(val.data()) == 0;
- }
- inline void eval_add(gmp_int& t, const gmp_int& o)
- {
- mpz_add(t.data(), t.data(), o.data());
- }
- inline void eval_multiply_add(gmp_int& t, const gmp_int& a, const gmp_int& b)
- {
- mpz_addmul(t.data(), a.data(), b.data());
- }
- inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, const gmp_int& b)
- {
- mpz_submul(t.data(), a.data(), b.data());
- }
- inline void eval_subtract(gmp_int& t, const gmp_int& o)
- {
- mpz_sub(t.data(), t.data(), o.data());
- }
- inline void eval_multiply(gmp_int& t, const gmp_int& o)
- {
- mpz_mul(t.data(), t.data(), o.data());
- }
- inline void eval_divide(gmp_int& t, const gmp_int& o)
- {
- if (eval_is_zero(o))
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpz_tdiv_q(t.data(), t.data(), o.data());
- }
- inline void eval_modulus(gmp_int& t, const gmp_int& o)
- {
- mpz_tdiv_r(t.data(), t.data(), o.data());
- }
- inline void eval_add(gmp_int& t, unsigned long i)
- {
- mpz_add_ui(t.data(), t.data(), i);
- }
- inline void eval_multiply_add(gmp_int& t, const gmp_int& a, unsigned long i)
- {
- mpz_addmul_ui(t.data(), a.data(), i);
- }
- inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, unsigned long i)
- {
- mpz_submul_ui(t.data(), a.data(), i);
- }
- inline void eval_subtract(gmp_int& t, unsigned long i)
- {
- mpz_sub_ui(t.data(), t.data(), i);
- }
- inline void eval_multiply(gmp_int& t, unsigned long i)
- {
- mpz_mul_ui(t.data(), t.data(), i);
- }
- inline void eval_modulus(gmp_int& t, unsigned long i)
- {
- mpz_tdiv_r_ui(t.data(), t.data(), i);
- }
- inline void eval_divide(gmp_int& t, unsigned long i)
- {
- if (i == 0)
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpz_tdiv_q_ui(t.data(), t.data(), i);
- }
- inline void eval_add(gmp_int& t, long i)
- {
- if (i > 0)
- mpz_add_ui(t.data(), t.data(), i);
- else
- mpz_sub_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- inline void eval_multiply_add(gmp_int& t, const gmp_int& a, long i)
- {
- if (i > 0)
- mpz_addmul_ui(t.data(), a.data(), i);
- else
- mpz_submul_ui(t.data(), a.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- inline void eval_multiply_subtract(gmp_int& t, const gmp_int& a, long i)
- {
- if (i > 0)
- mpz_submul_ui(t.data(), a.data(), i);
- else
- mpz_addmul_ui(t.data(), a.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- inline void eval_subtract(gmp_int& t, long i)
- {
- if (i > 0)
- mpz_sub_ui(t.data(), t.data(), i);
- else
- mpz_add_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- inline void eval_multiply(gmp_int& t, long i)
- {
- mpz_mul_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
- if (i < 0)
- mpz_neg(t.data(), t.data());
- }
- inline void eval_modulus(gmp_int& t, long i)
- {
- mpz_tdiv_r_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- inline void eval_divide(gmp_int& t, long i)
- {
- if (i == 0)
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpz_tdiv_q_ui(t.data(), t.data(), boost::multiprecision::detail::unsigned_abs(i));
- if (i < 0)
- mpz_neg(t.data(), t.data());
- }
- template <class UI>
- inline void eval_left_shift(gmp_int& t, UI i)
- {
- mpz_mul_2exp(t.data(), t.data(), static_cast<unsigned long>(i));
- }
- template <class UI>
- inline void eval_right_shift(gmp_int& t, UI i)
- {
- mpz_fdiv_q_2exp(t.data(), t.data(), static_cast<unsigned long>(i));
- }
- template <class UI>
- inline void eval_left_shift(gmp_int& t, const gmp_int& v, UI i)
- {
- mpz_mul_2exp(t.data(), v.data(), static_cast<unsigned long>(i));
- }
- template <class UI>
- inline void eval_right_shift(gmp_int& t, const gmp_int& v, UI i)
- {
- mpz_fdiv_q_2exp(t.data(), v.data(), static_cast<unsigned long>(i));
- }
- inline void eval_bitwise_and(gmp_int& result, const gmp_int& v)
- {
- mpz_and(result.data(), result.data(), v.data());
- }
- inline void eval_bitwise_or(gmp_int& result, const gmp_int& v)
- {
- mpz_ior(result.data(), result.data(), v.data());
- }
- inline void eval_bitwise_xor(gmp_int& result, const gmp_int& v)
- {
- mpz_xor(result.data(), result.data(), v.data());
- }
- inline void eval_add(gmp_int& t, const gmp_int& p, const gmp_int& o)
- {
- mpz_add(t.data(), p.data(), o.data());
- }
- inline void eval_subtract(gmp_int& t, const gmp_int& p, const gmp_int& o)
- {
- mpz_sub(t.data(), p.data(), o.data());
- }
- inline void eval_multiply(gmp_int& t, const gmp_int& p, const gmp_int& o)
- {
- mpz_mul(t.data(), p.data(), o.data());
- }
- inline void eval_divide(gmp_int& t, const gmp_int& p, const gmp_int& o)
- {
- if (eval_is_zero(o))
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpz_tdiv_q(t.data(), p.data(), o.data());
- }
- inline void eval_modulus(gmp_int& t, const gmp_int& p, const gmp_int& o)
- {
- mpz_tdiv_r(t.data(), p.data(), o.data());
- }
- inline void eval_add(gmp_int& t, const gmp_int& p, unsigned long i)
- {
- mpz_add_ui(t.data(), p.data(), i);
- }
- inline void eval_subtract(gmp_int& t, const gmp_int& p, unsigned long i)
- {
- mpz_sub_ui(t.data(), p.data(), i);
- }
- inline void eval_multiply(gmp_int& t, const gmp_int& p, unsigned long i)
- {
- mpz_mul_ui(t.data(), p.data(), i);
- }
- inline void eval_modulus(gmp_int& t, const gmp_int& p, unsigned long i)
- {
- mpz_tdiv_r_ui(t.data(), p.data(), i);
- }
- inline void eval_divide(gmp_int& t, const gmp_int& p, unsigned long i)
- {
- if (i == 0)
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpz_tdiv_q_ui(t.data(), p.data(), i);
- }
- inline void eval_add(gmp_int& t, const gmp_int& p, long i)
- {
- if (i > 0)
- mpz_add_ui(t.data(), p.data(), i);
- else
- mpz_sub_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- inline void eval_subtract(gmp_int& t, const gmp_int& p, long i)
- {
- if (i > 0)
- mpz_sub_ui(t.data(), p.data(), i);
- else
- mpz_add_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- inline void eval_multiply(gmp_int& t, const gmp_int& p, long i)
- {
- mpz_mul_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
- if (i < 0)
- mpz_neg(t.data(), t.data());
- }
- inline void eval_modulus(gmp_int& t, const gmp_int& p, long i)
- {
- mpz_tdiv_r_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
- }
- inline void eval_divide(gmp_int& t, const gmp_int& p, long i)
- {
- if (i == 0)
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpz_tdiv_q_ui(t.data(), p.data(), boost::multiprecision::detail::unsigned_abs(i));
- if (i < 0)
- mpz_neg(t.data(), t.data());
- }
- inline void eval_bitwise_and(gmp_int& result, const gmp_int& u, const gmp_int& v)
- {
- mpz_and(result.data(), u.data(), v.data());
- }
- inline void eval_bitwise_or(gmp_int& result, const gmp_int& u, const gmp_int& v)
- {
- mpz_ior(result.data(), u.data(), v.data());
- }
- inline void eval_bitwise_xor(gmp_int& result, const gmp_int& u, const gmp_int& v)
- {
- mpz_xor(result.data(), u.data(), v.data());
- }
- inline void eval_complement(gmp_int& result, const gmp_int& u)
- {
- mpz_com(result.data(), u.data());
- }
- inline int eval_get_sign(const gmp_int& val)
- {
- return mpz_sgn(val.data());
- }
- inline void eval_convert_to(unsigned long* result, const gmp_int& val)
- {
- if (mpz_sgn(val.data()) < 0)
- {
- BOOST_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
- }
- else
- *result = (unsigned long)mpz_get_ui(val.data());
- }
- inline void eval_convert_to(long* result, const gmp_int& val)
- {
- if (0 == mpz_fits_slong_p(val.data()))
- {
- *result = mpz_sgn(val.data()) < 0 ? (std::numeric_limits<long>::min)() : (std::numeric_limits<long>::max)();
- }
- else
- *result = (signed long)mpz_get_si(val.data());
- }
- inline void eval_convert_to(double* result, const gmp_int& val)
- {
- *result = mpz_get_d(val.data());
- }
- #ifdef BOOST_HAS_LONG_LONG
- inline void eval_convert_to(boost::ulong_long_type* result, const gmp_int& val)
- {
- if (mpz_sgn(val.data()) < 0)
- {
- BOOST_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
- }
- *result = 0;
- gmp_int t(val);
- unsigned parts = sizeof(boost::ulong_long_type) / sizeof(unsigned long);
- for (unsigned i = 0; i < parts; ++i)
- {
- boost::ulong_long_type part = mpz_get_ui(t.data());
- if (i)
- *result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
- else
- *result = part;
- mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
- }
- }
- inline void eval_convert_to(boost::long_long_type* result, const gmp_int& val)
- {
- int s = mpz_sgn(val.data());
- *result = 0;
- gmp_int t(val);
- unsigned parts = sizeof(boost::ulong_long_type) / sizeof(unsigned long);
- boost::ulong_long_type unsigned_result = 0;
- for (unsigned i = 0; i < parts; ++i)
- {
- boost::ulong_long_type part = mpz_get_ui(t.data());
- if (i)
- unsigned_result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
- else
- unsigned_result = part;
- mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
- }
- //
- // Overflow check:
- //
- bool overflow = false;
- if (mpz_sgn(t.data()))
- {
- overflow = true;
- }
- if ((s > 0) && (unsigned_result > static_cast<boost::ulong_long_type>((std::numeric_limits<boost::long_long_type>::max)())))
- overflow = true;
- if((s < 0) && (unsigned_result > 1u - static_cast<boost::ulong_long_type>((std::numeric_limits<boost::long_long_type>::min)() + 1)))
- overflow = true;
- if(overflow)
- *result = s < 0 ? (std::numeric_limits<boost::long_long_type>::min)() : (std::numeric_limits<boost::long_long_type>::max)();
- else
- *result = s < 0 ? -boost::long_long_type(unsigned_result - 1) - 1 : unsigned_result;
- }
- #endif
- #ifdef BOOST_HAS_INT128
- inline void eval_convert_to(unsigned __int128* result, const gmp_int& val)
- {
- if (mpz_sgn(val.data()) < 0)
- {
- BOOST_THROW_EXCEPTION(std::range_error("Conversion from negative integer to an unsigned type results in undefined behaviour"));
- }
- *result = 0;
- gmp_int t(val);
- unsigned parts = sizeof(unsigned __int128) / sizeof(unsigned long);
- for (unsigned i = 0; i < parts; ++i)
- {
- unsigned __int128 part = mpz_get_ui(t.data());
- if (i)
- *result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
- else
- *result = part;
- mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
- }
- }
- inline void eval_convert_to(__int128* result, const gmp_int& val)
- {
- int s = mpz_sgn(val.data());
- *result = 0;
- gmp_int t(val);
- unsigned parts = sizeof(unsigned __int128) / sizeof(unsigned long);
- unsigned __int128 unsigned_result = 0;
- for (unsigned i = 0; i < parts; ++i)
- {
- unsigned __int128 part = mpz_get_ui(t.data());
- if (i)
- unsigned_result |= part << (i * sizeof(unsigned long) * CHAR_BIT);
- else
- unsigned_result = part;
- mpz_tdiv_q_2exp(t.data(), t.data(), sizeof(unsigned long) * CHAR_BIT);
- }
- //
- // Overflow check:
- //
- constexpr const __int128 int128_max = static_cast<__int128>((static_cast<unsigned __int128>(1u) << 127) - 1);
- constexpr const __int128 int128_min = (static_cast<unsigned __int128>(1u) << 127);
- bool overflow = false;
- if (mpz_sgn(t.data()))
- {
- overflow = true;
- }
- if ((s > 0) && (unsigned_result > static_cast<unsigned __int128>(int128_max)))
- overflow = true;
- if ((s < 0) && (unsigned_result > 1u - static_cast<unsigned __int128>(int128_min + 1)))
- overflow = true;
- if (overflow)
- *result = s < 0 ? int128_min : int128_max;
- else
- *result = s < 0 ? -__int128(unsigned_result - 1) - 1 : unsigned_result;
- }
- #endif
- inline void eval_abs(gmp_int& result, const gmp_int& val)
- {
- mpz_abs(result.data(), val.data());
- }
- inline void eval_gcd(gmp_int& result, const gmp_int& a, const gmp_int& b)
- {
- mpz_gcd(result.data(), a.data(), b.data());
- }
- inline void eval_lcm(gmp_int& result, const gmp_int& a, const gmp_int& b)
- {
- mpz_lcm(result.data(), a.data(), b.data());
- }
- template <class I>
- 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)
- {
- mpz_gcd_ui(result.data(), a.data(), b);
- }
- template <class I>
- 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)
- {
- mpz_lcm_ui(result.data(), a.data(), b);
- }
- template <class I>
- 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)
- {
- mpz_gcd_ui(result.data(), a.data(), boost::multiprecision::detail::unsigned_abs(b));
- }
- template <class I>
- 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)
- {
- mpz_lcm_ui(result.data(), a.data(), boost::multiprecision::detail::unsigned_abs(b));
- }
- inline void eval_integer_sqrt(gmp_int& s, gmp_int& r, const gmp_int& x)
- {
- mpz_sqrtrem(s.data(), r.data(), x.data());
- }
- inline unsigned eval_lsb(const gmp_int& val)
- {
- int c = eval_get_sign(val);
- if (c == 0)
- {
- BOOST_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
- }
- if (c < 0)
- {
- BOOST_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
- }
- return static_cast<unsigned>(mpz_scan1(val.data(), 0));
- }
- inline unsigned eval_msb(const gmp_int& val)
- {
- int c = eval_get_sign(val);
- if (c == 0)
- {
- BOOST_THROW_EXCEPTION(std::domain_error("No bits were set in the operand."));
- }
- if (c < 0)
- {
- BOOST_THROW_EXCEPTION(std::domain_error("Testing individual bits in negative values is not supported - results are undefined."));
- }
- return static_cast<unsigned>(mpz_sizeinbase(val.data(), 2) - 1);
- }
- inline bool eval_bit_test(const gmp_int& val, unsigned index)
- {
- return mpz_tstbit(val.data(), index) ? true : false;
- }
- inline void eval_bit_set(gmp_int& val, unsigned index)
- {
- mpz_setbit(val.data(), index);
- }
- inline void eval_bit_unset(gmp_int& val, unsigned index)
- {
- mpz_clrbit(val.data(), index);
- }
- inline void eval_bit_flip(gmp_int& val, unsigned index)
- {
- mpz_combit(val.data(), index);
- }
- inline void eval_qr(const gmp_int& x, const gmp_int& y,
- gmp_int& q, gmp_int& r)
- {
- mpz_tdiv_qr(q.data(), r.data(), x.data(), y.data());
- }
- template <class Integer>
- inline typename std::enable_if<boost::multiprecision::detail::is_unsigned<Integer>::value, Integer>::type eval_integer_modulus(const gmp_int& x, Integer val)
- {
- #if defined(__MPIR_VERSION) && (__MPIR_VERSION >= 3)
- if ((sizeof(Integer) <= sizeof(mpir_ui)) || (val <= (std::numeric_limits<mpir_ui>::max)()))
- #else
- if ((sizeof(Integer) <= sizeof(long)) || (val <= (std::numeric_limits<unsigned long>::max)()))
- #endif
- {
- return static_cast<Integer>(mpz_tdiv_ui(x.data(), val));
- }
- else
- {
- return default_ops::eval_integer_modulus(x, val);
- }
- }
- template <class Integer>
- 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)
- {
- return eval_integer_modulus(x, boost::multiprecision::detail::unsigned_abs(val));
- }
- inline void eval_powm(gmp_int& result, const gmp_int& base, const gmp_int& p, const gmp_int& m)
- {
- if (eval_get_sign(p) < 0)
- {
- BOOST_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
- }
- mpz_powm(result.data(), base.data(), p.data(), m.data());
- }
- template <class Integer>
- inline typename std::enable_if<
- boost::multiprecision::detail::is_unsigned<Integer>::value && (sizeof(Integer) <= sizeof(unsigned long))>::type
- eval_powm(gmp_int& result, const gmp_int& base, Integer p, const gmp_int& m)
- {
- mpz_powm_ui(result.data(), base.data(), p, m.data());
- }
- template <class Integer>
- 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
- eval_powm(gmp_int& result, const gmp_int& base, Integer p, const gmp_int& m)
- {
- if (p < 0)
- {
- BOOST_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
- }
- mpz_powm_ui(result.data(), base.data(), p, m.data());
- }
- inline std::size_t hash_value(const gmp_int& val)
- {
- // We should really use mpz_limbs_read here, but that's unsupported on older versions:
- std::size_t result = 0;
- for (int i = 0; i < std::abs(val.data()[0]._mp_size); ++i)
- boost::hash_combine(result, val.data()[0]._mp_d[i]);
- boost::hash_combine(result, val.data()[0]._mp_size);
- return result;
- }
- struct gmp_rational;
- void eval_add(gmp_rational& t, const gmp_rational& o);
- struct gmp_rational
- {
- #ifdef BOOST_HAS_LONG_LONG
- using signed_types = std::tuple<long, boost::long_long_type> ;
- using unsigned_types = std::tuple<unsigned long, boost::ulong_long_type>;
- #else
- using signed_types = std::tuple<long> ;
- using unsigned_types = std::tuple<unsigned long>;
- #endif
- using float_types = std::tuple<double, long double>;
- gmp_rational()
- {
- mpq_init(this->m_data);
- }
- gmp_rational(const gmp_rational& o)
- {
- mpq_init(m_data);
- if (o.m_data[0]._mp_num._mp_d)
- mpq_set(m_data, o.m_data);
- }
- gmp_rational(const gmp_int& o)
- {
- mpq_init(m_data);
- mpq_set_z(m_data, o.data());
- }
- // rvalue copy
- gmp_rational(gmp_rational&& o) noexcept
- {
- m_data[0] = o.m_data[0];
- o.m_data[0]._mp_num._mp_d = 0;
- o.m_data[0]._mp_den._mp_d = 0;
- }
- gmp_rational(const mpq_t o)
- {
- mpq_init(m_data);
- mpq_set(m_data, o);
- }
- gmp_rational(const mpz_t o)
- {
- mpq_init(m_data);
- mpq_set_z(m_data, o);
- }
- gmp_rational& operator=(const gmp_rational& o)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- mpq_set(m_data, o.m_data);
- return *this;
- }
- // rvalue assign
- gmp_rational& operator=(gmp_rational&& o) noexcept
- {
- mpq_swap(m_data, o.m_data);
- return *this;
- }
- #ifdef BOOST_HAS_LONG_LONG
- #if defined(ULLONG_MAX) && (ULLONG_MAX == ULONG_MAX)
- gmp_rational& operator=(boost::ulong_long_type i)
- {
- *this = static_cast<unsigned long>(i);
- return *this;
- }
- #else
- gmp_rational& operator=(boost::ulong_long_type i)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- gmp_int zi;
- zi = i;
- mpq_set_z(m_data, zi.data());
- return *this;
- }
- gmp_rational& operator=(boost::long_long_type i)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- bool neg = i < 0;
- *this = boost::multiprecision::detail::unsigned_abs(i);
- if (neg)
- mpq_neg(m_data, m_data);
- return *this;
- }
- #endif
- #endif
- gmp_rational& operator=(unsigned long i)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- mpq_set_ui(m_data, i, 1);
- return *this;
- }
- gmp_rational& operator=(long i)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- mpq_set_si(m_data, i, 1);
- return *this;
- }
- gmp_rational& operator=(double d)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- mpq_set_d(m_data, d);
- return *this;
- }
- gmp_rational& operator=(long double a)
- {
- using default_ops::eval_add;
- using default_ops::eval_subtract;
- using std::floor;
- using std::frexp;
- using std::ldexp;
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- if (a == 0)
- {
- mpq_set_si(m_data, 0, 1);
- return *this;
- }
- if (a == 1)
- {
- mpq_set_si(m_data, 1, 1);
- return *this;
- }
- BOOST_ASSERT(!(boost::math::isinf)(a));
- BOOST_ASSERT(!(boost::math::isnan)(a));
- int e;
- long double f, term;
- mpq_set_ui(m_data, 0, 1);
- mpq_set_ui(m_data, 0u, 1);
- gmp_rational t;
- f = frexp(a, &e);
- constexpr const int shift = std::numeric_limits<int>::digits - 1;
- while (f)
- {
- // extract int sized bits from f:
- f = ldexp(f, shift);
- term = floor(f);
- e -= shift;
- mpq_mul_2exp(m_data, m_data, shift);
- t = static_cast<long>(term);
- eval_add(*this, t);
- f -= term;
- }
- if (e > 0)
- mpq_mul_2exp(m_data, m_data, e);
- else if (e < 0)
- mpq_div_2exp(m_data, m_data, -e);
- return *this;
- }
- gmp_rational& operator=(const char* s)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- if (0 != mpq_set_str(m_data, s, 10))
- BOOST_THROW_EXCEPTION(std::runtime_error(std::string("The string \"") + s + std::string("\"could not be interpreted as a valid rational number.")));
- return *this;
- }
- gmp_rational& operator=(const gmp_int& o)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- mpq_set_z(m_data, o.data());
- return *this;
- }
- gmp_rational& operator=(const mpq_t o)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- mpq_set(m_data, o);
- return *this;
- }
- gmp_rational& operator=(const mpz_t o)
- {
- if (m_data[0]._mp_den._mp_d == 0)
- mpq_init(m_data);
- mpq_set_z(m_data, o);
- return *this;
- }
- void swap(gmp_rational& o)
- {
- mpq_swap(m_data, o.m_data);
- }
- std::string str(std::streamsize /*digits*/, std::ios_base::fmtflags /*f*/) const
- {
- BOOST_ASSERT(m_data[0]._mp_num._mp_d);
- // TODO make a better job of this including handling of f!!
- void* (*alloc_func_ptr)(size_t);
- void* (*realloc_func_ptr)(void*, size_t, size_t);
- void (*free_func_ptr)(void*, size_t);
- const char* ps = mpq_get_str(0, 10, m_data);
- std::string s = ps;
- mp_get_memory_functions(&alloc_func_ptr, &realloc_func_ptr, &free_func_ptr);
- (*free_func_ptr)((void*)ps, std::strlen(ps) + 1);
- return s;
- }
- ~gmp_rational()
- {
- if (m_data[0]._mp_num._mp_d || m_data[0]._mp_den._mp_d)
- mpq_clear(m_data);
- }
- void negate()
- {
- BOOST_ASSERT(m_data[0]._mp_num._mp_d);
- mpq_neg(m_data, m_data);
- }
- int compare(const gmp_rational& o) const
- {
- BOOST_ASSERT(m_data[0]._mp_num._mp_d && o.m_data[0]._mp_num._mp_d);
- return mpq_cmp(m_data, o.m_data);
- }
- template <class V>
- int compare(V v) const
- {
- gmp_rational d;
- d = v;
- return compare(d);
- }
- int compare(unsigned long v) const
- {
- BOOST_ASSERT(m_data[0]._mp_num._mp_d);
- return mpq_cmp_ui(m_data, v, 1);
- }
- int compare(long v) const
- {
- BOOST_ASSERT(m_data[0]._mp_num._mp_d);
- return mpq_cmp_si(m_data, v, 1);
- }
- mpq_t& data()
- {
- BOOST_ASSERT(m_data[0]._mp_num._mp_d);
- return m_data;
- }
- const mpq_t& data() const
- {
- BOOST_ASSERT(m_data[0]._mp_num._mp_d);
- return m_data;
- }
- protected:
- mpq_t m_data;
- };
- inline bool eval_is_zero(const gmp_rational& val)
- {
- return mpq_sgn(val.data()) == 0;
- }
- template <class T>
- inline bool eval_eq(gmp_rational& a, const T& b)
- {
- return a.compare(b) == 0;
- }
- template <class T>
- inline bool eval_lt(gmp_rational& a, const T& b)
- {
- return a.compare(b) < 0;
- }
- template <class T>
- inline bool eval_gt(gmp_rational& a, const T& b)
- {
- return a.compare(b) > 0;
- }
- inline void eval_add(gmp_rational& t, const gmp_rational& o)
- {
- mpq_add(t.data(), t.data(), o.data());
- }
- inline void eval_subtract(gmp_rational& t, const gmp_rational& o)
- {
- mpq_sub(t.data(), t.data(), o.data());
- }
- inline void eval_multiply(gmp_rational& t, const gmp_rational& o)
- {
- mpq_mul(t.data(), t.data(), o.data());
- }
- inline void eval_divide(gmp_rational& t, const gmp_rational& o)
- {
- if (eval_is_zero(o))
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpq_div(t.data(), t.data(), o.data());
- }
- inline void eval_add(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
- {
- mpq_add(t.data(), p.data(), o.data());
- }
- inline void eval_subtract(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
- {
- mpq_sub(t.data(), p.data(), o.data());
- }
- inline void eval_multiply(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
- {
- mpq_mul(t.data(), p.data(), o.data());
- }
- inline void eval_divide(gmp_rational& t, const gmp_rational& p, const gmp_rational& o)
- {
- if (eval_is_zero(o))
- BOOST_THROW_EXCEPTION(std::overflow_error("Division by zero."));
- mpq_div(t.data(), p.data(), o.data());
- }
- inline int eval_get_sign(const gmp_rational& val)
- {
- return mpq_sgn(val.data());
- }
- template <class R>
- inline typename std::enable_if<number_category<R>::value == number_kind_floating_point>::type eval_convert_to(R* result, const gmp_rational& backend)
- {
- //
- // The generic conversion is as good as anything we can write here:
- //
- // This does not round correctly:
- //
- //*result = mpq_get_d(val.data());
- //
- // This does:
- //
- ::boost::multiprecision::detail::generic_convert_rational_to_float(*result, backend);
- }
- #ifdef BOOST_HAS_FLOAT128
- inline void eval_convert_to(__float128* result, const gmp_rational& val)
- {
- using default_ops::eval_convert_to;
- gmp_int n, d;
- __float128 fn, fd;
- mpz_set(n.data(), mpq_numref(val.data()));
- mpz_set(d.data(), mpq_denref(val.data()));
- eval_convert_to(&fn, n);
- eval_convert_to(&fd, d);
-
- *result = fn / fd;
- }
- #endif
- inline void eval_convert_to(long* result, const gmp_rational& val)
- {
- double r;
- eval_convert_to(&r, val);
- *result = static_cast<long>(r);
- }
- inline void eval_convert_to(unsigned long* result, const gmp_rational& val)
- {
- double r;
- eval_convert_to(&r, val);
- *result = static_cast<long>(r);
- }
- inline void eval_abs(gmp_rational& result, const gmp_rational& val)
- {
- mpq_abs(result.data(), val.data());
- }
- inline void assign_components(gmp_rational& result, unsigned long v1, unsigned long v2)
- {
- mpq_set_ui(result.data(), v1, v2);
- mpq_canonicalize(result.data());
- }
- inline void assign_components(gmp_rational& result, long v1, long v2)
- {
- mpq_set_si(result.data(), v1, v2);
- mpq_canonicalize(result.data());
- }
- inline void assign_components(gmp_rational& result, gmp_int const& v1, gmp_int const& v2)
- {
- mpz_set(mpq_numref(result.data()), v1.data());
- mpz_set(mpq_denref(result.data()), v2.data());
- mpq_canonicalize(result.data());
- }
- inline std::size_t hash_value(const gmp_rational& val)
- {
- std::size_t result = 0;
- for (int i = 0; i < std::abs(val.data()[0]._mp_num._mp_size); ++i)
- boost::hash_combine(result, val.data()[0]._mp_num._mp_d[i]);
- for (int i = 0; i < std::abs(val.data()[0]._mp_den._mp_size); ++i)
- boost::hash_combine(result, val.data()[0]._mp_den._mp_d[i]);
- boost::hash_combine(result, val.data()[0]._mp_num._mp_size);
- return result;
- }
- //
- // Some member functions that are dependent upon previous code go here:
- //
- template <unsigned Digits10>
- template <unsigned D>
- inline gmp_float<Digits10>::gmp_float(const gmp_float<D>& o, typename std::enable_if<D <= Digits10>::type*)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
- mpf_set(this->m_data, o.data());
- }
- template <unsigned Digits10>
- template <unsigned D>
- inline gmp_float<Digits10>::gmp_float(const gmp_float<D>& o, typename std::enable_if< !(D <= Digits10)>::type*)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
- mpf_set(this->m_data, o.data());
- }
- template <unsigned Digits10>
- inline gmp_float<Digits10>::gmp_float(const gmp_int& o)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
- mpf_set_z(this->data(), o.data());
- }
- template <unsigned Digits10>
- inline gmp_float<Digits10>::gmp_float(const gmp_rational& o)
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
- mpf_set_q(this->data(), o.data());
- }
- template <unsigned Digits10>
- template <unsigned D>
- inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_float<D>& o)
- {
- if (this->m_data[0]._mp_d == 0)
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
- mpf_set(this->m_data, o.data());
- return *this;
- }
- template <unsigned Digits10>
- inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_int& o)
- {
- if (this->m_data[0]._mp_d == 0)
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
- mpf_set_z(this->data(), o.data());
- return *this;
- }
- template <unsigned Digits10>
- inline gmp_float<Digits10>& gmp_float<Digits10>::operator=(const gmp_rational& o)
- {
- if (this->m_data[0]._mp_d == 0)
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(Digits10 ? Digits10 : (unsigned)this->get_default_precision()));
- mpf_set_q(this->data(), o.data());
- return *this;
- }
- inline gmp_float<0>::gmp_float(const gmp_int& o) : requested_precision(get_default_precision())
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- mpf_set_z(this->data(), o.data());
- }
- inline gmp_float<0>::gmp_float(const gmp_rational& o) : requested_precision(get_default_precision())
- {
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- mpf_set_q(this->data(), o.data());
- }
- inline gmp_float<0>& gmp_float<0>::operator=(const gmp_int& o)
- {
- if (this->m_data[0]._mp_d == 0)
- {
- requested_precision = this->get_default_precision();
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- }
- mpf_set_z(this->data(), o.data());
- return *this;
- }
- inline gmp_float<0>& gmp_float<0>::operator=(const gmp_rational& o)
- {
- if (this->m_data[0]._mp_d == 0)
- {
- requested_precision = this->get_default_precision();
- mpf_init2(this->m_data, multiprecision::detail::digits10_2_2(requested_precision));
- }
- mpf_set_q(this->data(), o.data());
- return *this;
- }
- inline gmp_int::gmp_int(const gmp_rational& o)
- {
- mpz_init(this->m_data);
- mpz_set_q(this->m_data, o.data());
- }
- inline gmp_int& gmp_int::operator=(const gmp_rational& o)
- {
- if (this->m_data[0]._mp_d == 0)
- mpz_init(this->m_data);
- mpz_set_q(this->m_data, o.data());
- return *this;
- }
- } //namespace backends
- using boost::multiprecision::backends::gmp_float;
- using boost::multiprecision::backends::gmp_int;
- using boost::multiprecision::backends::gmp_rational;
- template <expression_template_option ExpressionTemplates>
- struct component_type<number<gmp_rational, ExpressionTemplates> >
- {
- using type = number<gmp_int, ExpressionTemplates>;
- };
- template <expression_template_option ET>
- inline number<gmp_int, ET> numerator(const number<gmp_rational, ET>& val)
- {
- number<gmp_int, ET> result;
- mpz_set(result.backend().data(), (mpq_numref(val.backend().data())));
- return result;
- }
- template <expression_template_option ET>
- inline number<gmp_int, ET> denominator(const number<gmp_rational, ET>& val)
- {
- number<gmp_int, ET> result;
- mpz_set(result.backend().data(), (mpq_denref(val.backend().data())));
- return result;
- }
- namespace detail {
- template <>
- struct digits2<number<gmp_float<0>, et_on> >
- {
- static long value()
- {
- return multiprecision::detail::digits10_2_2(gmp_float<0>::default_precision());
- }
- };
- template <>
- struct digits2<number<gmp_float<0>, et_off> >
- {
- static long value()
- {
- return multiprecision::detail::digits10_2_2(gmp_float<0>::default_precision());
- }
- };
- template <>
- struct digits2<number<debug_adaptor<gmp_float<0> >, et_on> >
- {
- static long value()
- {
- return multiprecision::detail::digits10_2_2(gmp_float<0>::default_precision());
- }
- };
- template <>
- struct digits2<number<debug_adaptor<gmp_float<0> >, et_off> >
- {
- static long value()
- {
- return multiprecision::detail::digits10_2_2(gmp_float<0>::default_precision());
- }
- };
- template <unsigned Digits10>
- struct transcendental_reduction_type<boost::multiprecision::backends::gmp_float<Digits10> >
- {
- //
- // The type used for trigonometric reduction needs 3 times the precision of the base type.
- // This is double the precision of the original type, plus the largest exponent supported.
- // As a practical measure the largest argument supported is 1/eps, as supporting larger
- // arguments requires the division of argument by PI/2 to also be done at higher precision,
- // otherwise the result (an integer) can not be represented exactly.
- //
- // See ARGUMENT REDUCTION FOR HUGE ARGUMENTS. K C Ng.
- //
- using type = boost::multiprecision::backends::gmp_float<Digits10 * 3>;
- };
- } // namespace detail
- template <>
- struct number_category<detail::canonical<mpz_t, gmp_int>::type> : public std::integral_constant<int, number_kind_integer>
- {};
- template <>
- struct number_category<detail::canonical<mpq_t, gmp_rational>::type> : public std::integral_constant<int, number_kind_rational>
- {};
- template <>
- struct number_category<detail::canonical<mpf_t, gmp_float<0> >::type> : public std::integral_constant<int, number_kind_floating_point>
- {};
- namespace detail {
- template <>
- struct is_variable_precision<backends::gmp_float<0> > : public std::integral_constant<bool, true>
- {};
- } // namespace detail
- using mpf_float_50 = number<gmp_float<50> > ;
- using mpf_float_100 = number<gmp_float<100> > ;
- using mpf_float_500 = number<gmp_float<500> > ;
- using mpf_float_1000 = number<gmp_float<1000> >;
- using mpf_float = number<gmp_float<0> > ;
- using mpz_int = number<gmp_int> ;
- using mpq_rational = number<gmp_rational> ;
- } // namespace multiprecision
- namespace math { namespace tools {
- inline void set_output_precision(const boost::multiprecision::mpf_float& val, std::ostream& os)
- {
- os << std::setprecision(val.precision());
- }
- template <>
- inline int digits<boost::multiprecision::mpf_float>()
- #ifdef BOOST_MATH_NOEXCEPT
- noexcept
- #endif
- {
- return multiprecision::detail::digits10_2_2(boost::multiprecision::mpf_float::default_precision());
- }
- template <>
- inline int digits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
- #ifdef BOOST_MATH_NOEXCEPT
- noexcept
- #endif
- {
- return multiprecision::detail::digits10_2_2(boost::multiprecision::mpf_float::default_precision());
- }
- template <>
- inline boost::multiprecision::mpf_float
- max_value<boost::multiprecision::mpf_float>()
- {
- boost::multiprecision::mpf_float result(0.5);
- mpf_mul_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
- return result;
- }
- template <>
- inline boost::multiprecision::mpf_float
- min_value<boost::multiprecision::mpf_float>()
- {
- boost::multiprecision::mpf_float result(0.5);
- mpf_div_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
- return result;
- }
- template <>
- inline boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off>
- max_value<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
- {
- boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> result(0.5);
- mpf_mul_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
- return result;
- }
- template <>
- inline boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off>
- min_value<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> >()
- {
- boost::multiprecision::number<boost::multiprecision::gmp_float<0>, boost::multiprecision::et_off> result(0.5);
- mpf_div_2exp(result.backend().data(), result.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
- return result;
- }
- template <>
- inline int digits<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
- #ifdef BOOST_MATH_NOEXCEPT
- noexcept
- #endif
- {
- return multiprecision::detail::digits10_2_2(boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >::default_precision());
- }
- template <>
- inline int digits<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
- #ifdef BOOST_MATH_NOEXCEPT
- noexcept
- #endif
- {
- return multiprecision::detail::digits10_2_2(boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >::default_precision());
- }
- template <>
- inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >
- max_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
- {
- return max_value<boost::multiprecision::mpf_float>().backend();
- }
- template <>
- inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> >
- min_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::mpf_float::backend_type> > >()
- {
- return min_value<boost::multiprecision::mpf_float>().backend();
- }
- template <>
- inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off>
- max_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
- {
- return max_value<boost::multiprecision::mpf_float>().backend();
- }
- template <>
- inline boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off>
- min_value<boost::multiprecision::number<boost::multiprecision::debug_adaptor<boost::multiprecision::gmp_float<0> >, boost::multiprecision::et_off> >()
- {
- return min_value<boost::multiprecision::mpf_float>().backend();
- }
- }} // namespace math::tools
- } // namespace boost
- namespace std {
- //
- // numeric_limits [partial] specializations for the types declared in this header:
- //
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >
- {
- using number_type = boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates>;
- public:
- static constexpr bool is_specialized = true;
- //
- // min and max values chosen so as to not cause segfaults when calling
- // mpf_get_str on 64-bit Linux builds. Possibly we could use larger
- // exponent values elsewhere.
- //
- static number_type(min)()
- {
- static std::pair<bool, number_type> value;
- if (!value.first)
- {
- value.first = true;
- value.second = 1;
- mpf_div_2exp(value.second.backend().data(), value.second.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
- }
- return value.second;
- }
- static number_type(max)()
- {
- static std::pair<bool, number_type> value;
- if (!value.first)
- {
- value.first = true;
- value.second = 1;
- mpf_mul_2exp(value.second.backend().data(), value.second.backend().data(), (std::numeric_limits<mp_exp_t>::max)() / 64 + 1);
- }
- return value.second;
- }
- static constexpr number_type lowest()
- {
- return -(max)();
- }
- static constexpr int digits = static_cast<int>((Digits10 * 1000L) / 301L + ((Digits10 * 1000L) % 301L ? 2 : 1));
- static constexpr int digits10 = Digits10;
- // Have to allow for a possible extra limb inside the gmp data structure:
- static constexpr int max_digits10 = Digits10 + 3 + ((GMP_LIMB_BITS * 301L) / 1000L);
- static constexpr bool is_signed = true;
- static constexpr bool is_integer = false;
- static constexpr bool is_exact = false;
- static constexpr int radix = 2;
- static number_type epsilon()
- {
- static std::pair<bool, number_type> value;
- if (!value.first)
- {
- value.first = true;
- value.second = 1;
- mpf_div_2exp(value.second.backend().data(), value.second.backend().data(), std::numeric_limits<number_type>::digits - 1);
- }
- return value.second;
- }
- // What value should this be????
- static number_type round_error()
- {
- // returns epsilon/2
- static std::pair<bool, number_type> value;
- if (!value.first)
- {
- value.first = true;
- value.second = 1;
- }
- return value.second;
- }
- static constexpr long min_exponent = LONG_MIN;
- static constexpr long min_exponent10 = (LONG_MIN / 1000) * 301L;
- static constexpr long max_exponent = LONG_MAX;
- static constexpr long max_exponent10 = (LONG_MAX / 1000) * 301L;
- static constexpr bool has_infinity = false;
- static constexpr bool has_quiet_NaN = false;
- static constexpr bool has_signaling_NaN = false;
- static constexpr float_denorm_style has_denorm = denorm_absent;
- static constexpr bool has_denorm_loss = false;
- static constexpr number_type infinity() { return number_type(); }
- static constexpr number_type quiet_NaN() { return number_type(); }
- static constexpr number_type signaling_NaN() { return number_type(); }
- static constexpr number_type denorm_min() { return number_type(); }
- static constexpr bool is_iec559 = false;
- static constexpr bool is_bounded = true;
- static constexpr bool is_modulo = false;
- static constexpr bool traps = true;
- static constexpr bool tinyness_before = false;
- static constexpr float_round_style round_style = round_indeterminate;
- };
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::digits;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::digits10;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_digits10;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_signed;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_integer;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_exact;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::radix;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::min_exponent;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::min_exponent10;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_exponent;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr long numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::max_exponent10;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_infinity;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_quiet_NaN;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_signaling_NaN;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_denorm;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::has_denorm_loss;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_iec559;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_bounded;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::is_modulo;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::traps;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::tinyness_before;
- template <unsigned Digits10, boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<Digits10>, ExpressionTemplates> >::round_style;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >
- {
- using number_type = boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates>;
- public:
- static constexpr bool is_specialized = false;
- static number_type(min)() { return number_type(); }
- static number_type(max)() { return number_type(); }
- static number_type lowest() { return number_type(); }
- static constexpr int digits = 0;
- static constexpr int digits10 = 0;
- static constexpr int max_digits10 = 0;
- static constexpr bool is_signed = false;
- static constexpr bool is_integer = false;
- static constexpr bool is_exact = false;
- static constexpr int radix = 0;
- static number_type epsilon() { return number_type(); }
- static number_type round_error() { return number_type(); }
- static constexpr int min_exponent = 0;
- static constexpr int min_exponent10 = 0;
- static constexpr int max_exponent = 0;
- static constexpr int max_exponent10 = 0;
- static constexpr bool has_infinity = false;
- static constexpr bool has_quiet_NaN = false;
- static constexpr bool has_signaling_NaN = false;
- static constexpr float_denorm_style has_denorm = denorm_absent;
- static constexpr bool has_denorm_loss = false;
- static number_type infinity() { return number_type(); }
- static number_type quiet_NaN() { return number_type(); }
- static number_type signaling_NaN() { return number_type(); }
- static number_type denorm_min() { return number_type(); }
- static constexpr bool is_iec559 = false;
- static constexpr bool is_bounded = false;
- static constexpr bool is_modulo = false;
- static constexpr bool traps = false;
- static constexpr bool tinyness_before = false;
- static constexpr float_round_style round_style = round_indeterminate;
- };
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::digits;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::digits10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_digits10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_signed;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_integer;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_exact;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::radix;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::min_exponent;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::min_exponent10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_exponent;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::max_exponent10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_infinity;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_quiet_NaN;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_signaling_NaN;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_denorm;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::has_denorm_loss;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_iec559;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_bounded;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::is_modulo;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::traps;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::tinyness_before;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_float<0>, ExpressionTemplates> >::round_style;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >
- {
- using number_type = boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates>;
- public:
- static constexpr bool is_specialized = true;
- //
- // Largest and smallest numbers are bounded only by available memory, set
- // to zero:
- //
- static number_type(min)()
- {
- return number_type();
- }
- static number_type(max)()
- {
- return number_type();
- }
- static number_type lowest() { return (min)(); }
- static constexpr int digits = INT_MAX;
- static constexpr int digits10 = (INT_MAX / 1000) * 301L;
- static constexpr int max_digits10 = digits10 + 3;
- static constexpr bool is_signed = true;
- static constexpr bool is_integer = true;
- static constexpr bool is_exact = true;
- static constexpr int radix = 2;
- static number_type epsilon() { return number_type(); }
- static number_type round_error() { return number_type(); }
- static constexpr int min_exponent = 0;
- static constexpr int min_exponent10 = 0;
- static constexpr int max_exponent = 0;
- static constexpr int max_exponent10 = 0;
- static constexpr bool has_infinity = false;
- static constexpr bool has_quiet_NaN = false;
- static constexpr bool has_signaling_NaN = false;
- static constexpr float_denorm_style has_denorm = denorm_absent;
- static constexpr bool has_denorm_loss = false;
- static number_type infinity() { return number_type(); }
- static number_type quiet_NaN() { return number_type(); }
- static number_type signaling_NaN() { return number_type(); }
- static number_type denorm_min() { return number_type(); }
- static constexpr bool is_iec559 = false;
- static constexpr bool is_bounded = false;
- static constexpr bool is_modulo = false;
- static constexpr bool traps = false;
- static constexpr bool tinyness_before = false;
- static constexpr float_round_style round_style = round_toward_zero;
- };
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::digits;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::digits10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_digits10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_signed;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_integer;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_exact;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::radix;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::min_exponent;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::min_exponent10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_exponent;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::max_exponent10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_infinity;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_quiet_NaN;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_signaling_NaN;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_denorm;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::has_denorm_loss;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_iec559;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_bounded;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::is_modulo;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::traps;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::tinyness_before;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_int, ExpressionTemplates> >::round_style;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- class numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >
- {
- using number_type = boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates>;
- public:
- static constexpr bool is_specialized = true;
- //
- // Largest and smallest numbers are bounded only by available memory, set
- // to zero:
- //
- static number_type(min)()
- {
- return number_type();
- }
- static number_type(max)()
- {
- return number_type();
- }
- static number_type lowest() { return (min)(); }
- // Digits are unbounded, use zero for now:
- static constexpr int digits = INT_MAX;
- static constexpr int digits10 = (INT_MAX / 1000) * 301L;
- static constexpr int max_digits10 = digits10 + 3;
- static constexpr bool is_signed = true;
- static constexpr bool is_integer = false;
- static constexpr bool is_exact = true;
- static constexpr int radix = 2;
- static number_type epsilon() { return number_type(); }
- static number_type round_error() { return number_type(); }
- static constexpr int min_exponent = 0;
- static constexpr int min_exponent10 = 0;
- static constexpr int max_exponent = 0;
- static constexpr int max_exponent10 = 0;
- static constexpr bool has_infinity = false;
- static constexpr bool has_quiet_NaN = false;
- static constexpr bool has_signaling_NaN = false;
- static constexpr float_denorm_style has_denorm = denorm_absent;
- static constexpr bool has_denorm_loss = false;
- static number_type infinity() { return number_type(); }
- static number_type quiet_NaN() { return number_type(); }
- static number_type signaling_NaN() { return number_type(); }
- static number_type denorm_min() { return number_type(); }
- static constexpr bool is_iec559 = false;
- static constexpr bool is_bounded = false;
- static constexpr bool is_modulo = false;
- static constexpr bool traps = false;
- static constexpr bool tinyness_before = false;
- static constexpr float_round_style round_style = round_toward_zero;
- };
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::digits;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::digits10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_digits10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_signed;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_integer;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_exact;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::radix;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::min_exponent;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::min_exponent10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_exponent;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::max_exponent10;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_infinity;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_quiet_NaN;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_signaling_NaN;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_denorm;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::has_denorm_loss;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_iec559;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_bounded;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::is_modulo;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::traps;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::tinyness_before;
- template <boost::multiprecision::expression_template_option ExpressionTemplates>
- constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::gmp_rational, ExpressionTemplates> >::round_style;
- #ifdef BOOST_MSVC
- #pragma warning(pop)
- #endif
- } // namespace std
- #endif
|