cpp_dec_float.hpp 138 KB

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