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