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