cpp_dec_float.hpp 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright Christopher Kormanyos 2002 - 2013.
  3. // Copyright 2011 -2013 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. // Note that there are no "noexcept" specifications on the functions in this file: there are too many
  12. // calls to lexical_cast (and similar) to easily analyse the code for correctness. So until compilers
  13. // can detect noexcept misuse at compile time, the only realistic option is to simply not use it here.
  14. //
  15. #ifndef BOOST_MP_CPP_DEC_FLOAT_BACKEND_HPP
  16. #define BOOST_MP_CPP_DEC_FLOAT_BACKEND_HPP
  17. #include <boost/config.hpp>
  18. #include <cstdint>
  19. #include <limits>
  20. #include <array>
  21. #include <cstdint>
  22. #include <boost/functional/hash_fwd.hpp>
  23. #include <boost/multiprecision/number.hpp>
  24. #include <boost/multiprecision/detail/big_lanczos.hpp>
  25. #include <boost/multiprecision/detail/dynamic_array.hpp>
  26. #include <boost/multiprecision/detail/itos.hpp>
  27. //
  28. // Headers required for Boost.Math integration:
  29. //
  30. #include <boost/math/policies/policy.hpp>
  31. //
  32. // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
  33. //
  34. #include <boost/math/special_functions/asinh.hpp>
  35. #include <boost/math/special_functions/acosh.hpp>
  36. #include <boost/math/special_functions/atanh.hpp>
  37. #include <boost/math/special_functions/cbrt.hpp>
  38. #include <boost/math/special_functions/expm1.hpp>
  39. #include <boost/math/special_functions/gamma.hpp>
  40. #ifdef BOOST_MSVC
  41. #pragma warning(push)
  42. #pragma warning(disable : 6326) // comparison of two constants
  43. #endif
  44. namespace boost {
  45. namespace multiprecision {
  46. namespace backends {
  47. template <unsigned Digits10, class ExponentType = std::int32_t, class Allocator = void>
  48. class cpp_dec_float;
  49. } // namespace backends
  50. template <unsigned Digits10, class ExponentType, class Allocator>
  51. struct number_category<backends::cpp_dec_float<Digits10, ExponentType, Allocator> > : public std::integral_constant<int, number_kind_floating_point>
  52. {};
  53. namespace backends {
  54. template <unsigned Digits10, class ExponentType, class Allocator>
  55. class cpp_dec_float
  56. {
  57. private:
  58. static constexpr const std::int32_t cpp_dec_float_digits10_setting = Digits10;
  59. // We need at least 16-bits in the exponent type to do anything sensible:
  60. static_assert(boost::multiprecision::detail::is_signed<ExponentType>::value, "ExponentType must be a signed built in integer type.");
  61. static_assert(sizeof(ExponentType) > 1, "ExponentType is too small.");
  62. public:
  63. using signed_types = std::tuple<boost::long_long_type> ;
  64. using unsigned_types = std::tuple<boost::ulong_long_type>;
  65. using float_types = std::tuple<double, long double> ;
  66. using exponent_type = ExponentType ;
  67. static constexpr const std::int32_t cpp_dec_float_radix = 10L;
  68. static constexpr const std::int32_t cpp_dec_float_digits10_limit_lo = 9L;
  69. static constexpr const std::int32_t cpp_dec_float_digits10_limit_hi = boost::integer_traits<std::int32_t>::const_max - 100;
  70. static constexpr const std::int32_t cpp_dec_float_digits10 = ((cpp_dec_float_digits10_setting < cpp_dec_float_digits10_limit_lo) ? cpp_dec_float_digits10_limit_lo : ((cpp_dec_float_digits10_setting > cpp_dec_float_digits10_limit_hi) ? cpp_dec_float_digits10_limit_hi : cpp_dec_float_digits10_setting));
  71. static constexpr const ExponentType cpp_dec_float_max_exp10 = (static_cast<ExponentType>(1) << (std::numeric_limits<ExponentType>::digits - 5));
  72. static constexpr const ExponentType cpp_dec_float_min_exp10 = -cpp_dec_float_max_exp10;
  73. static constexpr const ExponentType cpp_dec_float_max_exp = cpp_dec_float_max_exp10;
  74. static constexpr const ExponentType cpp_dec_float_min_exp = cpp_dec_float_min_exp10;
  75. 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");
  76. private:
  77. static constexpr const std::int32_t cpp_dec_float_elem_digits10 = 8L;
  78. static constexpr const std::int32_t cpp_dec_float_elem_mask = 100000000L;
  79. static_assert(0 == cpp_dec_float_max_exp10 % cpp_dec_float_elem_digits10, "Failed digit sanity check");
  80. // There are three guard limbs.
  81. // 1) The first limb has 'play' from 1...8 decimal digits.
  82. // 2) The last limb also has 'play' from 1...8 decimal digits.
  83. // 3) One limb can get lost when justifying after multiply,
  84. // as only half of the triangle is multiplied and a carry
  85. // from below is missing.
  86. static constexpr const std::int32_t cpp_dec_float_elem_number_request = static_cast<std::int32_t>((cpp_dec_float_digits10 / cpp_dec_float_elem_digits10) + (((cpp_dec_float_digits10 % cpp_dec_float_elem_digits10) != 0) ? 1 : 0));
  87. // The number of elements needed (with a minimum of two) plus three added guard limbs.
  88. static constexpr const std::int32_t cpp_dec_float_elem_number = static_cast<std::int32_t>(((cpp_dec_float_elem_number_request < 2L) ? 2L : cpp_dec_float_elem_number_request) + 3L);
  89. public:
  90. static constexpr const std::int32_t cpp_dec_float_total_digits10 = static_cast<std::int32_t>(cpp_dec_float_elem_number * cpp_dec_float_elem_digits10);
  91. private:
  92. typedef enum enum_fpclass_type
  93. {
  94. cpp_dec_float_finite,
  95. cpp_dec_float_inf,
  96. cpp_dec_float_NaN
  97. } fpclass_type;
  98. using array_type = typename std::conditional<std::is_void<Allocator>::value,
  99. std::array<std::uint32_t, cpp_dec_float_elem_number>,
  100. detail::dynamic_array<std::uint32_t, cpp_dec_float_elem_number, Allocator> >::type;
  101. array_type data;
  102. ExponentType exp;
  103. bool neg;
  104. fpclass_type fpclass;
  105. std::int32_t prec_elem;
  106. //
  107. // Special values constructor:
  108. //
  109. cpp_dec_float(fpclass_type c) : data(),
  110. exp(static_cast<ExponentType>(0)),
  111. neg(false),
  112. fpclass(c),
  113. prec_elem(cpp_dec_float_elem_number) {}
  114. public:
  115. // Constructors
  116. cpp_dec_float() noexcept(noexcept(array_type())) : data(),
  117. exp(static_cast<ExponentType>(0)),
  118. neg(false),
  119. fpclass(cpp_dec_float_finite),
  120. prec_elem(cpp_dec_float_elem_number) {}
  121. cpp_dec_float(const char* s) : data(),
  122. exp(static_cast<ExponentType>(0)),
  123. neg(false),
  124. fpclass(cpp_dec_float_finite),
  125. prec_elem(cpp_dec_float_elem_number)
  126. {
  127. *this = s;
  128. }
  129. template <class I>
  130. cpp_dec_float(I i, typename std::enable_if<boost::multiprecision::detail::is_unsigned<I>::value >::type* = 0) : data(),
  131. exp(static_cast<ExponentType>(0)),
  132. neg(false),
  133. fpclass(cpp_dec_float_finite),
  134. prec_elem(cpp_dec_float_elem_number)
  135. {
  136. from_unsigned_long_long(i);
  137. }
  138. template <class I>
  139. cpp_dec_float(I i, typename std::enable_if<boost::multiprecision::detail::is_signed<I>::value && boost::multiprecision::detail::is_integral<I>::value>::type* = 0) : data(),
  140. exp(static_cast<ExponentType>(0)),
  141. neg(false),
  142. fpclass(cpp_dec_float_finite),
  143. prec_elem(cpp_dec_float_elem_number)
  144. {
  145. if (i < 0)
  146. {
  147. from_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(i));
  148. negate();
  149. }
  150. else
  151. from_unsigned_long_long(i);
  152. }
  153. cpp_dec_float(const cpp_dec_float& f) noexcept(noexcept(array_type(std::declval<const array_type&>()))) : data(f.data),
  154. exp(f.exp),
  155. neg(f.neg),
  156. fpclass(f.fpclass),
  157. prec_elem(f.prec_elem) {}
  158. template <unsigned D, class ET, class A>
  159. cpp_dec_float(const cpp_dec_float<D, ET, A>& f, typename std::enable_if<D <= Digits10>::type* = 0) : data(),
  160. exp(f.exp),
  161. neg(f.neg),
  162. fpclass(static_cast<fpclass_type>(static_cast<int>(f.fpclass))),
  163. prec_elem(cpp_dec_float_elem_number)
  164. {
  165. std::copy(f.data.begin(), f.data.begin() + f.prec_elem, data.begin());
  166. }
  167. template <unsigned D, class ET, class A>
  168. explicit cpp_dec_float(const cpp_dec_float<D, ET, A>& f, typename std::enable_if< !(D <= Digits10)>::type* = 0) : data(),
  169. exp(f.exp),
  170. neg(f.neg),
  171. fpclass(static_cast<fpclass_type>(static_cast<int>(f.fpclass))),
  172. prec_elem(cpp_dec_float_elem_number)
  173. {
  174. // TODO: this doesn't round!
  175. std::copy(f.data.begin(), f.data.begin() + prec_elem, data.begin());
  176. }
  177. template <class F>
  178. cpp_dec_float(const F val, typename std::enable_if<std::is_floating_point<F>::value
  179. #ifdef BOOST_HAS_FLOAT128
  180. && !std::is_same<F, __float128>::value
  181. #endif
  182. >::type* = 0) : data(),
  183. exp(static_cast<ExponentType>(0)),
  184. neg(false),
  185. fpclass(cpp_dec_float_finite),
  186. prec_elem(cpp_dec_float_elem_number)
  187. {
  188. *this = val;
  189. }
  190. cpp_dec_float(const double mantissa, const ExponentType exponent);
  191. std::size_t hash() const
  192. {
  193. std::size_t result = 0;
  194. for (int i = 0; i < prec_elem; ++i)
  195. boost::hash_combine(result, data[i]);
  196. boost::hash_combine(result, exp);
  197. boost::hash_combine(result, neg);
  198. boost::hash_combine(result, fpclass);
  199. return result;
  200. }
  201. // Specific special values.
  202. static const cpp_dec_float& nan()
  203. {
  204. static const cpp_dec_float val(cpp_dec_float_NaN);
  205. return val;
  206. }
  207. static const cpp_dec_float& inf()
  208. {
  209. static const cpp_dec_float val(cpp_dec_float_inf);
  210. return val;
  211. }
  212. static const cpp_dec_float&(max)()
  213. {
  214. static cpp_dec_float val_max = std::string("1.0e" + boost::multiprecision::detail::itos(cpp_dec_float_max_exp10)).c_str();
  215. return val_max;
  216. }
  217. static const cpp_dec_float&(min)()
  218. {
  219. static cpp_dec_float val_min = std::string("1.0e" + boost::multiprecision::detail::itos(cpp_dec_float_min_exp10)).c_str();
  220. return val_min;
  221. }
  222. static const cpp_dec_float& zero()
  223. {
  224. static cpp_dec_float val(static_cast<boost::ulong_long_type>(0u));
  225. return val;
  226. }
  227. static const cpp_dec_float& one()
  228. {
  229. static cpp_dec_float val(static_cast<boost::ulong_long_type>(1u));
  230. return val;
  231. }
  232. static const cpp_dec_float& two()
  233. {
  234. static cpp_dec_float val(static_cast<boost::ulong_long_type>(2u));
  235. return val;
  236. }
  237. static const cpp_dec_float& half()
  238. {
  239. static cpp_dec_float val(0.5L);
  240. return val;
  241. }
  242. static const cpp_dec_float& double_min()
  243. {
  244. static cpp_dec_float val((std::numeric_limits<double>::min)());
  245. return val;
  246. }
  247. static const cpp_dec_float& double_max()
  248. {
  249. static cpp_dec_float val((std::numeric_limits<double>::max)());
  250. return val;
  251. }
  252. static const cpp_dec_float& long_double_min()
  253. {
  254. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  255. static cpp_dec_float val(static_cast<long double>((std::numeric_limits<double>::min)()));
  256. #else
  257. static cpp_dec_float val((std::numeric_limits<long double>::min)());
  258. #endif
  259. return val;
  260. }
  261. static const cpp_dec_float& long_double_max()
  262. {
  263. #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
  264. static cpp_dec_float val(static_cast<long double>((std::numeric_limits<double>::max)()));
  265. #else
  266. static cpp_dec_float val((std::numeric_limits<long double>::max)());
  267. #endif
  268. return val;
  269. }
  270. static const cpp_dec_float& long_long_max()
  271. {
  272. static cpp_dec_float val((std::numeric_limits<boost::long_long_type>::max)());
  273. return val;
  274. }
  275. static const cpp_dec_float& long_long_min()
  276. {
  277. static cpp_dec_float val((std::numeric_limits<boost::long_long_type>::min)());
  278. return val;
  279. }
  280. static const cpp_dec_float& ulong_long_max()
  281. {
  282. static cpp_dec_float val((std::numeric_limits<boost::ulong_long_type>::max)());
  283. return val;
  284. }
  285. static const cpp_dec_float& eps()
  286. {
  287. static cpp_dec_float val(1.0, 1 - static_cast<int>(cpp_dec_float_digits10));
  288. return val;
  289. }
  290. // Basic operations.
  291. cpp_dec_float& operator=(const cpp_dec_float& v) noexcept(noexcept(std::declval<array_type&>() = std::declval<const array_type&>()))
  292. {
  293. data = v.data;
  294. exp = v.exp;
  295. neg = v.neg;
  296. fpclass = v.fpclass;
  297. prec_elem = v.prec_elem;
  298. return *this;
  299. }
  300. template <unsigned D>
  301. cpp_dec_float& operator=(const cpp_dec_float<D>& f)
  302. {
  303. exp = f.exp;
  304. neg = f.neg;
  305. fpclass = static_cast<enum_fpclass_type>(static_cast<int>(f.fpclass));
  306. unsigned elems = (std::min)(f.prec_elem, cpp_dec_float_elem_number);
  307. std::copy(f.data.begin(), f.data.begin() + elems, data.begin());
  308. std::fill(data.begin() + elems, data.end(), 0);
  309. prec_elem = cpp_dec_float_elem_number;
  310. return *this;
  311. }
  312. cpp_dec_float& operator=(boost::long_long_type v)
  313. {
  314. if (v < 0)
  315. {
  316. from_unsigned_long_long(1u - boost::ulong_long_type(v + 1)); // Avoid undefined behaviour in negation of minimum value for long long
  317. negate();
  318. }
  319. else
  320. from_unsigned_long_long(v);
  321. return *this;
  322. }
  323. cpp_dec_float& operator=(boost::ulong_long_type v)
  324. {
  325. from_unsigned_long_long(v);
  326. return *this;
  327. }
  328. template <class Float>
  329. typename std::enable_if<std::is_floating_point<Float>::value, cpp_dec_float&>::type operator=(Float v);
  330. cpp_dec_float& operator=(const char* v)
  331. {
  332. rd_string(v);
  333. return *this;
  334. }
  335. cpp_dec_float& operator+=(const cpp_dec_float& v);
  336. cpp_dec_float& operator-=(const cpp_dec_float& v);
  337. cpp_dec_float& operator*=(const cpp_dec_float& v);
  338. cpp_dec_float& operator/=(const cpp_dec_float& v);
  339. cpp_dec_float& add_unsigned_long_long(const boost::ulong_long_type n)
  340. {
  341. cpp_dec_float t;
  342. t.from_unsigned_long_long(n);
  343. return *this += t;
  344. }
  345. cpp_dec_float& sub_unsigned_long_long(const boost::ulong_long_type n)
  346. {
  347. cpp_dec_float t;
  348. t.from_unsigned_long_long(n);
  349. return *this -= t;
  350. }
  351. cpp_dec_float& mul_unsigned_long_long(const boost::ulong_long_type n);
  352. cpp_dec_float& div_unsigned_long_long(const boost::ulong_long_type n);
  353. // Elementary primitives.
  354. cpp_dec_float& calculate_inv();
  355. cpp_dec_float& calculate_sqrt();
  356. void negate()
  357. {
  358. if (!iszero())
  359. neg = !neg;
  360. }
  361. // Comparison functions
  362. bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_NaN); }
  363. bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_inf); }
  364. bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_finite); }
  365. bool iszero() const
  366. {
  367. return ((fpclass == cpp_dec_float_finite) && (data[0u] == 0u));
  368. }
  369. bool isone() const;
  370. bool isint() const;
  371. bool isneg() const { return neg; }
  372. // Operators pre-increment and pre-decrement
  373. cpp_dec_float& operator++()
  374. {
  375. return *this += one();
  376. }
  377. cpp_dec_float& operator--()
  378. {
  379. return *this -= one();
  380. }
  381. std::string str(std::intmax_t digits, std::ios_base::fmtflags f) const;
  382. int compare(const cpp_dec_float& v) const;
  383. template <class V>
  384. int compare(const V& v) const
  385. {
  386. cpp_dec_float<Digits10, ExponentType, Allocator> t;
  387. t = v;
  388. return compare(t);
  389. }
  390. void swap(cpp_dec_float& v)
  391. {
  392. data.swap(v.data);
  393. std::swap(exp, v.exp);
  394. std::swap(neg, v.neg);
  395. std::swap(fpclass, v.fpclass);
  396. std::swap(prec_elem, v.prec_elem);
  397. }
  398. double extract_double() const;
  399. long double extract_long_double() const;
  400. boost::long_long_type extract_signed_long_long() const;
  401. boost::ulong_long_type extract_unsigned_long_long() const;
  402. void extract_parts(double& mantissa, ExponentType& exponent) const;
  403. cpp_dec_float extract_integer_part() const;
  404. void precision(const std::int32_t prec_digits)
  405. {
  406. if (prec_digits >= cpp_dec_float_total_digits10)
  407. {
  408. prec_elem = cpp_dec_float_elem_number;
  409. }
  410. else
  411. {
  412. const std::int32_t elems = static_cast<std::int32_t>(static_cast<std::int32_t>((prec_digits + (cpp_dec_float_elem_digits10 / 2)) / cpp_dec_float_elem_digits10) + static_cast<std::int32_t>(((prec_digits % cpp_dec_float_elem_digits10) != 0) ? 1 : 0));
  413. prec_elem = (std::min)(cpp_dec_float_elem_number, (std::max)(elems, static_cast<std::int32_t>(2)));
  414. }
  415. }
  416. static cpp_dec_float pow2(boost::long_long_type i);
  417. ExponentType order() const
  418. {
  419. const bool bo_order_is_zero = ((!(isfinite)()) || (data[0] == static_cast<std::uint32_t>(0u)));
  420. //
  421. // Binary search to find the order of the leading term:
  422. //
  423. ExponentType prefix = 0;
  424. if (data[0] >= 100000UL)
  425. {
  426. if (data[0] >= 10000000UL)
  427. {
  428. if (data[0] >= 100000000UL)
  429. {
  430. if (data[0] >= 1000000000UL)
  431. prefix = 9;
  432. else
  433. prefix = 8;
  434. }
  435. else
  436. prefix = 7;
  437. }
  438. else
  439. {
  440. if (data[0] >= 1000000UL)
  441. prefix = 6;
  442. else
  443. prefix = 5;
  444. }
  445. }
  446. else
  447. {
  448. if (data[0] >= 1000UL)
  449. {
  450. if (data[0] >= 10000UL)
  451. prefix = 4;
  452. else
  453. prefix = 3;
  454. }
  455. else
  456. {
  457. if (data[0] >= 100)
  458. prefix = 2;
  459. else if (data[0] >= 10)
  460. prefix = 1;
  461. }
  462. }
  463. return (bo_order_is_zero ? static_cast<ExponentType>(0) : static_cast<ExponentType>(exp + prefix));
  464. }
  465. template <class Archive>
  466. void serialize(Archive& ar, const unsigned int /*version*/)
  467. {
  468. for (unsigned i = 0; i < data.size(); ++i)
  469. ar& boost::make_nvp("digit", data[i]);
  470. ar& boost::make_nvp("exponent", exp);
  471. ar& boost::make_nvp("sign", neg);
  472. ar& boost::make_nvp("class-type", fpclass);
  473. ar& boost::make_nvp("precision", prec_elem);
  474. }
  475. private:
  476. static bool data_elem_is_non_zero_predicate(const std::uint32_t& d) { return (d != static_cast<std::uint32_t>(0u)); }
  477. 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)); }
  478. static bool char_is_nonzero_predicate(const char& c) { return (c != static_cast<char>('0')); }
  479. void from_unsigned_long_long(const boost::ulong_long_type u);
  480. int cmp_data(const array_type& vd) const;
  481. static std::uint32_t mul_loop_uv(std::uint32_t* const u, const std::uint32_t* const v, const std::int32_t p);
  482. static std::uint32_t mul_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p);
  483. static std::uint32_t div_loop_n(std::uint32_t* const u, std::uint32_t n, const std::int32_t p);
  484. bool rd_string(const char* const s);
  485. template <unsigned D, class ET, class A>
  486. friend class cpp_dec_float;
  487. };
  488. template <unsigned Digits10, class ExponentType, class Allocator>
  489. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_radix;
  490. template <unsigned Digits10, class ExponentType, class Allocator>
  491. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10_setting;
  492. template <unsigned Digits10, class ExponentType, class Allocator>
  493. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10_limit_lo;
  494. template <unsigned Digits10, class ExponentType, class Allocator>
  495. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10_limit_hi;
  496. template <unsigned Digits10, class ExponentType, class Allocator>
  497. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  498. template <unsigned Digits10, class ExponentType, class Allocator>
  499. const ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp;
  500. template <unsigned Digits10, class ExponentType, class Allocator>
  501. const ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp;
  502. template <unsigned Digits10, class ExponentType, class Allocator>
  503. const ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp10;
  504. template <unsigned Digits10, class ExponentType, class Allocator>
  505. const ExponentType cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp10;
  506. template <unsigned Digits10, class ExponentType, class Allocator>
  507. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_digits10;
  508. template <unsigned Digits10, class ExponentType, class Allocator>
  509. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_number_request;
  510. template <unsigned Digits10, class ExponentType, class Allocator>
  511. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_number;
  512. template <unsigned Digits10, class ExponentType, class Allocator>
  513. const std::int32_t cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_elem_mask;
  514. template <unsigned Digits10, class ExponentType, class Allocator>
  515. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator+=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  516. {
  517. if ((isnan)())
  518. {
  519. return *this;
  520. }
  521. if ((isinf)())
  522. {
  523. if ((v.isinf)() && (isneg() != v.isneg()))
  524. {
  525. *this = nan();
  526. }
  527. return *this;
  528. }
  529. if (iszero())
  530. {
  531. return operator=(v);
  532. }
  533. if ((v.isnan)() || (v.isinf)())
  534. {
  535. *this = v;
  536. return *this;
  537. }
  538. // Get the offset for the add/sub operation.
  539. constexpr const ExponentType max_delta_exp = static_cast<ExponentType>((cpp_dec_float_elem_number - 1) * cpp_dec_float_elem_digits10);
  540. const ExponentType ofs_exp = static_cast<ExponentType>(exp - v.exp);
  541. // Check if the operation is out of range, requiring special handling.
  542. if (v.iszero() || (ofs_exp > max_delta_exp))
  543. {
  544. // Result is *this unchanged since v is negligible compared to *this.
  545. return *this;
  546. }
  547. else if (ofs_exp < -max_delta_exp)
  548. {
  549. // Result is *this = v since *this is negligible compared to v.
  550. return operator=(v);
  551. }
  552. // Do the add/sub operation.
  553. typename array_type::iterator p_u = data.begin();
  554. typename array_type::const_iterator p_v = v.data.begin();
  555. bool b_copy = false;
  556. const std::int32_t ofs = static_cast<std::int32_t>(static_cast<std::int32_t>(ofs_exp) / cpp_dec_float_elem_digits10);
  557. array_type n_data;
  558. if (neg == v.neg)
  559. {
  560. // Add v to *this, where the data array of either *this or v
  561. // might have to be treated with a positive, negative or zero offset.
  562. // The result is stored in *this. The data are added one element
  563. // at a time, each element with carry.
  564. if (ofs >= static_cast<std::int32_t>(0))
  565. {
  566. std::copy(v.data.begin(), v.data.end() - static_cast<size_t>(ofs), n_data.begin() + static_cast<size_t>(ofs));
  567. std::fill(n_data.begin(), n_data.begin() + static_cast<size_t>(ofs), static_cast<std::uint32_t>(0u));
  568. p_v = n_data.begin();
  569. }
  570. else
  571. {
  572. std::copy(data.begin(), data.end() - static_cast<size_t>(-ofs), n_data.begin() + static_cast<size_t>(-ofs));
  573. std::fill(n_data.begin(), n_data.begin() + static_cast<size_t>(-ofs), static_cast<std::uint32_t>(0u));
  574. p_u = n_data.begin();
  575. b_copy = true;
  576. }
  577. // Addition algorithm
  578. std::uint32_t carry = static_cast<std::uint32_t>(0u);
  579. for (std::int32_t j = static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)); j >= static_cast<std::int32_t>(0); j--)
  580. {
  581. std::uint32_t t = static_cast<std::uint32_t>(static_cast<std::uint32_t>(p_u[j] + p_v[j]) + carry);
  582. carry = t / static_cast<std::uint32_t>(cpp_dec_float_elem_mask);
  583. p_u[j] = static_cast<std::uint32_t>(t - static_cast<std::uint32_t>(carry * static_cast<std::uint32_t>(cpp_dec_float_elem_mask)));
  584. }
  585. if (b_copy)
  586. {
  587. data = n_data;
  588. exp = v.exp;
  589. }
  590. // There needs to be a carry into the element -1 of the array data
  591. if (carry != static_cast<std::uint32_t>(0u))
  592. {
  593. std::copy_backward(data.begin(), data.end() - static_cast<std::size_t>(1u), data.end());
  594. data[0] = carry;
  595. exp += static_cast<ExponentType>(cpp_dec_float_elem_digits10);
  596. }
  597. }
  598. else
  599. {
  600. // Subtract v from *this, where the data array of either *this or v
  601. // might have to be treated with a positive, negative or zero offset.
  602. if ((ofs > static_cast<std::int32_t>(0)) || ((ofs == static_cast<std::int32_t>(0)) && (cmp_data(v.data) > static_cast<std::int32_t>(0))))
  603. {
  604. // In this case, |u| > |v| and ofs is positive.
  605. // Copy the data of v, shifted down to a lower value
  606. // into the data array m_n. Set the operand pointer p_v
  607. // to point to the copied, shifted data m_n.
  608. std::copy(v.data.begin(), v.data.end() - static_cast<size_t>(ofs), n_data.begin() + static_cast<size_t>(ofs));
  609. std::fill(n_data.begin(), n_data.begin() + static_cast<size_t>(ofs), static_cast<std::uint32_t>(0u));
  610. p_v = n_data.begin();
  611. }
  612. else
  613. {
  614. if (ofs != static_cast<std::int32_t>(0))
  615. {
  616. // In this case, |u| < |v| and ofs is negative.
  617. // Shift the data of u down to a lower value.
  618. std::copy_backward(data.begin(), data.end() - static_cast<size_t>(-ofs), data.end());
  619. std::fill(data.begin(), data.begin() + static_cast<size_t>(-ofs), static_cast<std::uint32_t>(0u));
  620. }
  621. // Copy the data of v into the data array n_data.
  622. // Set the u-pointer p_u to point to m_n and the
  623. // operand pointer p_v to point to the shifted
  624. // data m_data.
  625. n_data = v.data;
  626. p_u = n_data.begin();
  627. p_v = data.begin();
  628. b_copy = true;
  629. }
  630. std::int32_t j;
  631. // Subtraction algorithm
  632. std::int32_t borrow = static_cast<std::int32_t>(0);
  633. for (j = static_cast<std::int32_t>(cpp_dec_float_elem_number - static_cast<std::int32_t>(1)); j >= static_cast<std::int32_t>(0); j--)
  634. {
  635. std::int32_t t = static_cast<std::int32_t>(static_cast<std::int32_t>(static_cast<std::int32_t>(p_u[j]) - static_cast<std::int32_t>(p_v[j])) - borrow);
  636. // Underflow? Borrow?
  637. if (t < static_cast<std::int32_t>(0))
  638. {
  639. // Yes, underflow and borrow
  640. t += static_cast<std::int32_t>(cpp_dec_float_elem_mask);
  641. borrow = static_cast<std::int32_t>(1);
  642. }
  643. else
  644. {
  645. borrow = static_cast<std::int32_t>(0);
  646. }
  647. p_u[j] = static_cast<std::uint32_t>(static_cast<std::uint32_t>(t) % static_cast<std::uint32_t>(cpp_dec_float_elem_mask));
  648. }
  649. if (b_copy)
  650. {
  651. data = n_data;
  652. exp = v.exp;
  653. neg = v.neg;
  654. }
  655. // Is it necessary to justify the data?
  656. const typename array_type::const_iterator first_nonzero_elem = std::find_if(data.begin(), data.end(), data_elem_is_non_zero_predicate);
  657. if (first_nonzero_elem != data.begin())
  658. {
  659. if (first_nonzero_elem == data.end())
  660. {
  661. // This result of the subtraction is exactly zero.
  662. // Reset the sign and the exponent.
  663. neg = false;
  664. exp = static_cast<ExponentType>(0);
  665. }
  666. else
  667. {
  668. // Justify the data
  669. const std::size_t sj = static_cast<std::size_t>(std::distance<typename array_type::const_iterator>(data.begin(), first_nonzero_elem));
  670. std::copy(data.begin() + static_cast<std::size_t>(sj), data.end(), data.begin());
  671. std::fill(data.end() - sj, data.end(), static_cast<std::uint32_t>(0u));
  672. exp -= static_cast<ExponentType>(sj * static_cast<std::size_t>(cpp_dec_float_elem_digits10));
  673. }
  674. }
  675. }
  676. // Handle underflow.
  677. if (iszero())
  678. return (*this = zero());
  679. // Check for potential overflow.
  680. const bool b_result_might_overflow = (exp >= static_cast<ExponentType>(cpp_dec_float_max_exp10));
  681. // Handle overflow.
  682. if (b_result_might_overflow)
  683. {
  684. const bool b_result_is_neg = neg;
  685. neg = false;
  686. if (compare((cpp_dec_float::max)()) > 0)
  687. *this = inf();
  688. neg = b_result_is_neg;
  689. }
  690. return *this;
  691. }
  692. template <unsigned Digits10, class ExponentType, class Allocator>
  693. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator-=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  694. {
  695. // Use *this - v = -(-*this + v).
  696. negate();
  697. *this += v;
  698. negate();
  699. return *this;
  700. }
  701. template <unsigned Digits10, class ExponentType, class Allocator>
  702. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator*=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  703. {
  704. // Evaluate the sign of the result.
  705. const bool b_result_is_neg = (neg != v.neg);
  706. // Artificially set the sign of the result to be positive.
  707. neg = false;
  708. // Handle special cases like zero, inf and NaN.
  709. const bool b_u_is_inf = (isinf)();
  710. const bool b_v_is_inf = (v.isinf)();
  711. const bool b_u_is_zero = iszero();
  712. const bool b_v_is_zero = v.iszero();
  713. if (((isnan)() || (v.isnan)()) || (b_u_is_inf && b_v_is_zero) || (b_v_is_inf && b_u_is_zero))
  714. {
  715. *this = nan();
  716. return *this;
  717. }
  718. if (b_u_is_inf || b_v_is_inf)
  719. {
  720. *this = inf();
  721. if (b_result_is_neg)
  722. negate();
  723. return *this;
  724. }
  725. if (b_u_is_zero || b_v_is_zero)
  726. {
  727. return *this = zero();
  728. }
  729. // Check for potential overflow or underflow.
  730. const bool b_result_might_overflow = ((exp + v.exp) >= static_cast<ExponentType>(cpp_dec_float_max_exp10));
  731. const bool b_result_might_underflow = ((exp + v.exp) <= static_cast<ExponentType>(cpp_dec_float_min_exp10));
  732. // Set the exponent of the result.
  733. exp += v.exp;
  734. const std::int32_t prec_mul = (std::min)(prec_elem, v.prec_elem);
  735. const std::uint32_t carry = mul_loop_uv(data.data(), v.data.data(), prec_mul);
  736. // Handle a potential carry.
  737. if (carry != static_cast<std::uint32_t>(0u))
  738. {
  739. exp += cpp_dec_float_elem_digits10;
  740. // Shift the result of the multiplication one element to the right...
  741. std::copy_backward(data.begin(),
  742. data.begin() + static_cast<std::size_t>(prec_elem - static_cast<std::int32_t>(1)),
  743. data.begin() + static_cast<std::size_t>(prec_elem));
  744. // ... And insert the carry.
  745. data.front() = carry;
  746. }
  747. // Handle overflow.
  748. if (b_result_might_overflow && (compare((cpp_dec_float::max)()) > 0))
  749. {
  750. *this = inf();
  751. }
  752. // Handle underflow.
  753. if (b_result_might_underflow && (compare((cpp_dec_float::min)()) < 0))
  754. {
  755. *this = zero();
  756. return *this;
  757. }
  758. // Set the sign of the result.
  759. neg = b_result_is_neg;
  760. return *this;
  761. }
  762. template <unsigned Digits10, class ExponentType, class Allocator>
  763. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::operator/=(const cpp_dec_float<Digits10, ExponentType, Allocator>& v)
  764. {
  765. if (iszero())
  766. {
  767. if ((v.isnan)())
  768. {
  769. return *this = v;
  770. }
  771. else if (v.iszero())
  772. {
  773. return *this = nan();
  774. }
  775. }
  776. const bool u_and_v_are_finite_and_identical = ((isfinite)() && (fpclass == v.fpclass) && (exp == v.exp) && (cmp_data(v.data) == static_cast<std::int32_t>(0)));
  777. if (u_and_v_are_finite_and_identical)
  778. {
  779. if (neg != v.neg)
  780. {
  781. *this = one();
  782. negate();
  783. }
  784. else
  785. *this = one();
  786. return *this;
  787. }
  788. else
  789. {
  790. cpp_dec_float t(v);
  791. t.calculate_inv();
  792. return operator*=(t);
  793. }
  794. }
  795. template <unsigned Digits10, class ExponentType, class Allocator>
  796. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::mul_unsigned_long_long(const boost::ulong_long_type n)
  797. {
  798. // Multiply *this with a constant boost::ulong_long_type.
  799. // Evaluate the sign of the result.
  800. const bool b_neg = neg;
  801. // Artificially set the sign of the result to be positive.
  802. neg = false;
  803. // Handle special cases like zero, inf and NaN.
  804. const bool b_u_is_inf = (isinf)();
  805. const bool b_n_is_zero = (n == static_cast<std::int32_t>(0));
  806. if ((isnan)() || (b_u_is_inf && b_n_is_zero))
  807. {
  808. return (*this = nan());
  809. }
  810. if (b_u_is_inf)
  811. {
  812. *this = inf();
  813. if (b_neg)
  814. negate();
  815. return *this;
  816. }
  817. if (iszero() || b_n_is_zero)
  818. {
  819. // Multiplication by zero.
  820. return *this = zero();
  821. }
  822. if (n >= static_cast<boost::ulong_long_type>(cpp_dec_float_elem_mask))
  823. {
  824. neg = b_neg;
  825. cpp_dec_float t;
  826. t = n;
  827. return operator*=(t);
  828. }
  829. if (n == static_cast<boost::ulong_long_type>(1u))
  830. {
  831. neg = b_neg;
  832. return *this;
  833. }
  834. // Set up the multiplication loop.
  835. const std::uint32_t nn = static_cast<std::uint32_t>(n);
  836. const std::uint32_t carry = mul_loop_n(data.data(), nn, prec_elem);
  837. // Handle the carry and adjust the exponent.
  838. if (carry != static_cast<std::uint32_t>(0u))
  839. {
  840. exp += static_cast<ExponentType>(cpp_dec_float_elem_digits10);
  841. // Shift the result of the multiplication one element to the right.
  842. std::copy_backward(data.begin(),
  843. data.begin() + static_cast<std::size_t>(prec_elem - static_cast<std::int32_t>(1)),
  844. data.begin() + static_cast<std::size_t>(prec_elem));
  845. data.front() = static_cast<std::uint32_t>(carry);
  846. }
  847. // Check for potential overflow.
  848. const bool b_result_might_overflow = (exp >= cpp_dec_float_max_exp10);
  849. // Handle overflow.
  850. if (b_result_might_overflow && (compare((cpp_dec_float::max)()) > 0))
  851. {
  852. *this = inf();
  853. }
  854. // Set the sign.
  855. neg = b_neg;
  856. return *this;
  857. }
  858. template <unsigned Digits10, class ExponentType, class Allocator>
  859. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::div_unsigned_long_long(const boost::ulong_long_type n)
  860. {
  861. // Divide *this by a constant boost::ulong_long_type.
  862. // Evaluate the sign of the result.
  863. const bool b_neg = neg;
  864. // Artificially set the sign of the result to be positive.
  865. neg = false;
  866. // Handle special cases like zero, inf and NaN.
  867. if ((isnan)())
  868. {
  869. return *this;
  870. }
  871. if ((isinf)())
  872. {
  873. *this = inf();
  874. if (b_neg)
  875. negate();
  876. return *this;
  877. }
  878. if (n == static_cast<boost::ulong_long_type>(0u))
  879. {
  880. // Divide by 0.
  881. if (iszero())
  882. {
  883. *this = nan();
  884. return *this;
  885. }
  886. else
  887. {
  888. *this = inf();
  889. if (isneg())
  890. negate();
  891. return *this;
  892. }
  893. }
  894. if (iszero())
  895. {
  896. return *this;
  897. }
  898. if (n >= static_cast<boost::ulong_long_type>(cpp_dec_float_elem_mask))
  899. {
  900. neg = b_neg;
  901. cpp_dec_float t;
  902. t = n;
  903. return operator/=(t);
  904. }
  905. const std::uint32_t nn = static_cast<std::uint32_t>(n);
  906. if (nn > static_cast<std::uint32_t>(1u))
  907. {
  908. // Do the division loop.
  909. const std::uint32_t prev = div_loop_n(data.data(), nn, prec_elem);
  910. // Determine if one leading zero is in the result data.
  911. if (data[0] == static_cast<std::uint32_t>(0u))
  912. {
  913. // Adjust the exponent
  914. exp -= static_cast<ExponentType>(cpp_dec_float_elem_digits10);
  915. // Shift result of the division one element to the left.
  916. std::copy(data.begin() + static_cast<std::size_t>(1u),
  917. data.begin() + static_cast<std::size_t>(prec_elem - static_cast<std::int32_t>(1)),
  918. data.begin());
  919. data[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);
  920. }
  921. }
  922. // Check for potential underflow.
  923. const bool b_result_might_underflow = (exp <= cpp_dec_float_min_exp10);
  924. // Handle underflow.
  925. if (b_result_might_underflow && (compare((cpp_dec_float::min)()) < 0))
  926. return (*this = zero());
  927. // Set the sign of the result.
  928. neg = b_neg;
  929. return *this;
  930. }
  931. template <unsigned Digits10, class ExponentType, class Allocator>
  932. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::calculate_inv()
  933. {
  934. // Compute the inverse of *this.
  935. const bool b_neg = neg;
  936. neg = false;
  937. // Handle special cases like zero, inf and NaN.
  938. if (iszero())
  939. {
  940. *this = inf();
  941. if (b_neg)
  942. negate();
  943. return *this;
  944. }
  945. if ((isnan)())
  946. {
  947. return *this;
  948. }
  949. if ((isinf)())
  950. {
  951. return *this = zero();
  952. }
  953. if (isone())
  954. {
  955. if (b_neg)
  956. negate();
  957. return *this;
  958. }
  959. // Save the original *this.
  960. cpp_dec_float<Digits10, ExponentType, Allocator> x(*this);
  961. // Generate the initial estimate using division.
  962. // Extract the mantissa and exponent for a "manual"
  963. // computation of the estimate.
  964. double dd;
  965. ExponentType ne;
  966. x.extract_parts(dd, ne);
  967. // Do the inverse estimate using double precision estimates of mantissa and exponent.
  968. operator=(cpp_dec_float<Digits10, ExponentType, Allocator>(1.0 / dd, -ne));
  969. // Compute the inverse of *this. Quadratically convergent Newton-Raphson iteration
  970. // is used. During the iterative steps, the precision of the calculation is limited
  971. // to the minimum required in order to minimize the run-time.
  972. constexpr const std::int32_t double_digits10_minus_a_few = std::numeric_limits<double>::digits10 - 3;
  973. for (std::int32_t digits = double_digits10_minus_a_few; digits <= cpp_dec_float_total_digits10; digits *= static_cast<std::int32_t>(2))
  974. {
  975. // Adjust precision of the terms.
  976. precision(static_cast<std::int32_t>((digits + 10) * static_cast<std::int32_t>(2)));
  977. x.precision(static_cast<std::int32_t>((digits + 10) * static_cast<std::int32_t>(2)));
  978. // Next iteration.
  979. cpp_dec_float t(*this);
  980. t *= x;
  981. t -= two();
  982. t.negate();
  983. *this *= t;
  984. }
  985. neg = b_neg;
  986. prec_elem = cpp_dec_float_elem_number;
  987. return *this;
  988. }
  989. template <unsigned Digits10, class ExponentType, class Allocator>
  990. cpp_dec_float<Digits10, ExponentType, Allocator>& cpp_dec_float<Digits10, ExponentType, Allocator>::calculate_sqrt()
  991. {
  992. // Compute the square root of *this.
  993. if ((isinf)() && !isneg())
  994. {
  995. return *this;
  996. }
  997. if (isneg() || (!(isfinite)()))
  998. {
  999. *this = nan();
  1000. errno = EDOM;
  1001. return *this;
  1002. }
  1003. if (iszero() || isone())
  1004. {
  1005. return *this;
  1006. }
  1007. // Save the original *this.
  1008. cpp_dec_float<Digits10, ExponentType, Allocator> x(*this);
  1009. // Generate the initial estimate using division.
  1010. // Extract the mantissa and exponent for a "manual"
  1011. // computation of the estimate.
  1012. double dd;
  1013. ExponentType ne;
  1014. extract_parts(dd, ne);
  1015. // Force the exponent to be an even multiple of two.
  1016. if ((ne % static_cast<ExponentType>(2)) != static_cast<ExponentType>(0))
  1017. {
  1018. ++ne;
  1019. dd /= 10.0;
  1020. }
  1021. // Setup the iteration.
  1022. // Estimate the square root using simple manipulations.
  1023. const double sqd = std::sqrt(dd);
  1024. *this = cpp_dec_float<Digits10, ExponentType, Allocator>(sqd, static_cast<ExponentType>(ne / static_cast<ExponentType>(2)));
  1025. // Estimate 1.0 / (2.0 * x0) using simple manipulations.
  1026. cpp_dec_float<Digits10, ExponentType, Allocator> vi(0.5 / sqd, static_cast<ExponentType>(-ne / static_cast<ExponentType>(2)));
  1027. // Compute the square root of x. Coupled Newton iteration
  1028. // as described in "Pi Unleashed" is used. During the
  1029. // iterative steps, the precision of the calculation is
  1030. // limited to the minimum required in order to minimize
  1031. // the run-time.
  1032. //
  1033. // Book references:
  1034. // https://doi.org/10.1007/978-3-642-56735-3
  1035. // http://www.amazon.com/exec/obidos/tg/detail/-/3540665722/qid=1035535482/sr=8-7/ref=sr_8_7/104-3357872-6059916?v=glance&n=507846
  1036. constexpr const std::uint32_t double_digits10_minus_a_few = std::numeric_limits<double>::digits10 - 3;
  1037. for (std::int32_t digits = double_digits10_minus_a_few; digits <= cpp_dec_float_total_digits10; digits *= 2u)
  1038. {
  1039. // Adjust precision of the terms.
  1040. precision((digits + 10) * 2);
  1041. vi.precision((digits + 10) * 2);
  1042. // Next iteration of vi
  1043. cpp_dec_float t(*this);
  1044. t *= vi;
  1045. t.negate();
  1046. t.mul_unsigned_long_long(2u);
  1047. t += one();
  1048. t *= vi;
  1049. vi += t;
  1050. // Next iteration of *this
  1051. t = *this;
  1052. t *= *this;
  1053. t.negate();
  1054. t += x;
  1055. t *= vi;
  1056. *this += t;
  1057. }
  1058. prec_elem = cpp_dec_float_elem_number;
  1059. return *this;
  1060. }
  1061. template <unsigned Digits10, class ExponentType, class Allocator>
  1062. int cpp_dec_float<Digits10, ExponentType, Allocator>::cmp_data(const array_type& vd) const
  1063. {
  1064. // Compare the data of *this with those of v.
  1065. // Return +1 for *this > v
  1066. // 0 for *this = v
  1067. // -1 for *this < v
  1068. const std::pair<typename array_type::const_iterator, typename array_type::const_iterator> mismatch_pair = std::mismatch(data.begin(), data.end(), vd.begin());
  1069. const bool is_equal = ((mismatch_pair.first == data.end()) && (mismatch_pair.second == vd.end()));
  1070. if (is_equal)
  1071. {
  1072. return 0;
  1073. }
  1074. else
  1075. {
  1076. return ((*mismatch_pair.first > *mismatch_pair.second) ? 1 : -1);
  1077. }
  1078. }
  1079. template <unsigned Digits10, class ExponentType, class Allocator>
  1080. int cpp_dec_float<Digits10, ExponentType, Allocator>::compare(const cpp_dec_float& v) const
  1081. {
  1082. // Compare v with *this.
  1083. // Return +1 for *this > v
  1084. // 0 for *this = v
  1085. // -1 for *this < v
  1086. // Handle all non-finite cases.
  1087. if ((!(isfinite)()) || (!(v.isfinite)()))
  1088. {
  1089. // NaN can never equal NaN. Return an implementation-dependent
  1090. // signed result. Also note that comparison of NaN with NaN
  1091. // using operators greater-than or less-than is undefined.
  1092. if ((isnan)() || (v.isnan)())
  1093. {
  1094. return ((isnan)() ? 1 : -1);
  1095. }
  1096. if ((isinf)() && (v.isinf)())
  1097. {
  1098. // Both *this and v are infinite. They are equal if they have the same sign.
  1099. // Otherwise, *this is less than v if and only if *this is negative.
  1100. return ((neg == v.neg) ? 0 : (neg ? -1 : 1));
  1101. }
  1102. if ((isinf)())
  1103. {
  1104. // *this is infinite, but v is finite.
  1105. // So negative infinite *this is less than any finite v.
  1106. // Whereas positive infinite *this is greater than any finite v.
  1107. return (isneg() ? -1 : 1);
  1108. }
  1109. else
  1110. {
  1111. // *this is finite, and v is infinite.
  1112. // So any finite *this is greater than negative infinite v.
  1113. // Whereas any finite *this is less than positive infinite v.
  1114. return (v.neg ? 1 : -1);
  1115. }
  1116. }
  1117. // And now handle all *finite* cases.
  1118. if (iszero())
  1119. {
  1120. // The value of *this is zero and v is either zero or non-zero.
  1121. return (v.iszero() ? 0
  1122. : (v.neg ? 1 : -1));
  1123. }
  1124. else if (v.iszero())
  1125. {
  1126. // The value of v is zero and *this is non-zero.
  1127. return (neg ? -1 : 1);
  1128. }
  1129. else
  1130. {
  1131. // Both *this and v are non-zero.
  1132. if (neg != v.neg)
  1133. {
  1134. // The signs are different.
  1135. return (neg ? -1 : 1);
  1136. }
  1137. else if (exp != v.exp)
  1138. {
  1139. // The signs are the same and the exponents are different.
  1140. const int val_cexpression = ((exp < v.exp) ? 1 : -1);
  1141. return (neg ? val_cexpression : -val_cexpression);
  1142. }
  1143. else
  1144. {
  1145. // The signs are the same and the exponents are the same.
  1146. // Compare the data.
  1147. const int val_cmp_data = cmp_data(v.data);
  1148. return ((!neg) ? val_cmp_data : -val_cmp_data);
  1149. }
  1150. }
  1151. }
  1152. template <unsigned Digits10, class ExponentType, class Allocator>
  1153. bool cpp_dec_float<Digits10, ExponentType, Allocator>::isone() const
  1154. {
  1155. // Check if the value of *this is identically 1 or very close to 1.
  1156. const bool not_negative_and_is_finite = ((!neg) && (isfinite)());
  1157. if (not_negative_and_is_finite)
  1158. {
  1159. if ((data[0u] == static_cast<std::uint32_t>(1u)) && (exp == static_cast<ExponentType>(0)))
  1160. {
  1161. const typename array_type::const_iterator it_non_zero = std::find_if(data.begin(), data.end(), data_elem_is_non_zero_predicate);
  1162. return (it_non_zero == data.end());
  1163. }
  1164. else if ((data[0u] == static_cast<std::uint32_t>(cpp_dec_float_elem_mask - 1)) && (exp == static_cast<ExponentType>(-cpp_dec_float_elem_digits10)))
  1165. {
  1166. const typename array_type::const_iterator it_non_nine = std::find_if(data.begin(), data.end(), data_elem_is_non_nine_predicate);
  1167. return (it_non_nine == data.end());
  1168. }
  1169. }
  1170. return false;
  1171. }
  1172. template <unsigned Digits10, class ExponentType, class Allocator>
  1173. bool cpp_dec_float<Digits10, ExponentType, Allocator>::isint() const
  1174. {
  1175. if (fpclass != cpp_dec_float_finite)
  1176. {
  1177. return false;
  1178. }
  1179. if (iszero())
  1180. {
  1181. return true;
  1182. }
  1183. if (exp < static_cast<ExponentType>(0))
  1184. {
  1185. return false;
  1186. } // |*this| < 1.
  1187. const typename array_type::size_type offset_decimal_part = static_cast<typename array_type::size_type>(exp / cpp_dec_float_elem_digits10) + 1u;
  1188. if (offset_decimal_part >= static_cast<typename array_type::size_type>(cpp_dec_float_elem_number))
  1189. {
  1190. // The number is too large to resolve the integer part.
  1191. // It considered to be a pure integer.
  1192. return true;
  1193. }
  1194. typename array_type::const_iterator it_non_zero = std::find_if(data.begin() + offset_decimal_part, data.end(), data_elem_is_non_zero_predicate);
  1195. return (it_non_zero == data.end());
  1196. }
  1197. template <unsigned Digits10, class ExponentType, class Allocator>
  1198. void cpp_dec_float<Digits10, ExponentType, Allocator>::extract_parts(double& mantissa, ExponentType& exponent) const
  1199. {
  1200. // Extract the approximate parts mantissa and base-10 exponent from the input cpp_dec_float<Digits10, ExponentType, Allocator> value x.
  1201. // Extracts the mantissa and exponent.
  1202. exponent = exp;
  1203. std::uint32_t p10 = static_cast<std::uint32_t>(1u);
  1204. std::uint32_t test = data[0u];
  1205. for (;;)
  1206. {
  1207. test /= static_cast<std::uint32_t>(10u);
  1208. if (test == static_cast<std::uint32_t>(0u))
  1209. {
  1210. break;
  1211. }
  1212. p10 *= static_cast<std::uint32_t>(10u);
  1213. ++exponent;
  1214. }
  1215. // Establish the upper bound of limbs for extracting the double.
  1216. 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;
  1217. // And make sure this upper bound stays within bounds of the elems.
  1218. 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));
  1219. // Extract into the mantissa the first limb, extracted as a double.
  1220. mantissa = static_cast<double>(data[0]);
  1221. double scale = 1.0;
  1222. // Extract the rest of the mantissa piecewise from the limbs.
  1223. for (std::size_t i = 1u; i < max_elem_extract_count; i++)
  1224. {
  1225. scale /= static_cast<double>(cpp_dec_float_elem_mask);
  1226. mantissa += (static_cast<double>(data[i]) * scale);
  1227. }
  1228. mantissa /= static_cast<double>(p10);
  1229. if (neg)
  1230. {
  1231. mantissa = -mantissa;
  1232. }
  1233. }
  1234. template <unsigned Digits10, class ExponentType, class Allocator>
  1235. double cpp_dec_float<Digits10, ExponentType, Allocator>::extract_double() const
  1236. {
  1237. // Returns the double conversion of a cpp_dec_float<Digits10, ExponentType, Allocator>.
  1238. // Check for non-normal cpp_dec_float<Digits10, ExponentType, Allocator>.
  1239. if (!(isfinite)())
  1240. {
  1241. if ((isnan)())
  1242. {
  1243. return std::numeric_limits<double>::quiet_NaN();
  1244. }
  1245. else
  1246. {
  1247. return ((!neg) ? std::numeric_limits<double>::infinity()
  1248. : -std::numeric_limits<double>::infinity());
  1249. }
  1250. }
  1251. cpp_dec_float<Digits10, ExponentType, Allocator> xx(*this);
  1252. if (xx.isneg())
  1253. xx.negate();
  1254. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is zero.
  1255. if (iszero() || (xx.compare(double_min()) < 0))
  1256. {
  1257. return 0.0;
  1258. }
  1259. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> exceeds the maximum of double.
  1260. if (xx.compare(double_max()) > 0)
  1261. {
  1262. return ((!neg) ? std::numeric_limits<double>::infinity()
  1263. : -std::numeric_limits<double>::infinity());
  1264. }
  1265. std::stringstream ss;
  1266. ss.imbue(std::locale::classic());
  1267. ss << str(std::numeric_limits<double>::digits10 + (2 + 1), std::ios_base::scientific);
  1268. double d;
  1269. ss >> d;
  1270. return d;
  1271. }
  1272. template <unsigned Digits10, class ExponentType, class Allocator>
  1273. long double cpp_dec_float<Digits10, ExponentType, Allocator>::extract_long_double() const
  1274. {
  1275. // Returns the long double conversion of a cpp_dec_float<Digits10, ExponentType, Allocator>.
  1276. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is subnormal.
  1277. if (!(isfinite)())
  1278. {
  1279. if ((isnan)())
  1280. {
  1281. return std::numeric_limits<long double>::quiet_NaN();
  1282. }
  1283. else
  1284. {
  1285. return ((!neg) ? std::numeric_limits<long double>::infinity()
  1286. : -std::numeric_limits<long double>::infinity());
  1287. }
  1288. }
  1289. cpp_dec_float<Digits10, ExponentType, Allocator> xx(*this);
  1290. if (xx.isneg())
  1291. xx.negate();
  1292. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> is zero.
  1293. if (iszero() || (xx.compare(long_double_min()) < 0))
  1294. {
  1295. return static_cast<long double>(0.0);
  1296. }
  1297. // Check if *this cpp_dec_float<Digits10, ExponentType, Allocator> exceeds the maximum of double.
  1298. if (xx.compare(long_double_max()) > 0)
  1299. {
  1300. return ((!neg) ? std::numeric_limits<long double>::infinity()
  1301. : -std::numeric_limits<long double>::infinity());
  1302. }
  1303. std::stringstream ss;
  1304. ss.imbue(std::locale::classic());
  1305. ss << str(std::numeric_limits<long double>::digits10 + (2 + 1), std::ios_base::scientific);
  1306. long double ld;
  1307. ss >> ld;
  1308. return ld;
  1309. }
  1310. template <unsigned Digits10, class ExponentType, class Allocator>
  1311. boost::long_long_type cpp_dec_float<Digits10, ExponentType, Allocator>::extract_signed_long_long() const
  1312. {
  1313. // Extracts a signed long long from *this.
  1314. // If (x > maximum of long long) or (x < minimum of long long),
  1315. // then the maximum or minimum of long long is returned accordingly.
  1316. if (exp < static_cast<ExponentType>(0))
  1317. {
  1318. return static_cast<boost::long_long_type>(0);
  1319. }
  1320. const bool b_neg = isneg();
  1321. boost::ulong_long_type val;
  1322. if ((!b_neg) && (compare(long_long_max()) > 0))
  1323. {
  1324. return (std::numeric_limits<boost::long_long_type>::max)();
  1325. }
  1326. else if (b_neg && (compare(long_long_min()) < 0))
  1327. {
  1328. return (std::numeric_limits<boost::long_long_type>::min)();
  1329. }
  1330. else
  1331. {
  1332. // Extract the data into an boost::ulong_long_type value.
  1333. cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1334. if (xn.isneg())
  1335. xn.negate();
  1336. val = static_cast<boost::ulong_long_type>(xn.data[0]);
  1337. 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)));
  1338. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1339. {
  1340. val *= static_cast<boost::ulong_long_type>(cpp_dec_float_elem_mask);
  1341. val += static_cast<boost::ulong_long_type>(xn.data[i]);
  1342. }
  1343. }
  1344. if (!b_neg)
  1345. {
  1346. return static_cast<boost::long_long_type>(val);
  1347. }
  1348. else
  1349. {
  1350. // This strange expression avoids a hardware trap in the corner case
  1351. // that val is the most negative value permitted in boost::long_long_type.
  1352. // See https://svn.boost.org/trac/boost/ticket/9740.
  1353. //
  1354. boost::long_long_type sval = static_cast<boost::long_long_type>(val - 1);
  1355. sval = -sval;
  1356. --sval;
  1357. return sval;
  1358. }
  1359. }
  1360. template <unsigned Digits10, class ExponentType, class Allocator>
  1361. boost::ulong_long_type cpp_dec_float<Digits10, ExponentType, Allocator>::extract_unsigned_long_long() const
  1362. {
  1363. // Extracts an boost::ulong_long_type from *this.
  1364. // If x exceeds the maximum of boost::ulong_long_type,
  1365. // then the maximum of boost::ulong_long_type is returned.
  1366. // If x is negative, then the boost::ulong_long_type cast of
  1367. // the long long extracted value is returned.
  1368. if (isneg())
  1369. {
  1370. return static_cast<boost::ulong_long_type>(extract_signed_long_long());
  1371. }
  1372. if (exp < static_cast<ExponentType>(0))
  1373. {
  1374. return static_cast<boost::ulong_long_type>(0u);
  1375. }
  1376. const cpp_dec_float<Digits10, ExponentType, Allocator> xn(extract_integer_part());
  1377. boost::ulong_long_type val;
  1378. if (xn.compare(ulong_long_max()) > 0)
  1379. {
  1380. return (std::numeric_limits<boost::ulong_long_type>::max)();
  1381. }
  1382. else
  1383. {
  1384. // Extract the data into an boost::ulong_long_type value.
  1385. val = static_cast<boost::ulong_long_type>(xn.data[0]);
  1386. 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)));
  1387. for (std::int32_t i = static_cast<std::int32_t>(1); i <= imax; i++)
  1388. {
  1389. val *= static_cast<boost::ulong_long_type>(cpp_dec_float_elem_mask);
  1390. val += static_cast<boost::ulong_long_type>(xn.data[i]);
  1391. }
  1392. }
  1393. return val;
  1394. }
  1395. template <unsigned Digits10, class ExponentType, class Allocator>
  1396. cpp_dec_float<Digits10, ExponentType, Allocator> cpp_dec_float<Digits10, ExponentType, Allocator>::extract_integer_part() const
  1397. {
  1398. // Compute the signed integer part of x.
  1399. if (!(isfinite)())
  1400. {
  1401. return *this;
  1402. }
  1403. if (exp < static_cast<ExponentType>(0))
  1404. {
  1405. // The absolute value of the number is smaller than 1.
  1406. // Thus the integer part is zero.
  1407. return zero();
  1408. }
  1409. // Truncate the digits from the decimal part, including guard digits
  1410. // that do not belong to the integer part.
  1411. // Make a local copy.
  1412. cpp_dec_float<Digits10, ExponentType, Allocator> x = *this;
  1413. // Clear out the decimal portion
  1414. const size_t first_clear = (static_cast<size_t>(x.exp) / static_cast<size_t>(cpp_dec_float_elem_digits10)) + 1u;
  1415. const size_t last_clear = static_cast<size_t>(cpp_dec_float_elem_number);
  1416. if (first_clear < last_clear)
  1417. std::fill(x.data.begin() + first_clear, x.data.begin() + last_clear, static_cast<std::uint32_t>(0u));
  1418. return x;
  1419. }
  1420. template <unsigned Digits10, class ExponentType, class Allocator>
  1421. std::string cpp_dec_float<Digits10, ExponentType, Allocator>::str(std::intmax_t number_of_digits, std::ios_base::fmtflags f) const
  1422. {
  1423. if ((this->isinf)())
  1424. {
  1425. if (this->isneg())
  1426. return "-inf";
  1427. else if (f & std::ios_base::showpos)
  1428. return "+inf";
  1429. else
  1430. return "inf";
  1431. }
  1432. else if ((this->isnan)())
  1433. {
  1434. return "nan";
  1435. }
  1436. std::string str;
  1437. std::intmax_t org_digits(number_of_digits);
  1438. ExponentType my_exp = order();
  1439. if (number_of_digits == 0)
  1440. number_of_digits = cpp_dec_float_total_digits10;
  1441. if (f & std::ios_base::fixed)
  1442. {
  1443. number_of_digits += my_exp + 1;
  1444. }
  1445. else if (f & std::ios_base::scientific)
  1446. ++number_of_digits;
  1447. // Determine the number of elements needed to provide the requested digits from cpp_dec_float<Digits10, ExponentType, Allocator>.
  1448. const std::size_t number_of_elements = (std::min)(static_cast<std::size_t>((number_of_digits / static_cast<std::size_t>(cpp_dec_float_elem_digits10)) + 2u),
  1449. static_cast<std::size_t>(cpp_dec_float_elem_number));
  1450. // Extract the remaining digits from cpp_dec_float<Digits10, ExponentType, Allocator> after the decimal point.
  1451. std::stringstream ss;
  1452. ss.imbue(std::locale::classic());
  1453. ss << data[0];
  1454. // Extract all of the digits from cpp_dec_float<Digits10, ExponentType, Allocator>, beginning with the first data element.
  1455. for (std::size_t i = static_cast<std::size_t>(1u); i < number_of_elements; i++)
  1456. {
  1457. ss << std::setw(static_cast<std::streamsize>(cpp_dec_float_elem_digits10))
  1458. << std::setfill(static_cast<char>('0'))
  1459. << data[i];
  1460. }
  1461. str += ss.str();
  1462. bool have_leading_zeros = false;
  1463. if (number_of_digits == 0)
  1464. {
  1465. // We only get here if the output format is "fixed" and we just need to
  1466. // round the first non-zero digit.
  1467. number_of_digits -= my_exp + 1; // reset to original value
  1468. str.insert(static_cast<std::string::size_type>(0), std::string::size_type(number_of_digits), '0');
  1469. have_leading_zeros = true;
  1470. }
  1471. if (number_of_digits < 0)
  1472. {
  1473. str = "0";
  1474. if (isneg())
  1475. str.insert(static_cast<std::string::size_type>(0), 1, '-');
  1476. boost::multiprecision::detail::format_float_string(str, 0, number_of_digits - my_exp - 1, f, this->iszero());
  1477. return str;
  1478. }
  1479. else
  1480. {
  1481. // Cut the output to the size of the precision.
  1482. if (str.length() > static_cast<std::string::size_type>(number_of_digits))
  1483. {
  1484. // Get the digit after the last needed digit for rounding
  1485. 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'));
  1486. bool need_round_up = round >= 5u;
  1487. if (round == 5u)
  1488. {
  1489. const std::uint32_t ix = 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'));
  1490. if ((ix & 1u) == 0)
  1491. {
  1492. // We have an even digit followed by a 5, so we might not actually need to round up
  1493. // if all the remaining digits are zero:
  1494. if (str.find_first_not_of('0', static_cast<std::string::size_type>(number_of_digits + 1)) == std::string::npos)
  1495. {
  1496. bool all_zeros = true;
  1497. // No none-zero trailing digits in the string, now check whatever parts we didn't convert to the string:
  1498. for (std::size_t i = number_of_elements; i < data.size(); i++)
  1499. {
  1500. if (data[i])
  1501. {
  1502. all_zeros = false;
  1503. break;
  1504. }
  1505. }
  1506. if (all_zeros)
  1507. need_round_up = false; // tie break - round to even.
  1508. }
  1509. }
  1510. }
  1511. // Truncate the string
  1512. str.erase(static_cast<std::string::size_type>(number_of_digits));
  1513. if (need_round_up)
  1514. {
  1515. std::size_t ix = static_cast<std::size_t>(str.length() - 1u);
  1516. // Every trailing 9 must be rounded up
  1517. while (ix && (static_cast<std::int32_t>(str.at(ix)) - static_cast<std::int32_t>('0') == static_cast<std::int32_t>(9)))
  1518. {
  1519. str.at(ix) = static_cast<char>('0');
  1520. --ix;
  1521. }
  1522. if (!ix)
  1523. {
  1524. // There were nothing but trailing nines.
  1525. 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))
  1526. {
  1527. // Increment up to the next order and adjust exponent.
  1528. str.at(ix) = static_cast<char>('1');
  1529. ++my_exp;
  1530. }
  1531. else
  1532. {
  1533. // Round up this digit.
  1534. ++str.at(ix);
  1535. }
  1536. }
  1537. else
  1538. {
  1539. // Round up the last digit.
  1540. ++str[ix];
  1541. }
  1542. }
  1543. }
  1544. }
  1545. if (have_leading_zeros)
  1546. {
  1547. // We need to take the zeros back out again, and correct the exponent
  1548. // if we rounded up:
  1549. if (str[std::string::size_type(number_of_digits - 1)] != '0')
  1550. {
  1551. ++my_exp;
  1552. str.erase(0, std::string::size_type(number_of_digits - 1));
  1553. }
  1554. else
  1555. str.erase(0, std::string::size_type(number_of_digits));
  1556. }
  1557. if (isneg())
  1558. str.insert(static_cast<std::string::size_type>(0), 1, '-');
  1559. boost::multiprecision::detail::format_float_string(str, my_exp, org_digits, f, this->iszero());
  1560. return str;
  1561. }
  1562. template <unsigned Digits10, class ExponentType, class Allocator>
  1563. bool cpp_dec_float<Digits10, ExponentType, Allocator>::rd_string(const char* const s)
  1564. {
  1565. #ifndef BOOST_NO_EXCEPTIONS
  1566. try
  1567. {
  1568. #endif
  1569. std::string str(s);
  1570. // TBD: Using several regular expressions may significantly reduce
  1571. // the code complexity (and perhaps the run-time) of rd_string().
  1572. // Get a possible exponent and remove it.
  1573. exp = static_cast<ExponentType>(0);
  1574. std::size_t pos;
  1575. if (((pos = str.find('e')) != std::string::npos) || ((pos = str.find('E')) != std::string::npos))
  1576. {
  1577. // Remove the exponent part from the string.
  1578. exp = boost::lexical_cast<ExponentType>(static_cast<const char*>(str.c_str() + (pos + 1u)));
  1579. str = str.substr(static_cast<std::size_t>(0u), pos);
  1580. }
  1581. // Get a possible +/- sign and remove it.
  1582. neg = false;
  1583. if (str.size())
  1584. {
  1585. if (str[0] == '-')
  1586. {
  1587. neg = true;
  1588. str.erase(0, 1);
  1589. }
  1590. else if (str[0] == '+')
  1591. {
  1592. str.erase(0, 1);
  1593. }
  1594. }
  1595. //
  1596. // Special cases for infinities and NaN's:
  1597. //
  1598. if ((str == "inf") || (str == "INF") || (str == "infinity") || (str == "INFINITY"))
  1599. {
  1600. if (neg)
  1601. {
  1602. *this = this->inf();
  1603. this->negate();
  1604. }
  1605. else
  1606. *this = this->inf();
  1607. return true;
  1608. }
  1609. if ((str.size() >= 3) && ((str.substr(0, 3) == "nan") || (str.substr(0, 3) == "NAN") || (str.substr(0, 3) == "NaN")))
  1610. {
  1611. *this = this->nan();
  1612. return true;
  1613. }
  1614. // Remove the leading zeros for all input types.
  1615. const std::string::iterator fwd_it_leading_zero = std::find_if(str.begin(), str.end(), char_is_nonzero_predicate);
  1616. if (fwd_it_leading_zero != str.begin())
  1617. {
  1618. if (fwd_it_leading_zero == str.end())
  1619. {
  1620. // The string contains nothing but leading zeros.
  1621. // This string represents zero.
  1622. operator=(zero());
  1623. return true;
  1624. }
  1625. else
  1626. {
  1627. str.erase(str.begin(), fwd_it_leading_zero);
  1628. }
  1629. }
  1630. // Put the input string into the standard cpp_dec_float<Digits10, ExponentType, Allocator> input form
  1631. // aaa.bbbbE+/-n, where aaa has 1...cpp_dec_float_elem_digits10, bbbb has an
  1632. // even multiple of cpp_dec_float_elem_digits10 which are possibly zero padded
  1633. // on the right-end, and n is a signed 64-bit integer which is an
  1634. // even multiple of cpp_dec_float_elem_digits10.
  1635. // Find a possible decimal point.
  1636. pos = str.find(static_cast<char>('.'));
  1637. if (pos != std::string::npos)
  1638. {
  1639. // Remove all trailing insignificant zeros.
  1640. const std::string::const_reverse_iterator rit_non_zero = std::find_if(str.rbegin(), str.rend(), char_is_nonzero_predicate);
  1641. if (rit_non_zero != static_cast<std::string::const_reverse_iterator>(str.rbegin()))
  1642. {
  1643. const std::string::size_type ofs = str.length() - std::distance<std::string::const_reverse_iterator>(str.rbegin(), rit_non_zero);
  1644. str.erase(str.begin() + ofs, str.end());
  1645. }
  1646. // Check if the input is identically zero.
  1647. if (str == std::string("."))
  1648. {
  1649. operator=(zero());
  1650. return true;
  1651. }
  1652. // Remove leading significant zeros just after the decimal point
  1653. // and adjust the exponent accordingly.
  1654. // Note that the while-loop operates only on strings of the form ".000abcd..."
  1655. // and peels away the zeros just after the decimal point.
  1656. if (str.at(static_cast<std::size_t>(0u)) == static_cast<char>('.'))
  1657. {
  1658. const std::string::iterator it_non_zero = std::find_if(str.begin() + 1u, str.end(), char_is_nonzero_predicate);
  1659. std::size_t delta_exp = static_cast<std::size_t>(0u);
  1660. if (str.at(static_cast<std::size_t>(1u)) == static_cast<char>('0'))
  1661. {
  1662. delta_exp = std::distance<std::string::const_iterator>(str.begin() + 1u, it_non_zero);
  1663. }
  1664. // Bring one single digit into the mantissa and adjust the exponent accordingly.
  1665. str.erase(str.begin(), it_non_zero);
  1666. str.insert(static_cast<std::string::size_type>(1u), ".");
  1667. exp -= static_cast<ExponentType>(delta_exp + 1u);
  1668. }
  1669. }
  1670. else
  1671. {
  1672. // Input string has no decimal point: Append decimal point.
  1673. str.append(".");
  1674. }
  1675. // Shift the decimal point such that the exponent is an even multiple of cpp_dec_float_elem_digits10.
  1676. std::size_t n_shift = static_cast<std::size_t>(0u);
  1677. const std::size_t n_exp_rem = static_cast<std::size_t>(exp % static_cast<ExponentType>(cpp_dec_float_elem_digits10));
  1678. if ((exp % static_cast<ExponentType>(cpp_dec_float_elem_digits10)) != static_cast<ExponentType>(0))
  1679. {
  1680. n_shift = ((exp < static_cast<ExponentType>(0))
  1681. ? static_cast<std::size_t>(n_exp_rem + static_cast<std::size_t>(cpp_dec_float_elem_digits10))
  1682. : static_cast<std::size_t>(n_exp_rem));
  1683. }
  1684. // Make sure that there are enough digits for the decimal point shift.
  1685. pos = str.find(static_cast<char>('.'));
  1686. std::size_t pos_plus_one = static_cast<std::size_t>(pos + 1u);
  1687. if ((str.length() - pos_plus_one) < n_shift)
  1688. {
  1689. const std::size_t sz = static_cast<std::size_t>(n_shift - (str.length() - pos_plus_one));
  1690. str.append(std::string(sz, static_cast<char>('0')));
  1691. }
  1692. // Do the decimal point shift.
  1693. if (n_shift != static_cast<std::size_t>(0u))
  1694. {
  1695. str.insert(static_cast<std::string::size_type>(pos_plus_one + n_shift), ".");
  1696. str.erase(pos, static_cast<std::string::size_type>(1u));
  1697. exp -= static_cast<ExponentType>(n_shift);
  1698. }
  1699. // Cut the size of the mantissa to <= cpp_dec_float_elem_digits10.
  1700. pos = str.find(static_cast<char>('.'));
  1701. pos_plus_one = static_cast<std::size_t>(pos + 1u);
  1702. if (pos > static_cast<std::size_t>(cpp_dec_float_elem_digits10))
  1703. {
  1704. const std::int32_t n_pos = static_cast<std::int32_t>(pos);
  1705. 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));
  1706. 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);
  1707. 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))), ".");
  1708. str.erase(pos_plus_one, static_cast<std::size_t>(1u));
  1709. exp += static_cast<ExponentType>(static_cast<ExponentType>(n) * static_cast<ExponentType>(cpp_dec_float_elem_digits10));
  1710. }
  1711. // Pad the decimal part such that its value is an even
  1712. // multiple of cpp_dec_float_elem_digits10.
  1713. pos = str.find(static_cast<char>('.'));
  1714. pos_plus_one = static_cast<std::size_t>(pos + 1u);
  1715. 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));
  1716. const std::int32_t n_rem = static_cast<std::int32_t>(n_dec % cpp_dec_float_elem_digits10);
  1717. std::int32_t n_cnt = ((n_rem != static_cast<std::int32_t>(0))
  1718. ? static_cast<std::int32_t>(cpp_dec_float_elem_digits10 - n_rem)
  1719. : static_cast<std::int32_t>(0));
  1720. if (n_cnt != static_cast<std::int32_t>(0))
  1721. {
  1722. str.append(static_cast<std::size_t>(n_cnt), static_cast<char>('0'));
  1723. }
  1724. // Truncate decimal part if it is too long.
  1725. const std::size_t max_dec = static_cast<std::size_t>((cpp_dec_float_elem_number - 1) * cpp_dec_float_elem_digits10);
  1726. if (static_cast<std::size_t>(str.length() - pos) > max_dec)
  1727. {
  1728. str = str.substr(static_cast<std::size_t>(0u),
  1729. static_cast<std::size_t>(pos_plus_one + max_dec));
  1730. }
  1731. // Now the input string has the standard cpp_dec_float<Digits10, ExponentType, Allocator> input form.
  1732. // (See the comment above.)
  1733. // Set all the data elements to 0.
  1734. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  1735. // Extract the data.
  1736. // First get the digits to the left of the decimal point...
  1737. data[0u] = boost::lexical_cast<std::uint32_t>(str.substr(static_cast<std::size_t>(0u), pos));
  1738. // ...then get the remaining digits to the right of the decimal point.
  1739. const std::string::size_type i_end = ((str.length() - pos_plus_one) / static_cast<std::string::size_type>(cpp_dec_float_elem_digits10));
  1740. for (std::string::size_type i = static_cast<std::string::size_type>(0u); i < i_end; i++)
  1741. {
  1742. const std::string::const_iterator it = str.begin() + pos_plus_one + (i * static_cast<std::string::size_type>(cpp_dec_float_elem_digits10));
  1743. data[i + 1u] = boost::lexical_cast<std::uint32_t>(std::string(it, it + static_cast<std::string::size_type>(cpp_dec_float_elem_digits10)));
  1744. }
  1745. // Check for overflow...
  1746. if (exp > cpp_dec_float_max_exp10)
  1747. {
  1748. const bool b_result_is_neg = neg;
  1749. *this = inf();
  1750. if (b_result_is_neg)
  1751. negate();
  1752. }
  1753. // ...and check for underflow.
  1754. if (exp <= cpp_dec_float_min_exp10)
  1755. {
  1756. if (exp == cpp_dec_float_min_exp10)
  1757. {
  1758. // Check for identity with the minimum value.
  1759. cpp_dec_float<Digits10, ExponentType, Allocator> test = *this;
  1760. test.exp = static_cast<ExponentType>(0);
  1761. if (test.isone())
  1762. {
  1763. *this = zero();
  1764. }
  1765. }
  1766. else
  1767. {
  1768. *this = zero();
  1769. }
  1770. }
  1771. #ifndef BOOST_NO_EXCEPTIONS
  1772. }
  1773. catch (const bad_lexical_cast&)
  1774. {
  1775. // Rethrow with better error message:
  1776. std::string msg = "Unable to parse the string \"";
  1777. msg += s;
  1778. msg += "\" as a floating point value.";
  1779. throw std::runtime_error(msg);
  1780. }
  1781. #endif
  1782. return true;
  1783. }
  1784. template <unsigned Digits10, class ExponentType, class Allocator>
  1785. cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float(const double mantissa, const ExponentType exponent)
  1786. : data(),
  1787. exp(static_cast<ExponentType>(0)),
  1788. neg(false),
  1789. fpclass(cpp_dec_float_finite),
  1790. prec_elem(cpp_dec_float_elem_number)
  1791. {
  1792. // Create *this cpp_dec_float<Digits10, ExponentType, Allocator> from a given mantissa and exponent.
  1793. // Note: This constructor does not maintain the full precision of double.
  1794. const bool mantissa_is_iszero = (::fabs(mantissa) < ((std::numeric_limits<double>::min)() * (1.0 + std::numeric_limits<double>::epsilon())));
  1795. if (mantissa_is_iszero)
  1796. {
  1797. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  1798. return;
  1799. }
  1800. const bool b_neg = (mantissa < 0.0);
  1801. double d = ((!b_neg) ? mantissa : -mantissa);
  1802. ExponentType e = exponent;
  1803. while (d > 10.0)
  1804. {
  1805. d /= 10.0;
  1806. ++e;
  1807. }
  1808. while (d < 1.0)
  1809. {
  1810. d *= 10.0;
  1811. --e;
  1812. }
  1813. std::int32_t shift = static_cast<std::int32_t>(e % static_cast<std::int32_t>(cpp_dec_float_elem_digits10));
  1814. while (static_cast<std::int32_t>(shift-- % cpp_dec_float_elem_digits10) != static_cast<std::int32_t>(0))
  1815. {
  1816. d *= 10.0;
  1817. --e;
  1818. }
  1819. exp = e;
  1820. neg = b_neg;
  1821. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  1822. constexpr const 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));
  1823. constexpr const std::int32_t digit_loops = static_cast<std::int32_t>(digit_ratio + static_cast<std::int32_t>(2));
  1824. for (std::int32_t i = static_cast<std::int32_t>(0); i < digit_loops; i++)
  1825. {
  1826. std::uint32_t n = static_cast<std::uint32_t>(static_cast<std::uint64_t>(d));
  1827. data[i] = static_cast<std::uint32_t>(n);
  1828. d -= static_cast<double>(n);
  1829. d *= static_cast<double>(cpp_dec_float_elem_mask);
  1830. }
  1831. }
  1832. template <unsigned Digits10, class ExponentType, class Allocator>
  1833. template <class Float>
  1834. 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)
  1835. {
  1836. // Christopher Kormanyos's original code used a cast to boost::long_long_type here, but that fails
  1837. // when long double has more digits than a boost::long_long_type.
  1838. using std::floor;
  1839. using std::frexp;
  1840. using std::ldexp;
  1841. if (a == 0)
  1842. return *this = zero();
  1843. if (a == 1)
  1844. return *this = one();
  1845. if ((boost::math::isinf)(a))
  1846. {
  1847. *this = inf();
  1848. if (a < 0)
  1849. this->negate();
  1850. return *this;
  1851. }
  1852. if ((boost::math::isnan)(a))
  1853. return *this = nan();
  1854. int e;
  1855. Float f, term;
  1856. *this = zero();
  1857. f = frexp(a, &e);
  1858. // See https://svn.boost.org/trac/boost/ticket/10924 for an example of why this may go wrong:
  1859. BOOST_ASSERT((boost::math::isfinite)(f));
  1860. constexpr const int shift = std::numeric_limits<int>::digits - 1;
  1861. while (f)
  1862. {
  1863. // extract int sized bits from f:
  1864. f = ldexp(f, shift);
  1865. BOOST_ASSERT((boost::math::isfinite)(f));
  1866. term = floor(f);
  1867. e -= shift;
  1868. *this *= pow2(shift);
  1869. if (term > 0)
  1870. add_unsigned_long_long(static_cast<unsigned>(term));
  1871. else
  1872. sub_unsigned_long_long(static_cast<unsigned>(-term));
  1873. f -= term;
  1874. }
  1875. if (e != 0)
  1876. *this *= pow2(e);
  1877. return *this;
  1878. }
  1879. template <unsigned Digits10, class ExponentType, class Allocator>
  1880. void cpp_dec_float<Digits10, ExponentType, Allocator>::from_unsigned_long_long(const boost::ulong_long_type u)
  1881. {
  1882. std::fill(data.begin(), data.end(), static_cast<std::uint32_t>(0u));
  1883. exp = static_cast<ExponentType>(0);
  1884. neg = false;
  1885. fpclass = cpp_dec_float_finite;
  1886. prec_elem = cpp_dec_float_elem_number;
  1887. if (u == 0)
  1888. {
  1889. return;
  1890. }
  1891. std::size_t i = static_cast<std::size_t>(0u);
  1892. boost::ulong_long_type uu = u;
  1893. std::uint32_t temp[(std::numeric_limits<boost::ulong_long_type>::digits10 / static_cast<int>(cpp_dec_float_elem_digits10)) + 3] = {static_cast<std::uint32_t>(0u)};
  1894. while (uu != static_cast<boost::ulong_long_type>(0u))
  1895. {
  1896. temp[i] = static_cast<std::uint32_t>(uu % static_cast<boost::ulong_long_type>(cpp_dec_float_elem_mask));
  1897. uu = static_cast<boost::ulong_long_type>(uu / static_cast<boost::ulong_long_type>(cpp_dec_float_elem_mask));
  1898. ++i;
  1899. }
  1900. if (i > static_cast<std::size_t>(1u))
  1901. {
  1902. exp += static_cast<ExponentType>((i - 1u) * static_cast<std::size_t>(cpp_dec_float_elem_digits10));
  1903. }
  1904. std::reverse(temp, temp + i);
  1905. std::copy(temp, temp + (std::min)(i, static_cast<std::size_t>(cpp_dec_float_elem_number)), data.begin());
  1906. }
  1907. template <unsigned Digits10, class ExponentType, class Allocator>
  1908. std::uint32_t cpp_dec_float<Digits10, ExponentType, Allocator>::mul_loop_uv(std::uint32_t* const u, const std::uint32_t* const v, const std::int32_t p)
  1909. {
  1910. //
  1911. // There is a limit on how many limbs this algorithm can handle without dropping digits
  1912. // due to overflow in the carry, it is:
  1913. //
  1914. // FLOOR( (2^64 - 1) / (10^8 * 10^8) ) == 1844
  1915. //
  1916. static_assert(cpp_dec_float_elem_number < 1800, "Too many limbs in the data type for the multiplication algorithm - unsupported precision in cpp_dec_float.");
  1917. std::uint64_t carry = static_cast<std::uint64_t>(0u);
  1918. for (std::int32_t j = static_cast<std::int32_t>(p - 1u); j >= static_cast<std::int32_t>(0); j--)
  1919. {
  1920. std::uint64_t sum = carry;
  1921. for (std::int32_t i = j; i >= static_cast<std::int32_t>(0); i--)
  1922. {
  1923. sum += static_cast<std::uint64_t>(u[j - i] * static_cast<std::uint64_t>(v[i]));
  1924. }
  1925. u[j] = static_cast<std::uint32_t>(sum % static_cast<std::uint32_t>(cpp_dec_float_elem_mask));
  1926. carry = static_cast<std::uint64_t>(sum / static_cast<std::uint32_t>(cpp_dec_float_elem_mask));
  1927. }
  1928. return static_cast<std::uint32_t>(carry);
  1929. }
  1930. template <unsigned Digits10, class ExponentType, class Allocator>
  1931. 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)
  1932. {
  1933. std::uint64_t carry = static_cast<std::uint64_t>(0u);
  1934. // Multiplication loop.
  1935. for (std::int32_t j = p - 1; j >= static_cast<std::int32_t>(0); j--)
  1936. {
  1937. const std::uint64_t t = static_cast<std::uint64_t>(carry + static_cast<std::uint64_t>(u[j] * static_cast<std::uint64_t>(n)));
  1938. carry = static_cast<std::uint64_t>(t / static_cast<std::uint32_t>(cpp_dec_float_elem_mask));
  1939. 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)));
  1940. }
  1941. return static_cast<std::uint32_t>(carry);
  1942. }
  1943. template <unsigned Digits10, class ExponentType, class Allocator>
  1944. 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)
  1945. {
  1946. std::uint64_t prev = static_cast<std::uint64_t>(0u);
  1947. for (std::int32_t j = static_cast<std::int32_t>(0); j < p; j++)
  1948. {
  1949. 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)));
  1950. u[j] = static_cast<std::uint32_t>(t / n);
  1951. prev = static_cast<std::uint64_t>(t - static_cast<std::uint64_t>(n * static_cast<std::uint64_t>(u[j])));
  1952. }
  1953. return static_cast<std::uint32_t>(prev);
  1954. }
  1955. template <unsigned Digits10, class ExponentType, class Allocator>
  1956. cpp_dec_float<Digits10, ExponentType, Allocator> cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(const boost::long_long_type p)
  1957. {
  1958. // Create a static const table of p^2 for -128 < p < +128.
  1959. // Note: The size of this table must be odd-numbered and
  1960. // symmetric about 0.
  1961. static const std::array<cpp_dec_float<Digits10, ExponentType, Allocator>, 255u> p2_data =
  1962. {{cpp_dec_float("5.877471754111437539843682686111228389093327783860437607543758531392086297273635864257812500000000000e-39"),
  1963. cpp_dec_float("1.175494350822287507968736537222245677818665556772087521508751706278417259454727172851562500000000000e-38"),
  1964. cpp_dec_float("2.350988701644575015937473074444491355637331113544175043017503412556834518909454345703125000000000000e-38"),
  1965. cpp_dec_float("4.701977403289150031874946148888982711274662227088350086035006825113669037818908691406250000000000000e-38"),
  1966. cpp_dec_float("9.403954806578300063749892297777965422549324454176700172070013650227338075637817382812500000000000000e-38"),
  1967. cpp_dec_float("1.880790961315660012749978459555593084509864890835340034414002730045467615127563476562500000000000000e-37"),
  1968. cpp_dec_float("3.761581922631320025499956919111186169019729781670680068828005460090935230255126953125000000000000000e-37"),
  1969. cpp_dec_float("7.523163845262640050999913838222372338039459563341360137656010920181870460510253906250000000000000000e-37"),
  1970. cpp_dec_float("1.504632769052528010199982767644474467607891912668272027531202184036374092102050781250000000000000000e-36"),
  1971. cpp_dec_float("3.009265538105056020399965535288948935215783825336544055062404368072748184204101562500000000000000000e-36"),
  1972. cpp_dec_float("6.018531076210112040799931070577897870431567650673088110124808736145496368408203125000000000000000000e-36"),
  1973. cpp_dec_float("1.203706215242022408159986214115579574086313530134617622024961747229099273681640625000000000000000000e-35"),
  1974. cpp_dec_float("2.407412430484044816319972428231159148172627060269235244049923494458198547363281250000000000000000000e-35"),
  1975. cpp_dec_float("4.814824860968089632639944856462318296345254120538470488099846988916397094726562500000000000000000000e-35"),
  1976. cpp_dec_float("9.629649721936179265279889712924636592690508241076940976199693977832794189453125000000000000000000000e-35"),
  1977. cpp_dec_float("1.925929944387235853055977942584927318538101648215388195239938795566558837890625000000000000000000000e-34"),
  1978. cpp_dec_float("3.851859888774471706111955885169854637076203296430776390479877591133117675781250000000000000000000000e-34"),
  1979. cpp_dec_float("7.703719777548943412223911770339709274152406592861552780959755182266235351562500000000000000000000000e-34"),
  1980. cpp_dec_float("1.540743955509788682444782354067941854830481318572310556191951036453247070312500000000000000000000000e-33"),
  1981. cpp_dec_float("3.081487911019577364889564708135883709660962637144621112383902072906494140625000000000000000000000000e-33"),
  1982. cpp_dec_float("6.162975822039154729779129416271767419321925274289242224767804145812988281250000000000000000000000000e-33"),
  1983. cpp_dec_float("1.232595164407830945955825883254353483864385054857848444953560829162597656250000000000000000000000000e-32"),
  1984. cpp_dec_float("2.465190328815661891911651766508706967728770109715696889907121658325195312500000000000000000000000000e-32"),
  1985. cpp_dec_float("4.930380657631323783823303533017413935457540219431393779814243316650390625000000000000000000000000000e-32"),
  1986. cpp_dec_float("9.860761315262647567646607066034827870915080438862787559628486633300781250000000000000000000000000000e-32"),
  1987. cpp_dec_float("1.972152263052529513529321413206965574183016087772557511925697326660156250000000000000000000000000000e-31"),
  1988. cpp_dec_float("3.944304526105059027058642826413931148366032175545115023851394653320312500000000000000000000000000000e-31"),
  1989. cpp_dec_float("7.888609052210118054117285652827862296732064351090230047702789306640625000000000000000000000000000000e-31"),
  1990. cpp_dec_float("1.577721810442023610823457130565572459346412870218046009540557861328125000000000000000000000000000000e-30"),
  1991. cpp_dec_float("3.155443620884047221646914261131144918692825740436092019081115722656250000000000000000000000000000000e-30"),
  1992. cpp_dec_float("6.310887241768094443293828522262289837385651480872184038162231445312500000000000000000000000000000000e-30"),
  1993. cpp_dec_float("1.262177448353618888658765704452457967477130296174436807632446289062500000000000000000000000000000000e-29"),
  1994. cpp_dec_float("2.524354896707237777317531408904915934954260592348873615264892578125000000000000000000000000000000000e-29"),
  1995. cpp_dec_float("5.048709793414475554635062817809831869908521184697747230529785156250000000000000000000000000000000000e-29"),
  1996. cpp_dec_float("1.009741958682895110927012563561966373981704236939549446105957031250000000000000000000000000000000000e-28"),
  1997. cpp_dec_float("2.019483917365790221854025127123932747963408473879098892211914062500000000000000000000000000000000000e-28"),
  1998. cpp_dec_float("4.038967834731580443708050254247865495926816947758197784423828125000000000000000000000000000000000000e-28"),
  1999. cpp_dec_float("8.077935669463160887416100508495730991853633895516395568847656250000000000000000000000000000000000000e-28"),
  2000. cpp_dec_float("1.615587133892632177483220101699146198370726779103279113769531250000000000000000000000000000000000000e-27"),
  2001. cpp_dec_float("3.231174267785264354966440203398292396741453558206558227539062500000000000000000000000000000000000000e-27"),
  2002. cpp_dec_float("6.462348535570528709932880406796584793482907116413116455078125000000000000000000000000000000000000000e-27"),
  2003. cpp_dec_float("1.292469707114105741986576081359316958696581423282623291015625000000000000000000000000000000000000000e-26"),
  2004. cpp_dec_float("2.584939414228211483973152162718633917393162846565246582031250000000000000000000000000000000000000000e-26"),
  2005. cpp_dec_float("5.169878828456422967946304325437267834786325693130493164062500000000000000000000000000000000000000000e-26"),
  2006. cpp_dec_float("1.033975765691284593589260865087453566957265138626098632812500000000000000000000000000000000000000000e-25"),
  2007. cpp_dec_float("2.067951531382569187178521730174907133914530277252197265625000000000000000000000000000000000000000000e-25"),
  2008. cpp_dec_float("4.135903062765138374357043460349814267829060554504394531250000000000000000000000000000000000000000000e-25"),
  2009. cpp_dec_float("8.271806125530276748714086920699628535658121109008789062500000000000000000000000000000000000000000000e-25"),
  2010. cpp_dec_float("1.654361225106055349742817384139925707131624221801757812500000000000000000000000000000000000000000000e-24"),
  2011. cpp_dec_float("3.308722450212110699485634768279851414263248443603515625000000000000000000000000000000000000000000000e-24"),
  2012. cpp_dec_float("6.617444900424221398971269536559702828526496887207031250000000000000000000000000000000000000000000000e-24"),
  2013. cpp_dec_float("1.323488980084844279794253907311940565705299377441406250000000000000000000000000000000000000000000000e-23"),
  2014. cpp_dec_float("2.646977960169688559588507814623881131410598754882812500000000000000000000000000000000000000000000000e-23"),
  2015. cpp_dec_float("5.293955920339377119177015629247762262821197509765625000000000000000000000000000000000000000000000000e-23"),
  2016. cpp_dec_float("1.058791184067875423835403125849552452564239501953125000000000000000000000000000000000000000000000000e-22"),
  2017. cpp_dec_float("2.117582368135750847670806251699104905128479003906250000000000000000000000000000000000000000000000000e-22"),
  2018. cpp_dec_float("4.235164736271501695341612503398209810256958007812500000000000000000000000000000000000000000000000000e-22"),
  2019. cpp_dec_float("8.470329472543003390683225006796419620513916015625000000000000000000000000000000000000000000000000000e-22"),
  2020. cpp_dec_float("1.694065894508600678136645001359283924102783203125000000000000000000000000000000000000000000000000000e-21"),
  2021. cpp_dec_float("3.388131789017201356273290002718567848205566406250000000000000000000000000000000000000000000000000000e-21"),
  2022. cpp_dec_float("6.776263578034402712546580005437135696411132812500000000000000000000000000000000000000000000000000000e-21"),
  2023. cpp_dec_float("1.355252715606880542509316001087427139282226562500000000000000000000000000000000000000000000000000000e-20"),
  2024. cpp_dec_float("2.710505431213761085018632002174854278564453125000000000000000000000000000000000000000000000000000000e-20"),
  2025. cpp_dec_float("5.421010862427522170037264004349708557128906250000000000000000000000000000000000000000000000000000000e-20"),
  2026. cpp_dec_float("1.084202172485504434007452800869941711425781250000000000000000000000000000000000000000000000000000000e-19"),
  2027. cpp_dec_float("2.168404344971008868014905601739883422851562500000000000000000000000000000000000000000000000000000000e-19"),
  2028. cpp_dec_float("4.336808689942017736029811203479766845703125000000000000000000000000000000000000000000000000000000000e-19"),
  2029. cpp_dec_float("8.673617379884035472059622406959533691406250000000000000000000000000000000000000000000000000000000000e-19"),
  2030. cpp_dec_float("1.734723475976807094411924481391906738281250000000000000000000000000000000000000000000000000000000000e-18"),
  2031. cpp_dec_float("3.469446951953614188823848962783813476562500000000000000000000000000000000000000000000000000000000000e-18"),
  2032. cpp_dec_float("6.938893903907228377647697925567626953125000000000000000000000000000000000000000000000000000000000000e-18"),
  2033. cpp_dec_float("1.387778780781445675529539585113525390625000000000000000000000000000000000000000000000000000000000000e-17"),
  2034. cpp_dec_float("2.775557561562891351059079170227050781250000000000000000000000000000000000000000000000000000000000000e-17"),
  2035. cpp_dec_float("5.551115123125782702118158340454101562500000000000000000000000000000000000000000000000000000000000000e-17"),
  2036. cpp_dec_float("1.110223024625156540423631668090820312500000000000000000000000000000000000000000000000000000000000000e-16"),
  2037. cpp_dec_float("2.220446049250313080847263336181640625000000000000000000000000000000000000000000000000000000000000000e-16"),
  2038. cpp_dec_float("4.440892098500626161694526672363281250000000000000000000000000000000000000000000000000000000000000000e-16"),
  2039. cpp_dec_float("8.881784197001252323389053344726562500000000000000000000000000000000000000000000000000000000000000000e-16"),
  2040. cpp_dec_float("1.776356839400250464677810668945312500000000000000000000000000000000000000000000000000000000000000000e-15"),
  2041. cpp_dec_float("3.552713678800500929355621337890625000000000000000000000000000000000000000000000000000000000000000000e-15"),
  2042. cpp_dec_float("7.105427357601001858711242675781250000000000000000000000000000000000000000000000000000000000000000000e-15"),
  2043. cpp_dec_float("1.421085471520200371742248535156250000000000000000000000000000000000000000000000000000000000000000000e-14"),
  2044. cpp_dec_float("2.842170943040400743484497070312500000000000000000000000000000000000000000000000000000000000000000000e-14"),
  2045. cpp_dec_float("5.684341886080801486968994140625000000000000000000000000000000000000000000000000000000000000000000000e-14"),
  2046. cpp_dec_float("1.136868377216160297393798828125000000000000000000000000000000000000000000000000000000000000000000000e-13"),
  2047. cpp_dec_float("2.273736754432320594787597656250000000000000000000000000000000000000000000000000000000000000000000000e-13"),
  2048. cpp_dec_float("4.547473508864641189575195312500000000000000000000000000000000000000000000000000000000000000000000000e-13"),
  2049. cpp_dec_float("9.094947017729282379150390625000000000000000000000000000000000000000000000000000000000000000000000000e-13"),
  2050. cpp_dec_float("1.818989403545856475830078125000000000000000000000000000000000000000000000000000000000000000000000000e-12"),
  2051. cpp_dec_float("3.637978807091712951660156250000000000000000000000000000000000000000000000000000000000000000000000000e-12"),
  2052. cpp_dec_float("7.275957614183425903320312500000000000000000000000000000000000000000000000000000000000000000000000000e-12"),
  2053. cpp_dec_float("1.455191522836685180664062500000000000000000000000000000000000000000000000000000000000000000000000000e-11"),
  2054. cpp_dec_float("2.910383045673370361328125000000000000000000000000000000000000000000000000000000000000000000000000000e-11"),
  2055. cpp_dec_float("5.820766091346740722656250000000000000000000000000000000000000000000000000000000000000000000000000000e-11"),
  2056. cpp_dec_float("1.164153218269348144531250000000000000000000000000000000000000000000000000000000000000000000000000000e-10"),
  2057. cpp_dec_float("2.328306436538696289062500000000000000000000000000000000000000000000000000000000000000000000000000000e-10"),
  2058. cpp_dec_float("4.656612873077392578125000000000000000000000000000000000000000000000000000000000000000000000000000000e-10"),
  2059. cpp_dec_float("9.313225746154785156250000000000000000000000000000000000000000000000000000000000000000000000000000000e-10"),
  2060. cpp_dec_float("1.862645149230957031250000000000000000000000000000000000000000000000000000000000000000000000000000000e-9"),
  2061. cpp_dec_float("3.725290298461914062500000000000000000000000000000000000000000000000000000000000000000000000000000000e-9"),
  2062. cpp_dec_float("7.450580596923828125000000000000000000000000000000000000000000000000000000000000000000000000000000000e-9"),
  2063. cpp_dec_float("1.490116119384765625000000000000000000000000000000000000000000000000000000000000000000000000000000000e-8"),
  2064. cpp_dec_float("2.980232238769531250000000000000000000000000000000000000000000000000000000000000000000000000000000000e-8"),
  2065. cpp_dec_float("5.960464477539062500000000000000000000000000000000000000000000000000000000000000000000000000000000000e-8"),
  2066. cpp_dec_float("1.192092895507812500000000000000000000000000000000000000000000000000000000000000000000000000000000000e-7"),
  2067. cpp_dec_float("2.384185791015625000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-7"),
  2068. cpp_dec_float("4.768371582031250000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-7"),
  2069. cpp_dec_float("9.536743164062500000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-7"),
  2070. cpp_dec_float("1.907348632812500000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-6"),
  2071. cpp_dec_float("3.814697265625000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-6"),
  2072. cpp_dec_float("7.629394531250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-6"),
  2073. cpp_dec_float("0.000015258789062500000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2074. cpp_dec_float("0.000030517578125000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2075. cpp_dec_float("0.000061035156250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2076. cpp_dec_float("0.000122070312500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2077. cpp_dec_float("0.000244140625000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2078. cpp_dec_float("0.000488281250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2079. cpp_dec_float("0.000976562500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2080. cpp_dec_float("0.001953125000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2081. cpp_dec_float("0.003906250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2082. cpp_dec_float("0.007812500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2083. cpp_dec_float("0.01562500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2084. cpp_dec_float("0.03125000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2085. cpp_dec_float("0.06250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"),
  2086. cpp_dec_float("0.125"),
  2087. cpp_dec_float("0.25"),
  2088. cpp_dec_float("0.5"),
  2089. one(),
  2090. two(),
  2091. cpp_dec_float(static_cast<boost::ulong_long_type>(4)),
  2092. cpp_dec_float(static_cast<boost::ulong_long_type>(8)),
  2093. cpp_dec_float(static_cast<boost::ulong_long_type>(16)),
  2094. cpp_dec_float(static_cast<boost::ulong_long_type>(32)),
  2095. cpp_dec_float(static_cast<boost::ulong_long_type>(64)),
  2096. cpp_dec_float(static_cast<boost::ulong_long_type>(128)),
  2097. cpp_dec_float(static_cast<boost::ulong_long_type>(256)),
  2098. cpp_dec_float(static_cast<boost::ulong_long_type>(512)),
  2099. cpp_dec_float(static_cast<boost::ulong_long_type>(1024)),
  2100. cpp_dec_float(static_cast<boost::ulong_long_type>(2048)),
  2101. cpp_dec_float(static_cast<boost::ulong_long_type>(4096)),
  2102. cpp_dec_float(static_cast<boost::ulong_long_type>(8192)),
  2103. cpp_dec_float(static_cast<boost::ulong_long_type>(16384)),
  2104. cpp_dec_float(static_cast<boost::ulong_long_type>(32768)),
  2105. cpp_dec_float(static_cast<boost::ulong_long_type>(65536)),
  2106. cpp_dec_float(static_cast<boost::ulong_long_type>(131072)),
  2107. cpp_dec_float(static_cast<boost::ulong_long_type>(262144)),
  2108. cpp_dec_float(static_cast<boost::ulong_long_type>(524288)),
  2109. cpp_dec_float(static_cast<std::uint64_t>(1uL << 20u)),
  2110. cpp_dec_float(static_cast<std::uint64_t>(1uL << 21u)),
  2111. cpp_dec_float(static_cast<std::uint64_t>(1uL << 22u)),
  2112. cpp_dec_float(static_cast<std::uint64_t>(1uL << 23u)),
  2113. cpp_dec_float(static_cast<std::uint64_t>(1uL << 24u)),
  2114. cpp_dec_float(static_cast<std::uint64_t>(1uL << 25u)),
  2115. cpp_dec_float(static_cast<std::uint64_t>(1uL << 26u)),
  2116. cpp_dec_float(static_cast<std::uint64_t>(1uL << 27u)),
  2117. cpp_dec_float(static_cast<std::uint64_t>(1uL << 28u)),
  2118. cpp_dec_float(static_cast<std::uint64_t>(1uL << 29u)),
  2119. cpp_dec_float(static_cast<std::uint64_t>(1uL << 30u)),
  2120. cpp_dec_float(static_cast<std::uint64_t>(1uL << 31u)),
  2121. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 32u)),
  2122. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 33u)),
  2123. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 34u)),
  2124. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 35u)),
  2125. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 36u)),
  2126. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 37u)),
  2127. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 38u)),
  2128. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 39u)),
  2129. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 40u)),
  2130. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 41u)),
  2131. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 42u)),
  2132. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 43u)),
  2133. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 44u)),
  2134. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 45u)),
  2135. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 46u)),
  2136. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 47u)),
  2137. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 48u)),
  2138. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 49u)),
  2139. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 50u)),
  2140. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 51u)),
  2141. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 52u)),
  2142. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 53u)),
  2143. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 54u)),
  2144. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 55u)),
  2145. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 56u)),
  2146. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 57u)),
  2147. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 58u)),
  2148. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 59u)),
  2149. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 60u)),
  2150. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 61u)),
  2151. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 62u)),
  2152. cpp_dec_float(static_cast<std::uint64_t>(1uLL << 63u)),
  2153. cpp_dec_float("1.844674407370955161600000000000000000000000000000000000000000000000000000000000000000000000000000000e19"),
  2154. cpp_dec_float("3.689348814741910323200000000000000000000000000000000000000000000000000000000000000000000000000000000e19"),
  2155. cpp_dec_float("7.378697629483820646400000000000000000000000000000000000000000000000000000000000000000000000000000000e19"),
  2156. cpp_dec_float("1.475739525896764129280000000000000000000000000000000000000000000000000000000000000000000000000000000e20"),
  2157. cpp_dec_float("2.951479051793528258560000000000000000000000000000000000000000000000000000000000000000000000000000000e20"),
  2158. cpp_dec_float("5.902958103587056517120000000000000000000000000000000000000000000000000000000000000000000000000000000e20"),
  2159. cpp_dec_float("1.180591620717411303424000000000000000000000000000000000000000000000000000000000000000000000000000000e21"),
  2160. cpp_dec_float("2.361183241434822606848000000000000000000000000000000000000000000000000000000000000000000000000000000e21"),
  2161. cpp_dec_float("4.722366482869645213696000000000000000000000000000000000000000000000000000000000000000000000000000000e21"),
  2162. cpp_dec_float("9.444732965739290427392000000000000000000000000000000000000000000000000000000000000000000000000000000e21"),
  2163. cpp_dec_float("1.888946593147858085478400000000000000000000000000000000000000000000000000000000000000000000000000000e22"),
  2164. cpp_dec_float("3.777893186295716170956800000000000000000000000000000000000000000000000000000000000000000000000000000e22"),
  2165. cpp_dec_float("7.555786372591432341913600000000000000000000000000000000000000000000000000000000000000000000000000000e22"),
  2166. cpp_dec_float("1.511157274518286468382720000000000000000000000000000000000000000000000000000000000000000000000000000e23"),
  2167. cpp_dec_float("3.022314549036572936765440000000000000000000000000000000000000000000000000000000000000000000000000000e23"),
  2168. cpp_dec_float("6.044629098073145873530880000000000000000000000000000000000000000000000000000000000000000000000000000e23"),
  2169. cpp_dec_float("1.208925819614629174706176000000000000000000000000000000000000000000000000000000000000000000000000000e24"),
  2170. cpp_dec_float("2.417851639229258349412352000000000000000000000000000000000000000000000000000000000000000000000000000e24"),
  2171. cpp_dec_float("4.835703278458516698824704000000000000000000000000000000000000000000000000000000000000000000000000000e24"),
  2172. cpp_dec_float("9.671406556917033397649408000000000000000000000000000000000000000000000000000000000000000000000000000e24"),
  2173. cpp_dec_float("1.934281311383406679529881600000000000000000000000000000000000000000000000000000000000000000000000000e25"),
  2174. cpp_dec_float("3.868562622766813359059763200000000000000000000000000000000000000000000000000000000000000000000000000e25"),
  2175. cpp_dec_float("7.737125245533626718119526400000000000000000000000000000000000000000000000000000000000000000000000000e25"),
  2176. cpp_dec_float("1.547425049106725343623905280000000000000000000000000000000000000000000000000000000000000000000000000e26"),
  2177. cpp_dec_float("3.094850098213450687247810560000000000000000000000000000000000000000000000000000000000000000000000000e26"),
  2178. cpp_dec_float("6.189700196426901374495621120000000000000000000000000000000000000000000000000000000000000000000000000e26"),
  2179. cpp_dec_float("1.237940039285380274899124224000000000000000000000000000000000000000000000000000000000000000000000000e27"),
  2180. cpp_dec_float("2.475880078570760549798248448000000000000000000000000000000000000000000000000000000000000000000000000e27"),
  2181. cpp_dec_float("4.951760157141521099596496896000000000000000000000000000000000000000000000000000000000000000000000000e27"),
  2182. cpp_dec_float("9.903520314283042199192993792000000000000000000000000000000000000000000000000000000000000000000000000e27"),
  2183. cpp_dec_float("1.980704062856608439838598758400000000000000000000000000000000000000000000000000000000000000000000000e28"),
  2184. cpp_dec_float("3.961408125713216879677197516800000000000000000000000000000000000000000000000000000000000000000000000e28"),
  2185. cpp_dec_float("7.922816251426433759354395033600000000000000000000000000000000000000000000000000000000000000000000000e28"),
  2186. cpp_dec_float("1.584563250285286751870879006720000000000000000000000000000000000000000000000000000000000000000000000e29"),
  2187. cpp_dec_float("3.169126500570573503741758013440000000000000000000000000000000000000000000000000000000000000000000000e29"),
  2188. cpp_dec_float("6.338253001141147007483516026880000000000000000000000000000000000000000000000000000000000000000000000e29"),
  2189. cpp_dec_float("1.267650600228229401496703205376000000000000000000000000000000000000000000000000000000000000000000000e30"),
  2190. cpp_dec_float("2.535301200456458802993406410752000000000000000000000000000000000000000000000000000000000000000000000e30"),
  2191. cpp_dec_float("5.070602400912917605986812821504000000000000000000000000000000000000000000000000000000000000000000000e30"),
  2192. cpp_dec_float("1.014120480182583521197362564300800000000000000000000000000000000000000000000000000000000000000000000e31"),
  2193. cpp_dec_float("2.028240960365167042394725128601600000000000000000000000000000000000000000000000000000000000000000000e31"),
  2194. cpp_dec_float("4.056481920730334084789450257203200000000000000000000000000000000000000000000000000000000000000000000e31"),
  2195. cpp_dec_float("8.112963841460668169578900514406400000000000000000000000000000000000000000000000000000000000000000000e31"),
  2196. cpp_dec_float("1.622592768292133633915780102881280000000000000000000000000000000000000000000000000000000000000000000e32"),
  2197. cpp_dec_float("3.245185536584267267831560205762560000000000000000000000000000000000000000000000000000000000000000000e32"),
  2198. cpp_dec_float("6.490371073168534535663120411525120000000000000000000000000000000000000000000000000000000000000000000e32"),
  2199. cpp_dec_float("1.298074214633706907132624082305024000000000000000000000000000000000000000000000000000000000000000000e33"),
  2200. cpp_dec_float("2.596148429267413814265248164610048000000000000000000000000000000000000000000000000000000000000000000e33"),
  2201. cpp_dec_float("5.192296858534827628530496329220096000000000000000000000000000000000000000000000000000000000000000000e33"),
  2202. cpp_dec_float("1.038459371706965525706099265844019200000000000000000000000000000000000000000000000000000000000000000e34"),
  2203. cpp_dec_float("2.076918743413931051412198531688038400000000000000000000000000000000000000000000000000000000000000000e34"),
  2204. cpp_dec_float("4.153837486827862102824397063376076800000000000000000000000000000000000000000000000000000000000000000e34"),
  2205. cpp_dec_float("8.307674973655724205648794126752153600000000000000000000000000000000000000000000000000000000000000000e34"),
  2206. cpp_dec_float("1.661534994731144841129758825350430720000000000000000000000000000000000000000000000000000000000000000e35"),
  2207. cpp_dec_float("3.323069989462289682259517650700861440000000000000000000000000000000000000000000000000000000000000000e35"),
  2208. cpp_dec_float("6.646139978924579364519035301401722880000000000000000000000000000000000000000000000000000000000000000e35"),
  2209. cpp_dec_float("1.329227995784915872903807060280344576000000000000000000000000000000000000000000000000000000000000000e36"),
  2210. cpp_dec_float("2.658455991569831745807614120560689152000000000000000000000000000000000000000000000000000000000000000e36"),
  2211. cpp_dec_float("5.316911983139663491615228241121378304000000000000000000000000000000000000000000000000000000000000000e36"),
  2212. cpp_dec_float("1.063382396627932698323045648224275660800000000000000000000000000000000000000000000000000000000000000e37"),
  2213. cpp_dec_float("2.126764793255865396646091296448551321600000000000000000000000000000000000000000000000000000000000000e37"),
  2214. cpp_dec_float("4.253529586511730793292182592897102643200000000000000000000000000000000000000000000000000000000000000e37"),
  2215. cpp_dec_float("8.507059173023461586584365185794205286400000000000000000000000000000000000000000000000000000000000000e37"),
  2216. cpp_dec_float("1.701411834604692317316873037158841057280000000000000000000000000000000000000000000000000000000000000e38")}};
  2217. if ((p > static_cast<boost::long_long_type>(-128)) && (p < static_cast<boost::long_long_type>(+128)))
  2218. {
  2219. return p2_data[static_cast<std::size_t>(p + ((p2_data.size() - 1u) / 2u))];
  2220. }
  2221. else
  2222. {
  2223. // Compute and return 2^p.
  2224. if (p < static_cast<boost::long_long_type>(0))
  2225. {
  2226. return pow2(static_cast<boost::long_long_type>(-p)).calculate_inv();
  2227. }
  2228. else
  2229. {
  2230. cpp_dec_float<Digits10, ExponentType, Allocator> t;
  2231. default_ops::detail::pow_imp(t, two(), p, std::integral_constant<bool, true>());
  2232. return t;
  2233. }
  2234. }
  2235. }
  2236. template <unsigned Digits10, class ExponentType, class Allocator>
  2237. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2238. {
  2239. result += o;
  2240. }
  2241. template <unsigned Digits10, class ExponentType, class Allocator>
  2242. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2243. {
  2244. result -= o;
  2245. }
  2246. template <unsigned Digits10, class ExponentType, class Allocator>
  2247. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2248. {
  2249. result *= o;
  2250. }
  2251. template <unsigned Digits10, class ExponentType, class Allocator>
  2252. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& o)
  2253. {
  2254. result /= o;
  2255. }
  2256. template <unsigned Digits10, class ExponentType, class Allocator>
  2257. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const boost::ulong_long_type& o)
  2258. {
  2259. result.add_unsigned_long_long(o);
  2260. }
  2261. template <unsigned Digits10, class ExponentType, class Allocator>
  2262. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const boost::ulong_long_type& o)
  2263. {
  2264. result.sub_unsigned_long_long(o);
  2265. }
  2266. template <unsigned Digits10, class ExponentType, class Allocator>
  2267. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const boost::ulong_long_type& o)
  2268. {
  2269. result.mul_unsigned_long_long(o);
  2270. }
  2271. template <unsigned Digits10, class ExponentType, class Allocator>
  2272. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const boost::ulong_long_type& o)
  2273. {
  2274. result.div_unsigned_long_long(o);
  2275. }
  2276. template <unsigned Digits10, class ExponentType, class Allocator>
  2277. inline void eval_add(cpp_dec_float<Digits10, ExponentType, Allocator>& result, boost::long_long_type o)
  2278. {
  2279. if (o < 0)
  2280. result.sub_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2281. else
  2282. result.add_unsigned_long_long(o);
  2283. }
  2284. template <unsigned Digits10, class ExponentType, class Allocator>
  2285. inline void eval_subtract(cpp_dec_float<Digits10, ExponentType, Allocator>& result, boost::long_long_type o)
  2286. {
  2287. if (o < 0)
  2288. result.add_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2289. else
  2290. result.sub_unsigned_long_long(o);
  2291. }
  2292. template <unsigned Digits10, class ExponentType, class Allocator>
  2293. inline void eval_multiply(cpp_dec_float<Digits10, ExponentType, Allocator>& result, boost::long_long_type o)
  2294. {
  2295. if (o < 0)
  2296. {
  2297. result.mul_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2298. result.negate();
  2299. }
  2300. else
  2301. result.mul_unsigned_long_long(o);
  2302. }
  2303. template <unsigned Digits10, class ExponentType, class Allocator>
  2304. inline void eval_divide(cpp_dec_float<Digits10, ExponentType, Allocator>& result, boost::long_long_type o)
  2305. {
  2306. if (o < 0)
  2307. {
  2308. result.div_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o));
  2309. result.negate();
  2310. }
  2311. else
  2312. result.div_unsigned_long_long(o);
  2313. }
  2314. template <unsigned Digits10, class ExponentType, class Allocator>
  2315. inline void eval_convert_to(boost::ulong_long_type* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2316. {
  2317. *result = val.extract_unsigned_long_long();
  2318. }
  2319. template <unsigned Digits10, class ExponentType, class Allocator>
  2320. inline void eval_convert_to(boost::long_long_type* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2321. {
  2322. *result = val.extract_signed_long_long();
  2323. }
  2324. template <unsigned Digits10, class ExponentType, class Allocator>
  2325. inline void eval_convert_to(long double* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2326. {
  2327. *result = val.extract_long_double();
  2328. }
  2329. template <unsigned Digits10, class ExponentType, class Allocator>
  2330. inline void eval_convert_to(double* result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2331. {
  2332. *result = val.extract_double();
  2333. }
  2334. //
  2335. // Non member function support:
  2336. //
  2337. template <unsigned Digits10, class ExponentType, class Allocator>
  2338. inline int eval_fpclassify(const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2339. {
  2340. if ((x.isinf)())
  2341. return FP_INFINITE;
  2342. if ((x.isnan)())
  2343. return FP_NAN;
  2344. if (x.iszero())
  2345. return FP_ZERO;
  2346. return FP_NORMAL;
  2347. }
  2348. template <unsigned Digits10, class ExponentType, class Allocator>
  2349. inline void eval_abs(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2350. {
  2351. result = x;
  2352. if (x.isneg())
  2353. result.negate();
  2354. }
  2355. template <unsigned Digits10, class ExponentType, class Allocator>
  2356. inline void eval_fabs(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2357. {
  2358. result = x;
  2359. if (x.isneg())
  2360. result.negate();
  2361. }
  2362. template <unsigned Digits10, class ExponentType, class Allocator>
  2363. inline void eval_sqrt(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2364. {
  2365. result = x;
  2366. result.calculate_sqrt();
  2367. }
  2368. template <unsigned Digits10, class ExponentType, class Allocator>
  2369. inline void eval_floor(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2370. {
  2371. result = x;
  2372. if (!(x.isfinite)() || x.isint())
  2373. {
  2374. if ((x.isnan)())
  2375. errno = EDOM;
  2376. return;
  2377. }
  2378. if (x.isneg())
  2379. result -= cpp_dec_float<Digits10, ExponentType, Allocator>::one();
  2380. result = result.extract_integer_part();
  2381. }
  2382. template <unsigned Digits10, class ExponentType, class Allocator>
  2383. inline void eval_ceil(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2384. {
  2385. result = x;
  2386. if (!(x.isfinite)() || x.isint())
  2387. {
  2388. if ((x.isnan)())
  2389. errno = EDOM;
  2390. return;
  2391. }
  2392. if (!x.isneg())
  2393. result += cpp_dec_float<Digits10, ExponentType, Allocator>::one();
  2394. result = result.extract_integer_part();
  2395. }
  2396. template <unsigned Digits10, class ExponentType, class Allocator>
  2397. inline void eval_trunc(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x)
  2398. {
  2399. if (x.isint() || !(x.isfinite)())
  2400. {
  2401. result = x;
  2402. if ((x.isnan)())
  2403. errno = EDOM;
  2404. return;
  2405. }
  2406. result = x.extract_integer_part();
  2407. }
  2408. template <unsigned Digits10, class ExponentType, class Allocator>
  2409. inline ExponentType eval_ilogb(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2410. {
  2411. if (val.iszero())
  2412. return (std::numeric_limits<ExponentType>::min)();
  2413. if ((val.isinf)())
  2414. return INT_MAX;
  2415. if ((val.isnan)())
  2416. #ifdef FP_ILOGBNAN
  2417. return FP_ILOGBNAN;
  2418. #else
  2419. return INT_MAX;
  2420. #endif
  2421. // Set result, to the exponent of val:
  2422. return val.order();
  2423. }
  2424. template <unsigned Digits10, class ExponentType, class Allocator, class ArgType>
  2425. inline void eval_scalbn(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& val, ArgType e_)
  2426. {
  2427. using default_ops::eval_multiply;
  2428. const ExponentType e = static_cast<ExponentType>(e_);
  2429. cpp_dec_float<Digits10, ExponentType, Allocator> t(1.0, e);
  2430. eval_multiply(result, val, t);
  2431. }
  2432. template <unsigned Digits10, class ExponentType, class Allocator, class ArgType>
  2433. inline void eval_ldexp(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x, ArgType e)
  2434. {
  2435. const boost::long_long_type the_exp = static_cast<boost::long_long_type>(e);
  2436. if ((the_exp > (std::numeric_limits<ExponentType>::max)()) || (the_exp < (std::numeric_limits<ExponentType>::min)()))
  2437. BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Exponent value is out of range.")));
  2438. result = x;
  2439. if ((the_exp > static_cast<boost::long_long_type>(-std::numeric_limits<boost::long_long_type>::digits)) && (the_exp < static_cast<boost::long_long_type>(0)))
  2440. result.div_unsigned_long_long(1ULL << static_cast<boost::long_long_type>(-the_exp));
  2441. else if ((the_exp < static_cast<boost::long_long_type>(std::numeric_limits<boost::long_long_type>::digits)) && (the_exp > static_cast<boost::long_long_type>(0)))
  2442. result.mul_unsigned_long_long(1ULL << the_exp);
  2443. else if (the_exp != static_cast<boost::long_long_type>(0))
  2444. {
  2445. if ((the_exp < cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp / 2) && (x.order() > 0))
  2446. {
  2447. boost::long_long_type half_exp = e / 2;
  2448. cpp_dec_float<Digits10, ExponentType, Allocator> t = cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(half_exp);
  2449. result *= t;
  2450. if (2 * half_exp != e)
  2451. t *= 2;
  2452. result *= t;
  2453. }
  2454. else
  2455. result *= cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(e);
  2456. }
  2457. }
  2458. template <unsigned Digits10, class ExponentType, class Allocator>
  2459. inline void eval_frexp(cpp_dec_float<Digits10, ExponentType, Allocator>& result, const cpp_dec_float<Digits10, ExponentType, Allocator>& x, ExponentType* e)
  2460. {
  2461. result = x;
  2462. if (result.iszero() || (result.isinf)() || (result.isnan)())
  2463. {
  2464. *e = 0;
  2465. return;
  2466. }
  2467. if (result.isneg())
  2468. result.negate();
  2469. ExponentType t = result.order();
  2470. BOOST_MP_USING_ABS
  2471. if (abs(t) < ((std::numeric_limits<ExponentType>::max)() / 1000))
  2472. {
  2473. t *= 1000;
  2474. t /= 301;
  2475. }
  2476. else
  2477. {
  2478. t /= 301;
  2479. t *= 1000;
  2480. }
  2481. result *= cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(-t);
  2482. if (result.iszero() || (result.isinf)() || (result.isnan)())
  2483. {
  2484. // pow2 overflowed, slip the calculation up:
  2485. result = x;
  2486. if (result.isneg())
  2487. result.negate();
  2488. t /= 2;
  2489. result *= cpp_dec_float<Digits10, ExponentType, Allocator>::pow2(-t);
  2490. }
  2491. BOOST_MP_USING_ABS
  2492. if (abs(result.order()) > 5)
  2493. {
  2494. // If our first estimate doesn't get close enough then try recursion until we do:
  2495. ExponentType e2;
  2496. cpp_dec_float<Digits10, ExponentType, Allocator> r2;
  2497. eval_frexp(r2, result, &e2);
  2498. // overflow protection:
  2499. if ((t > 0) && (e2 > 0) && (t > (std::numeric_limits<ExponentType>::max)() - e2))
  2500. BOOST_THROW_EXCEPTION(std::runtime_error("Exponent is too large to be represented as a power of 2."));
  2501. if ((t < 0) && (e2 < 0) && (t < (std::numeric_limits<ExponentType>::min)() - e2))
  2502. BOOST_THROW_EXCEPTION(std::runtime_error("Exponent is too large to be represented as a power of 2."));
  2503. t += e2;
  2504. result = r2;
  2505. }
  2506. while (result.compare(cpp_dec_float<Digits10, ExponentType, Allocator>::one()) >= 0)
  2507. {
  2508. result /= cpp_dec_float<Digits10, ExponentType, Allocator>::two();
  2509. ++t;
  2510. }
  2511. while (result.compare(cpp_dec_float<Digits10, ExponentType, Allocator>::half()) < 0)
  2512. {
  2513. result *= cpp_dec_float<Digits10, ExponentType, Allocator>::two();
  2514. --t;
  2515. }
  2516. *e = t;
  2517. if (x.isneg())
  2518. result.negate();
  2519. }
  2520. template <unsigned Digits10, class ExponentType, class Allocator>
  2521. 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)
  2522. {
  2523. ExponentType t;
  2524. eval_frexp(result, x, &t);
  2525. if ((t > (std::numeric_limits<int>::max)()) || (t < (std::numeric_limits<int>::min)()))
  2526. BOOST_THROW_EXCEPTION(std::runtime_error("Exponent is outside the range of an int"));
  2527. *e = static_cast<int>(t);
  2528. }
  2529. template <unsigned Digits10, class ExponentType, class Allocator>
  2530. inline bool eval_is_zero(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2531. {
  2532. return val.iszero();
  2533. }
  2534. template <unsigned Digits10, class ExponentType, class Allocator>
  2535. inline int eval_get_sign(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2536. {
  2537. return val.iszero() ? 0 : val.isneg() ? -1 : 1;
  2538. }
  2539. template <unsigned Digits10, class ExponentType, class Allocator>
  2540. inline std::size_t hash_value(const cpp_dec_float<Digits10, ExponentType, Allocator>& val)
  2541. {
  2542. return val.hash();
  2543. }
  2544. } // namespace backends
  2545. using boost::multiprecision::backends::cpp_dec_float;
  2546. using cpp_dec_float_50 = number<cpp_dec_float<50> > ;
  2547. using cpp_dec_float_100 = number<cpp_dec_float<100> >;
  2548. namespace detail {
  2549. template <unsigned Digits10, class ExponentType, class Allocator>
  2550. struct transcendental_reduction_type<boost::multiprecision::backends::cpp_dec_float<Digits10, ExponentType, Allocator> >
  2551. {
  2552. //
  2553. // The type used for trigonometric reduction needs 3 times the precision of the base type.
  2554. // This is double the precision of the original type, plus the largest exponent supported.
  2555. // As a practical measure the largest argument supported is 1/eps, as supporting larger
  2556. // arguments requires the division of argument by PI/2 to also be done at higher precision,
  2557. // otherwise the result (an integer) can not be represented exactly.
  2558. //
  2559. // See ARGUMENT REDUCTION FOR HUGE ARGUMENTS. K C Ng.
  2560. //
  2561. using type = boost::multiprecision::backends::cpp_dec_float<Digits10 * 3, ExponentType, Allocator>;
  2562. };
  2563. } // namespace detail
  2564. }} // namespace boost::multiprecision
  2565. namespace std {
  2566. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2567. class numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >
  2568. {
  2569. public:
  2570. static constexpr bool is_specialized = true;
  2571. static constexpr bool is_signed = true;
  2572. static constexpr bool is_integer = false;
  2573. static constexpr bool is_exact = false;
  2574. static constexpr bool is_bounded = true;
  2575. static constexpr bool is_modulo = false;
  2576. static constexpr bool is_iec559 = false;
  2577. static constexpr int digits = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  2578. static constexpr int digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  2579. static constexpr int max_digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_total_digits10;
  2580. static constexpr ExponentType min_exponent = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp; // Type differs from int.
  2581. static constexpr ExponentType min_exponent10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_min_exp10; // Type differs from int.
  2582. static constexpr ExponentType max_exponent = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp; // Type differs from int.
  2583. static constexpr ExponentType max_exponent10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_max_exp10; // Type differs from int.
  2584. static constexpr int radix = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_radix;
  2585. static constexpr std::float_round_style round_style = std::round_indeterminate;
  2586. static constexpr bool has_infinity = true;
  2587. static constexpr bool has_quiet_NaN = true;
  2588. static constexpr bool has_signaling_NaN = false;
  2589. static constexpr std::float_denorm_style has_denorm = std::denorm_absent;
  2590. static constexpr bool has_denorm_loss = false;
  2591. static constexpr bool traps = false;
  2592. static constexpr bool tinyness_before = false;
  2593. 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)(); }
  2594. 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)(); }
  2595. 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(); }
  2596. 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(); }
  2597. static constexpr boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> round_error() { return 0.5L; }
  2598. 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(); }
  2599. 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(); }
  2600. 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(); }
  2601. 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>::zero(); }
  2602. };
  2603. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2604. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::digits;
  2605. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2606. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::digits10;
  2607. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2608. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_digits10;
  2609. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2610. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_signed;
  2611. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2612. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_integer;
  2613. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2614. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_exact;
  2615. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2616. constexpr int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::radix;
  2617. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2618. constexpr ExponentType numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::min_exponent;
  2619. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2620. constexpr ExponentType numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::min_exponent10;
  2621. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2622. constexpr ExponentType numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_exponent;
  2623. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2624. constexpr ExponentType numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::max_exponent10;
  2625. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2626. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_infinity;
  2627. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2628. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_quiet_NaN;
  2629. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2630. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_signaling_NaN;
  2631. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2632. constexpr float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_denorm;
  2633. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2634. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::has_denorm_loss;
  2635. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2636. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_iec559;
  2637. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2638. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_bounded;
  2639. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2640. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::is_modulo;
  2641. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2642. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::traps;
  2643. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2644. constexpr bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::tinyness_before;
  2645. template <unsigned Digits10, class ExponentType, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
  2646. constexpr float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates> >::round_style;
  2647. } // namespace std
  2648. namespace boost {
  2649. namespace math {
  2650. namespace policies {
  2651. template <unsigned Digits10, class ExponentType, class Allocator, class Policy, boost::multiprecision::expression_template_option ExpressionTemplates>
  2652. struct precision<boost::multiprecision::number<boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>, ExpressionTemplates>, Policy>
  2653. {
  2654. // Define a local copy of cpp_dec_float_digits10 because it might differ
  2655. // from the template parameter Digits10 for small or large digit counts.
  2656. static constexpr const std::int32_t cpp_dec_float_digits10 = boost::multiprecision::cpp_dec_float<Digits10, ExponentType, Allocator>::cpp_dec_float_digits10;
  2657. using precision_type = typename Policy::precision_type ;
  2658. using digits_2 = digits2<((cpp_dec_float_digits10 + 1LL) * 1000LL) / 301LL>;
  2659. using type = typename std::conditional<
  2660. ((digits_2::value <= precision_type::value) || (Policy::precision_type::value <= 0)),
  2661. // Default case, full precision for RealType:
  2662. digits_2,
  2663. // User customized precision:
  2664. precision_type>::type;
  2665. };
  2666. }
  2667. }} // namespace boost::math::policies
  2668. #ifdef BOOST_MSVC
  2669. #pragma warning(pop)
  2670. #endif
  2671. #endif