cpp_dec_float.hpp 164 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright Christopher Kormanyos 2002 - 2021.
  3. // Copyright 2011 -2021 John Maddock. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // This work is based on an earlier work:
  8. // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations",
  9. // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469
  10. //
  11. // There are some "noexcept" specifications on the functions in this file.
  12. // Unlike in pre-C++11 versions, compilers can now detect noexcept misuse
  13. // at compile time, allowing for simple use of it here.
  14. //
  15. #ifndef BOOST_MP_CPP_DEC_FLOAT_HPP
  16. #define BOOST_MP_CPP_DEC_FLOAT_HPP
  17. #include <cmath>
  18. #include <cstdint>
  19. #include <cstdlib>
  20. #include <algorithm>
  21. #include <array>
  22. #include <initializer_list>
  23. #include <iomanip>
  24. #include <string>
  25. #include <limits>
  26. #include <stdexcept>
  27. #include <sstream>
  28. #include <locale>
  29. #include <ios>
  30. #include <boost/multiprecision/detail/standalone_config.hpp>
  31. #include <boost/multiprecision/number.hpp>
  32. #include <boost/multiprecision/detail/fpclassify.hpp>
  33. #include <boost/multiprecision/detail/dynamic_array.hpp>
  34. #include <boost/multiprecision/detail/hash.hpp>
  35. #include <boost/multiprecision/detail/float128_functions.hpp>
  36. #include <boost/multiprecision/detail/itos.hpp>
  37. #include <boost/multiprecision/detail/static_array.hpp>
  38. #include <boost/multiprecision/detail/tables.hpp>
  39. #include <boost/multiprecision/detail/no_exceptions_support.hpp>
  40. #include <boost/multiprecision/detail/assert.hpp>
  41. #ifdef BOOST_MP_MATH_AVAILABLE
  42. //
  43. // Headers required for Boost.Math integration:
  44. //
  45. #include <boost/math/policies/policy.hpp>
  46. //
  47. // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
  48. //
  49. #include <boost/math/special_functions/acosh.hpp>
  50. #include <boost/math/special_functions/asinh.hpp>
  51. #include <boost/math/special_functions/atanh.hpp>
  52. #include <boost/math/special_functions/cbrt.hpp>
  53. #include <boost/math/special_functions/expm1.hpp>
  54. #include <boost/math/special_functions/gamma.hpp>
  55. #endif
  56. #ifdef BOOST_MSVC
  57. #pragma warning(push)
  58. #pragma warning(disable : 6326) // comparison of two constants
  59. #endif
  60. namespace boost {
  61. namespace multiprecision {
  62. template <unsigned Digits10, class ExponentType, class Allocator>
  63. struct number_category<backends::cpp_dec_float<Digits10, ExponentType, Allocator> > : public std::integral_constant<int, number_kind_floating_point>
  64. {};
  65. namespace backends {
  66. template <unsigned Digits10, class ExponentType, class Allocator>
  67. class cpp_dec_float
  68. {
  69. private:
  70. // Perform some static sanity checks.
  71. static_assert(boost::multiprecision::detail::is_signed<ExponentType>::value,
  72. "ExponentType must be a signed built in integer type.");
  73. static_assert(sizeof(ExponentType) > 1,
  74. "ExponentType is too small.");
  75. static_assert(Digits10 < UINT32_C(0x80000000),
  76. "Digits10 exceeds the maximum.");
  77. // Private class-local constants.
  78. static constexpr std::int32_t cpp_dec_float_digits10_limit_lo = INT32_C(9);
  79. static constexpr std::int32_t cpp_dec_float_digits10_limit_hi = static_cast<std::int32_t>((std::numeric_limits<std::int32_t>::max)() - 100);
  80. static constexpr std::int32_t cpp_dec_float_elem_digits10 = INT32_C(8);
  81. static constexpr std::int32_t cpp_dec_float_elem_mask = INT32_C(100000000);
  82. static constexpr std::int32_t cpp_dec_float_elems_for_kara = static_cast<std::int32_t>(128 + 1);
  83. public:
  84. using signed_types = std::tuple<long long> ;
  85. using unsigned_types = std::tuple<unsigned long long>;
  86. using float_types = std::tuple<double, long double>;
  87. using exponent_type = ExponentType;
  88. // Public class-local constants.
  89. static constexpr std::int32_t cpp_dec_float_radix = INT32_C(10);
  90. static constexpr std::int32_t cpp_dec_float_digits10 = ((static_cast<std::int32_t>(Digits10) < cpp_dec_float_digits10_limit_lo) ? cpp_dec_float_digits10_limit_lo : ((static_cast<std::int32_t>(Digits10) > cpp_dec_float_digits10_limit_hi) ? cpp_dec_float_digits10_limit_hi : static_cast<std::int32_t>(Digits10)));
  91. static constexpr exponent_type cpp_dec_float_max_exp10 = (static_cast<exponent_type>(1) << (std::numeric_limits<exponent_type>::digits - 5));
  92. static constexpr exponent_type cpp_dec_float_min_exp10 = -cpp_dec_float_max_exp10;
  93. static constexpr exponent_type cpp_dec_float_max_exp = cpp_dec_float_max_exp10;
  94. static constexpr exponent_type cpp_dec_float_min_exp = cpp_dec_float_min_exp10;
  95. static_assert(cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp10 == -cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp10, "Failed exponent range check");
  96. static_assert(0 == cpp_dec_float_max_exp10 % cpp_dec_float_elem_digits10, "Failed digit sanity check");
  97. private:
  98. // There are three guard limbs.
  99. // 1) The first limb has 'play' from 1...8 decimal digits.
  100. // 2) The last limb also has 'play' from 1...8 decimal digits.
  101. // 3) One limb can get lost when justifying after multiply.
  102. static constexpr std::int32_t cpp_dec_float_elem_number = static_cast<std::int32_t>(((Digits10 / cpp_dec_float_elem_digits10) + (((Digits10 % cpp_dec_float_elem_digits10) != 0) ? 1 : 0)) + 3);
  103. public:
  104. static constexpr std::int32_t cpp_dec_float_max_digits10 = static_cast<std::int32_t>(cpp_dec_float_elem_number * cpp_dec_float_elem_digits10);
  105. private:
  106. using array_type =
  107. typename std::conditional<std::is_void<Allocator>::value,
  108. detail::static_array <std::uint32_t, static_cast<std::uint32_t>(cpp_dec_float_elem_number)>,
  109. detail::dynamic_array<std::uint32_t, static_cast<std::uint32_t>(cpp_dec_float_elem_number), Allocator> >::type;
  110. typedef enum enum_fpclass_type
  111. {
  112. cpp_dec_float_finite,
  113. cpp_dec_float_inf,
  114. cpp_dec_float_NaN
  115. } fpclass_type;
  116. array_type data;
  117. exponent_type exp;
  118. bool neg;
  119. fpclass_type fpclass;
  120. std::int32_t prec_elem;
  121. // Private constructor from the floating-point class type.
  122. explicit cpp_dec_float(fpclass_type c) : data(),
  123. exp(static_cast<exponent_type>(0)),
  124. neg(false),
  125. fpclass(c),
  126. prec_elem(cpp_dec_float_elem_number) {}
  127. // Constructor from an initializer_list, an optional
  128. // (value-aligned) exponent and a Boolean sign.
  129. static cpp_dec_float from_lst(std::initializer_list<std::uint32_t> lst,
  130. const exponent_type e = 0,
  131. const bool n = false)
  132. {
  133. cpp_dec_float a;
  134. a.data = array_type(lst);
  135. a.exp = e;
  136. a.neg = n;
  137. a.fpclass = cpp_dec_float_finite;
  138. a.prec_elem = cpp_dec_float_elem_number;
  139. return a;
  140. }
  141. public:
  142. // Public Constructors
  143. cpp_dec_float() noexcept(noexcept(array_type())) : data(),
  144. exp(static_cast<exponent_type>(0)),
  145. neg(false),
  146. fpclass(cpp_dec_float_finite),
  147. prec_elem(cpp_dec_float_elem_number) {}
  148. cpp_dec_float(const char* s) : data(),
  149. exp(static_cast<exponent_type>(0)),
  150. neg(false),
  151. fpclass(cpp_dec_float_finite),
  152. prec_elem(cpp_dec_float_elem_number)
  153. {
  154. *this = s;
  155. }
  156. template <class I>
  157. cpp_dec_float(I i,
  158. typename std::enable_if<boost::multiprecision::detail::is_unsigned<I>::value && (sizeof(I) <= sizeof(long long))>::type* = nullptr)
  159. : data(),
  160. exp(static_cast<exponent_type>(0)),
  161. neg(false),
  162. fpclass(cpp_dec_float_finite),
  163. prec_elem(cpp_dec_float_elem_number)
  164. {
  165. from_unsigned_long_long(i);
  166. }
  167. template <class I>
  168. cpp_dec_float(I i,
  169. typename std::enable_if<( boost::multiprecision::detail::is_signed<I>::value
  170. && boost::multiprecision::detail::is_integral<I>::value
  171. && (sizeof(I) <= sizeof(long long)))>::type* = nullptr)
  172. : data(),
  173. exp(static_cast<exponent_type>(0)),
  174. neg(false),
  175. fpclass(cpp_dec_float_finite),
  176. prec_elem(cpp_dec_float_elem_number)
  177. {
  178. if (i < 0)
  179. {
  180. from_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(i));
  181. negate();
  182. }
  183. else
  184. from_unsigned_long_long(static_cast<unsigned long long>(i));
  185. }
  186. cpp_dec_float(const cpp_dec_float& f) noexcept(noexcept(array_type(std::declval<const array_type&>())))
  187. : data(f.data),
  188. exp(f.exp),
  189. neg(f.neg),
  190. fpclass(f.fpclass),
  191. prec_elem(f.prec_elem) {}
  192. template <unsigned D, class ET, class A>
  193. cpp_dec_float(const cpp_dec_float<D, ET, A>& f, typename std::enable_if<D <= Digits10>::type* = nullptr)
  194. : data(),
  195. exp(f.exp),
  196. neg(f.neg),
  197. fpclass(static_cast<fpclass_type>(static_cast<int>(f.fpclass))),
  198. prec_elem(cpp_dec_float_elem_number)
  199. {
  200. std::copy(f.data.begin(), f.data.begin() + f.prec_elem, data.begin());
  201. }
  202. template <unsigned D, class ET, class A>
  203. explicit cpp_dec_float(const cpp_dec_float<D, ET, A>& f, typename std::enable_if< !(D <= Digits10)>::type* = nullptr)
  204. : data(),
  205. exp(f.exp),
  206. neg(f.neg),
  207. fpclass(static_cast<fpclass_type>(static_cast<int>(f.fpclass))),
  208. prec_elem(cpp_dec_float_elem_number)
  209. {
  210. // TODO: this doesn't round!
  211. std::copy(f.data.begin(), f.data.begin() + prec_elem, data.begin());
  212. }
  213. template <class F>
  214. cpp_dec_float(const F val, typename std::enable_if<std::is_floating_point<F>::value
  215. >::type* = nullptr) : data(),
  216. exp(static_cast<exponent_type>(0)),
  217. neg(false),
  218. fpclass(cpp_dec_float_finite),
  219. prec_elem(cpp_dec_float_elem_number)
  220. {
  221. *this = val;
  222. }
  223. cpp_dec_float(const double mantissa, const exponent_type exponent);
  224. std::size_t hash() const
  225. {
  226. std::size_t result = 0;
  227. for (int i = 0; i < prec_elem; ++i)
  228. boost::multiprecision::detail::hash_combine(result, data[i]);
  229. boost::multiprecision::detail::hash_combine(result, exp, neg, static_cast<std::size_t>(fpclass));
  230. return result;
  231. }
  232. // Specific special values.
  233. static const cpp_dec_float& nan () { static const cpp_dec_float val(cpp_dec_float_NaN); return val; }
  234. static const cpp_dec_float& inf () { static const cpp_dec_float val(cpp_dec_float_inf); return val; }
  235. static const cpp_dec_float& (max)() { static const cpp_dec_float val(from_lst({ std::uint32_t(1u) }, cpp_dec_float_max_exp10)); return val; }
  236. static const cpp_dec_float& (min)() { static const cpp_dec_float val(from_lst({ std::uint32_t(1u) }, cpp_dec_float_min_exp10)); return val; }
  237. static const cpp_dec_float& zero() { static const cpp_dec_float val(from_lst({ std::uint32_t(0u) })); return val; }
  238. static const cpp_dec_float& one () { static const cpp_dec_float val(from_lst({ std::uint32_t(1u) })); return val; }
  239. static const cpp_dec_float& two () { static const cpp_dec_float val(from_lst({ std::uint32_t(2u) })); return val; }
  240. static const cpp_dec_float& half() { static const cpp_dec_float val(from_lst({ std::uint32_t(cpp_dec_float_elem_mask / 2)}, -8)); return val; }
  241. static const cpp_dec_float& double_min() { static const cpp_dec_float val((std::numeric_limits<double>::min)()); return val; }
  242. static const cpp_dec_float& double_max() { static const cpp_dec_float val((std::numeric_limits<double>::max)()); return val; }
  243. static const cpp_dec_float& long_double_min()
  244. {
  245. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  246. static const cpp_dec_float val(static_cast<long double>((std::numeric_limits<double>::min)()));
  247. #else
  248. static const cpp_dec_float val((std::numeric_limits<long double>::min)());
  249. #endif
  250. return val;
  251. }
  252. static const cpp_dec_float& long_double_max()
  253. {
  254. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  255. static const cpp_dec_float val(static_cast<long double>((std::numeric_limits<double>::max)()));
  256. #else
  257. static const cpp_dec_float val((std::numeric_limits<long double>::max)());
  258. #endif
  259. return val;
  260. }
  261. static const cpp_dec_float& long_long_max () { static const cpp_dec_float val((std::numeric_limits<long long>::max)()); return val; }
  262. static const cpp_dec_float& long_long_min () { static const cpp_dec_float val((std::numeric_limits<long long>::min)()); return val; }
  263. static const cpp_dec_float& ulong_long_max() { static const cpp_dec_float val((std::numeric_limits<unsigned long long>::max)()); return val; }
  264. static const cpp_dec_float& eps()
  265. {
  266. static const cpp_dec_float val
  267. (
  268. from_lst
  269. (
  270. {
  271. (std::uint32_t) detail::pow10_maker((std::uint32_t) ((std::int32_t) (INT32_C(1) + (std::int32_t) (((cpp_dec_float_digits10 / cpp_dec_float_elem_digits10) + ((cpp_dec_float_digits10 % cpp_dec_float_elem_digits10) != 0 ? 1 : 0)) * cpp_dec_float_elem_digits10)) - cpp_dec_float_digits10))
  272. },
  273. -(exponent_type) (((cpp_dec_float_digits10 / cpp_dec_float_elem_digits10) + ((cpp_dec_float_digits10 % cpp_dec_float_elem_digits10) != 0 ? 1 : 0)) * cpp_dec_float_elem_digits10)
  274. )
  275. );
  276. return val;
  277. }
  278. // Basic operations.
  279. cpp_dec_float& operator=(const cpp_dec_float& v) noexcept(noexcept(std::declval<array_type&>() = std::declval<const array_type&>()))
  280. {
  281. data = v.data;
  282. exp = v.exp;
  283. neg = v.neg;
  284. fpclass = v.fpclass;
  285. prec_elem = v.prec_elem;
  286. return *this;
  287. }
  288. template <unsigned D>
  289. cpp_dec_float& operator=(const cpp_dec_float<D>& f)
  290. {
  291. exp = f.exp;
  292. neg = f.neg;
  293. fpclass = static_cast<enum_fpclass_type>(static_cast<int>(f.fpclass));
  294. unsigned elems = (std::min)(f.prec_elem, cpp_dec_float_elem_number);
  295. std::copy(f.data.begin(), f.data.begin() + elems, data.begin());
  296. std::fill(data.begin() + elems, data.end(), 0);
  297. prec_elem = cpp_dec_float_elem_number;
  298. return *this;
  299. }
  300. cpp_dec_float& operator=(long long v)
  301. {
  302. if (v < 0)
  303. {
  304. from_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(v));
  305. negate();
  306. }
  307. else
  308. {
  309. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  310. from_unsigned_long_long(static_cast<local_ulonglong_type>(v));
  311. }
  312. return *this;
  313. }
  314. cpp_dec_float& operator=(unsigned long long v)
  315. {
  316. from_unsigned_long_long(v);
  317. return *this;
  318. }
  319. #ifdef BOOST_HAS_INT128
  320. cpp_dec_float& operator=(int128_type v)
  321. {
  322. *this = boost::multiprecision::detail::unsigned_abs(v);
  323. if (v < 0)
  324. negate();
  325. return *this;
  326. }
  327. cpp_dec_float& operator=(uint128_type v)
  328. {
  329. using default_ops::eval_add;
  330. using default_ops::eval_multiply;
  331. constexpr unsigned bit_shift = sizeof(unsigned long long) * CHAR_BIT;
  332. constexpr uint128_type mask = (static_cast<uint128_type>(1u) << bit_shift) - 1;
  333. *this = static_cast<unsigned long long>(v & mask);
  334. v >>= bit_shift;
  335. while (v)
  336. {
  337. cpp_dec_float t(static_cast<unsigned long long>(v & mask));
  338. eval_multiply(t, cpp_dec_float::pow2(bit_shift));
  339. eval_add(*this, t);
  340. v >>= bit_shift;
  341. }
  342. return *this;
  343. }
  344. #endif
  345. template <class Float>
  346. typename std::enable_if<std::is_floating_point<Float>::value, cpp_dec_float&>::type operator=(Float v);
  347. cpp_dec_float& operator=(const char* v)
  348. {
  349. rd_string(v);
  350. return *this;
  351. }
  352. cpp_dec_float& operator+=(const cpp_dec_float& v);
  353. cpp_dec_float& operator-=(const cpp_dec_float& v);
  354. cpp_dec_float& operator*=(const cpp_dec_float& v);
  355. cpp_dec_float& operator/=(const cpp_dec_float& v);
  356. cpp_dec_float& add_unsigned_long_long(const unsigned long long n)
  357. {
  358. cpp_dec_float t;
  359. t.from_unsigned_long_long(n);
  360. return *this += t;
  361. }
  362. cpp_dec_float& sub_unsigned_long_long(const unsigned long long n)
  363. {
  364. cpp_dec_float t;
  365. t.from_unsigned_long_long(n);
  366. return *this -= t;
  367. }
  368. cpp_dec_float& mul_unsigned_long_long(const unsigned long long n);
  369. cpp_dec_float& div_unsigned_long_long(const unsigned long long n);
  370. // Elementary primitives.
  371. cpp_dec_float& calculate_inv();
  372. cpp_dec_float& calculate_sqrt();
  373. void negate()
  374. {
  375. if (!iszero())
  376. neg = !neg;
  377. }
  378. // Comparison functions
  379. bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_NaN); }
  380. bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_inf); }
  381. bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_finite); }
  382. bool iszero() const
  383. {
  384. return ((fpclass == cpp_dec_float_finite) && (data[0u] == 0u));
  385. }
  386. bool isone() const;
  387. bool isint() const;
  388. bool isneg() const { return neg; }
  389. // Operators pre-increment and pre-decrement
  390. cpp_dec_float& operator++()
  391. {
  392. return *this += one();
  393. }
  394. cpp_dec_float& operator--()
  395. {
  396. return *this -= one();
  397. }
  398. std::string str(std::intmax_t digits, std::ios_base::fmtflags f) const;
  399. int compare(const cpp_dec_float& v) const;
  400. template <class V>
  401. int compare(const V& v) const
  402. {
  403. cpp_dec_float<Digits10, ExponentType, Allocator> t;
  404. t = v;
  405. return compare(t);
  406. }
  407. void swap(cpp_dec_float& v)
  408. {
  409. data.swap(v.data);
  410. std::swap(exp, v.exp);
  411. std::swap(neg, v.neg);
  412. std::swap(fpclass, v.fpclass);
  413. std::swap(prec_elem, v.prec_elem);
  414. }
  415. double extract_double() const;
  416. long double extract_long_double() const;
  417. long long extract_signed_long_long() const;
  418. unsigned long long extract_unsigned_long_long() const;
  419. #ifdef BOOST_HAS_INT128
  420. int128_type extract_signed_int128() const;
  421. uint128_type extract_unsigned_int128() const;
  422. #endif
  423. void extract_parts(double& mantissa, exponent_type& exponent) const;
  424. cpp_dec_float extract_integer_part() const;
  425. void precision(const std::int32_t prec_digits)
  426. {
  427. const std::int32_t elems =
  428. static_cast<std::int32_t>( static_cast<std::int32_t>(prec_digits / cpp_dec_float_elem_digits10)
  429. + (((prec_digits % cpp_dec_float_elem_digits10) != 0) ? 1 : 0));
  430. prec_elem = (std::min)(cpp_dec_float_elem_number, (std::max)(elems, static_cast<std::int32_t>(2)));
  431. }
  432. static cpp_dec_float pow2(long long i);
  433. exponent_type order() const
  434. {
  435. const bool bo_order_is_zero = ((!(isfinite)()) || (data[0] == static_cast<std::uint32_t>(0u)));
  436. //
  437. // Binary search to find the order of the leading term:
  438. //
  439. exponent_type prefix = 0;
  440. if (data[0] >= 100000UL)
  441. {
  442. if (data[0] >= 10000000UL)
  443. {
  444. if (data[0] >= 100000000UL)
  445. {
  446. if (data[0] >= 1000000000UL)
  447. prefix = 9;
  448. else
  449. prefix = 8;
  450. }
  451. else
  452. prefix = 7;
  453. }
  454. else
  455. {
  456. if (data[0] >= 1000000UL)
  457. prefix = 6;
  458. else
  459. prefix = 5;
  460. }
  461. }
  462. else
  463. {
  464. if (data[0] >= 1000UL)
  465. {
  466. if (data[0] >= 10000UL)
  467. prefix = 4;
  468. else
  469. prefix = 3;
  470. }
  471. else
  472. {
  473. if (data[0] >= 100)
  474. prefix = 2;
  475. else if (data[0] >= 10)
  476. prefix = 1;
  477. }
  478. }
  479. return (bo_order_is_zero ? static_cast<exponent_type>(0) : static_cast<exponent_type>(exp + prefix));
  480. }
  481. #ifndef BOOST_MP_STANDALONE
  482. template <class Archive>
  483. void serialize(Archive& ar, const unsigned int /*version*/)
  484. {
  485. for (unsigned i = 0; i < data.size(); ++i)
  486. ar& boost::make_nvp("digit", data[i]);
  487. ar& boost::make_nvp("exponent", exp);
  488. ar& boost::make_nvp("sign", neg);
  489. ar& boost::make_nvp("class-type", fpclass);
  490. ar& boost::make_nvp("precision", prec_elem);
  491. }
  492. #endif
  493. private:
  494. static bool data_elem_is_non_zero_predicate(const std::uint32_t& d) { return (d != static_cast<std::uint32_t>(0u)); }
  495. static bool data_elem_is_non_nine_predicate(const std::uint32_t& d) { return (d != static_cast<std::uint32_t>(cpp_dec_float::cpp_dec_float_elem_mask - 1)); }
  496. static bool char_is_nonzero_predicate(const char& c) { return (c != static_cast<char>('0')); }
  497. void from_unsigned_long_long(const unsigned long long u);
  498. template <typename InputIteratorTypeLeft,
  499. typename InputIteratorTypeRight>
  500. static int compare_ranges(InputIteratorTypeLeft a,
  501. InputIteratorTypeRight b,
  502. const std::uint32_t count = cpp_dec_float_elem_number);
  503. static std::uint32_t eval_add_n( std::uint32_t* r,
  504. const std::uint32_t* u,
  505. const std::uint32_t* v,
  506. const std::int32_t count);
  507. static std::uint32_t eval_subtract_n( std::uint32_t* r,
  508. const std::uint32_t* u,
  509. const std::uint32_t* v,
  510. const std::int32_t count);
  511. static void eval_multiply_n_by_n_to_2n( std::uint32_t* r,
  512. const std::uint32_t* a,
  513. const std::uint32_t* b,
  514. const std::uint32_t count);
  515. static std::uint32_t mul_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p);
  516. static std::uint32_t div_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p);
  517. static void eval_multiply_kara_propagate_carry (std::uint32_t* t, const std::uint32_t n, const std::uint32_t carry);
  518. static void eval_multiply_kara_propagate_borrow(std::uint32_t* t, const std::uint32_t n, const bool has_borrow);
  519. static void eval_multiply_kara_n_by_n_to_2n ( std::uint32_t* r,
  520. const std::uint32_t* a,
  521. const std::uint32_t* b,
  522. const std::uint32_t n,
  523. std::uint32_t* t);
  524. template<unsigned D>
  525. void eval_mul_dispatch_multiplication_method(
  526. const cpp_dec_float<D, ExponentType, Allocator>& v,
  527. const std::int32_t prec_elems_for_multiply,
  528. const typename std::enable_if< (D == Digits10)
  529. && (cpp_dec_float<D, ExponentType, Allocator>::cpp_dec_float_elem_number < cpp_dec_float_elems_for_kara)>::type* = nullptr)
  530. {
  531. // Use school multiplication.
  532. using array_for_mul_result_type =
  533. typename std::conditional<std::is_void<Allocator>::value,
  534. detail::static_array <std::uint32_t, std::uint32_t(cpp_dec_float_elem_number * 2)>,
  535. detail::dynamic_array<std::uint32_t, std::uint32_t(cpp_dec_float_elem_number * 2), Allocator> >::type;
  536. array_for_mul_result_type result;
  537. eval_multiply_n_by_n_to_2n(result.data(), data.data(), v.data.data(), static_cast<std::uint32_t>(prec_elems_for_multiply));
  538. // Handle a potential carry.
  539. if(result[0U] != static_cast<std::uint32_t>(0U))
  540. {
  541. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  542. // Shift the result of the multiplication one element to the right.
  543. std::copy(result.cbegin(),
  544. result.cbegin() + static_cast<std::ptrdiff_t>(prec_elems_for_multiply),
  545. data.begin());
  546. }
  547. else
  548. {
  549. std::copy(result.cbegin() + static_cast<std::ptrdiff_t>(1),
  550. result.cbegin() + static_cast<std::ptrdiff_t>(1 + (std::min)(prec_elems_for_multiply, cpp_dec_float_elem_number)),
  551. data.begin());
  552. }
  553. }
  554. template<unsigned D>
  555. void eval_mul_dispatch_multiplication_method(
  556. const cpp_dec_float<D, ExponentType, Allocator>& v,
  557. const std::int32_t prec_elems_for_multiply,
  558. const typename std::enable_if< (D == Digits10)
  559. && !(cpp_dec_float<D, ExponentType, Allocator>::cpp_dec_float_elem_number < cpp_dec_float_elems_for_kara)>::type* = nullptr)
  560. {
  561. if(prec_elems_for_multiply < cpp_dec_float_elems_for_kara)
  562. {
  563. // Use school multiplication.
  564. using array_for_mul_result_type =
  565. typename std::conditional<std::is_void<Allocator>::value,
  566. detail::static_array <std::uint32_t, std::uint32_t(cpp_dec_float_elem_number * 2)>,
  567. detail::dynamic_array<std::uint32_t, std::uint32_t(cpp_dec_float_elem_number * 2), Allocator> >::type;
  568. array_for_mul_result_type result;
  569. eval_multiply_n_by_n_to_2n(result.data(), data.data(), v.data.data(), static_cast<std::uint32_t>(prec_elems_for_multiply));
  570. // Handle a potential carry.
  571. if(result[0U] != static_cast<std::uint32_t>(0U))
  572. {
  573. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  574. // Shift the result of the multiplication one element to the right.
  575. std::copy(result.cbegin(),
  576. result.cbegin() + static_cast<std::ptrdiff_t>(prec_elems_for_multiply),
  577. data.begin());
  578. }
  579. else
  580. {
  581. std::copy(result.cbegin() + static_cast<std::ptrdiff_t>(1),
  582. result.cbegin() + static_cast<std::ptrdiff_t>(1 + (std::min)(prec_elems_for_multiply, cpp_dec_float_elem_number)),
  583. data.begin());
  584. }
  585. }
  586. else
  587. {
  588. // Use Karatsuba multiplication.
  589. using array_for_kara_tmp_type =
  590. typename std::conditional<std::is_void<Allocator>::value,
  591. detail::static_array <std::uint32_t, detail::a029750::a029750_as_constexpr(static_cast<std::uint32_t>(cpp_dec_float_elem_number)) * 8U>,
  592. detail::dynamic_array<std::uint32_t, detail::a029750::a029750_as_constexpr(static_cast<std::uint32_t>(cpp_dec_float_elem_number)) * 8U, Allocator> >::type;
  593. // Sloanes's A029747: Numbers of the form 2^k times 1, 3 or 5.
  594. const std::uint32_t kara_elems_for_multiply =
  595. detail::a029750::a029750_as_runtime_value(static_cast<std::uint32_t>(prec_elems_for_multiply));
  596. array_for_kara_tmp_type my_kara_mul_pool;
  597. std::uint32_t* result = my_kara_mul_pool.data() + (kara_elems_for_multiply * 0U);
  598. std::uint32_t* t = my_kara_mul_pool.data() + (kara_elems_for_multiply * 2U);
  599. std::uint32_t* u_local = my_kara_mul_pool.data() + (kara_elems_for_multiply * 6U);
  600. std::uint32_t* v_local = my_kara_mul_pool.data() + (kara_elems_for_multiply * 7U);
  601. std::copy( data.cbegin(), data.cbegin() + prec_elems_for_multiply, u_local);
  602. std::copy(v.data.cbegin(), v.data.cbegin() + prec_elems_for_multiply, v_local);
  603. eval_multiply_kara_n_by_n_to_2n(result,
  604. u_local,
  605. v_local,
  606. kara_elems_for_multiply,
  607. t);
  608. // Handle a potential carry.
  609. if(result[0U] != static_cast<std::uint32_t>(0U))
  610. {
  611. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  612. // Shift the result of the multiplication one element to the right.
  613. std::copy(result,
  614. result + static_cast<std::ptrdiff_t>(prec_elems_for_multiply),
  615. data.begin());
  616. }
  617. else
  618. {
  619. std::copy(result + static_cast<std::ptrdiff_t>(1),
  620. result + static_cast<std::ptrdiff_t>(1 + (std::min)(prec_elems_for_multiply, cpp_dec_float_elem_number)),
  621. data.begin());
  622. }
  623. }
  624. }
  625. bool rd_string(const char* const s);
  626. template <unsigned D, class ET, class A>
  627. friend class cpp_dec_float;
  628. };
  629. template <unsigned Digits10, class ExponentType, class Allocator>
  630. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_radix;
  631. template <unsigned Digits10, class ExponentType, class Allocator>
  632. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10_limit_lo;
  633. template <unsigned Digits10, class ExponentType, class Allocator>
  634. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10_limit_hi;
  635. template <unsigned Digits10, class ExponentType, class Allocator>
  636. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  637. template <unsigned Digits10, class ExponentType, class Allocator>
  638. constexpr ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp;
  639. template <unsigned Digits10, class ExponentType, class Allocator>
  640. constexpr ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp;
  641. template <unsigned Digits10, class ExponentType, class Allocator>
  642. constexpr ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp10;
  643. template <unsigned Digits10, class ExponentType, class Allocator>
  644. constexpr ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp10;
  645. template <unsigned Digits10, class ExponentType, class Allocator>
  646. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_digits10;
  647. template <unsigned Digits10, class ExponentType, class Allocator>
  648. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_number;
  649. template <unsigned Digits10, class ExponentType, class Allocator>
  650. constexpr std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_mask;
  651. template <unsigned Digits10, class ExponentType, class Allocator>
  652. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator+=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  653. {
  654. if ((isnan)())
  655. {
  656. return *this;
  657. }
  658. if ((isinf)())
  659. {
  660. if ((v.isinf)() && (isneg() != v.isneg()))
  661. {
  662. *this = nan();
  663. }
  664. return *this;
  665. }
  666. if (iszero())
  667. {
  668. return operator=(v);
  669. }
  670. if ((v.isnan)() || (v.isinf)())
  671. {
  672. *this = v;
  673. return *this;
  674. }
  675. // Get the offset for the add/sub operation.
  676. constexpr exponent_type max_delta_exp =
  677. static_cast<exponent_type>((cpp_dec_float_elem_number - 1) * cpp_dec_float_elem_digits10);
  678. const exponent_type ofs_exp = static_cast<exponent_type>(exp - v.exp);
  679. // Check if the operation is out of range, requiring special handling.
  680. if (v.iszero() || (ofs_exp > max_delta_exp))
  681. {
  682. // Result is *this unchanged since v is negligible compared to *this.
  683. return *this;
  684. }
  685. else if (ofs_exp < -max_delta_exp)
  686. {
  687. // Result is *this = v since *this is negligible compared to v.
  688. return operator=(v);
  689. }
  690. // Do the add/sub operation.
  691. typename array_type::pointer p_u = data.data();
  692. typename array_type::const_pointer p_v = v.data.data();
  693. bool b_copy = false;
  694. const std::int32_t ofs = static_cast<std::int32_t>(static_cast<std::int32_t>(ofs_exp) / cpp_dec_float_elem_digits10);
  695. array_type n_data;
  696. if (neg == v.neg)
  697. {
  698. // Add v to *this, where the data array of either *this or v
  699. // might have to be treated with a positive, negative or zero offset.
  700. // The result is stored in *this. The data are added one element
  701. // at a time, each element with carry.
  702. if (ofs >= static_cast<std::int32_t>(0))
  703. {
  704. std::copy(v.data.cbegin(), v.data.cend() - static_cast<std::ptrdiff_t>(ofs), n_data.begin() + static_cast<std::ptrdiff_t>(ofs));
  705. std::fill(n_data.begin(), n_data.begin() + static_cast<std::ptrdiff_t>(ofs), static_cast<std::uint32_t>(0u));
  706. p_v = n_data.data();
  707. }
  708. else
  709. {
  710. std::copy(data.cbegin(), data.cend() - static_cast<std::ptrdiff_t>(-ofs), n_data.begin() + static_cast<std::ptrdiff_t>(-ofs));
  711. std::fill(n_data.begin(), n_data.begin() + static_cast<std::ptrdiff_t>(-ofs), static_cast<std::uint32_t>(0u));
  712. p_u = n_data.data();
  713. b_copy = true;
  714. }
  715. // Addition algorithm
  716. const std::uint32_t carry = eval_add_n(p_u, p_u, p_v, cpp_dec_float_elem_number);
  717. if (b_copy)
  718. {
  719. data = n_data;
  720. exp = v.exp;
  721. }
  722. // There needs to be a carry into the element -1 of the array data
  723. if (carry != static_cast<std::uint32_t>(0u))
  724. {
  725. std::copy_backward(data.cbegin(), data.cend() - static_cast<std::size_t>(1u), data.end());
  726. data[0] = carry;
  727. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  728. }
  729. }
  730. else
  731. {
  732. // Subtract v from *this, where the data array of either *this or v
  733. // might have to be treated with a positive, negative or zero offset.
  734. if ((ofs > static_cast<std::int32_t>(0)) || ((ofs == static_cast<std::int32_t>(0)) && (compare_ranges(data.cbegin(), v.data.cbegin()) > static_cast<std::int32_t>(0))))
  735. {
  736. // In this case, |u| > |v| and ofs is positive.
  737. // Copy the data of v, shifted down to a lower value
  738. // into the data array m_n. Set the operand pointer p_v
  739. // to point to the copied, shifted data m_n.
  740. std::copy(v.data.cbegin(), v.data.cend() - static_cast<std::ptrdiff_t>(ofs), n_data.begin() + static_cast<std::ptrdiff_t>(ofs));
  741. std::fill(n_data.begin(), n_data.begin() + static_cast<std::ptrdiff_t>(ofs), static_cast<std::uint32_t>(0u));
  742. p_v = n_data.data();
  743. }
  744. else
  745. {
  746. if (ofs != static_cast<std::int32_t>(0))
  747. {
  748. // In this case, |u| < |v| and ofs is negative.
  749. // Shift the data of u down to a lower value.
  750. std::copy_backward(data.cbegin(), data.cend() - static_cast<std::ptrdiff_t>(-ofs), data.end());
  751. std::fill(data.begin(), data.begin() + static_cast<std::ptrdiff_t>(-ofs), static_cast<std::uint32_t>(0u));
  752. }
  753. // Copy the data of v into the data array n_data.
  754. // Set the u-pointer p_u to point to m_n and the
  755. // operand pointer p_v to point to the shifted
  756. // data m_data.
  757. n_data = v.data;
  758. p_u = n_data.data();
  759. p_v = data.data();
  760. b_copy = true;
  761. }
  762. // Subtraction algorithm
  763. static_cast<void>(eval_subtract_n(p_u, p_u, p_v, cpp_dec_float_elem_number));
  764. if (b_copy)
  765. {
  766. data = n_data;
  767. exp = v.exp;
  768. neg = v.neg;
  769. }
  770. // Is it necessary to justify the data?
  771. const typename array_type::const_iterator first_nonzero_elem = std::find_if(data.begin(), data.end(), data_elem_is_non_zero_predicate);
  772. if (first_nonzero_elem != data.begin())
  773. {
  774. if (first_nonzero_elem == data.end())
  775. {
  776. // This result of the subtraction is exactly zero.
  777. // Reset the sign and the exponent.
  778. neg = false;
  779. exp = static_cast<exponent_type>(0);
  780. }
  781. else
  782. {
  783. // Justify the data
  784. const std::size_t sj = static_cast<std::size_t>(std::distance<typename array_type::const_iterator>(data.begin(), first_nonzero_elem));
  785. std::copy(data.begin() + static_cast<std::ptrdiff_t>(sj), data.end(), data.begin());
  786. std::fill(data.end() - static_cast<std::ptrdiff_t>(sj), data.end(), static_cast<std::uint32_t>(0u));
  787. exp -= static_cast<exponent_type>(sj * static_cast<std::size_t>(cpp_dec_float_elem_digits10));
  788. }
  789. }
  790. }
  791. // Handle underflow.
  792. if (iszero())
  793. return (*this = zero());
  794. // Check for potential overflow.
  795. const bool b_result_might_overflow = (exp >= static_cast<exponent_type>(cpp_dec_float_max_exp10));
  796. // Handle overflow.
  797. if (b_result_might_overflow)
  798. {
  799. const bool b_result_is_neg = neg;
  800. neg = false;
  801. if (compare((cpp_dec_float::max)()) > 0)
  802. *this = inf();
  803. neg = b_result_is_neg;
  804. }
  805. return *this;
  806. }
  807. template <unsigned Digits10, class ExponentType, class Allocator>
  808. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator-=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  809. {
  810. // Use *this - v = -(-*this + v).
  811. negate();
  812. *this += v;
  813. negate();
  814. return *this;
  815. }
  816. template <unsigned Digits10, class ExponentType, class Allocator>
  817. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator*=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  818. {
  819. // Evaluate the sign of the result.
  820. const bool b_result_is_neg = (neg != v.neg);
  821. // Artificially set the sign of the result to be positive.
  822. neg = false;
  823. // Handle special cases like zero, inf and NaN.
  824. const bool b_u_is_inf = (isinf)();
  825. const bool b_v_is_inf = (v.isinf)();
  826. const bool b_u_is_zero = iszero();
  827. const bool b_v_is_zero = v.iszero();
  828. if (((isnan)() || (v.isnan)()) || (b_u_is_inf && b_v_is_zero) || (b_v_is_inf && b_u_is_zero))
  829. {
  830. *this = nan();
  831. return *this;
  832. }
  833. if (b_u_is_inf || b_v_is_inf)
  834. {
  835. *this = inf();
  836. if (b_result_is_neg)
  837. negate();
  838. return *this;
  839. }
  840. if (b_u_is_zero || b_v_is_zero)
  841. {
  842. return *this = zero();
  843. }
  844. // Check for potential overflow or underflow.
  845. const bool b_result_might_overflow = ((exp + v.exp) >= static_cast<exponent_type>(cpp_dec_float_max_exp10));
  846. const bool b_result_might_underflow = ((exp + v.exp) <= static_cast<exponent_type>(cpp_dec_float_min_exp10));
  847. // Set the exponent of the result.
  848. exp += v.exp;
  849. const std::int32_t prec_mul = (std::min)(prec_elem, v.prec_elem);
  850. eval_mul_dispatch_multiplication_method(v, prec_mul);
  851. // Handle overflow.
  852. if (b_result_might_overflow && (compare((cpp_dec_float::max)()) > 0))
  853. {
  854. *this = inf();
  855. }
  856. // Handle underflow.
  857. if (b_result_might_underflow && (compare((cpp_dec_float::min)()) < 0))
  858. {
  859. *this = zero();
  860. return *this;
  861. }
  862. // Set the sign of the result.
  863. neg = b_result_is_neg;
  864. return *this;
  865. }
  866. template <unsigned Digits10, class ExponentType, class Allocator>
  867. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator/=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  868. {
  869. if (iszero())
  870. {
  871. if ((v.isnan)())
  872. {
  873. return *this = v;
  874. }
  875. else if (v.iszero())
  876. {
  877. return *this = nan();
  878. }
  879. }
  880. const bool u_and_v_are_finite_and_identical = ((isfinite)() && (fpclass == v.fpclass) && (exp == v.exp) && (compare_ranges(data.cbegin(), v.data.cbegin()) == static_cast<std::int32_t>(0)));
  881. if (u_and_v_are_finite_and_identical)
  882. {
  883. if (neg != v.neg)
  884. {
  885. *this = one();
  886. negate();
  887. }
  888. else
  889. *this = one();
  890. return *this;
  891. }
  892. else
  893. {
  894. cpp_dec_float t(v);
  895. t.calculate_inv();
  896. return operator*=(t);
  897. }
  898. }
  899. template <unsigned Digits10, class ExponentType, class Allocator>
  900. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::mul_unsigned_long_long(const unsigned long long n)
  901. {
  902. // Multiply *this with a constant unsigned long long.
  903. // Evaluate the sign of the result.
  904. const bool b_neg = neg;
  905. // Artificially set the sign of the result to be positive.
  906. neg = false;
  907. // Handle special cases like zero, inf and NaN.
  908. const bool b_u_is_inf = (isinf)();
  909. const bool b_n_is_zero = (n == static_cast<std::int32_t>(0));
  910. if ((isnan)() || (b_u_is_inf && b_n_is_zero))
  911. {
  912. return (*this = nan());
  913. }
  914. if (b_u_is_inf)
  915. {
  916. *this = inf();
  917. if (b_neg)
  918. negate();
  919. return *this;
  920. }
  921. if (iszero() || b_n_is_zero)
  922. {
  923. // Multiplication by zero.
  924. return *this = zero();
  925. }
  926. if (n >= static_cast<unsigned long long>(cpp_dec_float_elem_mask))
  927. {
  928. neg = b_neg;
  929. cpp_dec_float t;
  930. t = n;
  931. return operator*=(t);
  932. }
  933. if (n == static_cast<unsigned long long>(1u))
  934. {
  935. neg = b_neg;
  936. return *this;
  937. }
  938. // Set up the multiplication loop.
  939. const std::uint32_t nn = static_cast<std::uint32_t>(n);
  940. const std::uint32_t carry = mul_loop_n(data.data(), nn, prec_elem);
  941. // Handle the carry and adjust the exponent.
  942. if (carry != static_cast<std::uint32_t>(0u))
  943. {
  944. exp += static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  945. // Shift the result of the multiplication one element to the right.
  946. std::copy_backward(data.begin(),
  947. data.begin() + static_cast<std::ptrdiff_t>(prec_elem - static_cast<std::int32_t>(1)),
  948. data.begin() + static_cast<std::ptrdiff_t>(prec_elem));
  949. data.front() = static_cast<std::uint32_t>(carry);
  950. }
  951. // Check for potential overflow.
  952. const bool b_result_might_overflow = (exp >= cpp_dec_float_max_exp10);
  953. // Handle overflow.
  954. if (b_result_might_overflow && (compare((cpp_dec_float::max)()) > 0))
  955. {
  956. *this = inf();
  957. }
  958. // Set the sign.
  959. neg = b_neg;
  960. return *this;
  961. }
  962. template <unsigned Digits10, class ExponentType, class Allocator>
  963. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::div_unsigned_long_long(const unsigned long long n)
  964. {
  965. // Divide *this by a constant unsigned long long.
  966. // Evaluate the sign of the result.
  967. const bool b_neg = neg;
  968. // Artificially set the sign of the result to be positive.
  969. neg = false;
  970. // Handle special cases like zero, inf and NaN.
  971. if ((isnan)())
  972. {
  973. return *this;
  974. }
  975. if ((isinf)())
  976. {
  977. *this = inf();
  978. if (b_neg)
  979. negate();
  980. return *this;
  981. }
  982. if (n == static_cast<unsigned long long>(0u))
  983. {
  984. // Divide by 0.
  985. if (iszero())
  986. {
  987. *this = nan();
  988. return *this;
  989. }
  990. else
  991. {
  992. *this = inf();
  993. if (isneg())
  994. negate();
  995. return *this;
  996. }
  997. }
  998. if (iszero())
  999. {
  1000. return *this;
  1001. }
  1002. if (n >= static_cast<unsigned long long>(cpp_dec_float_elem_mask))
  1003. {
  1004. neg = b_neg;
  1005. cpp_dec_float t;
  1006. t = n;
  1007. return operator/=(t);
  1008. }
  1009. const std::uint32_t nn = static_cast<std::uint32_t>(n);
  1010. if (nn > static_cast<std::uint32_t>(1u))
  1011. {
  1012. // Do the division loop.
  1013. const std::uint32_t prev = div_loop_n(data.data(), nn, prec_elem);
  1014. // Determine if one leading zero is in the result data.
  1015. if (data[0] == static_cast<std::uint32_t>(0u))
  1016. {
  1017. // Adjust the exponent
  1018. exp -= static_cast<exponent_type>(cpp_dec_float_elem_digits10);
  1019. // Shift result of the division one element to the left.
  1020. std::copy(data.begin() + static_cast<std::ptrdiff_t>(1),
  1021. data.begin() + static_cast<std::ptrdiff_t>(prec_elem - static_cast<std::int32_t>(1)),
  1022. data.begin());
  1023. data[static_cast<std::size_t>(prec_elem - static_cast<std::int32_t>(1))] = static_cast<std::uint32_t>(static_cast<std::uint64_t>(prev * static_cast<std::uint64_t>(cpp_dec_float_elem_mask)) / nn);
  1024. }
  1025. }
  1026. // Check for potential underflow.
  1027. const bool b_result_might_underflow = (exp <= cpp_dec_float_min_exp10);
  1028. // Handle underflow.
  1029. if (b_result_might_underflow && (compare((cpp_dec_float::min)()) < 0))
  1030. return (*this = zero());
  1031. // Set the sign of the result.
  1032. neg = b_neg;
  1033. return *this;
  1034. }
  1035. template <unsigned Digits10, class ExponentType, class Allocator>
  1036. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::calculate_inv()
  1037. {
  1038. // Compute the inverse of *this.
  1039. const bool b_neg = neg;
  1040. neg = false;
  1041. // Handle special cases like zero, inf and NaN.
  1042. if (iszero())
  1043. {
  1044. *this = inf();
  1045. if (b_neg)
  1046. negate();
  1047. return *this;
  1048. }
  1049. if ((isnan)())
  1050. {
  1051. return *this;
  1052. }
  1053. if ((isinf)())
  1054. {
  1055. return *this = zero();
  1056. }
  1057. if (isone())
  1058. {
  1059. if (b_neg)
  1060. negate();
  1061. return *this;
  1062. }
  1063. // Save the original *this.
  1064. cpp_dec_float<Digits10, ExponentType, Allocator> x(*this);
  1065. // Generate the initial estimate using division.
  1066. // Extract the mantissa and exponent for a "manual"
  1067. // computation of the estimate.
  1068. double dd;
  1069. exponent_type ne;
  1070. x.extract_parts(dd, ne);
  1071. // Do the inverse estimate using double precision estimates of mantissa and exponent.
  1072. operator=(cpp_dec_float<Digits10, ExponentType, Allocator>(1.0 / dd, -ne));
  1073. // Compute the inverse of *this. Quadratically convergent Newton-Raphson iteration
  1074. // is used. During the iterative steps, the precision of the calculation is limited
  1075. // to the minimum required in order to minimize the run-time.
  1076. constexpr std::int32_t double_digits10_minus_a_few = std::numeric_limits<double>::digits10 - 3;
  1077. for (std::int32_t digits = double_digits10_minus_a_few; digits <= cpp_dec_float_max_digits10; digits *= static_cast<std::int32_t>(2))
  1078. {
  1079. // Adjust precision of the terms.
  1080. precision(static_cast<std::int32_t>((digits + 10) * static_cast<std::int32_t>(2)));
  1081. x.precision(static_cast<std::int32_t>((digits + 10) * static_cast<std::int32_t>(2)));
  1082. // Next iteration.
  1083. cpp_dec_float t(*this);
  1084. t *= x;
  1085. t -= two();
  1086. t.negate();
  1087. *this *= t;
  1088. }
  1089. neg = b_neg;
  1090. prec_elem = cpp_dec_float_elem_number;
  1091. return *this;
  1092. }
  1093. template <unsigned Digits10, class ExponentType, class Allocator>
  1094. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::calculate_sqrt()
  1095. {
  1096. // Compute the square root of *this.
  1097. if ((isinf)() && !isneg())
  1098. {
  1099. return *this;
  1100. }
  1101. if (isneg() || (!(isfinite)()))
  1102. {
  1103. *this = nan();
  1104. errno = EDOM;
  1105. return *this;
  1106. }
  1107. if (iszero() || isone())
  1108. {
  1109. return *this;
  1110. }
  1111. // Save the original *this.
  1112. cpp_dec_float<Digits10, ExponentType, Allocator> x(*this);
  1113. // Generate the initial estimate using division.
  1114. // Extract the mantissa and exponent for a "manual"
  1115. // computation of the estimate.
  1116. double dd;
  1117. exponent_type ne;
  1118. extract_parts(dd, ne);
  1119. // Force the exponent to be an even multiple of two.
  1120. if ((ne % static_cast<exponent_type>(2)) != static_cast<exponent_type>(0))
  1121. {
  1122. ++ne;
  1123. dd /= 10.0;
  1124. }
  1125. // Setup the iteration.
  1126. // Estimate the square root using simple manipulations.
  1127. const double sqd = std::sqrt(dd);
  1128. *this = cpp_dec_float<Digits10, ExponentType, Allocator>(sqd, static_cast<ExponentType>(ne / static_cast<ExponentType>(2)));
  1129. // Estimate 1.0 / (2.0 * x0) using simple manipulations.
  1130. cpp_dec_float<Digits10, ExponentType, Allocator> vi(0.5 / sqd, static_cast<ExponentType>(-ne / static_cast<ExponentType>(2)));
  1131. // Compute the square root of x. Coupled Newton iteration
  1132. // as described in "Pi Unleashed" is used. During the
  1133. // iterative steps, the precision of the calculation is
  1134. // limited to the minimum required in order to minimize
  1135. // the run-time.
  1136. //
  1137. // Book reference to "Pi Unleashed:
  1138. // https://www.springer.com/gp/book/9783642567353
  1139. constexpr std::uint32_t double_digits10_minus_a_few = std::numeric_limits<double>::digits10 - 3;
  1140. for (std::int32_t digits = double_digits10_minus_a_few; digits <= cpp_dec_float_max_digits10; digits *= 2)
  1141. {
  1142. // Adjust precision of the terms.
  1143. precision((digits + 10) * 2);
  1144. vi.precision((digits + 10) * 2);
  1145. // Next iteration of vi
  1146. cpp_dec_float t(*this);
  1147. t *= vi;
  1148. t.negate();
  1149. t.mul_unsigned_long_long(2u);
  1150. t += one();
  1151. t *= vi;
  1152. vi += t;
  1153. // Next iteration of *this
  1154. t = *this;
  1155. t *= *this;
  1156. t.negate();
  1157. t += x;
  1158. t *= vi;
  1159. *this += t;
  1160. }
  1161. prec_elem = cpp_dec_float_elem_number;
  1162. return *this;
  1163. }
  1164. template <unsigned Digits10, class ExponentType, class Allocator>
  1165. int cpp_dec_float<Digits10, ExponentType, Allocator>::compare(const cpp_dec_float& v) const
  1166. {
  1167. // Compare v with *this.
  1168. // Return +1 for *this > v
  1169. // 0 for *this = v
  1170. // -1 for *this < v
  1171. // Handle all non-finite cases.
  1172. if ((!(isfinite)()) || (!(v.isfinite)()))
  1173. {
  1174. // NaN can never equal NaN. Return an implementation-dependent
  1175. // signed result. Also note that comparison of NaN with NaN
  1176. // using operators greater-than or less-than is undefined.
  1177. if ((isnan)() || (v.isnan)())
  1178. {
  1179. return ((isnan)() ? 1 : -1);
  1180. }
  1181. if ((isinf)() && (v.isinf)())
  1182. {
  1183. // Both *this and v are infinite. They are equal if they have the same sign.
  1184. // Otherwise, *this is less than v if and only if *this is negative.
  1185. return ((neg == v.neg) ? 0 : (neg ? -1 : 1));
  1186. }
  1187. if ((isinf)())
  1188. {
  1189. // *this is infinite, but v is finite.
  1190. // So negative infinite *this is less than any finite v.
  1191. // Whereas positive infinite *this is greater than any finite v.
  1192. return (isneg() ? -1 : 1);
  1193. }
  1194. else
  1195. {
  1196. // *this is finite, and v is infinite.
  1197. // So any finite *this is greater than negative infinite v.
  1198. // Whereas any finite *this is less than positive infinite v.
  1199. return (v.neg ? 1 : -1);
  1200. }
  1201. }
  1202. // And now handle all *finite* cases.
  1203. if (iszero())
  1204. {
  1205. // The value of *this is zero and v is either zero or non-zero.
  1206. return (v.iszero() ? 0
  1207. : (v.neg ? 1 : -1));
  1208. }
  1209. else if (v.iszero())
  1210. {
  1211. // The value of v is zero and *this is non-zero.
  1212. return (neg ? -1 : 1);
  1213. }
  1214. else
  1215. {
  1216. // Both *this and v are non-zero.
  1217. if (neg != v.neg)
  1218. {
  1219. // The signs are different.
  1220. return (neg ? -1 : 1);
  1221. }
  1222. else if (exp != v.exp)
  1223. {
  1224. // The signs are the same and the exponents are different.
  1225. const int val_cexpression = ((exp < v.exp) ? 1 : -1);
  1226. return (neg ? val_cexpression : -val_cexpression);
  1227. }
  1228. else
  1229. {
  1230. // The signs are the same and the exponents are the same.
  1231. // Compare the data.
  1232. const int val_cmp_data = compare_ranges(data.cbegin(), v.data.cbegin());
  1233. return ((!neg) ? val_cmp_data : -val_cmp_data);
  1234. }
  1235. }
  1236. }
  1237. template <unsigned Digits10, class ExponentType, class Allocator>
  1238. bool cpp_dec_float<Digits10, ExponentType, Allocator>::isone() const
  1239. {
  1240. // Check if the value of *this is identically 1 or very close to 1.
  1241. const bool not_negative_and_is_finite = ((!neg) && (isfinite)());
  1242. if (not_negative_and_is_finite)
  1243. {
  1244. if ((data[0u] == static_cast<std::uint32_t>(1u)) && (exp == static_cast<exponent_type>(0)))
  1245. {
  1246. const typename array_type::const_iterator it_non_zero = std::find_if(data.begin(), data.end(), data_elem_is_non_zero_predicate);
  1247. return (it_non_zero == data.end());
  1248. }
  1249. else if ((data[0u] == static_cast<std::uint32_t>(cpp_dec_float_elem_mask - 1)) && (exp == static_cast<exponent_type>(-cpp_dec_float_elem_digits10)))
  1250. {
  1251. const typename array_type::const_iterator it_non_nine = std::find_if(data.begin(), data.end(), data_elem_is_non_nine_predicate);
  1252. return (it_non_nine == data.end());
  1253. }
  1254. }
  1255. return false;
  1256. }
  1257. template <unsigned Digits10, class ExponentType, class Allocator>
  1258. bool cpp_dec_float<Digits10, ExponentType, Allocator>::isint() const
  1259. {
  1260. if (fpclass != cpp_dec_float_finite)
  1261. {
  1262. return false;
  1263. }
  1264. if (iszero())
  1265. {
  1266. return true;
  1267. }
  1268. if (exp < static_cast<exponent_type>(0))
  1269. {
  1270. return false;
  1271. } // |*this| < 1.
  1272. const typename array_type::size_type offset_decimal_part = static_cast<typename array_type::size_type>(exp / cpp_dec_float_elem_digits10) + 1u;
  1273. if (offset_decimal_part >= static_cast<typename array_type::size_type>(cpp_dec_float_elem_number))
  1274. {
  1275. // The number is too large to resolve the integer part.
  1276. // It considered to be a pure integer.
  1277. return true;
  1278. }
  1279. typename array_type::const_iterator it_non_zero = std::find_if(data.begin() + static_cast<std::ptrdiff_t>(offset_decimal_part), data.end(), data_elem_is_non_zero_predicate);
  1280. return (it_non_zero == data.end());
  1281. }
  1282. template <unsigned Digits10, class ExponentType, class Allocator>
  1283. void cpp_dec_float<Digits10, ExponentType, Allocator>::extract_parts(double& mantissa, ExponentType& exponent) const
  1284. {
  1285. // Extract the approximate parts mantissa and base-10 exponent from the input cpp_dec_float<Digits10, ExponentType, Allocator> value x.
  1286. // Extracts the mantissa and exponent.
  1287. exponent = exp;
  1288. std::uint32_t p10 = static_cast<std::uint32_t>(1u);
  1289. std::uint32_t test = data[0u];
  1290. for (;;)
  1291. {
  1292. test /= static_cast<std::uint32_t>(10u);
  1293. if (test == static_cast<std::uint32_t>(0u))
  1294. {
  1295. break;
  1296. }
  1297. p10 *= static_cast<std::uint32_t>(10u);
  1298. ++exponent;
  1299. }
  1300. // Establish the upper bound of limbs for extracting the double.
  1301. const int max_elem_in_double_count = static_cast<int>(static_cast<std::int32_t>(std::numeric_limits<double>::digits10) / cpp_dec_float_elem_digits10) + (static_cast<int>(static_cast<std::int32_t>(std::numeric_limits<double>::digits10) % cpp_dec_float_elem_digits10) != 0 ? 1 : 0) + 1;
  1302. // And make sure this upper bound stays within bounds of the elems.
  1303. const std::size_t max_elem_extract_count = static_cast<std::size_t>((std::min)(static_cast<std::int32_t>(max_elem_in_double_count), cpp_dec_float_elem_number));
  1304. // Extract into the mantissa the first limb, extracted as a double.
  1305. mantissa = static_cast<double>(data[0]);
  1306. double scale = 1.0;
  1307. // Extract the rest of the mantissa piecewise from the limbs.
  1308. for (std::size_t i = 1u; i < max_elem_extract_count; i++)
  1309. {
  1310. scale /= static_cast<double>(cpp_dec_float_elem_mask);
  1311. mantissa += (static_cast<double>(data[i]) * scale);
  1312. }
  1313. mantissa /= static_cast<double>(p10);
  1314. if (neg)
  1315. {
  1316. mantissa = -mantissa;
  1317. }
  1318. }
  1319. template <unsigned Digits10, class ExponentType, class Allocator>
  1320. double cpp_dec_float<Digits10, ExponentType, Allocator>::extract_double() const
  1321. {
  1322. // Returns the double conversion of a cpp_dec_float<Digits10, ExponentType, Allocator>.
  1323. // Check for non-normal cpp_dec_float<Digits10, ExponentType, Allocator>.
  1324. if (!(isfinite)())
  1325. {
  1326. if ((isnan)())
  1327. {
  1328. return std::numeric_limits<double>::quiet_NaN();
  1329. }
  1330. else
  1331. {
  1332. return ((!neg) ? std::numeric_limits<double>::infinity()
  1333. : -std::numeric_limits<double>::infinity());
  1334. }
  1335. }
  1336. cpp_dec_float<Digits10, ExponentType, Allocator> xx(*this);
  1337. if (xx.isneg())
  1338. xx.negate();
  1339. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is zero.
  1340. if (iszero() || (xx.compare(double_min()) < 0))
  1341. {
  1342. return 0.0;
  1343. }
  1344. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> exceeds the maximum of double.
  1345. if (xx.compare(double_max()) > 0)
  1346. {
  1347. return ((!neg) ? std::numeric_limits<double>::infinity()
  1348. : -std::numeric_limits<double>::infinity());
  1349. }
  1350. std::stringstream ss;
  1351. ss.imbue(std::locale::classic());
  1352. ss << str(std::numeric_limits<double>::digits10 + (2 + 1), std::ios_base::scientific);
  1353. double d;
  1354. ss >> d;
  1355. return d;
  1356. }
  1357. template <unsigned Digits10, class ExponentType, class Allocator>
  1358. long double cpp_dec_float<Digits10, ExponentType, Allocator>::extract_long_double() const
  1359. {
  1360. // Returns the long double conversion of a cpp_dec_float<Digits10, ExponentType, Allocator>.
  1361. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is subnormal.
  1362. if (!(isfinite)())
  1363. {
  1364. if ((isnan)())
  1365. {
  1366. return std::numeric_limits<long double>::quiet_NaN();
  1367. }
  1368. else
  1369. {
  1370. return ((!neg) ? std::numeric_limits<long double>::infinity()
  1371. : -std::numeric_limits<long double>::infinity());
  1372. }
  1373. }
  1374. cpp_dec_float<Digits10, ExponentType, Allocator> xx(*this);
  1375. if (xx.isneg())
  1376. xx.negate();
  1377. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is zero.
  1378. if (iszero() || (xx.compare(long_double_min()) < 0))
  1379. {
  1380. return static_cast<long double>(0.0);
  1381. }
  1382. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> exceeds the maximum of double.
  1383. if (xx.compare(long_double_max()) > 0)
  1384. {
  1385. return ((!neg) ? std::numeric_limits<long double>::infinity()
  1386. : -std::numeric_limits<long double>::infinity());
  1387. }
  1388. std::stringstream ss;
  1389. ss.imbue(std::locale::classic());
  1390. ss << str(std::numeric_limits<long double>::digits10 + (2 + 1), std::ios_base::scientific);
  1391. long double ld;
  1392. ss >> ld;
  1393. return ld;
  1394. }
  1395. template <unsigned Digits10, class ExponentType, class Allocator>
  1396. long long cpp_dec_float<Digits10, ExponentType, Allocator>::extract_signed_long_long() const
  1397. {
  1398. // Extracts a signed long long from *this.
  1399. // If (x > maximum of long long) or (x < minimum of long long),
  1400. // then the maximum or minimum of long long is returned accordingly.
  1401. if (exp < static_cast<exponent_type>(0))
  1402. {
  1403. return static_cast<long long>(0);
  1404. }
  1405. const bool b_neg = isneg();
  1406. unsigned long long val;
  1407. if ((!b_neg) && (compare(long_long_max()) > 0))
  1408. {
  1409. return (std::numeric_limits<long long>::max)();
  1410. }
  1411. else if (b_neg && (compare(long_long_min()) < 0))
  1412. {
  1413. return (std::numeric_limits<long long>::min)();
  1414. }
  1415. else
  1416. {
  1417. // Extract the data into an unsigned long long value.
  1418. cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1419. if (xn.isneg())
  1420. xn.negate();
  1421. val = static_cast<unsigned long long>(xn.data[0]);
  1422. const std::int32_t imax = (std::min)(static_cast<std::int32_t>(static_cast<std::int32_t>(xn.exp) / cpp_dec_float_elem_digits10), static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)));
  1423. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1424. {
  1425. val *= static_cast<unsigned long long>(cpp_dec_float_elem_mask);
  1426. val += static_cast<unsigned long long>(xn.data[static_cast<std::size_t>(i)]);
  1427. }
  1428. }
  1429. if (!b_neg)
  1430. {
  1431. return static_cast<long long>(val);
  1432. }
  1433. else
  1434. {
  1435. // This strange expression avoids a hardware trap in the corner case
  1436. // that val is the most negative value permitted in long long.
  1437. // See https://svn.boost.org/trac/boost/ticket/9740.
  1438. //
  1439. long long sval = static_cast<long long>(val - 1);
  1440. sval = -sval;
  1441. --sval;
  1442. return sval;
  1443. }
  1444. }
  1445. template <unsigned Digits10, class ExponentType, class Allocator>
  1446. unsigned long long cpp_dec_float<Digits10, ExponentType, Allocator>::extract_unsigned_long_long() const
  1447. {
  1448. // Extracts an unsigned long long from *this.
  1449. // If x exceeds the maximum of unsigned long long,
  1450. // then the maximum of unsigned long long is returned.
  1451. // If x is negative, then the unsigned long long cast of
  1452. // the long long extracted value is returned.
  1453. if (isneg())
  1454. {
  1455. return static_cast<unsigned long long>(extract_signed_long_long());
  1456. }
  1457. if (exp < static_cast<exponent_type>(0))
  1458. {
  1459. return static_cast<unsigned long long>(0u);
  1460. }
  1461. const cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1462. unsigned long long val;
  1463. if (xn.compare(ulong_long_max()) > 0)
  1464. {
  1465. return (std::numeric_limits<unsigned long long>::max)();
  1466. }
  1467. else
  1468. {
  1469. // Extract the data into an unsigned long long value.
  1470. val = static_cast<unsigned long long>(xn.data[0]);
  1471. const std::int32_t imax = (std::min)(static_cast<std::int32_t>(static_cast<std::int32_t>(xn.exp) / cpp_dec_float_elem_digits10), static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)));
  1472. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1473. {
  1474. val *= static_cast<unsigned long long>(cpp_dec_float_elem_mask);
  1475. val += static_cast<unsigned long long>(xn.data[i]);
  1476. }
  1477. }
  1478. return val;
  1479. }
  1480. #ifdef BOOST_HAS_INT128
  1481. template <unsigned Digits10, class ExponentType, class Allocator>
  1482. int128_type cpp_dec_float<Digits10, ExponentType, Allocator>::extract_signed_int128() const
  1483. {
  1484. // Extracts a signed __int128 from *this.
  1485. // If (x > maximum of __int128) or (x < minimum of __int128),
  1486. // then the maximum or minimum of long long is returned accordingly.
  1487. if (exp < static_cast<exponent_type>(0))
  1488. {
  1489. return static_cast<int128_type>(0);
  1490. }
  1491. const bool b_neg = isneg();
  1492. cpp_dec_float<Digits10, ExponentType, Allocator> i128max;
  1493. i128max = ((~static_cast<uint128_type>(0)) >> 1);
  1494. cpp_dec_float<Digits10, ExponentType, Allocator> i128min;
  1495. i128min = (-1 - static_cast<int128_type>((static_cast<uint128_type>(1) << 127) - 1));
  1496. uint128_type val;
  1497. if ((!b_neg) && (compare(i128max) > 0))
  1498. {
  1499. return ((~static_cast<uint128_type>(0)) >> 1);
  1500. }
  1501. else if (b_neg && (compare(i128min) < 0))
  1502. {
  1503. return (-1 - static_cast<int128_type>((static_cast<uint128_type>(1) << 127) - 1));
  1504. }
  1505. else
  1506. {
  1507. // Extract the data into an unsigned long long value.
  1508. cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1509. if (xn.isneg())
  1510. xn.negate();
  1511. val = static_cast<uint128_type>(xn.data[0]);
  1512. const std::int32_t imax = (std::min)(static_cast<std::int32_t>(static_cast<std::int32_t>(xn.exp) / cpp_dec_float_elem_digits10), static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)));
  1513. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1514. {
  1515. val *= static_cast<uint128_type>(cpp_dec_float_elem_mask);
  1516. val += static_cast<uint128_type>(xn.data[static_cast<std::size_t>(i)]);
  1517. }
  1518. }
  1519. if (!b_neg)
  1520. {
  1521. return static_cast<int128_type>(val);
  1522. }
  1523. else
  1524. {
  1525. // This strange expression avoids a hardware trap in the corner case
  1526. // that val is the most negative value permitted in long long.
  1527. // See https://svn.boost.org/trac/boost/ticket/9740.
  1528. //
  1529. int128_type sval = static_cast<int128_type>(val - 1);
  1530. sval = -sval;
  1531. --sval;
  1532. return sval;
  1533. }
  1534. }
  1535. template <unsigned Digits10, class ExponentType, class Allocator>
  1536. uint128_type cpp_dec_float<Digits10, ExponentType, Allocator>::extract_unsigned_int128() const
  1537. {
  1538. // Extracts an unsigned __int128 from *this.
  1539. // If x exceeds the maximum of unsigned __int128,
  1540. // then the maximum of unsigned __int128 is returned.
  1541. // If x is negative, then the unsigned __int128 cast of
  1542. // the __int128 extracted value is returned.
  1543. if (isneg())
  1544. {
  1545. return static_cast<uint128_type>(extract_signed_int128());
  1546. }
  1547. if (exp < static_cast<exponent_type>(0))
  1548. {
  1549. return 0u;
  1550. }
  1551. const cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1552. cpp_dec_float<Digits10, ExponentType, Allocator> i128max;
  1553. i128max = (~static_cast<uint128_type>(0));
  1554. uint128_type val;
  1555. if (xn.compare(i128max) > 0)
  1556. {
  1557. return (~static_cast<uint128_type>(0));
  1558. }
  1559. else
  1560. {
  1561. // Extract the data into an unsigned long long value.
  1562. val = static_cast<uint128_type>(xn.data[0]);
  1563. const std::int32_t imax = (std::min)(static_cast<std::int32_t>(static_cast<std::int32_t>(xn.exp) / cpp_dec_float_elem_digits10), static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)));
  1564. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1565. {
  1566. val *= static_cast<uint128_type>(cpp_dec_float_elem_mask);
  1567. val += static_cast<uint128_type>(xn.data[i]);
  1568. }
  1569. }
  1570. return val;
  1571. }
  1572. #endif
  1573. template <unsigned Digits10, class ExponentType, class Allocator>
  1574. cpp_dec_float<Digits10, ExponentType, Allocator> cpp_dec_float<Digits10, ExponentType, Allocator>::extract_integer_part() const
  1575. {
  1576. // Compute the signed integer part of x.
  1577. if (!(isfinite)())
  1578. {
  1579. return *this;
  1580. }
  1581. if (exp < static_cast<ExponentType>(0))
  1582. {
  1583. // The absolute value of the number is smaller than 1.
  1584. // Thus the integer part is zero.
  1585. return zero();
  1586. }
  1587. // Truncate the digits from the decimal part, including guard digits
  1588. // that do not belong to the integer part.
  1589. // Make a local copy.
  1590. cpp_dec_float<Digits10, ExponentType, Allocator> x = *this;
  1591. // Clear out the decimal portion
  1592. const std::size_t first_clear = (static_cast<std::size_t>(x.exp) / static_cast<std::size_t>(cpp_dec_float_elem_digits10)) + 1u;
  1593. const std::size_t last_clear = static_cast<std::size_t>(cpp_dec_float_elem_number);
  1594. if (first_clear < last_clear)
  1595. std::fill(x.data.begin() + static_cast<std::ptrdiff_t>(first_clear), x.data.begin() + static_cast<std::ptrdiff_t>(last_clear), static_cast<std::uint32_t>(0u));
  1596. return x;
  1597. }
  1598. template <unsigned Digits10, class ExponentType, class Allocator>
  1599. std::string cpp_dec_float<Digits10, ExponentType, Allocator>::str(std::intmax_t number_of_digits, std::ios_base::fmtflags f) const
  1600. {
  1601. if ((this->isinf)())
  1602. {
  1603. if (this->isneg())
  1604. return "-inf";
  1605. else if (f & std::ios_base::showpos)
  1606. return "+inf";
  1607. else
  1608. return "inf";
  1609. }
  1610. else if ((this->isnan)())
  1611. {
  1612. return "nan";
  1613. }
  1614. std::string str;
  1615. std::intmax_t org_digits(number_of_digits);
  1616. exponent_type my_exp = order();
  1617. if (!(f & std::ios_base::fixed) && (number_of_digits == 0))
  1618. number_of_digits = cpp_dec_float_max_digits10;
  1619. if (f & std::ios_base::fixed)
  1620. {
  1621. number_of_digits += my_exp + 1;
  1622. }
  1623. else if (f & std::ios_base::scientific)
  1624. ++number_of_digits;
  1625. // Determine the number of elements needed to provide the requested digits from cpp_dec_float<Digits10, ExponentType, Allocator>.
  1626. const std::size_t number_of_elements = (std::min)(static_cast<std::size_t>(static_cast<std::size_t>(number_of_digits / static_cast<std::intmax_t>(cpp_dec_float_elem_digits10)) + 2u),
  1627. static_cast<std::size_t>(cpp_dec_float_elem_number));
  1628. // Extract the remaining digits from cpp_dec_float<Digits10, ExponentType, Allocator> after the decimal point.
  1629. std::stringstream ss;
  1630. ss.imbue(std::locale::classic());
  1631. ss << data[0];
  1632. // Extract all of the digits from cpp_dec_float<Digits10, ExponentType, Allocator>, beginning with the first data element.
  1633. for (std::size_t i = static_cast<std::size_t>(1u); i < number_of_elements; i++)
  1634. {
  1635. ss << std::setw(static_cast<std::streamsize>(cpp_dec_float_elem_digits10))
  1636. << std::setfill(static_cast<char>('0'))
  1637. << data[i];
  1638. }
  1639. str += ss.str();
  1640. bool have_leading_zeros = false;
  1641. if (number_of_digits == 0)
  1642. {
  1643. // We only get here if the output format is "fixed" and we just need to
  1644. // round the first non-zero digit.
  1645. number_of_digits -= my_exp + 1; // reset to original value
  1646. if (number_of_digits)
  1647. {
  1648. str.insert(static_cast<std::string::size_type>(0), std::string::size_type(number_of_digits), '0');
  1649. have_leading_zeros = true;
  1650. }
  1651. }
  1652. if (number_of_digits < 0)
  1653. {
  1654. str = "0";
  1655. if (isneg())
  1656. str.insert(static_cast<std::string::size_type>(0), 1, '-');
  1657. boost::multiprecision::detail::format_float_string(str, 0, number_of_digits - my_exp - 1, f, this->iszero());
  1658. return str;
  1659. }
  1660. else
  1661. {
  1662. // Cut the output to the size of the precision.
  1663. if (str.length() > static_cast<std::string::size_type>(number_of_digits))
  1664. {
  1665. // Get the digit after the last needed digit for rounding
  1666. const std::uint32_t round = static_cast<std::uint32_t>(static_cast<std::uint32_t>(str[static_cast<std::string::size_type>(number_of_digits)]) - static_cast<std::uint32_t>('0'));
  1667. bool need_round_up = round >= 5u;
  1668. if (round == 5u)
  1669. {
  1670. const std::uint32_t ix = number_of_digits == 0 ? 0 : static_cast<std::uint32_t>(static_cast<std::uint32_t>(str[static_cast<std::string::size_type>(number_of_digits - 1)]) - static_cast<std::uint32_t>('0'));
  1671. if ((ix & 1u) == 0)
  1672. {
  1673. // We have an even digit followed by a 5, so we might not actually need to round up
  1674. // if all the remaining digits are zero:
  1675. if (str.find_first_not_of('0', static_cast<std::string::size_type>(number_of_digits + 1)) == std::string::npos)
  1676. {
  1677. bool all_zeros = true;
  1678. // No none-zero trailing digits in the string, now check whatever parts we didn't convert to the string:
  1679. for (std::size_t i = number_of_elements; i < data.size(); i++)
  1680. {
  1681. if (data[i])
  1682. {
  1683. all_zeros = false;
  1684. break;
  1685. }
  1686. }
  1687. if (all_zeros)
  1688. need_round_up = false; // tie break - round to even.
  1689. }
  1690. }
  1691. }
  1692. // Truncate the string
  1693. str.erase(static_cast<std::string::size_type>(number_of_digits));
  1694. if (need_round_up)
  1695. {
  1696. if (str.size())
  1697. {
  1698. std::size_t ix = static_cast<std::size_t>(str.length() - 1u);
  1699. // Every trailing 9 must be rounded up
  1700. while (ix && (static_cast<std::int32_t>(str.at(ix)) - static_cast<std::int32_t>('0') == static_cast<std::int32_t>(9)))
  1701. {
  1702. str.at(ix) = static_cast<char>('0');
  1703. --ix;
  1704. }
  1705. if (!ix)
  1706. {
  1707. // There were nothing but trailing nines.
  1708. if (static_cast<std::int32_t>(static_cast<std::int32_t>(str.at(ix)) - static_cast<std::int32_t>(0x30)) == static_cast<std::int32_t>(9))
  1709. {
  1710. // Increment up to the next order and adjust exponent.
  1711. str.at(ix) = static_cast<char>('1');
  1712. ++my_exp;
  1713. }
  1714. else
  1715. {
  1716. // Round up this digit.
  1717. ++str.at(ix);
  1718. }
  1719. }
  1720. else
  1721. {
  1722. // Round up the last digit.
  1723. ++str[ix];
  1724. }
  1725. }
  1726. else
  1727. {
  1728. str = "1";
  1729. ++my_exp;
  1730. }
  1731. }
  1732. }
  1733. }
  1734. if (have_leading_zeros)
  1735. {
  1736. // We need to take the zeros back out again, and correct the exponent
  1737. // if we rounded up:
  1738. if (str[std::string::size_type(number_of_digits - 1)] != '0')
  1739. {
  1740. ++my_exp;
  1741. str.erase(0, std::string::size_type(number_of_digits - 1));
  1742. }
  1743. else
  1744. str.erase(0, std::string::size_type(number_of_digits));
  1745. }
  1746. if (isneg())
  1747. str.insert(static_cast<std::string::size_type>(0), 1, '-');
  1748. boost::multiprecision::detail::format_float_string(str, my_exp, org_digits, f, this->iszero());
  1749. return str;
  1750. }
  1751. template <unsigned Digits10, class ExponentType, class Allocator>
  1752. bool cpp_dec_float<Digits10, ExponentType, Allocator>::rd_string(const char* const s)
  1753. {
  1754. #ifndef BOOST_NO_EXCEPTIONS
  1755. try
  1756. {
  1757. #endif
  1758. std::string str(s);
  1759. static const std::string valid_characters{"0123456789"};
  1760. // TBD: Using several regular expressions may significantly reduce
  1761. // the code complexity (and perhaps the run-time) of rd_string().
  1762. // Get a possible exponent and remove it.
  1763. exp = static_cast<exponent_type>(0);
  1764. std::size_t pos;
  1765. if (((pos = str.find('e')) != std::string::npos) || ((pos = str.find('E')) != std::string::npos))
  1766. {
  1767. // Remove the exponent part from the string.
  1768. #ifndef BOOST_MP_STANDALONE
  1769. exp = boost::lexical_cast<exponent_type>(static_cast<const char*>(str.c_str() + (pos + 1u)));
  1770. #else
  1771. if (str.find_first_not_of(valid_characters, ((str[pos + 1] == '+') || (str[pos + 1] == '-')) ? pos + 2 : pos + 1) != std::string::npos)
  1772. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with non-numeric content"));
  1773. exp = static_cast<exponent_type>(std::atoll(static_cast<const char*>(str.c_str() + (pos + 1u))));
  1774. #endif
  1775. str = str.substr(static_cast<std::size_t>(0u), pos);
  1776. }
  1777. // Get a possible +/- sign and remove it.
  1778. neg = false;
  1779. if (str.size())
  1780. {
  1781. if (str[0] == '-')
  1782. {
  1783. neg = true;
  1784. str.erase(0, 1);
  1785. }
  1786. else if (str[0] == '+')
  1787. {
  1788. str.erase(0, 1);
  1789. }
  1790. }
  1791. //
  1792. // Special cases for infinities and NaN's:
  1793. //
  1794. if ((str == "inf") || (str == "INF") || (str == "infinity") || (str == "INFINITY"))
  1795. {
  1796. if (neg)
  1797. {
  1798. *this = this->inf();
  1799. this->negate();
  1800. }
  1801. else
  1802. *this = this->inf();
  1803. return true;
  1804. }
  1805. if ((str.size() >= 3) && ((str.substr(0, 3) == "nan") || (str.substr(0, 3) == "NAN") || (str.substr(0, 3) == "NaN")))
  1806. {
  1807. *this = this->nan();
  1808. return true;
  1809. }
  1810. // Remove the leading zeros for all input types.
  1811. const std::string::iterator fwd_it_leading_zero = std::find_if(str.begin(), str.end(), char_is_nonzero_predicate);
  1812. if (fwd_it_leading_zero != str.begin())
  1813. {
  1814. if (fwd_it_leading_zero == str.end())
  1815. {
  1816. // The string contains nothing but leading zeros.
  1817. // This string represents zero.
  1818. operator=(zero());
  1819. return true;
  1820. }
  1821. else
  1822. {
  1823. str.erase(str.begin(), fwd_it_leading_zero);
  1824. }
  1825. }
  1826. // Put the input string into the standard cpp_dec_float<Digits10, ExponentType, Allocator> input form
  1827. // aaa.bbbbE+/-n, where aaa has 1...cpp_dec_float_elem_digits10, bbbb has an
  1828. // even multiple of cpp_dec_float_elem_digits10 which are possibly zero padded
  1829. // on the right-end, and n is a signed 64-bit integer which is an
  1830. // even multiple of cpp_dec_float_elem_digits10.
  1831. // Find a possible decimal point.
  1832. pos = str.find(static_cast<char>('.'));
  1833. if (pos != std::string::npos)
  1834. {
  1835. // Check we have only digits either side of the point:
  1836. if (str.find_first_not_of(valid_characters) != pos)
  1837. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with non-numeric content"));
  1838. if (str.find_first_not_of(valid_characters, pos + 1) != std::string::npos)
  1839. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with non-numeric content"));
  1840. // Remove all trailing insignificant zeros.
  1841. const std::string::const_reverse_iterator rit_non_zero = std::find_if(str.rbegin(), str.rend(), char_is_nonzero_predicate);
  1842. if (rit_non_zero != static_cast<std::string::const_reverse_iterator>(str.rbegin()))
  1843. {
  1844. const std::string::size_type ofs =
  1845. static_cast<std::string::size_type>
  1846. (
  1847. static_cast<std::ptrdiff_t>(str.length())
  1848. - std::distance<std::string::const_reverse_iterator>(str.rbegin(), rit_non_zero)
  1849. );
  1850. str.erase(str.begin() + static_cast<std::ptrdiff_t>(ofs), str.end());
  1851. }
  1852. // Check if the input is identically zero.
  1853. if (str == std::string("."))
  1854. {
  1855. operator=(zero());
  1856. return true;
  1857. }
  1858. // Remove leading significant zeros just after the decimal point
  1859. // and adjust the exponent accordingly.
  1860. // Note that the while-loop operates only on strings of the form ".000abcd..."
  1861. // and peels away the zeros just after the decimal point.
  1862. if (str.at(static_cast<std::size_t>(0u)) == static_cast<char>('.'))
  1863. {
  1864. const std::string::iterator it_non_zero = std::find_if(str.begin() + 1u, str.end(), char_is_nonzero_predicate);
  1865. std::size_t delta_exp = static_cast<std::size_t>(0u);
  1866. if (str.at(static_cast<std::size_t>(1u)) == static_cast<char>('0'))
  1867. {
  1868. delta_exp = static_cast<std::size_t>(std::distance<std::string::const_iterator>(str.begin() + 1u, it_non_zero));
  1869. }
  1870. // Bring one single digit into the mantissa and adjust the exponent accordingly.
  1871. str.erase(str.begin(), it_non_zero);
  1872. str.insert(static_cast<std::string::size_type>(1u), ".");
  1873. exp -= static_cast<exponent_type>(delta_exp + 1u);
  1874. }
  1875. }
  1876. else
  1877. {
  1878. // We should have only digits:
  1879. if (str.find_first_not_of(valid_characters) != std::string::npos)
  1880. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with non-numeric content"));
  1881. // Input string has no decimal point: Append decimal point.
  1882. str.append(".");
  1883. }
  1884. // Shift the decimal point such that the exponent is an even multiple of cpp_dec_float_elem_digits10.
  1885. std::ptrdiff_t n_shift = static_cast<std::ptrdiff_t>(0);
  1886. const std::ptrdiff_t n_exp_rem = static_cast<std::ptrdiff_t>(exp % static_cast<exponent_type>(cpp_dec_float_elem_digits10));
  1887. if((exp % static_cast<exponent_type>(cpp_dec_float_elem_digits10)) != static_cast<exponent_type>(0))
  1888. {
  1889. n_shift = ((exp < static_cast<exponent_type>(0))
  1890. ? static_cast<std::ptrdiff_t>(n_exp_rem + static_cast<std::ptrdiff_t>(cpp_dec_float_elem_digits10))
  1891. : static_cast<std::ptrdiff_t>(n_exp_rem));
  1892. }
  1893. // Make sure that there are enough digits for the decimal point shift.
  1894. pos = str.find(static_cast<char>('.'));
  1895. std::ptrdiff_t pos_plus_one = static_cast<std::ptrdiff_t>(pos + 1);
  1896. if ((static_cast<std::ptrdiff_t>(str.length()) - pos_plus_one) < n_shift)
  1897. {
  1898. const std::ptrdiff_t sz = static_cast<std::ptrdiff_t>(n_shift - (static_cast<std::ptrdiff_t>(str.length()) - pos_plus_one));
  1899. str.append(std::string(static_cast<std::string::size_type>(sz), static_cast<char>('0')));
  1900. }
  1901. // Do the decimal point shift.
  1902. if (n_shift != static_cast<std::ptrdiff_t>(0))
  1903. {
  1904. str.insert(static_cast<std::string::size_type>(pos_plus_one + n_shift), ".");
  1905. str.erase(pos, static_cast<std::ptrdiff_t>(1));
  1906. exp -= static_cast<exponent_type>(n_shift);
  1907. }
  1908. // Cut the size of the mantissa to <= cpp_dec_float_elem_digits10.
  1909. pos = str.find(static_cast<char>('.'));
  1910. pos_plus_one = static_cast<std::ptrdiff_t>(pos + 1u);
  1911. if (pos > static_cast<std::size_t>(cpp_dec_float_elem_digits10))
  1912. {
  1913. const std::int32_t n_pos = static_cast<std::int32_t>(pos);
  1914. const std::int32_t n_rem_is_zero = ((static_cast<std::int32_t>(n_pos % cpp_dec_float_elem_digits10) == static_cast<std::int32_t>(0)) ? static_cast<std::int32_t>(1) : static_cast<std::int32_t>(0));
  1915. const std::int32_t n = static_cast<std::int32_t>(static_cast<std::int32_t>(n_pos / cpp_dec_float_elem_digits10) - n_rem_is_zero);
  1916. str.insert(static_cast<std::size_t>(static_cast<std::int32_t>(n_pos - static_cast<std::int32_t>(n * cpp_dec_float_elem_digits10))), ".");
  1917. str.erase(static_cast<std::size_t>(pos_plus_one), static_cast<std::size_t>(1u));
  1918. exp += static_cast<exponent_type>(static_cast<exponent_type>(n) * static_cast<exponent_type>(cpp_dec_float_elem_digits10));
  1919. }
  1920. // Pad the decimal part such that its value is an even
  1921. // multiple of cpp_dec_float_elem_digits10.
  1922. pos = str.find(static_cast<char>('.'));
  1923. pos_plus_one = static_cast<std::ptrdiff_t>(pos + 1u);
  1924. // Throws an error for a strange construction like 3.14L
  1925. if(pos != std::string::npos && (str.back() == 'L' || str.back() == 'l' || str.back() == 'u' || str.back() == 'U'))
  1926. {
  1927. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Can not construct a floating point with an integer literal"));
  1928. }
  1929. const std::int32_t n_dec = static_cast<std::int32_t>(static_cast<std::int32_t>(str.length() - 1u) - static_cast<std::int32_t>(pos));
  1930. const std::int32_t n_rem = static_cast<std::int32_t>(n_dec % cpp_dec_float_elem_digits10);
  1931. std::int32_t n_cnt = ((n_rem != static_cast<std::int32_t>(0))
  1932. ? static_cast<std::int32_t>(cpp_dec_float_elem_digits10 - n_rem)
  1933. : static_cast<std::int32_t>(0));
  1934. if (n_cnt != static_cast<std::int32_t>(0))
  1935. {
  1936. str.append(static_cast<std::size_t>(n_cnt), static_cast<char>('0'));
  1937. }
  1938. // Truncate decimal part if it is too long.
  1939. const std::size_t max_dec = static_cast<std::size_t>((cpp_dec_float_elem_number - 1) * cpp_dec_float_elem_digits10);
  1940. if (static_cast<std::size_t>(str.length() - pos) > max_dec)
  1941. {
  1942. str = str.substr(static_cast<std::size_t>(0u),
  1943. static_cast<std::size_t>(pos_plus_one + static_cast<std::ptrdiff_t>(max_dec)));
  1944. }
  1945. // Now the input string has the standard cpp_dec_float<Digits10, ExponentType, Allocator> input form.
  1946. // (See the comment above.)
  1947. // Set all the data elements to 0.
  1948. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  1949. // Extract the data.
  1950. // First get the digits to the left of the decimal point...
  1951. data[0u] = static_cast<std::uint32_t>(std::stol(str.substr(static_cast<std::size_t>(0u), pos)));
  1952. // ...then get the remaining digits to the right of the decimal point.
  1953. const std::string::size_type i_end =
  1954. (
  1955. static_cast<std::string::size_type>(str.length() - static_cast<std::string::size_type>(pos_plus_one))
  1956. / static_cast<std::string::size_type>(cpp_dec_float_elem_digits10)
  1957. );
  1958. for (std::string::size_type i = static_cast<std::string::size_type>(0u); i < i_end; i++)
  1959. {
  1960. const std::string::const_iterator it =
  1961. str.begin()
  1962. + static_cast<std::ptrdiff_t>
  1963. (
  1964. static_cast<std::string::size_type>(pos_plus_one)
  1965. + static_cast<std::string::size_type>(i * static_cast<std::string::size_type>(cpp_dec_float_elem_digits10))
  1966. );
  1967. data[i + 1u] = static_cast<std::uint32_t>(std::stol(std::string(it, it + static_cast<std::string::size_type>(cpp_dec_float_elem_digits10))));
  1968. }
  1969. // Check for overflow...
  1970. if (exp > cpp_dec_float_max_exp10)
  1971. {
  1972. const bool b_result_is_neg = neg;
  1973. *this = inf();
  1974. if (b_result_is_neg)
  1975. negate();
  1976. }
  1977. // ...and check for underflow.
  1978. if (exp <= cpp_dec_float_min_exp10)
  1979. {
  1980. if (exp == cpp_dec_float_min_exp10)
  1981. {
  1982. // Check for identity with the minimum value.
  1983. cpp_dec_float<Digits10, ExponentType, Allocator> test = *this;
  1984. test.exp = static_cast<exponent_type>(0);
  1985. if (test.isone())
  1986. {
  1987. *this = zero();
  1988. }
  1989. }
  1990. else
  1991. {
  1992. *this = zero();
  1993. }
  1994. }
  1995. #ifndef BOOST_NO_EXCEPTIONS
  1996. }
  1997. #ifndef BOOST_MP_STANDALONE
  1998. catch (const bad_lexical_cast&)
  1999. #else
  2000. catch (const std::exception&)
  2001. #endif
  2002. {
  2003. // Rethrow with better error message:
  2004. std::string msg = "Unable to parse the string \"";
  2005. msg += s;
  2006. msg += "\" as a floating point value.";
  2007. throw std::runtime_error(msg);
  2008. }
  2009. #endif
  2010. return true;
  2011. }
  2012. template <unsigned Digits10, class ExponentType, class Allocator>
  2013. cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float(const double mantissa, const ExponentType exponent)
  2014. : data(),
  2015. exp(static_cast<ExponentType>(0)),
  2016. neg(false),
  2017. fpclass(cpp_dec_float_finite),
  2018. prec_elem(cpp_dec_float_elem_number)
  2019. {
  2020. // Create *this cpp_dec_float<Digits10, ExponentType, Allocator> from a given mantissa and exponent.
  2021. // Note: This constructor does not maintain the full precision of double.
  2022. const bool mantissa_is_iszero = (::fabs(mantissa) < ((std::numeric_limits<double>::min)() * (1.0 + std::numeric_limits<double>::epsilon())));
  2023. if (mantissa_is_iszero)
  2024. {
  2025. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  2026. return;
  2027. }
  2028. const bool b_neg = (mantissa < 0.0);
  2029. double d = ((!b_neg) ? mantissa : -mantissa);
  2030. exponent_type e = exponent;
  2031. while (d > 10.0)
  2032. {
  2033. d /= 10.0;
  2034. ++e;
  2035. }
  2036. while (d < 1.0)
  2037. {
  2038. d *= 10.0;
  2039. --e;
  2040. }
  2041. std::int32_t shift = static_cast<std::int32_t>(e % static_cast<std::int32_t>(cpp_dec_float_elem_digits10));
  2042. while (static_cast<std::int32_t>(shift-- % cpp_dec_float_elem_digits10) != static_cast<std::int32_t>(0))
  2043. {
  2044. d *= 10.0;
  2045. --e;
  2046. }
  2047. exp = e;
  2048. neg = b_neg;
  2049. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  2050. constexpr std::int32_t digit_ratio = static_cast<std::int32_t>(static_cast<std::int32_t>(std::numeric_limits<double>::digits10) / static_cast<std::int32_t>(cpp_dec_float_elem_digits10));
  2051. constexpr std::int32_t digit_loops = static_cast<std::int32_t>(digit_ratio + static_cast<std::int32_t>(2));
  2052. for (std::int32_t i = static_cast<std::int32_t>(0); i < digit_loops; i++)
  2053. {
  2054. std::uint32_t n = static_cast<std::uint32_t>(static_cast<std::uint64_t>(d));
  2055. data[static_cast<std::size_t>(i)] = static_cast<std::uint32_t>(n);
  2056. d -= static_cast<double>(n);
  2057. d *= static_cast<double>(cpp_dec_float_elem_mask);
  2058. }
  2059. }
  2060. template <unsigned Digits10, class ExponentType, class Allocator>
  2061. template <class Float>
  2062. typename std::enable_if<std::is_floating_point<Float>::value, cpp_dec_float<Digits10, ExponentType, Allocator>&>::type cpp_dec_float<Digits10, ExponentType, Allocator>::operator=(Float a)
  2063. {
  2064. // Christopher Kormanyos's original code used a cast to long long here, but that fails
  2065. // when long double has more digits than a long long.
  2066. BOOST_MP_FLOAT128_USING
  2067. using std::floor;
  2068. using std::frexp;
  2069. using std::ldexp;
  2070. if (a == 0)
  2071. return *this = zero();
  2072. if (a == 1)
  2073. return *this = one();
  2074. if (BOOST_MP_ISINF(a))
  2075. {
  2076. *this = inf();
  2077. if (a < 0)
  2078. this->negate();
  2079. return *this;
  2080. }
  2081. if (BOOST_MP_ISNAN(a))
  2082. return *this = nan();
  2083. int e;
  2084. Float f, term;
  2085. *this = zero();
  2086. f = frexp(a, &e);
  2087. // See https://svn.boost.org/trac/boost/ticket/10924 for an example of why this may go wrong:
  2088. BOOST_MP_ASSERT(!BOOST_MP_ISNAN(f) && !BOOST_MP_ISINF(f));
  2089. constexpr int shift = std::numeric_limits<int>::digits - 1;
  2090. while (f != static_cast<Float>(0.0f))
  2091. {
  2092. // extract int sized bits from f:
  2093. f = ldexp(f, shift);
  2094. BOOST_MP_ASSERT(!BOOST_MP_ISNAN(f) && !BOOST_MP_ISINF(f));
  2095. term = floor(f);
  2096. e -= shift;
  2097. *this *= pow2(shift);
  2098. if (term > 0)
  2099. add_unsigned_long_long(static_cast<unsigned>(term));
  2100. else
  2101. sub_unsigned_long_long(static_cast<unsigned>(-term));
  2102. f -= term;
  2103. }
  2104. if (e != 0)
  2105. *this *= pow2(e);
  2106. return *this;
  2107. }
  2108. template <unsigned Digits10, class ExponentType, class Allocator>
  2109. void cpp_dec_float<Digits10, ExponentType, Allocator>::from_unsigned_long_long(const unsigned long long u)
  2110. {
  2111. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  2112. exp = static_cast<exponent_type>(0);
  2113. neg = false;
  2114. fpclass = cpp_dec_float_finite;
  2115. prec_elem = cpp_dec_float_elem_number;
  2116. if (u == 0)
  2117. {
  2118. return;
  2119. }
  2120. std::size_t i = static_cast<std::size_t>(0u);
  2121. unsigned long long uu = u;
  2122. std::uint32_t temp[(std::numeric_limits<unsigned long long>::digits10 / static_cast<int>(cpp_dec_float_elem_digits10)) + 3] = {static_cast<std::uint32_t>(0u)};
  2123. while (uu != static_cast<unsigned long long>(0u))
  2124. {
  2125. temp[i] = static_cast<std::uint32_t>(uu % static_cast<unsigned long long>(cpp_dec_float_elem_mask));
  2126. uu = static_cast<unsigned long long>(uu / static_cast<unsigned long long>(cpp_dec_float_elem_mask));
  2127. ++i;
  2128. }
  2129. if (i > static_cast<std::size_t>(1u))
  2130. {
  2131. exp += static_cast<exponent_type>((i - 1u) * static_cast<std::size_t>(cpp_dec_float_elem_digits10));
  2132. }
  2133. std::reverse(temp, temp + i);
  2134. std::copy(temp, temp + (std::min)(i, static_cast<std::size_t>(cpp_dec_float_elem_number)), data.begin());
  2135. }
  2136. template <unsigned Digits10, class ExponentType, class Allocator>
  2137. template <typename InputIteratorTypeLeft, typename InputIteratorTypeRight>
  2138. int cpp_dec_float<Digits10, ExponentType, Allocator>::compare_ranges(InputIteratorTypeLeft a,
  2139. InputIteratorTypeRight b,
  2140. const std::uint32_t count)
  2141. {
  2142. using local_iterator_left_type = InputIteratorTypeLeft;
  2143. using local_iterator_right_type = InputIteratorTypeRight;
  2144. local_iterator_left_type begin_a(a);
  2145. local_iterator_left_type end_a (a + static_cast<typename std::iterator_traits<local_iterator_left_type >::difference_type>(count));
  2146. local_iterator_right_type begin_b(b);
  2147. local_iterator_right_type end_b (b + static_cast<typename std::iterator_traits<local_iterator_right_type>::difference_type>(count));
  2148. const auto mismatch_pair = std::mismatch(begin_a, end_a, begin_b);
  2149. int n_return;
  2150. if((mismatch_pair.first != end_a) || (mismatch_pair.second != end_b))
  2151. {
  2152. const typename std::iterator_traits<local_iterator_left_type >::value_type left = *mismatch_pair.first;
  2153. const typename std::iterator_traits<local_iterator_right_type>::value_type right = *mismatch_pair.second;
  2154. n_return = ((left > right) ? 1 : -1);
  2155. }
  2156. else
  2157. {
  2158. n_return = 0;
  2159. }
  2160. return n_return;
  2161. }
  2162. template <unsigned Digits10, class ExponentType, class Allocator>
  2163. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::eval_add_n( std::uint32_t* r,
  2164. const std::uint32_t* u,
  2165. const std::uint32_t* v,
  2166. const std::int32_t count)
  2167. {
  2168. // Addition algorithm
  2169. std::uint_fast8_t carry = static_cast<std::uint_fast8_t>(0U);
  2170. for(std::int32_t j = static_cast<std::int32_t>(count - static_cast<std::int32_t>(1)); j >= static_cast<std::int32_t>(0); --j)
  2171. {
  2172. const std::uint32_t t = static_cast<std::uint32_t>(static_cast<std::uint32_t>(u[j] + v[j]) + carry);
  2173. carry = ((t >= static_cast<std::uint32_t>(cpp_dec_float_elem_mask)) ? static_cast<std::uint_fast8_t>(1U)
  2174. : static_cast<std::uint_fast8_t>(0U));
  2175. r[j] = static_cast<std::uint32_t>(t - ((carry != 0U) ? static_cast<std::uint32_t>(cpp_dec_float_elem_mask)
  2176. : static_cast<std::uint32_t>(0U)));
  2177. }
  2178. return static_cast<std::uint32_t>(carry);
  2179. }
  2180. template <unsigned Digits10, class ExponentType, class Allocator>
  2181. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::eval_subtract_n( std::uint32_t* r,
  2182. const std::uint32_t* u,
  2183. const std::uint32_t* v,
  2184. const std::int32_t count)
  2185. {
  2186. // Subtraction algorithm
  2187. std::int_fast8_t borrow = static_cast<std::int_fast8_t>(0);
  2188. for(std::uint32_t j = static_cast<std::uint32_t>(count - static_cast<std::int32_t>(1)); static_cast<std::int32_t>(j) >= static_cast<std::int32_t>(0); --j)
  2189. {
  2190. std::int32_t t = static_cast<std::int32_t>( static_cast<std::int32_t>(u[j])
  2191. - static_cast<std::int32_t>(v[j])) - borrow;
  2192. // Underflow? Borrow?
  2193. if(t < 0)
  2194. {
  2195. // Yes, underflow and borrow
  2196. t += static_cast<std::int32_t>(cpp_dec_float_elem_mask);
  2197. borrow = static_cast<std::int_fast8_t>(1);
  2198. }
  2199. else
  2200. {
  2201. borrow = static_cast<std::int_fast8_t>(0);
  2202. }
  2203. r[j] = static_cast<std::uint32_t>(t);
  2204. }
  2205. return static_cast<std::uint32_t>(borrow);
  2206. }
  2207. template <unsigned Digits10, class ExponentType, class Allocator>
  2208. void cpp_dec_float<Digits10, ExponentType, Allocator>::eval_multiply_n_by_n_to_2n( std::uint32_t* r,
  2209. const std::uint32_t* a,
  2210. const std::uint32_t* b,
  2211. const std::uint32_t count)
  2212. {
  2213. using local_limb_type = std::uint32_t;
  2214. using local_double_limb_type = std::uint64_t;
  2215. using local_reverse_iterator_type = std::reverse_iterator<local_limb_type*>;
  2216. local_reverse_iterator_type ir(r + (count * 2));
  2217. local_double_limb_type carry = 0U;
  2218. for(std::int32_t j = static_cast<std::int32_t>(count - 1); j >= static_cast<std::int32_t>(1); --j)
  2219. {
  2220. local_double_limb_type sum = carry;
  2221. for(std::int32_t i = static_cast<std::int32_t>(count - 1); i >= j; --i)
  2222. {
  2223. sum += local_double_limb_type(
  2224. local_double_limb_type(a[i]) * b[ static_cast<std::int32_t>(count - 1)
  2225. - static_cast<std::int32_t>(i - j)]);
  2226. }
  2227. carry = static_cast<local_double_limb_type>(sum / static_cast<local_limb_type> (cpp_dec_float_elem_mask));
  2228. *ir++ = static_cast<local_limb_type> (sum - static_cast<local_double_limb_type>(static_cast<local_double_limb_type>(carry) * static_cast<local_limb_type>(cpp_dec_float_elem_mask)));
  2229. }
  2230. for(std::int32_t j = static_cast<std::int32_t>(count - 1); j >= static_cast<std::int32_t>(0); --j)
  2231. {
  2232. local_double_limb_type sum = carry;
  2233. for(std::int32_t i = j; i >= static_cast<std::int32_t>(0); --i)
  2234. {
  2235. sum += static_cast<local_double_limb_type>(a[j - i] * static_cast<local_double_limb_type>(b[i]));
  2236. }
  2237. carry = static_cast<local_double_limb_type>(sum / static_cast<local_limb_type>(cpp_dec_float_elem_mask));
  2238. *ir++ = static_cast<local_limb_type> (sum - static_cast<local_double_limb_type>(static_cast<local_double_limb_type>(carry) * static_cast<local_limb_type>(cpp_dec_float_elem_mask)));
  2239. }
  2240. *ir = static_cast<local_limb_type>(carry);
  2241. }
  2242. template <unsigned Digits10, class ExponentType, class Allocator>
  2243. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::mul_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p)
  2244. {
  2245. std::uint64_t carry = static_cast<std::uint64_t>(0u);
  2246. // Multiplication loop.
  2247. for (std::int32_t j = p - 1; j >= static_cast<std::int32_t>(0); j--)
  2248. {
  2249. const std::uint64_t t = static_cast<std::uint64_t>(carry + static_cast<std::uint64_t>(u[j] * static_cast<std::uint64_t>(n)));
  2250. carry = static_cast<std::uint64_t>(t / static_cast<std::uint32_t>(cpp_dec_float_elem_mask));
  2251. u[j] = static_cast<std::uint32_t>(t - static_cast<std::uint64_t>(static_cast<std::uint32_t>(cpp_dec_float_elem_mask) * static_cast<std::uint64_t>(carry)));
  2252. }
  2253. return static_cast<std::uint32_t>(carry);
  2254. }
  2255. template <unsigned Digits10, class ExponentType, class Allocator>
  2256. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::div_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p)
  2257. {
  2258. std::uint64_t prev = static_cast<std::uint64_t>(0u);
  2259. for (std::int32_t j = static_cast<std::int32_t>(0); j < p; j++)
  2260. {
  2261. const std::uint64_t t = static_cast<std::uint64_t>(u[j] + static_cast<std::uint64_t>(prev * static_cast<std::uint32_t>(cpp_dec_float_elem_mask)));
  2262. u[j] = static_cast<std::uint32_t>(t / n);
  2263. prev = static_cast<std::uint64_t>(t - static_cast<std::uint64_t>(n * static_cast<std::uint64_t>(u[j])));
  2264. }
  2265. return static_cast<std::uint32_t>(prev);
  2266. }
  2267. template <unsigned Digits10, class ExponentType, class Allocator>
  2268. void cpp_dec_float<Digits10, ExponentType, Allocator>::eval_multiply_kara_propagate_carry(std::uint32_t* t, const std::uint32_t n, const std::uint32_t carry)
  2269. {
  2270. std::uint_fast8_t carry_out = ((carry != 0U) ? static_cast<std::uint_fast8_t>(1U)
  2271. : static_cast<std::uint_fast8_t>(0U));
  2272. using local_reverse_iterator_type = std::reverse_iterator<std::uint32_t*>;
  2273. local_reverse_iterator_type ri_t (t + n);
  2274. local_reverse_iterator_type rend_t(t);
  2275. while((carry_out != 0U) && (ri_t != rend_t))
  2276. {
  2277. const std::uint64_t tt = *ri_t + carry_out;
  2278. carry_out = ((tt >= static_cast<std::uint32_t>(cpp_dec_float_elem_mask)) ? static_cast<std::uint_fast8_t>(1U)
  2279. : static_cast<std::uint_fast8_t>(0U));
  2280. *ri_t++ = static_cast<std::uint32_t>(tt - ((carry_out != 0U) ? static_cast<std::uint32_t>(cpp_dec_float_elem_mask)
  2281. : static_cast<std::uint32_t>(0U)));
  2282. }
  2283. }
  2284. template <unsigned Digits10, class ExponentType, class Allocator>
  2285. void cpp_dec_float<Digits10, ExponentType, Allocator>::eval_multiply_kara_propagate_borrow(std::uint32_t* t, const std::uint32_t n, const bool has_borrow)
  2286. {
  2287. std::int_fast8_t borrow = (has_borrow ? static_cast<std::int_fast8_t>(1)
  2288. : static_cast<std::int_fast8_t>(0));
  2289. using local_reverse_iterator_type = std::reverse_iterator<std::uint32_t*>;
  2290. local_reverse_iterator_type ri_t (t + n);
  2291. local_reverse_iterator_type rend_t(t);
  2292. while((borrow != 0U) && (ri_t != rend_t))
  2293. {
  2294. std::int32_t tt = static_cast<std::int32_t>(static_cast<std::int32_t>(*ri_t) - borrow);
  2295. // Underflow? Borrow?
  2296. if(tt < 0)
  2297. {
  2298. // Yes, underflow and borrow
  2299. tt += static_cast<std::int32_t>(cpp_dec_float_elem_mask);
  2300. borrow = static_cast<int_fast8_t>(1);
  2301. }
  2302. else
  2303. {
  2304. borrow = static_cast<int_fast8_t>(0);
  2305. }
  2306. *ri_t++ = static_cast<std::uint32_t>(tt);
  2307. }
  2308. }
  2309. template <unsigned Digits10, class ExponentType, class Allocator>
  2310. void cpp_dec_float<Digits10, ExponentType, Allocator>::eval_multiply_kara_n_by_n_to_2n( std::uint32_t* r,
  2311. const std::uint32_t* a,
  2312. const std::uint32_t* b,
  2313. const std::uint32_t n,
  2314. std::uint32_t* t)
  2315. {
  2316. if(n <= 32U)
  2317. {
  2318. static_cast<void>(t);
  2319. eval_multiply_n_by_n_to_2n(r, a, b, n);
  2320. }
  2321. else
  2322. {
  2323. // Based on "Algorithm 1.3 KaratsubaMultiply", Sect. 1.3.2, page 5
  2324. // of R.P. Brent and P. Zimmermann, "Modern Computer Arithmetic",
  2325. // Cambridge University Press (2011).
  2326. // The Karatsuba multipliation computes the product of a*b as:
  2327. // [b^N + b^(N/2)] a1*b1 + [b^(N/2)](a1 - a0)(b0 - b1) + [b^(N/2) + 1] a0*b0
  2328. // Here we visualize a and b in two components 1,0 corresponding
  2329. // to the high and low order parts, respectively.
  2330. // Step 1
  2331. // Calculate a1*b1 and store it in the upper-order part of r.
  2332. // Calculate a0*b0 and store it in the lower-order part of r.
  2333. // copy r to t0.
  2334. // Step 2
  2335. // Add a1*b1 (which is t2) to the middle two-quarters of r (which is r1)
  2336. // Add a0*b0 (which is t0) to the middle two-quarters of r (which is r1)
  2337. // Step 3
  2338. // Calculate |a1-a0| in t0 and note the sign (i.e., the borrow flag)
  2339. // Step 4
  2340. // Calculate |b0-b1| in t1 and note the sign (i.e., the borrow flag)
  2341. // Step 5
  2342. // Call kara mul to calculate |a1-a0|*|b0-b1| in (t2),
  2343. // while using temporary storage in t4 along the way.
  2344. // Step 6
  2345. // Check the borrow signs. If a1-a0 and b0-b1 have the same signs,
  2346. // then add |a1-a0|*|b0-b1| to r1, otherwise subtract it from r1.
  2347. const std::uint_fast32_t nh = n / 2U;
  2348. const std::uint32_t* a0 = a + nh;
  2349. const std::uint32_t* a1 = a + 0U;
  2350. const std::uint32_t* b0 = b + nh;
  2351. const std::uint32_t* b1 = b + 0U;
  2352. std::uint32_t* r0 = r + 0U;
  2353. std::uint32_t* r1 = r + nh;
  2354. std::uint32_t* r2 = r + n;
  2355. std::uint32_t* t0 = t + 0U;
  2356. std::uint32_t* t1 = t + nh;
  2357. std::uint32_t* t2 = t + n;
  2358. std::uint32_t* t4 = t + (n + n);
  2359. // Step 1
  2360. eval_multiply_kara_n_by_n_to_2n(r0, a1, b1, static_cast<std::uint32_t>(nh), t);
  2361. eval_multiply_kara_n_by_n_to_2n(r2, a0, b0, static_cast<std::uint32_t>(nh), t);
  2362. std::copy(r0, r0 + (2U * n), t0);
  2363. // Step 2
  2364. std::uint32_t carry;
  2365. carry = eval_add_n(r1, r1, t0, static_cast<std::int32_t>(n));
  2366. eval_multiply_kara_propagate_carry(r0, static_cast<std::uint32_t>(nh), carry);
  2367. carry = eval_add_n(r1, r1, t2, static_cast<std::int32_t>(n));
  2368. eval_multiply_kara_propagate_carry(r0, static_cast<std::uint32_t>(nh), carry);
  2369. // Step 3
  2370. const int cmp_result_a1a0 = compare_ranges(a1, a0, static_cast<std::uint32_t>(nh));
  2371. if(cmp_result_a1a0 == 1)
  2372. static_cast<void>(eval_subtract_n(t0, a1, a0, static_cast<std::int32_t>(nh)));
  2373. else if(cmp_result_a1a0 == -1)
  2374. static_cast<void>(eval_subtract_n(t0, a0, a1, static_cast<std::int32_t>(nh)));
  2375. // Step 4
  2376. const int cmp_result_b0b1 = compare_ranges(b0, b1, static_cast<std::uint32_t>(nh));
  2377. if(cmp_result_b0b1 == 1)
  2378. static_cast<void>(eval_subtract_n(t1, b0, b1, static_cast<std::int32_t>(nh)));
  2379. else if(cmp_result_b0b1 == -1)
  2380. static_cast<void>(eval_subtract_n(t1, b1, b0, static_cast<std::int32_t>(nh)));
  2381. // Step 5
  2382. eval_multiply_kara_n_by_n_to_2n(t2, t0, t1, static_cast<std::uint32_t>(nh), t4);
  2383. // Step 6
  2384. if((cmp_result_a1a0 * cmp_result_b0b1) == 1)
  2385. {
  2386. carry = eval_add_n(r1, r1, t2, static_cast<std::int32_t>(n));
  2387. eval_multiply_kara_propagate_carry(r0, static_cast<std::uint32_t>(nh), carry);
  2388. }
  2389. else if((cmp_result_a1a0 * cmp_result_b0b1) == -1)
  2390. {
  2391. const bool has_borrow = eval_subtract_n(r1, r1, t2, static_cast<std::int32_t>(n));
  2392. eval_multiply_kara_propagate_borrow(r0, static_cast<std::uint32_t>(nh), has_borrow);
  2393. }
  2394. }
  2395. }
  2396. template <unsigned Digits10, class ExponentType, class Allocator>
  2397. cpp_dec_float<Digits10, ExponentType, Allocator> cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(const long long p)
  2398. {
  2399. static const std::array<cpp_dec_float<Digits10, ExponentType, Allocator>, 256u> local_pow2_data =
  2400. {{
  2401. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 29u, 38735877u, 5571876u, 99218413u, 43055614u, 19454666u, 38919302u, 18803771u, 87926569u, 60431486u, 36817932u, 12890625u }, -40 ),
  2402. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 58u, 77471754u, 11143753u, 98436826u, 86111228u, 38909332u, 77838604u, 37607543u, 75853139u, 20862972u, 73635864u, 25781250u }, -40 ),
  2403. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 117u, 54943508u, 22287507u, 96873653u, 72222456u, 77818665u, 55677208u, 75215087u, 51706278u, 41725945u, 47271728u, 51562500u }, -40 ),
  2404. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 235u, 9887016u, 44575015u, 93747307u, 44444913u, 55637331u, 11354417u, 50430175u, 3412556u, 83451890u, 94543457u, 3125000u }, -40 ),
  2405. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 470u, 19774032u, 89150031u, 87494614u, 88889827u, 11274662u, 22708835u, 860350u, 6825113u, 66903781u, 89086914u, 6250000u }, -40 ),
  2406. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 940u, 39548065u, 78300063u, 74989229u, 77779654u, 22549324u, 45417670u, 1720700u, 13650227u, 33807563u, 78173828u, 12500000u }, -40 ),
  2407. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1880u, 79096131u, 56600127u, 49978459u, 55559308u, 45098648u, 90835340u, 3441400u, 27300454u, 67615127u, 56347656u, 25000000u }, -40 ),
  2408. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3761u, 58192263u, 13200254u, 99956919u, 11118616u, 90197297u, 81670680u, 6882800u, 54600909u, 35230255u, 12695312u, 50000000u }, -40 ),
  2409. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7523u, 16384526u, 26400509u, 99913838u, 22237233u, 80394595u, 63341360u, 13765601u, 9201818u, 70460510u, 25390625u }, -40 ),
  2410. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 15046u, 32769052u, 52801019u, 99827676u, 44474467u, 60789191u, 26682720u, 27531202u, 18403637u, 40921020u, 50781250u }, -40 ),
  2411. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 30092u, 65538105u, 5602039u, 99655352u, 88948935u, 21578382u, 53365440u, 55062404u, 36807274u, 81842041u, 1562500u }, -40 ),
  2412. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 60185u, 31076210u, 11204079u, 99310705u, 77897870u, 43156765u, 6730881u, 10124808u, 73614549u, 63684082u, 3125000u }, -40 ),
  2413. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 120370u, 62152420u, 22408159u, 98621411u, 55795740u, 86313530u, 13461762u, 20249617u, 47229099u, 27368164u, 6250000u }, -40 ),
  2414. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 240741u, 24304840u, 44816319u, 97242823u, 11591481u, 72627060u, 26923524u, 40499234u, 94458198u, 54736328u, 12500000u }, -40 ),
  2415. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 481482u, 48609680u, 89632639u, 94485646u, 23182963u, 45254120u, 53847048u, 80998469u, 88916397u, 9472656u, 25000000u }, -40 ),
  2416. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 962964u, 97219361u, 79265279u, 88971292u, 46365926u, 90508241u, 7694097u, 61996939u, 77832794u, 18945312u, 50000000u }, -40 ),
  2417. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1925929u, 94438723u, 58530559u, 77942584u, 92731853u, 81016482u, 15388195u, 23993879u, 55665588u, 37890625u }, -40 ),
  2418. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3851859u, 88877447u, 17061119u, 55885169u, 85463707u, 62032964u, 30776390u, 47987759u, 11331176u, 75781250u }, -40 ),
  2419. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7703719u, 77754894u, 34122239u, 11770339u, 70927415u, 24065928u, 61552780u, 95975518u, 22662353u, 51562500u }, -40 ),
  2420. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 15407439u, 55509788u, 68244478u, 23540679u, 41854830u, 48131857u, 23105561u, 91951036u, 45324707u, 3125000u }, -40 ),
  2421. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 30814879u, 11019577u, 36488956u, 47081358u, 83709660u, 96263714u, 46211123u, 83902072u, 90649414u, 6250000u }, -40 ),
  2422. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 61629758u, 22039154u, 72977912u, 94162717u, 67419321u, 92527428u, 92422247u, 67804145u, 81298828u, 12500000u }, -40 ),
  2423. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 23259516u, 44078309u, 45955825u, 88325435u, 34838643u, 85054857u, 84844495u, 35608291u, 62597656u, 25000000u }, -32 ),
  2424. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 46519032u, 88156618u, 91911651u, 76650870u, 69677287u, 70109715u, 69688990u, 71216583u, 25195312u, 50000000u }, -32 ),
  2425. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4u, 93038065u, 76313237u, 83823303u, 53301741u, 39354575u, 40219431u, 39377981u, 42433166u, 50390625u }, -32 ),
  2426. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9u, 86076131u, 52626475u, 67646607u, 6603482u, 78709150u, 80438862u, 78755962u, 84866333u, 781250u }, -32 ),
  2427. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 19u, 72152263u, 5252951u, 35293214u, 13206965u, 57418301u, 60877725u, 57511925u, 69732666u, 1562500u }, -32 ),
  2428. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 39u, 44304526u, 10505902u, 70586428u, 26413931u, 14836603u, 21755451u, 15023851u, 39465332u, 3125000u }, -32 ),
  2429. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 78u, 88609052u, 21011805u, 41172856u, 52827862u, 29673206u, 43510902u, 30047702u, 78930664u, 6250000u }, -32 ),
  2430. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 157u, 77218104u, 42023610u, 82345713u, 5655724u, 59346412u, 87021804u, 60095405u, 57861328u, 12500000u }, -32 ),
  2431. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 315u, 54436208u, 84047221u, 64691426u, 11311449u, 18692825u, 74043609u, 20190811u, 15722656u, 25000000u }, -32 ),
  2432. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 631u, 8872417u, 68094443u, 29382852u, 22622898u, 37385651u, 48087218u, 40381622u, 31445312u, 50000000u }, -32 ),
  2433. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1262u, 17744835u, 36188886u, 58765704u, 45245796u, 74771302u, 96174436u, 80763244u, 62890625u }, -32 ),
  2434. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2524u, 35489670u, 72377773u, 17531408u, 90491593u, 49542605u, 92348873u, 61526489u, 25781250u }, -32 ),
  2435. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5048u, 70979341u, 44755546u, 35062817u, 80983186u, 99085211u, 84697747u, 23052978u, 51562500u }, -32 ),
  2436. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10097u, 41958682u, 89511092u, 70125635u, 61966373u, 98170423u, 69395494u, 46105957u, 3125000u }, -32 ),
  2437. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 20194u, 83917365u, 79022185u, 40251271u, 23932747u, 96340847u, 38790988u, 92211914u, 6250000u }, -32 ),
  2438. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 40389u, 67834731u, 58044370u, 80502542u, 47865495u, 92681694u, 77581977u, 84423828u, 12500000u }, -32 ),
  2439. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 80779u, 35669463u, 16088741u, 61005084u, 95730991u, 85363389u, 55163955u, 68847656u, 25000000u }, -32 ),
  2440. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 161558u, 71338926u, 32177483u, 22010169u, 91461983u, 70726779u, 10327911u, 37695312u, 50000000u }, -32 ),
  2441. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 323117u, 42677852u, 64354966u, 44020339u, 82923967u, 41453558u, 20655822u, 75390625u }, -32 ),
  2442. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 646234u, 85355705u, 28709932u, 88040679u, 65847934u, 82907116u, 41311645u, 50781250u }, -32 ),
  2443. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1292469u, 70711410u, 57419865u, 76081359u, 31695869u, 65814232u, 82623291u, 1562500u }, -32 ),
  2444. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2584939u, 41422821u, 14839731u, 52162718u, 63391739u, 31628465u, 65246582u, 3125000u }, -32 ),
  2445. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5169878u, 82845642u, 29679463u, 4325437u, 26783478u, 63256931u, 30493164u, 6250000u }, -32 ),
  2446. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10339757u, 65691284u, 59358926u, 8650874u, 53566957u, 26513862u, 60986328u, 12500000u }, -32 ),
  2447. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 20679515u, 31382569u, 18717852u, 17301749u, 7133914u, 53027725u, 21972656u, 25000000u }, -32 ),
  2448. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 41359030u, 62765138u, 37435704u, 34603498u, 14267829u, 6055450u, 43945312u, 50000000u }, -32 ),
  2449. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 82718061u, 25530276u, 74871408u, 69206996u, 28535658u, 12110900u, 87890625u }, -32 ),
  2450. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 65436122u, 51060553u, 49742817u, 38413992u, 57071316u, 24221801u, 75781250u }, -24 ),
  2451. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3u, 30872245u, 2121106u, 99485634u, 76827985u, 14142632u, 48443603u, 51562500u }, -24 ),
  2452. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6u, 61744490u, 4242213u, 98971269u, 53655970u, 28285264u, 96887207u, 3125000u }, -24 ),
  2453. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 13u, 23488980u, 8484427u, 97942539u, 7311940u, 56570529u, 93774414u, 6250000u }, -24 ),
  2454. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 26u, 46977960u, 16968855u, 95885078u, 14623881u, 13141059u, 87548828u, 12500000u }, -24 ),
  2455. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 52u, 93955920u, 33937711u, 91770156u, 29247762u, 26282119u, 75097656u, 25000000u }, -24 ),
  2456. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 105u, 87911840u, 67875423u, 83540312u, 58495524u, 52564239u, 50195312u, 50000000u }, -24 ),
  2457. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 211u, 75823681u, 35750847u, 67080625u, 16991049u, 5128479u, 390625u }, -24 ),
  2458. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 423u, 51647362u, 71501695u, 34161250u, 33982098u, 10256958u, 781250u }, -24 ),
  2459. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 847u, 3294725u, 43003390u, 68322500u, 67964196u, 20513916u, 1562500u }, -24 ),
  2460. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1694u, 6589450u, 86006781u, 36645001u, 35928392u, 41027832u, 3125000u }, -24 ),
  2461. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3388u, 13178901u, 72013562u, 73290002u, 71856784u, 82055664u, 6250000u }, -24 ),
  2462. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6776u, 26357803u, 44027125u, 46580005u, 43713569u, 64111328u, 12500000u }, -24 ),
  2463. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 13552u, 52715606u, 88054250u, 93160010u, 87427139u, 28222656u, 25000000u }, -24 ),
  2464. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 27105u, 5431213u, 76108501u, 86320021u, 74854278u, 56445312u, 50000000u }, -24 ),
  2465. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 54210u, 10862427u, 52217003u, 72640043u, 49708557u, 12890625u }, -24 ),
  2466. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 108420u, 21724855u, 4434007u, 45280086u, 99417114u, 25781250u }, -24 ),
  2467. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 216840u, 43449710u, 8868014u, 90560173u, 98834228u, 51562500u }, -24 ),
  2468. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 433680u, 86899420u, 17736029u, 81120347u, 97668457u, 3125000u }, -24 ),
  2469. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 867361u, 73798840u, 35472059u, 62240695u, 95336914u, 6250000u }, -24 ),
  2470. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1734723u, 47597680u, 70944119u, 24481391u, 90673828u, 12500000u }, -24 ),
  2471. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3469446u, 95195361u, 41888238u, 48962783u, 81347656u, 25000000u }, -24 ),
  2472. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6938893u, 90390722u, 83776476u, 97925567u, 62695312u, 50000000u }, -24 ),
  2473. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 13877787u, 80781445u, 67552953u, 95851135u, 25390625u }, -24 ),
  2474. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 27755575u, 61562891u, 35105907u, 91702270u, 50781250u }, -24 ),
  2475. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 55511151u, 23125782u, 70211815u, 83404541u, 1562500u }, -24 ),
  2476. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 11022302u, 46251565u, 40423631u, 66809082u, 3125000u }, -16 ),
  2477. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 22044604u, 92503130u, 80847263u, 33618164u, 6250000u }, -16 ),
  2478. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4u, 44089209u, 85006261u, 61694526u, 67236328u, 12500000u }, -16 ),
  2479. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 8u, 88178419u, 70012523u, 23389053u, 34472656u, 25000000u }, -16 ),
  2480. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 17u, 76356839u, 40025046u, 46778106u, 68945312u, 50000000u }, -16 ),
  2481. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 35u, 52713678u, 80050092u, 93556213u, 37890625u }, -16 ),
  2482. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 71u, 5427357u, 60100185u, 87112426u, 75781250u }, -16 ),
  2483. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 142u, 10854715u, 20200371u, 74224853u, 51562500u }, -16 ),
  2484. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 284u, 21709430u, 40400743u, 48449707u, 3125000u }, -16 ),
  2485. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 568u, 43418860u, 80801486u, 96899414u, 6250000u }, -16 ),
  2486. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1136u, 86837721u, 61602973u, 93798828u, 12500000u }, -16 ),
  2487. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2273u, 73675443u, 23205947u, 87597656u, 25000000u }, -16 ),
  2488. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4547u, 47350886u, 46411895u, 75195312u, 50000000u }, -16 ),
  2489. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9094u, 94701772u, 92823791u, 50390625u }, -16 ),
  2490. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 18189u, 89403545u, 85647583u, 781250u }, -16 ),
  2491. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 36379u, 78807091u, 71295166u, 1562500u }, -16 ),
  2492. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 72759u, 57614183u, 42590332u, 3125000u }, -16 ),
  2493. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 145519u, 15228366u, 85180664u, 6250000u }, -16 ),
  2494. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 291038u, 30456733u, 70361328u, 12500000u }, -16 ),
  2495. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 582076u, 60913467u, 40722656u, 25000000u }, -16 ),
  2496. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1164153u, 21826934u, 81445312u, 50000000u }, -16 ),
  2497. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2328306u, 43653869u, 62890625u }, -16 ),
  2498. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4656612u, 87307739u, 25781250u }, -16 ),
  2499. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9313225u, 74615478u, 51562500u }, -16 ),
  2500. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 18626451u, 49230957u, 3125000u }, -16 ),
  2501. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 37252902u, 98461914u, 6250000u }, -16 ),
  2502. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 74505805u, 96923828u, 12500000u }, -16 ),
  2503. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 49011611u, 93847656u, 25000000u }, -8 ),
  2504. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 98023223u, 87695312u, 50000000u }, -8 ),
  2505. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5u, 96046447u, 75390625u }, -8 ),
  2506. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 11u, 92092895u, 50781250u }, -8 ),
  2507. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 23u, 84185791u, 1562500u }, -8 ),
  2508. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 47u, 68371582u, 3125000u }, -8 ),
  2509. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 95u, 36743164u, 6250000u }, -8 ),
  2510. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 190u, 73486328u, 12500000u }, -8 ),
  2511. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 381u, 46972656u, 25000000u }, -8 ),
  2512. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 762u, 93945312u, 50000000u }, -8 ),
  2513. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1525u, 87890625u }, -8 ),
  2514. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3051u, 75781250u }, -8 ),
  2515. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6103u, 51562500u }, -8 ),
  2516. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 12207u, 3125000u }, -8 ),
  2517. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 24414u, 6250000u }, -8 ),
  2518. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 48828u, 12500000u }, -8 ),
  2519. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 97656u, 25000000u }, -8 ),
  2520. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 195312u, 50000000u }, -8 ),
  2521. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 390625u }, -8 ),
  2522. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 781250u }, -8 ),
  2523. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1562500u }, -8 ),
  2524. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3125000u }, -8 ),
  2525. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6250000u }, -8 ),
  2526. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 12500000u }, -8 ),
  2527. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 25000000u }, -8 ),
  2528. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 50000000u }, -8 ),
  2529. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u }, 0 ),
  2530. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u }, 0 ),
  2531. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4u }, 0 ),
  2532. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 8u }, 0 ),
  2533. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 16u }, 0 ),
  2534. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 32u }, 0 ),
  2535. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 64u }, 0 ),
  2536. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 128u }, 0 ),
  2537. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 256u }, 0 ),
  2538. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 512u }, 0 ),
  2539. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1024u }, 0 ),
  2540. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2048u }, 0 ),
  2541. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4096u }, 0 ),
  2542. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 8192u }, 0 ),
  2543. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 16384u }, 0 ),
  2544. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 32768u }, 0 ),
  2545. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 65536u }, 0 ),
  2546. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 131072u }, 0 ),
  2547. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 262144u }, 0 ),
  2548. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 524288u }, 0 ),
  2549. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1048576u }, 0 ),
  2550. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2097152u }, 0 ),
  2551. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4194304u }, 0 ),
  2552. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 8388608u }, 0 ),
  2553. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 16777216u }, 0 ),
  2554. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 33554432u }, 0 ),
  2555. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 67108864u }, 0 ),
  2556. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 34217728u }, 8 ),
  2557. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 68435456u }, 8 ),
  2558. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5u, 36870912u }, 8 ),
  2559. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10u, 73741824u }, 8 ),
  2560. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 21u, 47483648u }, 8 ),
  2561. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 42u, 94967296u }, 8 ),
  2562. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 85u, 89934592u }, 8 ),
  2563. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 171u, 79869184u }, 8 ),
  2564. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 343u, 59738368u }, 8 ),
  2565. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 687u, 19476736u }, 8 ),
  2566. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1374u, 38953472u }, 8 ),
  2567. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2748u, 77906944u }, 8 ),
  2568. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5497u, 55813888u }, 8 ),
  2569. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10995u, 11627776u }, 8 ),
  2570. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 21990u, 23255552u }, 8 ),
  2571. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 43980u, 46511104u }, 8 ),
  2572. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 87960u, 93022208u }, 8 ),
  2573. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 175921u, 86044416u }, 8 ),
  2574. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 351843u, 72088832u }, 8 ),
  2575. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 703687u, 44177664u }, 8 ),
  2576. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1407374u, 88355328u }, 8 ),
  2577. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2814749u, 76710656u }, 8 ),
  2578. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5629499u, 53421312u }, 8 ),
  2579. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 11258999u, 6842624u }, 8 ),
  2580. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 22517998u, 13685248u }, 8 ),
  2581. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 45035996u, 27370496u }, 8 ),
  2582. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 90071992u, 54740992u }, 8 ),
  2583. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 80143985u, 9481984u }, 16 ),
  2584. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3u, 60287970u, 18963968u }, 16 ),
  2585. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7u, 20575940u, 37927936u }, 16 ),
  2586. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 14u, 41151880u, 75855872u }, 16 ),
  2587. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 28u, 82303761u, 51711744u }, 16 ),
  2588. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 57u, 64607523u, 3423488u }, 16 ),
  2589. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 115u, 29215046u, 6846976u }, 16 ),
  2590. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 230u, 58430092u, 13693952u }, 16 ),
  2591. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 461u, 16860184u, 27387904u }, 16 ),
  2592. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 922u, 33720368u, 54775808u }, 16 ),
  2593. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1844u, 67440737u, 9551616u }, 16 ),
  2594. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3689u, 34881474u, 19103232u }, 16 ),
  2595. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7378u, 69762948u, 38206464u }, 16 ),
  2596. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 14757u, 39525896u, 76412928u }, 16 ),
  2597. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 29514u, 79051793u, 52825856u }, 16 ),
  2598. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 59029u, 58103587u, 5651712u }, 16 ),
  2599. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 118059u, 16207174u, 11303424u }, 16 ),
  2600. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 236118u, 32414348u, 22606848u }, 16 ),
  2601. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 472236u, 64828696u, 45213696u }, 16 ),
  2602. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 944473u, 29657392u, 90427392u }, 16 ),
  2603. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1888946u, 59314785u, 80854784u }, 16 ),
  2604. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3777893u, 18629571u, 61709568u }, 16 ),
  2605. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 7555786u, 37259143u, 23419136u }, 16 ),
  2606. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 15111572u, 74518286u, 46838272u }, 16 ),
  2607. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 30223145u, 49036572u, 93676544u }, 16 ),
  2608. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 60446290u, 98073145u, 87353088u }, 16 ),
  2609. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 20892581u, 96146291u, 74706176u }, 24 ),
  2610. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2u, 41785163u, 92292583u, 49412352u }, 24 ),
  2611. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4u, 83570327u, 84585166u, 98824704u }, 24 ),
  2612. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9u, 67140655u, 69170333u, 97649408u }, 24 ),
  2613. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 19u, 34281311u, 38340667u, 95298816u }, 24 ),
  2614. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 38u, 68562622u, 76681335u, 90597632u }, 24 ),
  2615. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 77u, 37125245u, 53362671u, 81195264u }, 24 ),
  2616. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 154u, 74250491u, 6725343u, 62390528u }, 24 ),
  2617. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 309u, 48500982u, 13450687u, 24781056u }, 24 ),
  2618. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 618u, 97001964u, 26901374u, 49562112u }, 24 ),
  2619. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1237u, 94003928u, 53802748u, 99124224u }, 24 ),
  2620. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2475u, 88007857u, 7605497u, 98248448u }, 24 ),
  2621. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 4951u, 76015714u, 15210995u, 96496896u }, 24 ),
  2622. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 9903u, 52031428u, 30421991u, 92993792u }, 24 ),
  2623. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 19807u, 4062856u, 60843983u, 85987584u }, 24 ),
  2624. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 39614u, 8125713u, 21687967u, 71975168u }, 24 ),
  2625. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 79228u, 16251426u, 43375935u, 43950336u }, 24 ),
  2626. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 158456u, 32502852u, 86751870u, 87900672u }, 24 ),
  2627. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 316912u, 65005705u, 73503741u, 75801344u }, 24 ),
  2628. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 633825u, 30011411u, 47007483u, 51602688u }, 24 ),
  2629. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1267650u, 60022822u, 94014967u, 3205376u }, 24 ),
  2630. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 2535301u, 20045645u, 88029934u, 6410752u }, 24 ),
  2631. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 5070602u, 40091291u, 76059868u, 12821504u }, 24 ),
  2632. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 10141204u, 80182583u, 52119736u, 25643008u }, 24 ),
  2633. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 20282409u, 60365167u, 4239472u, 51286016u }, 24 ),
  2634. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 40564819u, 20730334u, 8478945u, 2572032u }, 24 ),
  2635. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 81129638u, 41460668u, 16957890u, 5144064u }, 24 ),
  2636. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1u, 62259276u, 82921336u, 33915780u, 10288128u }, 32 ),
  2637. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3u, 24518553u, 65842672u, 67831560u, 20576256u }, 32 ),
  2638. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6u, 49037107u, 31685345u, 35663120u, 41152512u }, 32 ),
  2639. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 12u, 98074214u, 63370690u, 71326240u, 82305024u }, 32 ),
  2640. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 25u, 96148429u, 26741381u, 42652481u, 64610048u }, 32 ),
  2641. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 51u, 92296858u, 53482762u, 85304963u, 29220096u }, 32 ),
  2642. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 103u, 84593717u, 6965525u, 70609926u, 58440192u }, 32 ),
  2643. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 207u, 69187434u, 13931051u, 41219853u, 16880384u }, 32 ),
  2644. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 415u, 38374868u, 27862102u, 82439706u, 33760768u }, 32 ),
  2645. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 830u, 76749736u, 55724205u, 64879412u, 67521536u }, 32 ),
  2646. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1661u, 53499473u, 11448411u, 29758825u, 35043072u }, 32 ),
  2647. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 3323u, 6998946u, 22896822u, 59517650u, 70086144u }, 32 ),
  2648. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 6646u, 13997892u, 45793645u, 19035301u, 40172288u }, 32 ),
  2649. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 13292u, 27995784u, 91587290u, 38070602u, 80344576u }, 32 ),
  2650. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 26584u, 55991569u, 83174580u, 76141205u, 60689152u }, 32 ),
  2651. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 53169u, 11983139u, 66349161u, 52282411u, 21378304u }, 32 ),
  2652. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 106338u, 23966279u, 32698323u, 4564822u, 42756608u }, 32 ),
  2653. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 212676u, 47932558u, 65396646u, 9129644u, 85513216u }, 32 ),
  2654. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 425352u, 95865117u, 30793292u, 18259289u, 71026432u }, 32 ),
  2655. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 850705u, 91730234u, 61586584u, 36518579u, 42052864u }, 32 ),
  2656. cpp_dec_float<Digits10, ExponentType, Allocator>::from_lst( { 1701411u, 83460469u, 23173168u, 73037158u, 84105728u }, 32 ),
  2657. }};
  2658. cpp_dec_float<Digits10, ExponentType, Allocator> t;
  2659. if(p < -128L)
  2660. default_ops::detail::pow_imp(t, cpp_dec_float<Digits10, ExponentType, Allocator>::half(), static_cast<unsigned long long>(-p), std::integral_constant<bool, false>());
  2661. else if ((p >= -128L) && (p <= 127L))
  2662. t = local_pow2_data[std::size_t(p + 128)];
  2663. else
  2664. default_ops::detail::pow_imp(t, cpp_dec_float<Digits10, ExponentType, Allocator>::two(), static_cast<unsigned long long>(p), std::integral_constant<bool, false>());
  2665. return t;
  2666. }
  2667. template <unsigned Digits10, class ExponentType, class Allocator>
  2668. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2669. {
  2670. result += o;
  2671. }
  2672. template <unsigned Digits10, class ExponentType, class Allocator>
  2673. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2674. {
  2675. result -= o;
  2676. }
  2677. template <unsigned Digits10, class ExponentType, class Allocator>
  2678. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2679. {
  2680. result *= o;
  2681. }
  2682. template <unsigned Digits10, class ExponentType, class Allocator>
  2683. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2684. {
  2685. result /= o;
  2686. }
  2687. template <unsigned Digits10, class ExponentType, class Allocator>
  2688. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const unsigned long long& o)
  2689. {
  2690. result.add_unsigned_long_long(o);
  2691. }
  2692. template <unsigned Digits10, class ExponentType, class Allocator>
  2693. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const unsigned long long& o)
  2694. {
  2695. result.sub_unsigned_long_long(o);
  2696. }
  2697. template <unsigned Digits10, class ExponentType, class Allocator>
  2698. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const unsigned long long& o)
  2699. {
  2700. result.mul_unsigned_long_long(o);
  2701. }
  2702. template <unsigned Digits10, class ExponentType, class Allocator>
  2703. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const unsigned long long& o)
  2704. {
  2705. result.div_unsigned_long_long(o);
  2706. }
  2707. template <unsigned Digits10, class ExponentType, class Allocator>
  2708. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, long long o)
  2709. {
  2710. if (o < 0)
  2711. result.sub_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2712. else
  2713. {
  2714. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  2715. result.add_unsigned_long_long(static_cast<local_ulonglong_type>(o));
  2716. }
  2717. }
  2718. template <unsigned Digits10, class ExponentType, class Allocator>
  2719. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, long long o)
  2720. {
  2721. if (o < 0)
  2722. result.add_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2723. else
  2724. {
  2725. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  2726. result.sub_unsigned_long_long(static_cast<local_ulonglong_type>(o));
  2727. }
  2728. }
  2729. template <unsigned Digits10, class ExponentType, class Allocator>
  2730. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, long long o)
  2731. {
  2732. if (o < 0)
  2733. {
  2734. result.mul_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2735. result.negate();
  2736. }
  2737. else
  2738. {
  2739. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  2740. result.mul_unsigned_long_long(static_cast<local_ulonglong_type>(o));
  2741. }
  2742. }
  2743. template <unsigned Digits10, class ExponentType, class Allocator>
  2744. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, long long o)
  2745. {
  2746. if (o < 0)
  2747. {
  2748. result.div_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2749. result.negate();
  2750. }
  2751. else
  2752. {
  2753. using local_ulonglong_type = typename boost::multiprecision::detail::make_unsigned<long long>::type;
  2754. result.div_unsigned_long_long(static_cast<local_ulonglong_type>(o));
  2755. }
  2756. }
  2757. template <unsigned Digits10, class ExponentType, class Allocator>
  2758. inline void eval_convert_to(unsigned long long* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2759. {
  2760. *result = val.extract_unsigned_long_long();
  2761. }
  2762. template <unsigned Digits10, class ExponentType, class Allocator>
  2763. inline void eval_convert_to(long long* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2764. {
  2765. *result = val.extract_signed_long_long();
  2766. }
  2767. #ifdef BOOST_HAS_INT128
  2768. template <unsigned Digits10, class ExponentType, class Allocator>
  2769. inline void eval_convert_to(uint128_type* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2770. {
  2771. *result = val.extract_unsigned_int128();
  2772. }
  2773. template <unsigned Digits10, class ExponentType, class Allocator>
  2774. inline void eval_convert_to(int128_type* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2775. {
  2776. *result = val.extract_signed_int128();
  2777. }
  2778. #endif
  2779. template <unsigned Digits10, class ExponentType, class Allocator>
  2780. inline void eval_convert_to(long double* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2781. {
  2782. *result = val.extract_long_double();
  2783. }
  2784. template <unsigned Digits10, class ExponentType, class Allocator>
  2785. inline void eval_convert_to(double* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2786. {
  2787. *result = val.extract_double();
  2788. }
  2789. #if defined(BOOST_HAS_FLOAT128)
  2790. template <unsigned Digits10, class ExponentType, class Allocator>
  2791. inline void eval_convert_to(float128_type* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2792. {
  2793. *result = float128_procs::strtoflt128(val.str(0, std::ios_base::scientific).c_str(), nullptr);
  2794. }
  2795. #endif
  2796. //
  2797. // Non member function support:
  2798. //
  2799. template <unsigned Digits10, class ExponentType, class Allocator>
  2800. inline int eval_fpclassify(const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2801. {
  2802. if ((x.isinf)())
  2803. return FP_INFINITE;
  2804. if ((x.isnan)())
  2805. return FP_NAN;
  2806. if (x.iszero())
  2807. return FP_ZERO;
  2808. return FP_NORMAL;
  2809. }
  2810. template <unsigned Digits10, class ExponentType, class Allocator>
  2811. inline void eval_abs(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2812. {
  2813. result = x;
  2814. if (x.isneg())
  2815. result.negate();
  2816. }
  2817. template <unsigned Digits10, class ExponentType, class Allocator>
  2818. inline void eval_fabs(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2819. {
  2820. result = x;
  2821. if (x.isneg())
  2822. result.negate();
  2823. }
  2824. template <unsigned Digits10, class ExponentType, class Allocator>
  2825. inline void eval_sqrt(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2826. {
  2827. result = x;
  2828. result.calculate_sqrt();
  2829. }
  2830. template <unsigned Digits10, class ExponentType, class Allocator>
  2831. inline void eval_floor(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2832. {
  2833. result = x;
  2834. if (!(x.isfinite)() || x.isint())
  2835. {
  2836. if ((x.isnan)())
  2837. errno = EDOM;
  2838. return;
  2839. }
  2840. if (x.isneg())
  2841. result -= cpp_dec_float<Digits10, ExponentType, Allocator>::one();
  2842. result = result.extract_integer_part();
  2843. }
  2844. template <unsigned Digits10, class ExponentType, class Allocator>
  2845. inline void eval_ceil(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2846. {
  2847. result = x;
  2848. if (!(x.isfinite)() || x.isint())
  2849. {
  2850. if ((x.isnan)())
  2851. errno = EDOM;
  2852. return;
  2853. }
  2854. if (!x.isneg())
  2855. result += cpp_dec_float<Digits10, ExponentType, Allocator>::one();
  2856. result = result.extract_integer_part();
  2857. }
  2858. template <unsigned Digits10, class ExponentType, class Allocator>
  2859. inline void eval_trunc(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2860. {
  2861. if (x.isint() || !(x.isfinite)())
  2862. {
  2863. result = x;
  2864. if ((x.isnan)())
  2865. errno = EDOM;
  2866. return;
  2867. }
  2868. result = x.extract_integer_part();
  2869. }
  2870. template <unsigned Digits10, class ExponentType, class Allocator>
  2871. inline ExponentType eval_ilogb(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2872. {
  2873. if (val.iszero())
  2874. return (std::numeric_limits<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>::min)();
  2875. if ((val.isinf)())
  2876. return INT_MAX;
  2877. if ((val.isnan)())
  2878. #ifdef FP_ILOGBNAN
  2879. return FP_ILOGBNAN;
  2880. #else
  2881. return INT_MAX;
  2882. #endif
  2883. // Set result, to the exponent of val:
  2884. return val.order();
  2885. }
  2886. template <unsigned Digits10, class ExponentType, class Allocator, class ArgType>
  2887. inline void eval_scalbn(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val, ArgType e_)
  2888. {
  2889. using default_ops::eval_multiply;
  2890. const typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type e = static_cast<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>(e_);
  2891. cpp_dec_float<Digits10, ExponentType, Allocator> t(1.0, e);
  2892. eval_multiply(result, val, t);
  2893. }
  2894. template <unsigned Digits10, class ExponentType, class Allocator, class ArgType>
  2895. inline void eval_ldexp(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x, ArgType e)
  2896. {
  2897. const long long the_exp = static_cast<long long>(e);
  2898. if ((the_exp > (std::numeric_limits<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>::max)()) || (the_exp < (std::numeric_limits<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>::min)()))
  2899. BOOST_MP_THROW_EXCEPTION(std::runtime_error(std::string("Exponent value is out of range.")));
  2900. result = x;
  2901. if ((the_exp > static_cast<long long>(-std::numeric_limits<long long>::digits)) && (the_exp < static_cast<long long>(0)))
  2902. result.div_unsigned_long_long(1ULL << static_cast<long long>(-the_exp));
  2903. else if ((the_exp < static_cast<long long>(std::numeric_limits<long long>::digits)) && (the_exp > static_cast<long long>(0)))
  2904. result.mul_unsigned_long_long(1ULL << the_exp);
  2905. else if (the_exp != static_cast<long long>(0))
  2906. {
  2907. if ((the_exp < cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp / 2) && (x.order() > 0))
  2908. {
  2909. long long half_exp = e / 2;
  2910. cpp_dec_float<Digits10, ExponentType, Allocator> t = cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(half_exp);
  2911. result *= t;
  2912. if (2 * half_exp != e)
  2913. t *= 2;
  2914. result *= t;
  2915. }
  2916. else
  2917. result *= cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(e);
  2918. }
  2919. }
  2920. template <unsigned Digits10, class ExponentType, class Allocator>
  2921. inline void eval_frexp(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x, ExponentType* e)
  2922. {
  2923. result = x;
  2924. if (result.iszero() || (result.isinf)() || (result.isnan)())
  2925. {
  2926. *e = 0;
  2927. return;
  2928. }
  2929. if (result.isneg())
  2930. result.negate();
  2931. typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type t = result.order();
  2932. BOOST_MP_USING_ABS
  2933. if (abs(t) < ((std::numeric_limits<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>::max)() / 1000))
  2934. {
  2935. t *= 1000;
  2936. t /= 301;
  2937. }
  2938. else
  2939. {
  2940. t /= 301;
  2941. t *= 1000;
  2942. }
  2943. result *= cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(-t);
  2944. if (result.iszero() || (result.isinf)() || (result.isnan)())
  2945. {
  2946. // pow2 overflowed, slip the calculation up:
  2947. result = x;
  2948. if (result.isneg())
  2949. result.negate();
  2950. t /= 2;
  2951. result *= cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(-t);
  2952. }
  2953. BOOST_MP_USING_ABS
  2954. if (abs(result.order()) > 5)
  2955. {
  2956. // If our first estimate doesn't get close enough then try recursion until we do:
  2957. typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type e2;
  2958. cpp_dec_float<Digits10, ExponentType, Allocator> r2;
  2959. eval_frexp(r2, result, &e2);
  2960. // overflow protection:
  2961. if ((t > 0) && (e2 > 0) && (t > (std::numeric_limits<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>::max)() - e2))
  2962. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Exponent is too large to be represented as a power of 2."));
  2963. if ((t < 0) && (e2 < 0) && (t < (std::numeric_limits<typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type>::min)() - e2))
  2964. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Exponent is too large to be represented as a power of 2."));
  2965. t += e2;
  2966. result = r2;
  2967. }
  2968. while (result.compare(cpp_dec_float<Digits10, ExponentType, Allocator>::one()) >= 0)
  2969. {
  2970. result /= cpp_dec_float<Digits10, ExponentType, Allocator>::two();
  2971. ++t;
  2972. }
  2973. while (result.compare(cpp_dec_float<Digits10, ExponentType, Allocator>::half()) < 0)
  2974. {
  2975. result *= cpp_dec_float<Digits10, ExponentType, Allocator>::two();
  2976. --t;
  2977. }
  2978. *e = t;
  2979. if (x.isneg())
  2980. result.negate();
  2981. }
  2982. template <unsigned Digits10, class ExponentType, class Allocator>
  2983. inline typename std::enable_if< !std::is_same<ExponentType, int>::value>::type eval_frexp(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x, int* e)
  2984. {
  2985. typename cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type t;
  2986. eval_frexp(result, x, &t);
  2987. if ((t > (std::numeric_limits<int>::max)()) || (t < (std::numeric_limits<int>::min)()))
  2988. BOOST_MP_THROW_EXCEPTION(std::runtime_error("Exponent is outside the range of an int"));
  2989. *e = static_cast<int>(t);
  2990. }
  2991. template <unsigned Digits10, class ExponentType, class Allocator>
  2992. inline bool eval_is_zero(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2993. {
  2994. return val.iszero();
  2995. }
  2996. template <unsigned Digits10, class ExponentType, class Allocator>
  2997. inline int eval_get_sign(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2998. {
  2999. return val.iszero() ? 0 : val.isneg() ? -1 : 1;
  3000. }
  3001. template <unsigned Digits10, class ExponentType, class Allocator>
  3002. inline std::size_t hash_value(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  3003. {
  3004. return val.hash();
  3005. }
  3006. } // namespace backends
  3007. namespace detail {
  3008. template <unsigned Digits10, class ExponentType, class Allocator>
  3009. struct transcendental_reduction_type<boost::multiprecision::backends::cpp_dec_float<Digits10, ExponentType, Allocator> >
  3010. {
  3011. //
  3012. // The type used for trigonometric reduction needs 3 times the precision of the base type.
  3013. // This is double the precision of the original type, plus the largest exponent supported.
  3014. // As a practical measure the largest argument supported is 1/eps, as supporting larger
  3015. // arguments requires the division of argument by PI/2 to also be done at higher precision,
  3016. // otherwise the result (an integer) can not be represented exactly.
  3017. //
  3018. // See ARGUMENT REDUCTION FOR HUGE ARGUMENTS. K C Ng.
  3019. //
  3020. using type = boost::multiprecision::backends::cpp_dec_float<Digits10 * 3, ExponentType, Allocator>;
  3021. };
  3022. } // namespace detail
  3023. }} // namespace boost::multiprecision
  3024. namespace std {
  3025. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3026. class numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >
  3027. {
  3028. public:
  3029. static constexpr bool is_specialized = true;
  3030. static constexpr bool is_signed = true;
  3031. static constexpr bool is_integer = false;
  3032. static constexpr bool is_exact = false;
  3033. static constexpr bool is_bounded = true;
  3034. static constexpr bool is_modulo = false;
  3035. static constexpr bool is_iec559 = false;
  3036. static constexpr int digits = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  3037. static constexpr int digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  3038. static constexpr int max_digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_digits10;
  3039. static constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type min_exponent = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp; // Type differs from int.
  3040. static constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type min_exponent10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp10; // Type differs from int.
  3041. static constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type max_exponent = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp; // Type differs from int.
  3042. static constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type max_exponent10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp10; // Type differs from int.
  3043. static constexpr int radix = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_radix;
  3044. static constexpr std::float_round_style round_style = std::round_indeterminate;
  3045. static constexpr bool has_infinity = true;
  3046. static constexpr bool has_quiet_NaN = true;
  3047. static constexpr bool has_signaling_NaN = false;
  3048. #ifdef _MSC_VER
  3049. #pragma warning(push)
  3050. #pragma warning(disable : 4996)
  3051. #endif
  3052. static constexpr std::float_denorm_style has_denorm = std::denorm_absent;
  3053. #ifdef _MSC_VER
  3054. #pragma warning(pop)
  3055. #endif
  3056. static constexpr bool has_denorm_loss = false;
  3057. static constexpr bool traps = false;
  3058. static constexpr bool tinyness_before = false;
  3059. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates>(min)() { return (boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::min)(); }
  3060. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates>(max)() { return (boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::max)(); }
  3061. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> lowest() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::zero(); }
  3062. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> epsilon() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::eps(); }
  3063. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> round_error() { return 0.5L; }
  3064. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> infinity() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::inf(); }
  3065. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> quiet_NaN() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::nan(); }
  3066. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> signaling_NaN() { return boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::zero(); }
  3067. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> denorm_min() { return (boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::min)(); }
  3068. };
  3069. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3070. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::digits;
  3071. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3072. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::digits10;
  3073. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3074. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_digits10;
  3075. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3076. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_signed;
  3077. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3078. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_integer;
  3079. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3080. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_exact;
  3081. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3082. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::radix;
  3083. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3084. constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::min_exponent;
  3085. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3086. constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::min_exponent10;
  3087. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3088. constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_exponent;
  3089. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3090. constexpr typename boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_exponent10;
  3091. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3092. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_infinity;
  3093. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3094. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_quiet_NaN;
  3095. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3096. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_signaling_NaN;
  3097. #ifdef _MSC_VER
  3098. #pragma warning(push)
  3099. #pragma warning(disable : 4996)
  3100. #endif
  3101. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3102. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_denorm;
  3103. #ifdef _MSC_VER
  3104. #pragma warning(pop)
  3105. #endif
  3106. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3107. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_denorm_loss;
  3108. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3109. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_iec559;
  3110. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3111. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_bounded;
  3112. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3113. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_modulo;
  3114. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3115. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::traps;
  3116. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3117. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::tinyness_before;
  3118. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  3119. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::round_style;
  3120. } // namespace std
  3121. #ifdef BOOST_MP_MATH_AVAILABLE
  3122. namespace boost {
  3123. namespace math {
  3124. namespace policies {
  3125. template <unsigned Digits10, class ExponentType, class Allocator, class Policy, boost::multiprecision::expression_template_option ExpressionTemplates>
  3126. struct precision<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates>, Policy>
  3127. {
  3128. // Define a local copy of cpp_dec_float_digits10 because it might differ
  3129. // from the template parameter Digits10 for small or large digit counts.
  3130. static constexpr std::int32_t cpp_dec_float_digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  3131. using precision_type = typename Policy::precision_type ;
  3132. using digits_2 = digits2<static_cast<int>(((cpp_dec_float_digits10 + 1LL) * 1000LL) / 301LL)>;
  3133. using type = typename std::conditional<
  3134. ((digits_2::value <= precision_type::value) || (Policy::precision_type::value <= 0)),
  3135. // Default case, full precision for RealType:
  3136. digits_2,
  3137. // User customized precision:
  3138. precision_type>::type;
  3139. };
  3140. }
  3141. }} // namespace boost::math::policies
  3142. #endif
  3143. #ifdef BOOST_MSVC
  3144. #pragma warning(pop)
  3145. #endif
  3146. #endif