1 ///////////////////////////////////////////////////////////////
2 // Copyright 2013 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
6 #ifndef BOOST_MATH_CPP_BIN_FLOAT_HPP
7 #define BOOST_MATH_CPP_BIN_FLOAT_HPP
9 #include <boost/multiprecision/cpp_int.hpp>
10 #include <boost/multiprecision/integer.hpp>
11 #include <boost/math/special_functions/trunc.hpp>
12 #include <boost/multiprecision/detail/float_string_cvt.hpp>
15 // Some includes we need from Boost.Math, since we rely on that library to provide these functions:
17 #include <boost/math/special_functions/asinh.hpp>
18 #include <boost/math/special_functions/acosh.hpp>
19 #include <boost/math/special_functions/atanh.hpp>
20 #include <boost/math/special_functions/cbrt.hpp>
21 #include <boost/math/special_functions/expm1.hpp>
22 #include <boost/math/special_functions/gamma.hpp>
24 #ifdef BOOST_HAS_FLOAT128
28 namespace boost{ namespace multiprecision{ namespace backends{
38 #pragma warning(disable:4522 6326) // multiple assignment operators specified, comparison of two constants
44 inline typename enable_if_c<is_unsigned<U>::value, bool>::type is_negative(U) { return false; }
46 inline typename disable_if_c<is_unsigned<S>::value, bool>::type is_negative(S s) { return s < 0; }
50 template <unsigned Digits, digit_base_type DigitBase = digit_base_10, class Allocator = void, class Exponent = int, Exponent MinExponent = 0, Exponent MaxExponent = 0>
54 static const unsigned bit_count = DigitBase == digit_base_2 ? Digits : (Digits * 1000uL) / 301uL + (((Digits * 1000uL) % 301) ? 2u : 1u);
55 typedef cpp_int_backend<is_void<Allocator>::value ? bit_count : 0, bit_count, is_void<Allocator>::value ? unsigned_magnitude : signed_magnitude, unchecked, Allocator> rep_type;
56 typedef cpp_int_backend<is_void<Allocator>::value ? 2 * bit_count : 0, 2 * bit_count, is_void<Allocator>::value ? unsigned_magnitude : signed_magnitude, unchecked, Allocator> double_rep_type;
58 typedef typename rep_type::signed_types signed_types;
59 typedef typename rep_type::unsigned_types unsigned_types;
60 typedef boost::mpl::list<float, double, long double> float_types;
61 typedef Exponent exponent_type;
63 static const exponent_type max_exponent_limit = boost::integer_traits<exponent_type>::const_max - 2 * static_cast<exponent_type>(bit_count);
64 static const exponent_type min_exponent_limit = boost::integer_traits<exponent_type>::const_min + 2 * static_cast<exponent_type>(bit_count);
66 BOOST_STATIC_ASSERT_MSG(MinExponent >= min_exponent_limit, "Template parameter MinExponent is too negative for our internal logic to function correctly, sorry!");
67 BOOST_STATIC_ASSERT_MSG(MaxExponent <= max_exponent_limit, "Template parameter MaxExponent is too large for our internal logic to function correctly, sorry!");
68 BOOST_STATIC_ASSERT_MSG(MinExponent <= 0, "Template parameter MinExponent can not be positive!");
69 BOOST_STATIC_ASSERT_MSG(MaxExponent >= 0, "Template parameter MaxExponent can not be negative!");
71 static const exponent_type max_exponent = MaxExponent == 0 ? max_exponent_limit : MaxExponent;
72 static const exponent_type min_exponent = MinExponent == 0 ? min_exponent_limit : MinExponent;
74 static const exponent_type exponent_zero = max_exponent + 1;
75 static const exponent_type exponent_infinity = max_exponent + 2;
76 static const exponent_type exponent_nan = max_exponent + 3;
81 exponent_type m_exponent;
84 cpp_bin_float() BOOST_MP_NOEXCEPT_IF(noexcept(rep_type())) : m_data(), m_exponent(exponent_zero), m_sign(false) {}
86 cpp_bin_float(const cpp_bin_float &o) BOOST_MP_NOEXCEPT_IF(noexcept(rep_type(std::declval<const rep_type&>())))
87 : m_data(o.m_data), m_exponent(o.m_exponent), m_sign(o.m_sign) {}
89 template <unsigned D, digit_base_type B, class A, class E, E MinE, E MaxE>
90 cpp_bin_float(const cpp_bin_float<D, B, A, E, MinE, MaxE> &o, typename boost::enable_if_c<(bit_count >= cpp_bin_float<D, B, A, E, MinE, MaxE>::bit_count)>::type const* = 0)
94 template <unsigned D, digit_base_type B, class A, class E, E MinE, E MaxE>
95 explicit cpp_bin_float(const cpp_bin_float<D, B, A, E, MinE, MaxE> &o, typename boost::disable_if_c<(bit_count >= cpp_bin_float<D, B, A, E, MinE, MaxE>::bit_count)>::type const* = 0)
96 : m_exponent(o.exponent()), m_sign(o.sign())
100 template <class Float>
101 cpp_bin_float(const Float& f,
102 typename boost::enable_if_c<
103 (number_category<Float>::value == number_kind_floating_point)
104 && (std::numeric_limits<Float>::digits <= (int)bit_count)
105 && (std::numeric_limits<Float>::radix == 2)
106 && (std::numeric_limits<Float>::is_specialized)
107 #ifdef BOOST_HAS_FLOAT128
108 && !boost::is_same<Float, __float128>::value
111 : m_data(), m_exponent(0), m_sign(false)
113 this->assign_float(f);
116 template <class Float>
117 explicit cpp_bin_float(const Float& f,
118 typename boost::enable_if_c<
119 (number_category<Float>::value == number_kind_floating_point)
120 && (std::numeric_limits<Float>::digits > (int)bit_count)
121 && (std::numeric_limits<Float>::radix == 2)
122 && (std::numeric_limits<Float>::is_specialized)
123 #ifdef BOOST_HAS_FLOAT128
124 && !boost::is_same<Float, __float128>::value
127 : m_data(), m_exponent(0), m_sign(false)
129 this->assign_float(f);
131 #ifdef BOOST_HAS_FLOAT128
132 template <class Float>
133 cpp_bin_float(const Float& f,
134 typename boost::enable_if_c<
135 boost::is_same<Float, __float128>::value
136 && ((int)bit_count >= 113)
138 : m_data(), m_exponent(0), m_sign(false)
140 this->assign_float(f);
142 template <class Float>
143 explicit cpp_bin_float(const Float& f,
144 typename boost::enable_if_c<
145 boost::is_same<Float, __float128>::value
146 && ((int)bit_count < 113)
148 : m_data(), m_exponent(0), m_sign(false)
150 this->assign_float(f);
153 cpp_bin_float& operator=(const cpp_bin_float &o) BOOST_MP_NOEXCEPT_IF(noexcept(std::declval<rep_type&>() = std::declval<const rep_type&>()))
156 m_exponent = o.m_exponent;
161 template <unsigned D, digit_base_type B, class A, class E, E MinE, E MaxE>
162 cpp_bin_float& operator=(const cpp_bin_float<D, B, A, E, MinE, MaxE> &f)
164 switch(eval_fpclassify(f))
167 m_data = limb_type(0);
169 m_exponent = exponent_zero;
172 m_data = limb_type(0);
174 m_exponent = exponent_nan;
177 m_data = limb_type(0);
179 m_exponent = exponent_infinity;
182 typename cpp_bin_float<D, B, A, E, MinE, MaxE>::rep_type b(f.bits());
183 this->exponent() = f.exponent() + (int)bit_count - (int)cpp_bin_float<D, B, A, E, MinE, MaxE>::bit_count;
184 this->sign() = f.sign();
185 copy_and_round(*this, b);
189 #ifdef BOOST_HAS_FLOAT128
190 template <class Float>
191 typename boost::enable_if_c<
192 (number_category<Float>::value == number_kind_floating_point)
193 //&& (std::numeric_limits<Float>::digits <= (int)bit_count)
194 && ((std::numeric_limits<Float>::radix == 2) || (boost::is_same<Float, __float128>::value)), cpp_bin_float&>::type
195 operator=(const Float& f)
197 template <class Float>
198 typename boost::enable_if_c<
199 (number_category<Float>::value == number_kind_floating_point)
200 //&& (std::numeric_limits<Float>::digits <= (int)bit_count)
201 && (std::numeric_limits<Float>::radix == 2), cpp_bin_float&>::type
202 operator=(const Float& f)
205 return assign_float(f);
208 #ifdef BOOST_HAS_FLOAT128
209 template <class Float>
210 typename boost::enable_if_c<boost::is_same<Float, __float128>::value, cpp_bin_float& >::type assign_float(Float f)
212 using default_ops::eval_add;
213 typedef typename boost::multiprecision::detail::canonical<int, cpp_bin_float>::type bf_int_type;
216 m_data = limb_type(0);
217 m_sign = (signbitq(f) > 0);
218 m_exponent = exponent_zero;
223 m_data = limb_type(0);
225 m_exponent = exponent_nan;
230 m_data = limb_type(0);
232 m_exponent = exponent_infinity;
242 typedef typename mpl::front<unsigned_types>::type ui_type;
243 m_data = static_cast<ui_type>(0u);
247 static const int bits = sizeof(int) * CHAR_BIT - 1;
254 int ipart = (int)truncq(f);
258 t = static_cast<bf_int_type>(ipart);
261 m_exponent += static_cast<Exponent>(e);
265 #ifdef BOOST_HAS_FLOAT128
266 template <class Float>
267 typename boost::enable_if_c<is_floating_point<Float>::value && !is_same<Float, __float128>::value, cpp_bin_float&>::type assign_float(Float f)
269 template <class Float>
270 typename boost::enable_if_c<is_floating_point<Float>::value, cpp_bin_float&>::type assign_float(Float f)
274 using default_ops::eval_add;
275 typedef typename boost::multiprecision::detail::canonical<int, cpp_bin_float>::type bf_int_type;
277 switch((boost::math::fpclassify)(f))
280 m_data = limb_type(0);
281 m_sign = ((boost::math::signbit)(f) > 0);
282 m_exponent = exponent_zero;
285 m_data = limb_type(0);
287 m_exponent = exponent_nan;
290 m_data = limb_type(0);
292 m_exponent = exponent_infinity;
302 typedef typename mpl::front<unsigned_types>::type ui_type;
303 m_data = static_cast<ui_type>(0u);
307 static const int bits = sizeof(int) * CHAR_BIT - 1;
314 #ifndef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS
315 int ipart = itrunc(f);
317 int ipart = static_cast<int>(f);
322 t = static_cast<bf_int_type>(ipart);
325 m_exponent += static_cast<Exponent>(e);
329 template <class Float>
330 typename boost::enable_if_c<
331 (number_category<Float>::value == number_kind_floating_point)
332 && !boost::is_floating_point<Float>::value
333 /*&& (std::numeric_limits<number<Float> >::radix == 2)*/,
334 cpp_bin_float&>::type assign_float(Float f)
337 using default_ops::eval_add;
338 using default_ops::eval_get_sign;
339 using default_ops::eval_convert_to;
340 using default_ops::eval_subtract;
342 typedef typename boost::multiprecision::detail::canonical<int, Float>::type f_int_type;
343 typedef typename boost::multiprecision::detail::canonical<int, cpp_bin_float>::type bf_int_type;
345 switch(eval_fpclassify(f))
348 m_data = limb_type(0);
349 m_sign = ((boost::math::signbit)(f) > 0);
350 m_exponent = exponent_zero;
353 m_data = limb_type(0);
355 m_exponent = exponent_nan;
358 m_data = limb_type(0);
360 m_exponent = exponent_infinity;
363 if(eval_get_sign(f) < 0)
371 typedef typename mpl::front<unsigned_types>::type ui_type;
372 m_data = static_cast<ui_type>(0u);
376 static const int bits = sizeof(int) * CHAR_BIT - 1;
378 eval_frexp(f, f, &e);
379 while(eval_get_sign(f) != 0)
381 eval_ldexp(f, f, bits);
384 eval_convert_to(&ipart, f);
385 eval_subtract(f, static_cast<f_int_type>(ipart));
387 eval_add(*this, static_cast<bf_int_type>(ipart));
390 if(m_exponent > max_exponent)
391 m_exponent = exponent_infinity;
392 if(m_exponent < min_exponent)
394 m_data = limb_type(0u);
395 m_exponent = exponent_zero;
396 m_sign = ((boost::math::signbit)(f) > 0);
398 else if(eval_get_sign(m_data) == 0)
400 m_exponent = exponent_zero;
401 m_sign = ((boost::math::signbit)(f) > 0);
407 typename boost::enable_if<is_integral<I>, cpp_bin_float&>::type operator=(const I& i)
409 using default_ops::eval_bit_test;
412 m_data = static_cast<limb_type>(0);
413 m_exponent = exponent_zero;
418 typedef typename make_unsigned<I>::type ui_type;
419 ui_type fi = static_cast<ui_type>(boost::multiprecision::detail::unsigned_abs(i));
420 typedef typename boost::multiprecision::detail::canonical<ui_type, rep_type>::type ar_type;
421 m_data = static_cast<ar_type>(fi);
422 unsigned shift = msb(fi);
423 if(shift >= bit_count)
425 m_exponent = static_cast<Exponent>(shift);
426 m_data = static_cast<ar_type>(fi >> (shift + 1 - bit_count));
430 m_exponent = static_cast<Exponent>(shift);
431 eval_left_shift(m_data, bit_count - shift - 1);
433 BOOST_ASSERT(eval_bit_test(m_data, bit_count-1));
434 m_sign = detail::is_negative(i);
439 cpp_bin_float& operator=(const char *s);
441 void swap(cpp_bin_float &o) BOOST_NOEXCEPT
443 m_data.swap(o.m_data);
444 std::swap(m_exponent, o.m_exponent);
445 std::swap(m_sign, o.m_sign);
448 std::string str(std::streamsize dig, std::ios_base::fmtflags f) const;
452 if(m_exponent != exponent_nan)
456 int compare(const cpp_bin_float &o) const BOOST_NOEXCEPT
458 if(m_sign != o.m_sign)
459 return (m_exponent == exponent_zero) && (m_exponent == o.m_exponent) ? 0 : m_sign ? -1 : 1;
461 if(m_exponent == exponent_nan)
463 else if(m_exponent != o.m_exponent)
465 if(m_exponent == exponent_zero)
467 else if(o.m_exponent == exponent_zero)
470 result = m_exponent > o.m_exponent ? 1 : -1;
473 result = m_data.compare(o.m_data);
479 int compare(const A& o) const BOOST_NOEXCEPT
486 rep_type& bits() { return m_data; }
487 const rep_type& bits()const { return m_data; }
488 exponent_type& exponent() { return m_exponent; }
489 const exponent_type& exponent()const { return m_exponent; }
490 bool& sign() { return m_sign; }
491 const bool& sign()const { return m_sign; }
492 void check_invariants()
494 using default_ops::eval_bit_test;
495 using default_ops::eval_is_zero;
496 if((m_exponent <= max_exponent) && (m_exponent >= min_exponent))
498 BOOST_ASSERT(eval_bit_test(m_data, bit_count - 1));
502 BOOST_ASSERT(m_exponent > max_exponent);
503 BOOST_ASSERT(m_exponent <= exponent_nan);
504 BOOST_ASSERT(eval_is_zero(m_data));
507 template<class Archive>
508 void serialize(Archive & ar, const unsigned int /*version*/)
520 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class Int>
521 inline void copy_and_round(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, Int &arg, int bits_to_keep = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count)
523 // Precondition: exponent of res must have been set before this function is called
524 // as we may need to adjust it based on how many bits_to_keep in arg are set.
525 using default_ops::eval_msb;
526 using default_ops::eval_lsb;
527 using default_ops::eval_left_shift;
528 using default_ops::eval_bit_test;
529 using default_ops::eval_right_shift;
530 using default_ops::eval_increment;
531 using default_ops::eval_get_sign;
533 // cancellation may have resulted in arg being all zeros:
534 if(eval_get_sign(arg) == 0)
536 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
538 res.bits() = static_cast<limb_type>(0u);
541 int msb = eval_msb(arg);
542 if(static_cast<int>(bits_to_keep) > msb + 1)
544 // Must have had cancellation in subtraction,
545 // or be converting from a narrower type, so shift left:
547 eval_left_shift(res.bits(), bits_to_keep - msb - 1);
548 res.exponent() -= static_cast<Exponent>(bits_to_keep - msb - 1);
550 else if(static_cast<int>(bits_to_keep) < msb + 1)
552 // We have more bits_to_keep than we need, so round as required,
553 // first get the rounding bit:
554 bool roundup = eval_bit_test(arg, msb - bits_to_keep);
555 // Then check for a tie:
556 if(roundup && (msb - bits_to_keep == (int)eval_lsb(arg)))
558 // Ties round towards even:
559 if(!eval_bit_test(arg, msb - bits_to_keep + 1))
562 // Shift off the bits_to_keep we don't need:
563 eval_right_shift(arg, msb - bits_to_keep + 1);
564 res.exponent() += static_cast<Exponent>(msb - bits_to_keep + 1);
570 if(eval_bit_test(arg, bits_to_keep))
572 // This happens very very rairly, all the bits left after
573 // truncation must be 1's and we're rounding up an order of magnitude:
574 eval_right_shift(arg, 1u);
580 // We get here when bits_to_keep is zero but we're rounding up,
581 // as a result we end up with a single digit that is a 1:
585 if(bits_to_keep != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count)
587 // Normalize result when we're rounding to fewer bits than we can hold, only happens in conversions
588 // to narrower types:
589 eval_left_shift(arg, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - bits_to_keep);
590 res.exponent() -= static_cast<Exponent>(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - bits_to_keep);
598 if(!bits_to_keep && !res.bits().limbs()[0])
600 // We're keeping zero bits and did not round up, so result is zero:
601 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
604 // Result must be normalized:
605 BOOST_ASSERT(((int)eval_msb(res.bits()) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1));
607 if(res.exponent() > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
610 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
611 res.bits() = static_cast<limb_type>(0u);
613 else if(res.exponent() < cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent)
616 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
617 res.bits() = static_cast<limb_type>(0u);
621 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
622 inline void do_eval_add(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &b)
624 if(a.exponent() < b.exponent())
627 do_eval_add(res, b, a);
633 using default_ops::eval_add;
634 using default_ops::eval_bit_test;
636 typedef typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type exponent_type;
638 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type dt;
640 // Special cases first:
643 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
650 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
651 if(b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan)
655 return; // result is still infinite.
656 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
658 return; // result is still a NaN.
662 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
665 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
669 return; // result is infinite.
670 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
672 return; // result is a NaN.
675 BOOST_STATIC_ASSERT(boost::integer_traits<exponent_type>::const_max - cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent);
679 if(a.exponent() > (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + b.exponent())
681 res.exponent() = a.exponent();
685 exponent_type e_diff = a.exponent() - b.exponent();
686 BOOST_ASSERT(e_diff >= 0);
687 eval_left_shift(dt, e_diff);
688 res.exponent() = a.exponent() - e_diff;
689 eval_add(dt, b.bits());
692 copy_and_round(res, dt);
693 res.check_invariants();
698 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
699 inline void do_eval_subtract(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &b)
701 using default_ops::eval_subtract;
702 using default_ops::eval_bit_test;
703 using default_ops::eval_decrement;
705 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type dt;
707 // Special cases first:
710 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
711 if(b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan)
712 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
717 if(res.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero)
719 else if(res.sign() == s)
723 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
724 if((b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan) || (b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity))
725 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
729 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
731 return; // result is still a NaN.
735 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
738 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
739 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
740 res.sign() = !a.sign();
741 res.bits() = static_cast<limb_type>(0u);
742 return; // result is a NaN.
743 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
745 return; // result is still a NaN.
749 if((a.exponent() > b.exponent()) || ((a.exponent() == b.exponent()) && a.bits().compare(b.bits()) >= 0))
752 if(a.exponent() <= (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + b.exponent())
754 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type e_diff = a.exponent() - b.exponent();
755 eval_left_shift(dt, e_diff);
756 res.exponent() = a.exponent() - e_diff;
757 eval_subtract(dt, b.bits());
759 else if(a.exponent() == (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + b.exponent() + 1)
761 if(eval_lsb(b.bits()) != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1)
763 eval_left_shift(dt, 1);
765 res.exponent() = a.exponent() - 1;
768 res.exponent() = a.exponent();
771 res.exponent() = a.exponent();
776 if(b.exponent() <= (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + a.exponent())
778 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type e_diff = a.exponent() - b.exponent();
779 eval_left_shift(dt, -e_diff);
780 res.exponent() = b.exponent() + e_diff;
781 eval_subtract(dt, a.bits());
783 else if(b.exponent() == (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + a.exponent() + 1)
785 if(eval_lsb(a.bits()) != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1)
787 eval_left_shift(dt, 1);
789 res.exponent() = b.exponent() - 1;
792 res.exponent() = b.exponent();
795 res.exponent() = b.exponent();
799 copy_and_round(res, dt);
800 if(res.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero)
802 else if(res.sign() != s)
804 res.check_invariants();
807 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
808 inline void eval_add(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &b)
810 if(a.sign() == b.sign())
811 do_eval_add(res, a, b);
813 do_eval_subtract(res, a, b);
816 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
817 inline void eval_add(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a)
819 return eval_add(res, res, a);
822 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
823 inline void eval_subtract(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &b)
825 if(a.sign() != b.sign())
826 do_eval_add(res, a, b);
828 do_eval_subtract(res, a, b);
831 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
832 inline void eval_subtract(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a)
834 return eval_subtract(res, res, a);
837 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
838 inline void eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &b)
840 using default_ops::eval_bit_test;
841 using default_ops::eval_multiply;
843 // Special cases first:
846 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
848 if(b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan)
850 else if(b.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity)
851 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
854 bool s = a.sign() != b.sign();
860 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
863 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
864 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
866 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
870 bool s = a.sign() != b.sign();
876 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
880 if(b.exponent() > cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent)
882 bool s = a.sign() != b.sign();
887 if((a.exponent() > 0) && (b.exponent() > 0))
889 if(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent + 2 - a.exponent() < b.exponent())
891 // We will certainly overflow:
892 bool s = a.sign() != b.sign();
893 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
895 res.bits() = static_cast<limb_type>(0u);
899 if((a.exponent() < 0) && (b.exponent() < 0))
901 if(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent - 2 - a.exponent() > b.exponent())
903 // We will certainly underflow:
904 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
905 res.sign() = a.sign() != b.sign();
906 res.bits() = static_cast<limb_type>(0u);
911 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type dt;
912 eval_multiply(dt, a.bits(), b.bits());
913 res.exponent() = a.exponent() + b.exponent() - cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count + 1;
914 copy_and_round(res, dt);
915 res.check_invariants();
916 res.sign() = a.sign() != b.sign();
919 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
920 inline void eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a)
922 eval_multiply(res, res, a);
925 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class U>
926 inline typename enable_if_c<is_unsigned<U>::value>::type eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a, const U &b)
928 using default_ops::eval_bit_test;
929 using default_ops::eval_multiply;
931 // Special cases first:
934 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
941 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
943 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
947 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
952 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type dt;
953 typedef typename boost::multiprecision::detail::canonical<U, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type>::type canon_ui_type;
954 eval_multiply(dt, a.bits(), static_cast<canon_ui_type>(b));
955 res.exponent() = a.exponent();
956 copy_and_round(res, dt);
957 res.check_invariants();
958 res.sign() = a.sign();
961 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class U>
962 inline typename enable_if_c<is_unsigned<U>::value>::type eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const U &b)
964 eval_multiply(res, res, b);
967 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class S>
968 inline typename enable_if_c<is_signed<S>::value>::type eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a, const S &b)
970 typedef typename make_unsigned<S>::type ui_type;
971 eval_multiply(res, a, static_cast<ui_type>(boost::multiprecision::detail::unsigned_abs(b)));
976 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class S>
977 inline typename enable_if_c<is_signed<S>::value>::type eval_multiply(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const S &b)
979 eval_multiply(res, res, b);
982 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
983 inline void eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &u, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &v)
986 #pragma warning(push)
987 #pragma warning(disable:6326) // comparison of two constants
989 using default_ops::eval_subtract;
990 using default_ops::eval_qr;
991 using default_ops::eval_bit_test;
992 using default_ops::eval_get_sign;
993 using default_ops::eval_increment;
996 // Special cases first:
1000 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1002 switch(v.exponent())
1004 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1005 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1006 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
1009 bool s = u.sign() != v.sign();
1014 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1016 switch(v.exponent())
1018 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1019 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1020 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
1023 bool s = u.sign() != v.sign();
1028 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1029 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
1032 switch(v.exponent())
1034 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1036 bool s = u.sign() != v.sign();
1037 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
1041 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1042 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
1043 res.bits() = limb_type(0);
1044 res.sign() = u.sign() != v.sign();
1046 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1047 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
1051 // We can scale u and v so that both are integers, then perform integer
1052 // division to obtain quotient q and remainder r, such that:
1060 // From this, assuming q has cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count
1061 // bits we only need to determine whether
1062 // r/v is less than, equal to, or greater than 0.5 to determine rounding -
1063 // this we can do with a shift and comparison.
1065 // We can set the exponent and sign of the result up front:
1067 if((v.exponent() < 0) && (u.exponent() > 0))
1069 // Check for overflow:
1070 if(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent + v.exponent() < u.exponent() - 1)
1072 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
1073 res.sign() = u.sign() != v.sign();
1074 res.bits() = static_cast<limb_type>(0u);
1078 else if((v.exponent() > 0) && (u.exponent() < 0))
1080 // Check for underflow:
1081 if(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent + v.exponent() > u.exponent())
1083 // We will certainly underflow:
1084 res.exponent() = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
1085 res.sign() = u.sign() != v.sign();
1086 res.bits() = static_cast<limb_type>(0u);
1090 res.exponent() = u.exponent() - v.exponent() - 1;
1091 res.sign() = u.sign() != v.sign();
1093 // Now get the quotient and remainder:
1095 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type t(u.bits()), t2(v.bits()), q, r;
1096 eval_left_shift(t, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count);
1097 eval_qr(t, t2, q, r);
1099 // We now have either "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count"
1100 // or "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count+1" significant
1103 static const unsigned limb_bits = sizeof(limb_type) * CHAR_BIT;
1104 if(eval_bit_test(q, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count))
1107 // OK we have cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count+1 bits,
1108 // so we already have rounding info,
1109 // we just need to changes things if the last bit is 1 and either the
1110 // remainder is non-zero (ie we do not have a tie) or the quotient would
1111 // be odd if it were shifted to the correct number of bits (ie a tiebreak).
1113 BOOST_ASSERT((eval_msb(q) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count));
1114 if((q.limbs()[0] & 1u) && (eval_get_sign(r) || (q.limbs()[0] & 2u)))
1122 // We have exactly "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count" bits in q.
1123 // Get rounding info, which we can get by comparing 2r with v.
1124 // We want to call copy_and_round to handle rounding and general cleanup,
1125 // so we'll left shift q and add some fake digits on the end to represent
1126 // how we'll be rounding.
1128 BOOST_ASSERT((eval_msb(q) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1));
1129 static const unsigned lshift = (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count < limb_bits) ? 2 : limb_bits;
1130 eval_left_shift(q, lshift);
1131 res.exponent() -= lshift;
1132 eval_left_shift(r, 1u);
1133 int c = r.compare(v.bits());
1135 q.limbs()[0] |= static_cast<limb_type>(1u) << (lshift - 1);
1137 q.limbs()[0] |= (static_cast<limb_type>(1u) << (lshift - 1)) + static_cast<limb_type>(1u);
1139 copy_and_round(res, q);
1141 #pragma warning(pop)
1145 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1146 inline void eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1148 eval_divide(res, res, arg);
1151 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class U>
1152 inline typename enable_if_c<is_unsigned<U>::value>::type eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &u, const U &v)
1155 #pragma warning(push)
1156 #pragma warning(disable:6326) // comparison of two constants
1158 using default_ops::eval_subtract;
1159 using default_ops::eval_qr;
1160 using default_ops::eval_bit_test;
1161 using default_ops::eval_get_sign;
1162 using default_ops::eval_increment;
1165 // Special cases first:
1167 switch(u.exponent())
1169 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1173 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
1176 bool s = u.sign() != (v < 0);
1181 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1184 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1185 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
1191 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
1196 // We can scale u and v so that both are integers, then perform integer
1197 // division to obtain quotient q and remainder r, such that:
1205 // From this, assuming q has "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count" cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count, we only need to determine whether
1206 // r/v is less than, equal to, or greater than 0.5 to determine rounding -
1207 // this we can do with a shift and comparison.
1209 // We can set the exponent and sign of the result up front:
1212 res.exponent() = u.exponent() - static_cast<Exponent>(gb) - static_cast<Exponent>(1);
1213 res.sign() = u.sign();
1215 // Now get the quotient and remainder:
1217 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type t(u.bits()), q, r;
1218 eval_left_shift(t, gb + 1);
1219 eval_qr(t, number<typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type>::canonical_value(v), q, r);
1221 // We now have either "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count" or "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count+1" significant cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count in q.
1223 static const unsigned limb_bits = sizeof(limb_type) * CHAR_BIT;
1224 if(eval_bit_test(q, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count))
1227 // OK we have cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count+1 cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count, so we already have rounding info,
1228 // we just need to changes things if the last bit is 1 and the
1229 // remainder is non-zero (ie we do not have a tie).
1231 BOOST_ASSERT((eval_msb(q) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count));
1232 if((q.limbs()[0] & 1u) && eval_get_sign(r))
1240 // We have exactly "cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count" cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count in q.
1241 // Get rounding info, which we can get by comparing 2r with v.
1242 // We want to call copy_and_round to handle rounding and general cleanup,
1243 // so we'll left shift q and add some fake cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count on the end to represent
1244 // how we'll be rounding.
1246 BOOST_ASSERT((eval_msb(q) == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1));
1247 static const unsigned lshift = cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count < limb_bits ? 2 : limb_bits;
1248 eval_left_shift(q, lshift);
1249 res.exponent() -= lshift;
1250 eval_left_shift(r, 1u);
1251 int c = r.compare(number<typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type>::canonical_value(v));
1253 q.limbs()[0] |= static_cast<limb_type>(1u) << (lshift - 1);
1255 q.limbs()[0] |= (static_cast<limb_type>(1u) << (lshift - 1)) + static_cast<limb_type>(1u);
1257 copy_and_round(res, q);
1259 #pragma warning(pop)
1263 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class U>
1264 inline typename enable_if_c<is_unsigned<U>::value>::type eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const U &v)
1266 eval_divide(res, res, v);
1269 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class S>
1270 inline typename enable_if_c<is_signed<S>::value>::type eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &u, const S &v)
1272 typedef typename make_unsigned<S>::type ui_type;
1273 eval_divide(res, u, static_cast<ui_type>(boost::multiprecision::detail::unsigned_abs(v)));
1278 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class S>
1279 inline typename enable_if_c<is_signed<S>::value>::type eval_divide(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const S &v)
1281 eval_divide(res, res, v);
1284 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1285 inline int eval_get_sign(const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1287 return arg.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero ? 0 : arg.sign() ? -1 : 1;
1290 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1291 inline bool eval_is_zero(const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1293 return arg.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero;
1296 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1297 inline bool eval_eq(const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &a, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &b)
1299 if(a.exponent() == b.exponent())
1301 if(a.exponent() == cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero)
1303 return (a.sign() == b.sign())
1304 && (a.bits().compare(b.bits()) == 0)
1305 && (a.exponent() != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan);
1310 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1311 inline void eval_convert_to(boost::long_long_type *res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1313 switch(arg.exponent())
1315 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1318 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1319 BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
1320 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1321 *res = (std::numeric_limits<boost::long_long_type>::max)();
1326 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::rep_type man(arg.bits());
1327 typename mpl::if_c<sizeof(typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof(int), int, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type shift
1328 = (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - arg.exponent();
1329 if(shift > (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1)
1334 if(arg.sign() && (arg.compare((std::numeric_limits<boost::long_long_type>::min)()) <= 0))
1336 *res = (std::numeric_limits<boost::long_long_type>::min)();
1339 else if(!arg.sign() && (arg.compare((std::numeric_limits<boost::long_long_type>::max)()) >= 0))
1341 *res = (std::numeric_limits<boost::long_long_type>::max)();
1344 eval_right_shift(man, shift);
1345 eval_convert_to(res, man);
1352 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1353 inline void eval_convert_to(boost::ulong_long_type *res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1355 switch(arg.exponent())
1357 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1360 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1361 BOOST_THROW_EXCEPTION(std::runtime_error("Could not convert NaN to integer."));
1362 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1363 *res = (std::numeric_limits<boost::ulong_long_type>::max)();
1366 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::rep_type man(arg.bits());
1367 typename mpl::if_c<sizeof(typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof(int), int, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type shift
1368 = (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - arg.exponent();
1369 if(shift > (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1)
1376 // TODO: what if we have fewer cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count than a boost::long_long_type?
1377 *res = (std::numeric_limits<boost::long_long_type>::max)();
1380 eval_right_shift(man, shift);
1381 eval_convert_to(res, man);
1384 template <class Float, unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1385 inline typename boost::enable_if_c<boost::is_float<Float>::value>::type eval_convert_to(Float *res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &original_arg)
1387 typedef cpp_bin_float<std::numeric_limits<Float>::digits, digit_base_2, void, Exponent, MinE, MaxE> conv_type;
1388 typedef typename common_type<typename conv_type::exponent_type, int>::type common_exp_type;
1390 // Special cases first:
1392 switch(original_arg.exponent())
1394 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1396 if(original_arg.sign())
1399 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1400 *res = std::numeric_limits<Float>::quiet_NaN();
1402 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1403 *res = (std::numeric_limits<Float>::infinity)();
1404 if(original_arg.sign())
1409 // Check for super large exponent that must be converted to infinity:
1411 if(original_arg.exponent() > std::numeric_limits<Float>::max_exponent)
1413 *res = std::numeric_limits<Float>::has_infinity ? std::numeric_limits<Float>::infinity() : (std::numeric_limits<Float>::max)();
1414 if(original_arg.sign())
1419 // Figure out how many digits we will have in our result,
1420 // allowing for a possibly denormalized result:
1422 common_exp_type digits_to_round_to = std::numeric_limits<Float>::digits;
1423 if(original_arg.exponent() < std::numeric_limits<Float>::min_exponent - 1)
1425 common_exp_type diff = original_arg.exponent();
1426 diff -= std::numeric_limits<Float>::min_exponent - 1;
1427 digits_to_round_to += diff;
1429 if(digits_to_round_to < 0)
1431 // Result must be zero:
1433 if(original_arg.sign())
1438 // Perform rounding first, then afterwards extract the digits:
1440 cpp_bin_float<std::numeric_limits<Float>::digits, digit_base_2, Allocator, Exponent, MinE, MaxE> arg;
1441 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::rep_type bits(original_arg.bits());
1442 arg.exponent() = original_arg.exponent();
1443 copy_and_round(arg, bits, (int)digits_to_round_to);
1444 common_exp_type e = arg.exponent();
1445 e -= cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1;
1446 static const unsigned limbs_needed = std::numeric_limits<Float>::digits / (sizeof(*arg.bits().limbs()) * CHAR_BIT)
1447 + (std::numeric_limits<Float>::digits % (sizeof(*arg.bits().limbs()) * CHAR_BIT) ? 1 : 0);
1448 unsigned first_limb_needed = arg.bits().size() - limbs_needed;
1450 e += first_limb_needed * sizeof(*arg.bits().limbs()) * CHAR_BIT;
1451 while(first_limb_needed < arg.bits().size())
1453 *res += std::ldexp(static_cast<Float>(arg.bits().limbs()[first_limb_needed]), static_cast<int>(e));
1454 ++first_limb_needed;
1455 e += sizeof(*arg.bits().limbs()) * CHAR_BIT;
1457 if(original_arg.sign())
1461 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1462 inline void eval_frexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg, Exponent *e)
1464 switch(arg.exponent())
1466 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1467 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1468 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1474 *e = arg.exponent() + 1;
1475 res.exponent() = -1;
1478 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class I>
1479 inline void eval_frexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg, I *pe)
1482 eval_frexp(res, arg, &e);
1483 if((e > (std::numeric_limits<I>::max)()) || (e < (std::numeric_limits<I>::min)()))
1485 BOOST_THROW_EXCEPTION(std::runtime_error("Exponent was outside of the range of the argument type to frexp."));
1487 *pe = static_cast<I>(e);
1490 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1491 inline void eval_ldexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg, Exponent e)
1493 switch(arg.exponent())
1495 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1496 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1497 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1501 if((e > 0) && (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent - e < arg.exponent()))
1504 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
1505 res.sign() = arg.sign();
1507 else if((e < 0) && (cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent - e > arg.exponent()))
1515 res.exponent() += e;
1519 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class I>
1520 inline typename enable_if_c<is_unsigned<I>::value>::type eval_ldexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg, I e)
1522 typedef typename make_signed<I>::type si_type;
1523 if(e > static_cast<I>((std::numeric_limits<si_type>::max)()))
1524 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
1526 eval_ldexp(res, arg, static_cast<si_type>(e));
1529 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, class I>
1530 inline typename enable_if_c<is_signed<I>::value>::type eval_ldexp(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg, I e)
1532 if((e > (std::numeric_limits<Exponent>::max)()) || (e < (std::numeric_limits<Exponent>::min)()))
1534 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity().backend();
1539 eval_ldexp(res, arg, static_cast<Exponent>(e));
1546 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1547 inline void eval_abs(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1553 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1554 inline void eval_fabs(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1560 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1561 inline int eval_fpclassify(const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1563 switch(arg.exponent())
1565 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1567 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1569 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1575 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1576 inline void eval_sqrt(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1578 using default_ops::eval_integer_sqrt;
1579 using default_ops::eval_bit_test;
1580 using default_ops::eval_increment;
1581 switch(arg.exponent())
1583 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1586 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1589 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1592 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
1601 res = std::numeric_limits<number<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN().backend();
1606 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::double_rep_type t(arg.bits()), r, s;
1607 eval_left_shift(t, arg.exponent() & 1 ? cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count : cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1);
1608 eval_integer_sqrt(s, r, t);
1610 if(!eval_bit_test(s, cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count))
1612 // We have exactly the right number of cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count in the result, round as required:
1613 if(s.compare(r) < 0)
1618 typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type ae = arg.exponent();
1619 res.exponent() = ae / 2;
1620 if((ae & 1) && (ae < 0))
1622 copy_and_round(res, s);
1625 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1626 inline void eval_floor(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1628 using default_ops::eval_increment;
1629 switch(arg.exponent())
1631 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1634 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1635 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1639 typename mpl::if_c<sizeof(typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof(int), int, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type shift =
1640 (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - arg.exponent() - 1;
1641 if((arg.exponent() > (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent) || (shift <= 0))
1643 // Either arg is already an integer, or a special value:
1647 if(shift >= (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count)
1649 res = static_cast<signed_limb_type>(arg.sign() ? -1 : 0);
1652 bool fractional = (int)eval_lsb(arg.bits()) < shift;
1654 eval_right_shift(res.bits(), shift);
1655 if(fractional && res.sign())
1657 eval_increment(res.bits());
1658 if(eval_msb(res.bits()) != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - shift)
1660 // Must have extended result by one bit in the increment:
1665 eval_left_shift(res.bits(), shift);
1668 template <unsigned Digits, digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1669 inline void eval_ceil(cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &res, const cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> &arg)
1671 using default_ops::eval_increment;
1672 switch(arg.exponent())
1674 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity:
1677 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_zero:
1678 case cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan:
1682 typename mpl::if_c<sizeof(typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type) < sizeof(int), int, typename cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type>::type shift = (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - arg.exponent() - 1;
1683 if((arg.exponent() > (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent) || (shift <= 0))
1685 // Either arg is already an integer, or a special value:
1689 if(shift >= (int)cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count)
1691 bool s = arg.sign(); // takes care of signed zeros
1692 res = static_cast<signed_limb_type>(arg.sign() ? 0 : 1);
1696 bool fractional = (int)eval_lsb(arg.bits()) < shift;
1698 eval_right_shift(res.bits(), shift);
1699 if(fractional && !res.sign())
1701 eval_increment(res.bits());
1702 if(eval_msb(res.bits()) != cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count - 1 - shift)
1704 // Must have extended result by one bit in the increment:
1709 eval_left_shift(res.bits(), shift);
1712 template<unsigned D1, backends::digit_base_type B1, class A1, class E1, E1 M1, E1 M2>
1713 int eval_signbit(const cpp_bin_float<D1, B1, A1, E1, M1, M2>& val)
1718 template<unsigned D1, backends::digit_base_type B1, class A1, class E1, E1 M1, E1 M2>
1719 inline std::size_t hash_value(const cpp_bin_float<D1, B1, A1, E1, M1, M2>& val)
1721 std::size_t result = hash_value(val.bits());
1722 boost::hash_combine(result, val.exponent());
1723 boost::hash_combine(result, val.sign());
1728 } // namespace backends
1730 #ifdef BOOST_NO_SFINAE_EXPR
1734 template<unsigned D1, backends::digit_base_type B1, class A1, class E1, E1 M1, E1 M2, unsigned D2, backends::digit_base_type B2, class A2, class E2, E2 M3, E2 M4>
1735 struct is_explicitly_convertible<backends::cpp_bin_float<D1, B1, A1, E1, M1, M2>, backends::cpp_bin_float<D2, B2, A2, E2, M3, M4> > : public mpl::true_ {};
1736 template<class FloatT, unsigned D2, backends::digit_base_type B2, class A2, class E2, E2 M3, E2 M4>
1737 struct is_explicitly_convertible<FloatT, backends::cpp_bin_float<D2, B2, A2, E2, M3, M4> > : public boost::is_floating_point<FloatT> {};
1742 template<unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Exponent, Exponent MinE, Exponent MaxE, class Allocator, boost::multiprecision::expression_template_option ExpressionTemplates>
1743 inline boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates>
1744 copysign BOOST_PREVENT_MACRO_SUBSTITUTION(
1745 const boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates>& a,
1746 const boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates>& b)
1748 boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> res(a);
1749 res.backend().sign() = b.backend().sign();
1753 using backends::cpp_bin_float;
1754 using backends::digit_base_2;
1755 using backends::digit_base_10;
1757 template<unsigned Digits, backends::digit_base_type DigitBase, class Exponent, Exponent MinE, Exponent MaxE, class Allocator>
1758 struct number_category<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > : public boost::mpl::int_<boost::multiprecision::number_kind_floating_point>{};
1760 template<unsigned Digits, backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE>
1761 struct expression_template_default<cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> >
1763 static const expression_template_option value = is_void<Allocator>::value ? et_off : et_on;
1766 typedef number<backends::cpp_bin_float<50> > cpp_bin_float_50;
1767 typedef number<backends::cpp_bin_float<100> > cpp_bin_float_100;
1769 typedef number<backends::cpp_bin_float<24, backends::digit_base_2, void, boost::int16_t, -126, 127>, et_off> cpp_bin_float_single;
1770 typedef number<backends::cpp_bin_float<53, backends::digit_base_2, void, boost::int16_t, -1022, 1023>, et_off> cpp_bin_float_double;
1771 typedef number<backends::cpp_bin_float<64, backends::digit_base_2, void, boost::int16_t, -16382, 16383>, et_off> cpp_bin_float_double_extended;
1772 typedef number<backends::cpp_bin_float<113, backends::digit_base_2, void, boost::int16_t, -16382, 16383>, et_off> cpp_bin_float_quad;
1774 } // namespace multiprecision
1778 using boost::multiprecision::signbit;
1779 using boost::multiprecision::copysign;
1783 } // namespace boost
1785 #include <boost/multiprecision/cpp_bin_float/io.hpp>
1786 #include <boost/multiprecision/cpp_bin_float/transcendental.hpp>
1791 // numeric_limits [partial] specializations for the types declared in this header:
1793 template<unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1794 class numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >
1796 typedef boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> number_type;
1798 BOOST_STATIC_CONSTEXPR bool is_specialized = true;
1799 static number_type (min)()
1801 initializer.do_nothing();
1802 static std::pair<bool, number_type> value;
1807 value.second.backend().exponent() = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent;
1809 return value.second;
1811 static number_type (max)()
1813 initializer.do_nothing();
1814 static std::pair<bool, number_type> value;
1818 eval_complement(value.second.backend().bits(), value.second.backend().bits());
1819 value.second.backend().exponent() = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent;
1821 return value.second;
1823 BOOST_STATIC_CONSTEXPR number_type lowest()
1827 BOOST_STATIC_CONSTEXPR int digits = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::bit_count;
1828 BOOST_STATIC_CONSTEXPR int digits10 = (digits - 1) * 301 / 1000;
1829 // Is this really correct???
1830 BOOST_STATIC_CONSTEXPR int max_digits10 = (digits * 301 / 1000) + 3;
1831 BOOST_STATIC_CONSTEXPR bool is_signed = true;
1832 BOOST_STATIC_CONSTEXPR bool is_integer = false;
1833 BOOST_STATIC_CONSTEXPR bool is_exact = false;
1834 BOOST_STATIC_CONSTEXPR int radix = 2;
1835 static number_type epsilon()
1837 initializer.do_nothing();
1838 static std::pair<bool, number_type> value;
1843 value.second = ldexp(value.second, 1 - (int)digits);
1845 return value.second;
1847 // What value should this be????
1848 static number_type round_error()
1851 initializer.do_nothing();
1852 static std::pair<bool, number_type> value;
1857 value.second = ldexp(value.second, -1);
1859 return value.second;
1861 BOOST_STATIC_CONSTEXPR typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type min_exponent = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::min_exponent;
1862 BOOST_STATIC_CONSTEXPR typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type min_exponent10 = (min_exponent / 1000) * 301L;
1863 BOOST_STATIC_CONSTEXPR typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type max_exponent = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::max_exponent;
1864 BOOST_STATIC_CONSTEXPR typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type max_exponent10 = (max_exponent / 1000) * 301L;
1865 BOOST_STATIC_CONSTEXPR bool has_infinity = true;
1866 BOOST_STATIC_CONSTEXPR bool has_quiet_NaN = true;
1867 BOOST_STATIC_CONSTEXPR bool has_signaling_NaN = false;
1868 BOOST_STATIC_CONSTEXPR float_denorm_style has_denorm = denorm_absent;
1869 BOOST_STATIC_CONSTEXPR bool has_denorm_loss = false;
1870 static number_type infinity()
1872 initializer.do_nothing();
1873 static std::pair<bool, number_type> value;
1877 value.second.backend().exponent() = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_infinity;
1879 return value.second;
1881 static number_type quiet_NaN()
1883 initializer.do_nothing();
1884 static std::pair<bool, number_type> value;
1888 value.second.backend().exponent() = boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_nan;
1890 return value.second;
1892 BOOST_STATIC_CONSTEXPR number_type signaling_NaN()
1894 return number_type(0);
1896 BOOST_STATIC_CONSTEXPR number_type denorm_min() { return number_type(0); }
1897 BOOST_STATIC_CONSTEXPR bool is_iec559 = false;
1898 BOOST_STATIC_CONSTEXPR bool is_bounded = true;
1899 BOOST_STATIC_CONSTEXPR bool is_modulo = false;
1900 BOOST_STATIC_CONSTEXPR bool traps = true;
1901 BOOST_STATIC_CONSTEXPR bool tinyness_before = false;
1902 BOOST_STATIC_CONSTEXPR float_round_style round_style = round_to_nearest;
1904 struct data_initializer
1908 std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::epsilon();
1909 std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::round_error();
1910 (std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::min)();
1911 (std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::max)();
1912 std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::infinity();
1913 std::numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE> > >::quiet_NaN();
1915 void do_nothing()const{}
1917 static const data_initializer initializer;
1920 template<unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1921 const typename numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::data_initializer numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::initializer;
1923 #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
1925 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1926 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::digits;
1927 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1928 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::digits10;
1929 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1930 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::max_digits10;
1931 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1932 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_signed;
1933 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1934 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_integer;
1935 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1936 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_exact;
1937 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1938 BOOST_CONSTEXPR_OR_CONST int numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::radix;
1939 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1940 BOOST_CONSTEXPR_OR_CONST typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::min_exponent;
1941 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1942 BOOST_CONSTEXPR_OR_CONST typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::min_exponent10;
1943 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1944 BOOST_CONSTEXPR_OR_CONST typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::max_exponent;
1945 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1946 BOOST_CONSTEXPR_OR_CONST typename boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>::exponent_type numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::max_exponent10;
1947 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1948 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_infinity;
1949 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1950 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_quiet_NaN;
1951 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1952 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_signaling_NaN;
1953 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1954 BOOST_CONSTEXPR_OR_CONST float_denorm_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_denorm;
1955 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1956 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::has_denorm_loss;
1957 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1958 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_iec559;
1959 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1960 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_bounded;
1961 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1962 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::is_modulo;
1963 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1964 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::traps;
1965 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1966 BOOST_CONSTEXPR_OR_CONST bool numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::tinyness_before;
1967 template <unsigned Digits, boost::multiprecision::backends::digit_base_type DigitBase, class Allocator, class Exponent, Exponent MinE, Exponent MaxE, boost::multiprecision::expression_template_option ExpressionTemplates>
1968 BOOST_CONSTEXPR_OR_CONST float_round_style numeric_limits<boost::multiprecision::number<boost::multiprecision::cpp_bin_float<Digits, DigitBase, Allocator, Exponent, MinE, MaxE>, ExpressionTemplates> >::round_style;