cpp_dec_float.hpp 137 KB

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