cpp_dec_float.hpp 131 KB

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