Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@101: // Copyright Christopher Kormanyos 2002 - 2013. Chris@101: // Copyright 2011 -2013 John Maddock. Distributed under the Boost Chris@101: // Software License, Version 1.0. (See accompanying file Chris@101: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // This work is based on an earlier work: Chris@16: // "Algorithm 910: A Portable C++ Multiple-Precision System for Special-Function Calculations", Chris@16: // in ACM TOMS, {VOL 37, ISSUE 4, (February 2011)} (C) ACM, 2011. http://doi.acm.org/10.1145/1916461.1916469 Chris@16: // Chris@16: // Note that there are no "noexcept" specifications on the functions in this file: there are too many Chris@101: // calls to lexical_cast (and similar) to easily analyse the code for correctness. So until compilers Chris@16: // can detect noexcept misuse at compile time, the only realistic option is to simply not use it here. Chris@16: // Chris@16: Chris@16: #ifndef BOOST_MP_CPP_DEC_FLOAT_BACKEND_HPP Chris@16: #define BOOST_MP_CPP_DEC_FLOAT_BACKEND_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #ifndef BOOST_NO_CXX11_HDR_ARRAY Chris@16: #include Chris@16: #else Chris@16: #include Chris@16: #endif Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // Chris@16: // Headers required for Boost.Math integration: Chris@16: // Chris@16: #include Chris@16: Chris@16: namespace boost{ Chris@16: namespace multiprecision{ Chris@16: namespace backends{ Chris@16: Chris@16: template Chris@16: class cpp_dec_float; Chris@16: Chris@16: } // namespace Chris@16: Chris@16: template Chris@16: struct number_category > : public mpl::int_{}; Chris@16: Chris@16: namespace backends{ Chris@16: Chris@16: template Chris@16: class cpp_dec_float Chris@16: { Chris@16: private: Chris@16: static const boost::int32_t cpp_dec_float_digits10_setting = Digits10; Chris@16: Chris@16: // We need at least 16-bits in the exponent type to do anything sensible: Chris@16: BOOST_STATIC_ASSERT_MSG(boost::is_signed::value, "ExponentType must be a signed built in integer type."); Chris@16: BOOST_STATIC_ASSERT_MSG(sizeof(ExponentType) > 1, "ExponentType is too small."); Chris@16: Chris@16: public: Chris@101: typedef mpl::list signed_types; Chris@101: typedef mpl::list unsigned_types; Chris@101: typedef mpl::list float_types; Chris@101: typedef ExponentType exponent_type; Chris@101: Chris@101: static const boost::int32_t cpp_dec_float_radix = 10L; Chris@16: static const boost::int32_t cpp_dec_float_digits10_limit_lo = 9L; Chris@16: static const boost::int32_t cpp_dec_float_digits10_limit_hi = boost::integer_traits::const_max - 100; Chris@101: 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)); Chris@101: static const ExponentType cpp_dec_float_max_exp10 = (static_cast(1) << (std::numeric_limits::digits - 5)); Chris@101: static const ExponentType cpp_dec_float_min_exp10 = -cpp_dec_float_max_exp10; Chris@101: static const ExponentType cpp_dec_float_max_exp = cpp_dec_float_max_exp10; Chris@101: static const ExponentType cpp_dec_float_min_exp = cpp_dec_float_min_exp10; Chris@16: Chris@16: BOOST_STATIC_ASSERT((cpp_dec_float::cpp_dec_float_max_exp10 == -cpp_dec_float::cpp_dec_float_min_exp10)); Chris@16: Chris@16: private: Chris@16: static const boost::int32_t cpp_dec_float_elem_digits10 = 8L; Chris@101: static const boost::int32_t cpp_dec_float_elem_mask = 100000000L; Chris@16: Chris@16: BOOST_STATIC_ASSERT(0 == cpp_dec_float_max_exp10 % cpp_dec_float_elem_digits10); Chris@16: Chris@16: // There are three guard limbs. Chris@16: // 1) The first limb has 'play' from 1...8 decimal digits. Chris@16: // 2) The last limb also has 'play' from 1...8 decimal digits. Chris@16: // 3) One limb can get lost when justifying after multiply, Chris@101: // as only half of the triangle is multiplied and a carry Chris@101: // from below is missing. Chris@16: static const boost::int32_t cpp_dec_float_elem_number_request = static_cast((cpp_dec_float_digits10 / cpp_dec_float_elem_digits10) + (((cpp_dec_float_digits10 % cpp_dec_float_elem_digits10) != 0) ? 1 : 0)); Chris@16: Chris@16: // The number of elements needed (with a minimum of two) plus three added guard limbs. Chris@16: static const boost::int32_t cpp_dec_float_elem_number = static_cast(((cpp_dec_float_elem_number_request < 2L) ? 2L : cpp_dec_float_elem_number_request) + 3L); Chris@16: Chris@16: public: Chris@16: static const boost::int32_t cpp_dec_float_total_digits10 = static_cast(cpp_dec_float_elem_number * cpp_dec_float_elem_digits10); Chris@16: Chris@16: private: Chris@16: Chris@16: typedef enum enum_fpclass_type Chris@16: { Chris@16: cpp_dec_float_finite, Chris@16: cpp_dec_float_inf, Chris@16: cpp_dec_float_NaN Chris@16: } Chris@16: fpclass_type; Chris@16: Chris@16: #ifndef BOOST_NO_CXX11_HDR_ARRAY Chris@16: typedef typename mpl::if_, Chris@16: std::array, Chris@16: detail::dynamic_array Chris@16: >::type array_type; Chris@16: #else Chris@16: typedef typename mpl::if_, Chris@16: boost::array, Chris@16: detail::dynamic_array Chris@16: >::type array_type; Chris@16: #endif Chris@16: Chris@101: array_type data; Chris@101: ExponentType exp; Chris@101: bool neg; Chris@101: fpclass_type fpclass; Chris@101: boost::int32_t prec_elem; Chris@16: Chris@16: // Chris@16: // Special values constructor: Chris@16: // Chris@101: cpp_dec_float(fpclass_type c) : Chris@16: data(), Chris@101: exp (static_cast(0)), Chris@101: neg (false), Chris@101: fpclass (c), Chris@16: prec_elem(cpp_dec_float_elem_number) { } Chris@16: Chris@16: // Chris@16: // Static data initializer: Chris@16: // Chris@16: struct initializer Chris@16: { Chris@16: initializer() Chris@16: { Chris@101: cpp_dec_float::nan(); Chris@101: cpp_dec_float::inf(); Chris@16: (cpp_dec_float::min)(); Chris@16: (cpp_dec_float::max)(); Chris@101: cpp_dec_float::zero(); Chris@101: cpp_dec_float::one(); Chris@101: cpp_dec_float::two(); Chris@101: cpp_dec_float::half(); Chris@101: cpp_dec_float::double_min(); Chris@101: cpp_dec_float::double_max(); Chris@101: cpp_dec_float::long_double_max(); Chris@101: cpp_dec_float::long_double_min(); Chris@101: cpp_dec_float::long_long_max(); Chris@101: cpp_dec_float::long_long_min(); Chris@101: cpp_dec_float::ulong_long_max(); Chris@101: cpp_dec_float::eps(); Chris@101: cpp_dec_float::pow2(0); Chris@16: } Chris@16: void do_nothing(){} Chris@16: }; Chris@16: Chris@16: static initializer init; Chris@16: Chris@16: public: Chris@16: // Constructors Chris@101: cpp_dec_float() BOOST_NOEXCEPT_IF(noexcept(array_type())) : Chris@16: data(), Chris@101: exp (static_cast(0)), Chris@101: neg (false), Chris@101: fpclass (cpp_dec_float_finite), Chris@16: prec_elem(cpp_dec_float_elem_number) { } Chris@16: Chris@101: cpp_dec_float(const char* s) : Chris@16: data(), Chris@101: exp (static_cast(0)), Chris@101: neg (false), Chris@101: fpclass (cpp_dec_float_finite), Chris@101: prec_elem(cpp_dec_float_elem_number) Chris@16: { Chris@16: *this = s; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float(I i, typename enable_if >::type* = 0) : Chris@16: data(), Chris@101: exp (static_cast(0)), Chris@101: neg (false), Chris@101: fpclass (cpp_dec_float_finite), Chris@101: prec_elem(cpp_dec_float_elem_number) Chris@16: { Chris@16: from_unsigned_long_long(i); Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float(I i, typename enable_if >::type* = 0) : Chris@16: data(), Chris@101: exp (static_cast(0)), Chris@101: neg (false), Chris@101: fpclass (cpp_dec_float_finite), Chris@101: prec_elem(cpp_dec_float_elem_number) Chris@16: { Chris@16: if(i < 0) Chris@16: { Chris@101: from_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(i)); Chris@16: negate(); Chris@16: } Chris@16: else Chris@16: from_unsigned_long_long(i); Chris@16: } Chris@16: Chris@101: cpp_dec_float(const cpp_dec_float& f) BOOST_NOEXCEPT_IF(noexcept(array_type(std::declval()))) : Chris@101: data (f.data), Chris@101: exp (f.exp), Chris@101: neg (f.neg), Chris@101: fpclass (f.fpclass), Chris@16: prec_elem(f.prec_elem) { } Chris@16: Chris@16: template Chris@101: cpp_dec_float(const cpp_dec_float& f, typename enable_if_c::type* = 0) : Chris@16: data(), Chris@101: exp (f.exp), Chris@101: neg (f.neg), Chris@101: fpclass (static_cast(static_cast(f.fpclass))), Chris@16: prec_elem(cpp_dec_float_elem_number) Chris@16: { Chris@16: std::copy(f.data.begin(), f.data.begin() + f.prec_elem, data.begin()); Chris@16: } Chris@16: template Chris@101: explicit cpp_dec_float(const cpp_dec_float& f, typename disable_if_c::type* = 0) : Chris@16: data(), Chris@101: exp (f.exp), Chris@101: neg (f.neg), Chris@101: fpclass (static_cast(static_cast(f.fpclass))), Chris@16: prec_elem(cpp_dec_float_elem_number) Chris@16: { Chris@16: // TODO: this doesn't round! Chris@16: std::copy(f.data.begin(), f.data.begin() + prec_elem, data.begin()); Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float(const F val, typename enable_if >::type* = 0) : Chris@16: data(), Chris@101: exp (static_cast(0)), Chris@101: neg (false), Chris@101: fpclass (cpp_dec_float_finite), Chris@101: prec_elem(cpp_dec_float_elem_number) Chris@16: { Chris@16: *this = val; Chris@16: } Chris@16: Chris@101: cpp_dec_float(const double mantissa, const ExponentType exponent); Chris@16: Chris@16: // Specific special values. Chris@101: static const cpp_dec_float& nan() Chris@16: { Chris@16: static const cpp_dec_float val(cpp_dec_float_NaN); Chris@16: init.do_nothing(); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& inf() Chris@16: { Chris@16: static const cpp_dec_float val(cpp_dec_float_inf); Chris@16: init.do_nothing(); Chris@16: return val; Chris@16: } Chris@101: Chris@16: static const cpp_dec_float& (max)() Chris@16: { Chris@16: init.do_nothing(); Chris@101: static cpp_dec_float val_max = std::string("1.0e" + boost::lexical_cast(cpp_dec_float_max_exp10)).c_str(); Chris@16: return val_max; Chris@16: } Chris@16: Chris@16: static const cpp_dec_float& (min)() Chris@16: { Chris@16: init.do_nothing(); Chris@101: static cpp_dec_float val_min = std::string("1.0e" + boost::lexical_cast(cpp_dec_float_min_exp10)).c_str(); Chris@16: return val_min; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& zero() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val(static_cast(0u)); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& one() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val(static_cast(1u)); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& two() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val(static_cast(2u)); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& half() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val(0.5L); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& double_min() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val(static_cast((std::numeric_limits::min)())); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& double_max() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val(static_cast((std::numeric_limits::max)())); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& long_double_min() Chris@16: { Chris@16: init.do_nothing(); Chris@101: #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS Chris@101: static cpp_dec_float val(static_cast((std::numeric_limits::min)())); Chris@101: #else Chris@16: static cpp_dec_float val((std::numeric_limits::min)()); Chris@101: #endif Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& long_double_max() Chris@16: { Chris@16: init.do_nothing(); Chris@101: #ifdef BOOST_MATH_NO_LONG_DOUBLE_MATH_FUNCTIONS Chris@101: static cpp_dec_float val(static_cast((std::numeric_limits::max)())); Chris@101: #else Chris@16: static cpp_dec_float val((std::numeric_limits::max)()); Chris@101: #endif Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& long_long_max() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val((std::numeric_limits::max)()); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& long_long_min() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val((std::numeric_limits::min)()); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& ulong_long_max() Chris@16: { Chris@16: init.do_nothing(); Chris@16: static cpp_dec_float val((std::numeric_limits::max)()); Chris@16: return val; Chris@16: } Chris@101: Chris@101: static const cpp_dec_float& eps() Chris@16: { Chris@16: init.do_nothing(); Chris@101: static cpp_dec_float val(1.0, 1 - static_cast(cpp_dec_float_digits10)); Chris@16: return val; Chris@16: } Chris@16: Chris@16: // Basic operations. Chris@101: cpp_dec_float& operator=(const cpp_dec_float& v) BOOST_NOEXCEPT_IF(noexcept(std::declval() = std::declval())) Chris@16: { Chris@16: data = v.data; Chris@16: exp = v.exp; Chris@16: neg = v.neg; Chris@16: fpclass = v.fpclass; Chris@16: prec_elem = v.prec_elem; Chris@16: return *this; Chris@16: } Chris@101: Chris@16: template Chris@101: cpp_dec_float& operator=(const cpp_dec_float& f) Chris@101: { Chris@16: exp = f.exp; Chris@16: neg = f.neg; Chris@16: fpclass = static_cast(static_cast(f.fpclass)); Chris@16: unsigned elems = (std::min)(f.prec_elem, cpp_dec_float_elem_number); Chris@16: std::copy(f.data.begin(), f.data.begin() + elems, data.begin()); Chris@16: std::fill(data.begin() + elems, data.end(), 0); Chris@16: prec_elem = cpp_dec_float_elem_number; Chris@16: return *this; Chris@16: } Chris@101: Chris@101: cpp_dec_float& operator=(long long v) Chris@16: { Chris@16: if(v < 0) Chris@16: { Chris@16: from_unsigned_long_long(-v); Chris@16: negate(); Chris@16: } Chris@16: else Chris@16: from_unsigned_long_long(v); Chris@16: return *this; Chris@16: } Chris@101: Chris@101: cpp_dec_float& operator=(unsigned long long v) Chris@16: { Chris@16: from_unsigned_long_long(v); Chris@16: return *this; Chris@16: } Chris@101: Chris@101: cpp_dec_float& operator=(long double v); Chris@101: Chris@101: cpp_dec_float& operator=(const char* v) Chris@16: { Chris@16: rd_string(v); Chris@16: return *this; Chris@16: } Chris@16: Chris@101: cpp_dec_float& operator+=(const cpp_dec_float& v); Chris@101: cpp_dec_float& operator-=(const cpp_dec_float& v); Chris@101: cpp_dec_float& operator*=(const cpp_dec_float& v); Chris@101: cpp_dec_float& operator/=(const cpp_dec_float& v); Chris@101: Chris@101: cpp_dec_float& add_unsigned_long_long(const unsigned long long n) Chris@16: { Chris@16: cpp_dec_float t; Chris@16: t.from_unsigned_long_long(n); Chris@16: return *this += t; Chris@16: } Chris@101: Chris@101: cpp_dec_float& sub_unsigned_long_long(const unsigned long long n) Chris@16: { Chris@16: cpp_dec_float t; Chris@16: t.from_unsigned_long_long(n); Chris@16: return *this -= t; Chris@16: } Chris@101: Chris@16: cpp_dec_float& mul_unsigned_long_long(const unsigned long long n); Chris@16: cpp_dec_float& div_unsigned_long_long(const unsigned long long n); Chris@16: Chris@16: // Elementary primitives. Chris@101: cpp_dec_float& calculate_inv (); Chris@101: cpp_dec_float& calculate_sqrt(); Chris@101: Chris@101: void negate() Chris@101: { Chris@16: if(!iszero()) Chris@16: neg = !neg; Chris@16: } Chris@16: Chris@16: // Comparison functions Chris@101: bool isnan BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_NaN); } Chris@101: bool isinf BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_inf); } Chris@101: bool isfinite BOOST_PREVENT_MACRO_SUBSTITUTION() const { return (fpclass == cpp_dec_float_finite); } Chris@101: Chris@101: bool iszero () const Chris@16: { Chris@16: return ((fpclass == cpp_dec_float_finite) && (data[0u] == 0u)); Chris@16: } Chris@101: Chris@101: bool isone () const; Chris@101: bool isint () const; Chris@101: bool isneg () const { return neg; } Chris@16: Chris@16: // Operators pre-increment and pre-decrement Chris@101: cpp_dec_float& operator++() Chris@16: { Chris@16: return *this += one(); Chris@16: } Chris@101: Chris@101: cpp_dec_float& operator--() Chris@16: { Chris@16: return *this -= one(); Chris@16: } Chris@16: Chris@16: std::string str(boost::intmax_t digits, std::ios_base::fmtflags f)const; Chris@16: Chris@101: int compare(const cpp_dec_float& v)const; Chris@101: Chris@16: template Chris@101: int compare(const V& v)const Chris@16: { Chris@16: cpp_dec_float t; Chris@16: t = v; Chris@16: return compare(t); Chris@16: } Chris@16: Chris@101: void swap(cpp_dec_float& v) Chris@16: { Chris@16: data.swap(v.data); Chris@16: std::swap(exp, v.exp); Chris@16: std::swap(neg, v.neg); Chris@16: std::swap(fpclass, v.fpclass); Chris@16: std::swap(prec_elem, v.prec_elem); Chris@16: } Chris@16: Chris@101: double extract_double() const; Chris@101: long double extract_long_double() const; Chris@101: signed long long extract_signed_long_long() const; Chris@101: unsigned long long extract_unsigned_long_long() const; Chris@101: void extract_parts(double& mantissa, ExponentType& exponent) const; Chris@101: cpp_dec_float extract_integer_part() const; Chris@101: Chris@101: void precision(const boost::int32_t prec_digits) Chris@16: { Chris@16: if(prec_digits >= cpp_dec_float_total_digits10) Chris@16: { Chris@16: prec_elem = cpp_dec_float_elem_number; Chris@16: } Chris@16: else Chris@16: { Chris@101: const boost::int32_t elems = static_cast( static_cast( (prec_digits + (cpp_dec_float_elem_digits10 / 2)) / cpp_dec_float_elem_digits10) Chris@101: + static_cast(((prec_digits % cpp_dec_float_elem_digits10) != 0) ? 1 : 0)); Chris@16: Chris@16: prec_elem = (std::min)(cpp_dec_float_elem_number, (std::max)(elems, static_cast(2))); Chris@16: } Chris@16: } Chris@16: static cpp_dec_float pow2(long long i); Chris@101: ExponentType order()const Chris@16: { Chris@16: const bool bo_order_is_zero = ((!(isfinite)()) || (data[0] == static_cast(0u))); Chris@16: // Chris@16: // Binary search to find the order of the leading term: Chris@16: // Chris@16: ExponentType prefix = 0; Chris@16: Chris@16: if(data[0] >= 100000UL) Chris@16: { Chris@16: if(data[0] >= 10000000UL) Chris@16: { Chris@16: if(data[0] >= 100000000UL) Chris@16: { Chris@16: if(data[0] >= 1000000000UL) Chris@16: prefix = 9; Chris@16: else Chris@16: prefix = 8; Chris@16: } Chris@16: else Chris@16: prefix = 7; Chris@16: } Chris@16: else Chris@16: { Chris@16: if(data[0] >= 1000000UL) Chris@16: prefix = 6; Chris@16: else Chris@16: prefix = 5; Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: if(data[0] >= 1000UL) Chris@16: { Chris@16: if(data[0] >= 10000UL) Chris@16: prefix = 4; Chris@16: else Chris@16: prefix = 3; Chris@16: } Chris@16: else Chris@16: { Chris@16: if(data[0] >= 100) Chris@16: prefix = 2; Chris@16: else if(data[0] >= 10) Chris@16: prefix = 1; Chris@16: } Chris@16: } Chris@16: Chris@16: return (bo_order_is_zero ? static_cast(0) : static_cast(exp + prefix)); Chris@16: } Chris@16: Chris@16: template Chris@16: void serialize(Archive & ar, const unsigned int /*version*/) Chris@16: { Chris@16: for(unsigned i = 0; i < data.size(); ++i) Chris@16: ar & data[i]; Chris@16: ar & exp; Chris@16: ar & neg; Chris@16: ar & fpclass; Chris@16: ar & prec_elem; Chris@16: } Chris@16: Chris@16: private: Chris@101: static bool data_elem_is_non_zero_predicate(const boost::uint32_t& d) { return (d != static_cast(0u)); } Chris@101: static bool data_elem_is_non_nine_predicate(const boost::uint32_t& d) { return (d != static_cast(cpp_dec_float::cpp_dec_float_elem_mask - 1)); } Chris@101: static bool char_is_nonzero_predicate(const char& c) { return (c != static_cast('0')); } Chris@101: Chris@101: void from_unsigned_long_long(const unsigned long long u); Chris@101: Chris@101: int cmp_data(const array_type& vd) const; Chris@101: Chris@101: Chris@101: static boost::uint32_t mul_loop_uv(boost::uint32_t* const u, const boost::uint32_t* const v, const boost::int32_t p); Chris@101: static boost::uint32_t mul_loop_n (boost::uint32_t* const u, boost::uint32_t n, const boost::int32_t p); Chris@101: static boost::uint32_t div_loop_n (boost::uint32_t* const u, boost::uint32_t n, const boost::int32_t p); Chris@16: Chris@16: bool rd_string(const char* const s); Chris@16: Chris@16: template Chris@16: friend class cpp_dec_float; Chris@16: }; Chris@16: Chris@16: template Chris@16: typename cpp_dec_float::initializer cpp_dec_float::init; Chris@16: Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_radix; Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_digits10_setting; Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_digits10_limit_lo; Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_digits10_limit_hi; Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_digits10; Chris@16: template Chris@16: const ExponentType cpp_dec_float::cpp_dec_float_max_exp; Chris@16: template Chris@16: const ExponentType cpp_dec_float::cpp_dec_float_min_exp; Chris@16: template Chris@16: const ExponentType cpp_dec_float::cpp_dec_float_max_exp10; Chris@16: template Chris@16: const ExponentType cpp_dec_float::cpp_dec_float_min_exp10; Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_elem_digits10; Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_elem_number_request; Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_elem_number; Chris@16: template Chris@16: const boost::int32_t cpp_dec_float::cpp_dec_float_elem_mask; Chris@16: Chris@16: template Chris@16: cpp_dec_float& cpp_dec_float::operator+=(const cpp_dec_float& v) Chris@16: { Chris@16: if((isnan)()) Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if((isinf)()) Chris@16: { Chris@16: if((v.isinf)() && (isneg() != v.isneg())) Chris@16: { Chris@16: *this = nan(); Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if(iszero()) Chris@16: { Chris@16: return operator=(v); Chris@16: } Chris@16: Chris@16: // Get the offset for the add/sub operation. Chris@16: static const ExponentType max_delta_exp = static_cast((cpp_dec_float_elem_number - 1) * cpp_dec_float_elem_digits10); Chris@16: Chris@16: const ExponentType ofs_exp = static_cast(exp - v.exp); Chris@16: Chris@16: // Check if the operation is out of range, requiring special handling. Chris@16: if(v.iszero() || (ofs_exp > max_delta_exp)) Chris@16: { Chris@16: // Result is *this unchanged since v is negligible compared to *this. Chris@16: return *this; Chris@16: } Chris@16: else if(ofs_exp < -max_delta_exp) Chris@16: { Chris@16: // Result is *this = v since *this is negligible compared to v. Chris@16: return operator=(v); Chris@16: } Chris@16: Chris@16: // Do the add/sub operation. Chris@16: Chris@101: typename array_type::iterator p_u = data.begin(); Chris@101: typename array_type::const_iterator p_v = v.data.begin(); Chris@101: bool b_copy = false; Chris@101: const boost::int32_t ofs = static_cast(static_cast(ofs_exp) / cpp_dec_float_elem_digits10); Chris@101: array_type n_data; Chris@16: Chris@16: if(neg == v.neg) Chris@16: { Chris@16: // Add v to *this, where the data array of either *this or v Chris@16: // might have to be treated with a positive, negative or zero offset. Chris@16: // The result is stored in *this. The data are added one element Chris@16: // at a time, each element with carry. Chris@16: if(ofs >= static_cast(0)) Chris@16: { Chris@16: std::copy(v.data.begin(), v.data.end() - static_cast(ofs), n_data.begin() + static_cast(ofs)); Chris@16: std::fill(n_data.begin(), n_data.begin() + static_cast(ofs), static_cast(0u)); Chris@16: p_v = n_data.begin(); Chris@16: } Chris@16: else Chris@16: { Chris@16: std::copy(data.begin(), data.end() - static_cast(-ofs), n_data.begin() + static_cast(-ofs)); Chris@16: std::fill(n_data.begin(), n_data.begin() + static_cast(-ofs), static_cast(0u)); Chris@16: p_u = n_data.begin(); Chris@16: b_copy = true; Chris@16: } Chris@16: Chris@16: // Addition algorithm Chris@16: boost::uint32_t carry = static_cast(0u); Chris@16: Chris@16: for(boost::int32_t j = static_cast(cpp_dec_float_elem_number - static_cast(1)); j >= static_cast(0); j--) Chris@16: { Chris@16: boost::uint32_t t = static_cast(static_cast(p_u[j] + p_v[j]) + carry); Chris@101: carry = t / static_cast(cpp_dec_float_elem_mask); Chris@101: p_u[j] = static_cast(t - static_cast(carry * static_cast(cpp_dec_float_elem_mask))); Chris@16: } Chris@16: Chris@16: if(b_copy) Chris@16: { Chris@16: data = n_data; Chris@101: exp = v.exp; Chris@16: } Chris@16: Chris@16: // There needs to be a carry into the element -1 of the array data Chris@16: if(carry != static_cast(0u)) Chris@16: { Chris@16: std::copy_backward(data.begin(), data.end() - static_cast(1u), data.end()); Chris@16: data[0] = carry; Chris@16: exp += static_cast(cpp_dec_float_elem_digits10); Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: // Subtract v from *this, where the data array of either *this or v Chris@16: // might have to be treated with a positive, negative or zero offset. Chris@16: if((ofs > static_cast(0)) Chris@101: || ( (ofs == static_cast(0)) Chris@16: && (cmp_data(v.data) > static_cast(0))) Chris@16: ) Chris@16: { Chris@16: // In this case, |u| > |v| and ofs is positive. Chris@16: // Copy the data of v, shifted down to a lower value Chris@16: // into the data array m_n. Set the operand pointer p_v Chris@16: // to point to the copied, shifted data m_n. Chris@16: std::copy(v.data.begin(), v.data.end() - static_cast(ofs), n_data.begin() + static_cast(ofs)); Chris@16: std::fill(n_data.begin(), n_data.begin() + static_cast(ofs), static_cast(0u)); Chris@16: p_v = n_data.begin(); Chris@16: } Chris@16: else Chris@16: { Chris@16: if(ofs != static_cast(0)) Chris@16: { Chris@16: // In this case, |u| < |v| and ofs is negative. Chris@16: // Shift the data of u down to a lower value. Chris@16: std::copy_backward(data.begin(), data.end() - static_cast(-ofs), data.end()); Chris@16: std::fill(data.begin(), data.begin() + static_cast(-ofs), static_cast(0u)); Chris@16: } Chris@16: Chris@16: // Copy the data of v into the data array n_data. Chris@16: // Set the u-pointer p_u to point to m_n and the Chris@16: // operand pointer p_v to point to the shifted Chris@16: // data m_data. Chris@16: n_data = v.data; Chris@101: p_u = n_data.begin(); Chris@101: p_v = data.begin(); Chris@16: b_copy = true; Chris@16: } Chris@16: Chris@16: boost::int32_t j; Chris@16: Chris@16: // Subtraction algorithm Chris@16: boost::int32_t borrow = static_cast(0); Chris@16: Chris@16: for(j = static_cast(cpp_dec_float_elem_number - static_cast(1)); j >= static_cast(0); j--) Chris@16: { Chris@101: boost::int32_t t = static_cast(static_cast( static_cast(p_u[j]) Chris@16: - static_cast(p_v[j])) - borrow); Chris@16: Chris@16: // Underflow? Borrow? Chris@16: if(t < static_cast(0)) Chris@16: { Chris@16: // Yes, underflow and borrow Chris@101: t += static_cast(cpp_dec_float_elem_mask); Chris@16: borrow = static_cast(1); Chris@16: } Chris@16: else Chris@16: { Chris@16: borrow = static_cast(0); Chris@16: } Chris@16: Chris@16: p_u[j] = static_cast(static_cast(t) % static_cast(cpp_dec_float_elem_mask)); Chris@16: } Chris@16: Chris@16: if(b_copy) Chris@16: { Chris@16: data = n_data; Chris@101: exp = v.exp; Chris@101: neg = v.neg; Chris@16: } Chris@16: Chris@16: // Is it necessary to justify the data? Chris@16: const typename array_type::const_iterator first_nonzero_elem = std::find_if(data.begin(), data.end(), data_elem_is_non_zero_predicate); Chris@16: Chris@16: if(first_nonzero_elem != data.begin()) Chris@16: { Chris@16: if(first_nonzero_elem == data.end()) Chris@16: { Chris@16: // This result of the subtraction is exactly zero. Chris@16: // Reset the sign and the exponent. Chris@16: neg = false; Chris@16: exp = static_cast(0); Chris@16: } Chris@16: else Chris@16: { Chris@16: // Justify the data Chris@16: const std::size_t sj = static_cast(std::distance(data.begin(), first_nonzero_elem)); Chris@16: Chris@16: std::copy(data.begin() + static_cast(sj), data.end(), data.begin()); Chris@16: std::fill(data.end() - sj, data.end(), static_cast(0u)); Chris@16: Chris@16: exp -= static_cast(sj * static_cast(cpp_dec_float_elem_digits10)); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@101: // Handle underflow. Chris@101: if(iszero()) Chris@101: return (*this = zero()); Chris@101: Chris@101: // Check for potential overflow. Chris@101: const bool b_result_might_overflow = (exp >= static_cast(cpp_dec_float_max_exp10)); Chris@101: Chris@101: // Handle overflow. Chris@101: if(b_result_might_overflow) Chris@16: { Chris@16: const bool b_result_is_neg = neg; Chris@101: neg = false; Chris@101: Chris@101: if(compare((cpp_dec_float::max)()) > 0) Chris@101: *this = inf(); Chris@101: Chris@101: neg = b_result_is_neg; Chris@16: } Chris@16: Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float& cpp_dec_float::operator-=(const cpp_dec_float& v) Chris@16: { Chris@16: // Use *this - v = -(-*this + v). Chris@16: negate(); Chris@16: *this += v; Chris@16: negate(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float& cpp_dec_float::operator*=(const cpp_dec_float& v) Chris@16: { Chris@16: // Evaluate the sign of the result. Chris@16: const bool b_result_is_neg = (neg != v.neg); Chris@16: Chris@16: // Artificially set the sign of the result to be positive. Chris@16: neg = false; Chris@16: Chris@16: // Handle special cases like zero, inf and NaN. Chris@101: const bool b_u_is_inf = (isinf)(); Chris@16: const bool b_v_is_inf = (v.isinf)(); Chris@101: const bool b_u_is_zero = iszero(); Chris@16: const bool b_v_is_zero = v.iszero(); Chris@16: Chris@16: if( ((isnan)() || (v.isnan)()) Chris@16: || (b_u_is_inf && b_v_is_zero) Chris@16: || (b_v_is_inf && b_u_is_zero) Chris@16: ) Chris@16: { Chris@16: *this = nan(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if(b_u_is_inf || b_v_is_inf) Chris@16: { Chris@16: *this = inf(); Chris@16: if(b_result_is_neg) Chris@16: negate(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if(b_u_is_zero || b_v_is_zero) Chris@16: { Chris@16: return *this = zero(); Chris@16: } Chris@16: Chris@101: // Check for potential overflow or underflow. Chris@101: const bool b_result_might_overflow = ((exp + v.exp) >= static_cast(cpp_dec_float_max_exp10)); Chris@101: const bool b_result_might_underflow = ((exp + v.exp) <= static_cast(cpp_dec_float_min_exp10)); Chris@16: Chris@16: // Set the exponent of the result. Chris@16: exp += v.exp; Chris@16: Chris@16: const boost::int32_t prec_mul = (std::min)(prec_elem, v.prec_elem); Chris@16: Chris@16: const boost::uint32_t carry = mul_loop_uv(data.data(), v.data.data(), prec_mul); Chris@16: Chris@16: // Handle a potential carry. Chris@16: if(carry != static_cast(0u)) Chris@16: { Chris@16: exp += cpp_dec_float_elem_digits10; Chris@16: Chris@16: // Shift the result of the multiplication one element to the right... Chris@16: std::copy_backward(data.begin(), Chris@16: data.begin() + static_cast(prec_elem - static_cast(1)), Chris@16: data.begin() + static_cast(prec_elem)); Chris@16: Chris@16: // ... And insert the carry. Chris@16: data.front() = carry; Chris@16: } Chris@16: Chris@101: // Handle overflow. Chris@101: if(b_result_might_overflow && (compare((cpp_dec_float::max)()) > 0)) Chris@101: { Chris@101: *this = inf(); Chris@101: } Chris@101: Chris@101: // Handle underflow. Chris@101: if(b_result_might_underflow && (compare((cpp_dec_float::min)()) < 0)) Chris@101: { Chris@101: *this = zero(); Chris@101: Chris@101: return *this; Chris@101: } Chris@101: Chris@16: // Set the sign of the result. Chris@16: neg = b_result_is_neg; Chris@16: Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float& cpp_dec_float::operator/=(const cpp_dec_float& v) Chris@16: { Chris@101: const bool u_and_v_are_finite_and_identical = ( (isfinite)() Chris@16: && (fpclass == v.fpclass) Chris@101: && (exp == v.exp) Chris@16: && (cmp_data(v.data) == static_cast(0))); Chris@16: Chris@16: if(u_and_v_are_finite_and_identical) Chris@16: { Chris@16: if(neg != v.neg) Chris@16: { Chris@16: *this = one(); Chris@16: negate(); Chris@16: } Chris@16: else Chris@16: *this = one(); Chris@16: return *this; Chris@16: } Chris@16: else Chris@16: { Chris@16: if(iszero()) Chris@16: { Chris@16: if((v.isnan)() || v.iszero()) Chris@16: { Chris@16: return *this = v; Chris@16: } Chris@16: return *this; Chris@16: } Chris@16: cpp_dec_float t(v); Chris@16: t.calculate_inv(); Chris@16: return operator*=(t); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float& cpp_dec_float::mul_unsigned_long_long(const unsigned long long n) Chris@16: { Chris@16: // Multiply *this with a constant unsigned long long. Chris@16: Chris@16: // Evaluate the sign of the result. Chris@16: const bool b_neg = neg; Chris@16: Chris@16: // Artificially set the sign of the result to be positive. Chris@16: neg = false; Chris@16: Chris@16: // Handle special cases like zero, inf and NaN. Chris@101: const bool b_u_is_inf = (isinf)(); Chris@16: const bool b_n_is_zero = (n == static_cast(0)); Chris@16: Chris@16: if((isnan)() || (b_u_is_inf && b_n_is_zero)) Chris@16: { Chris@16: return (*this = nan()); Chris@16: } Chris@16: Chris@16: if(b_u_is_inf) Chris@16: { Chris@16: *this = inf(); Chris@16: if(b_neg) Chris@16: negate(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if(iszero() || b_n_is_zero) Chris@16: { Chris@16: // Multiplication by zero. Chris@16: return *this = zero(); Chris@16: } Chris@16: Chris@16: if(n >= static_cast(cpp_dec_float_elem_mask)) Chris@16: { Chris@16: neg = b_neg; Chris@16: cpp_dec_float t; Chris@16: t = n; Chris@16: return operator*=(t); Chris@16: } Chris@16: Chris@16: if(n == static_cast(1u)) Chris@16: { Chris@16: neg = b_neg; Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Set up the multiplication loop. Chris@16: const boost::uint32_t nn = static_cast(n); Chris@16: const boost::uint32_t carry = mul_loop_n(data.data(), nn, prec_elem); Chris@16: Chris@16: // Handle the carry and adjust the exponent. Chris@16: if(carry != static_cast(0u)) Chris@16: { Chris@16: exp += static_cast(cpp_dec_float_elem_digits10); Chris@16: Chris@16: // Shift the result of the multiplication one element to the right. Chris@16: std::copy_backward(data.begin(), Chris@16: data.begin() + static_cast(prec_elem - static_cast(1)), Chris@16: data.begin() + static_cast(prec_elem)); Chris@16: Chris@16: data.front() = static_cast(carry); Chris@16: } Chris@16: Chris@101: // Check for potential overflow. Chris@101: const bool b_result_might_overflow = (exp >= cpp_dec_float_max_exp10); Chris@101: Chris@101: // Handle overflow. Chris@101: if(b_result_might_overflow && (compare((cpp_dec_float::max)()) > 0)) Chris@16: { Chris@16: *this = inf(); Chris@16: } Chris@16: Chris@16: // Set the sign. Chris@16: neg = b_neg; Chris@16: Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float& cpp_dec_float::div_unsigned_long_long(const unsigned long long n) Chris@16: { Chris@16: // Divide *this by a constant unsigned long long. Chris@16: Chris@16: // Evaluate the sign of the result. Chris@16: const bool b_neg = neg; Chris@16: Chris@16: // Artificially set the sign of the result to be positive. Chris@16: neg = false; Chris@16: Chris@16: // Handle special cases like zero, inf and NaN. Chris@16: if((isnan)()) Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if((isinf)()) Chris@16: { Chris@16: *this = inf(); Chris@16: if(b_neg) Chris@16: negate(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if(n == static_cast(0u)) Chris@16: { Chris@16: // Divide by 0. Chris@16: if(iszero()) Chris@16: { Chris@16: *this = nan(); Chris@16: return *this; Chris@16: } Chris@16: else Chris@16: { Chris@16: *this = inf(); Chris@16: if(isneg()) Chris@16: negate(); Chris@16: return *this; Chris@16: } Chris@16: } Chris@16: Chris@16: if(iszero()) Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if(n >= static_cast(cpp_dec_float_elem_mask)) Chris@16: { Chris@16: neg = b_neg; Chris@16: cpp_dec_float t; Chris@16: t = n; Chris@16: return operator/=(t); Chris@16: } Chris@16: Chris@16: const boost::uint32_t nn = static_cast(n); Chris@16: Chris@16: if(nn > static_cast(1u)) Chris@16: { Chris@16: // Do the division loop. Chris@16: const boost::uint32_t prev = div_loop_n(data.data(), nn, prec_elem); Chris@16: Chris@16: // Determine if one leading zero is in the result data. Chris@16: if(data[0] == static_cast(0u)) Chris@16: { Chris@16: // Adjust the exponent Chris@16: exp -= static_cast(cpp_dec_float_elem_digits10); Chris@16: Chris@16: // Shift result of the division one element to the left. Chris@16: std::copy(data.begin() + static_cast(1u), Chris@16: data.begin() + static_cast(prec_elem - static_cast(1)), Chris@16: data.begin()); Chris@16: Chris@16: data[prec_elem - static_cast(1)] = static_cast(static_cast(prev * static_cast(cpp_dec_float_elem_mask)) / nn); Chris@16: } Chris@16: } Chris@16: Chris@101: // Check for potential underflow. Chris@101: const bool b_result_might_underflow = (exp <= cpp_dec_float_min_exp10); Chris@101: Chris@101: // Handle underflow. Chris@101: if(b_result_might_underflow && (compare((cpp_dec_float::min)()) < 0)) Chris@101: return (*this = zero()); Chris@16: Chris@16: // Set the sign of the result. Chris@16: neg = b_neg; Chris@16: Chris@101: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float& cpp_dec_float::calculate_inv() Chris@16: { Chris@16: // Compute the inverse of *this. Chris@16: const bool b_neg = neg; Chris@16: Chris@16: neg = false; Chris@16: Chris@16: // Handle special cases like zero, inf and NaN. Chris@16: if(iszero()) Chris@16: { Chris@16: *this = inf(); Chris@16: if(b_neg) Chris@16: negate(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if((isnan)()) Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if((isinf)()) Chris@16: { Chris@16: return *this = zero(); Chris@16: } Chris@16: Chris@16: if(isone()) Chris@16: { Chris@16: if(b_neg) Chris@16: negate(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Save the original *this. Chris@16: cpp_dec_float x(*this); Chris@16: Chris@16: // Generate the initial estimate using division. Chris@16: // Extract the mantissa and exponent for a "manual" Chris@16: // computation of the estimate. Chris@16: double dd; Chris@101: ExponentType ne; Chris@16: x.extract_parts(dd, ne); Chris@16: Chris@16: // Do the inverse estimate using double precision estimates of mantissa and exponent. Chris@16: operator=(cpp_dec_float(1.0 / dd, -ne)); Chris@16: Chris@16: // Compute the inverse of *this. Quadratically convergent Newton-Raphson iteration Chris@16: // is used. During the iterative steps, the precision of the calculation is limited Chris@16: // to the minimum required in order to minimize the run-time. Chris@16: Chris@16: static const boost::int32_t double_digits10_minus_a_few = std::numeric_limits::digits10 - 3; Chris@16: Chris@16: for(boost::int32_t digits = double_digits10_minus_a_few; digits <= cpp_dec_float_total_digits10; digits *= static_cast(2)) Chris@16: { Chris@16: // Adjust precision of the terms. Chris@16: precision(static_cast((digits + 10) * static_cast(2))); Chris@16: x.precision(static_cast((digits + 10) * static_cast(2))); Chris@16: Chris@16: // Next iteration. Chris@16: cpp_dec_float t(*this); Chris@16: t *= x; Chris@16: t -= two(); Chris@16: t.negate(); Chris@16: *this *= t; Chris@16: } Chris@16: Chris@16: neg = b_neg; Chris@16: Chris@16: prec_elem = cpp_dec_float_elem_number; Chris@16: Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float& cpp_dec_float::calculate_sqrt() Chris@16: { Chris@16: // Compute the square root of *this. Chris@16: Chris@16: if(isneg() || (!(isfinite)())) Chris@16: { Chris@16: *this = nan(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if(iszero() || isone()) Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: // Save the original *this. Chris@16: cpp_dec_float x(*this); Chris@16: Chris@16: // Generate the initial estimate using division. Chris@16: // Extract the mantissa and exponent for a "manual" Chris@16: // computation of the estimate. Chris@16: double dd; Chris@101: ExponentType ne; Chris@16: extract_parts(dd, ne); Chris@16: Chris@16: // Force the exponent to be an even multiple of two. Chris@16: if((ne % static_cast(2)) != static_cast(0)) Chris@16: { Chris@16: ++ne; Chris@16: dd /= 10.0; Chris@16: } Chris@16: Chris@16: // Setup the iteration. Chris@16: // Estimate the square root using simple manipulations. Chris@16: const double sqd = std::sqrt(dd); Chris@16: Chris@16: *this = cpp_dec_float(sqd, static_cast(ne / static_cast(2))); Chris@16: Chris@16: // Estimate 1.0 / (2.0 * x0) using simple manipulations. Chris@16: cpp_dec_float vi(0.5 / sqd, static_cast(-ne / static_cast(2))); Chris@16: Chris@16: // Compute the square root of x. Coupled Newton iteration Chris@16: // as described in "Pi Unleashed" is used. During the Chris@16: // iterative steps, the precision of the calculation is Chris@16: // limited to the minimum required in order to minimize Chris@16: // the run-time. Chris@16: // Chris@16: // Book references: Chris@16: // http://www.jjj.de/pibook/pibook.html Chris@16: // 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 Chris@16: Chris@16: static const boost::uint32_t double_digits10_minus_a_few = std::numeric_limits::digits10 - 3; Chris@16: Chris@16: for(boost::int32_t digits = double_digits10_minus_a_few; digits <= cpp_dec_float_total_digits10; digits *= 2u) Chris@16: { Chris@16: // Adjust precision of the terms. Chris@16: precision((digits + 10) * 2); Chris@16: vi.precision((digits + 10) * 2); Chris@16: Chris@16: // Next iteration of vi Chris@16: cpp_dec_float t(*this); Chris@16: t *= vi; Chris@16: t.negate(); Chris@16: t.mul_unsigned_long_long(2u); Chris@16: t += one(); Chris@16: t *= vi; Chris@16: vi += t; Chris@16: Chris@16: // Next iteration of *this Chris@16: t = *this; Chris@16: t *= *this; Chris@16: t.negate(); Chris@16: t += x; Chris@16: t *= vi; Chris@16: *this += t; Chris@16: } Chris@16: Chris@16: prec_elem = cpp_dec_float_elem_number; Chris@16: Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: int cpp_dec_float::cmp_data(const array_type& vd) const Chris@16: { Chris@16: // Compare the data of *this with those of v. Chris@101: // Return +1 for *this > v Chris@101: // 0 for *this = v Chris@101: // -1 for *this < v Chris@16: Chris@16: const std::pair mismatch_pair = std::mismatch(data.begin(), data.end(), vd.begin()); Chris@16: Chris@16: const bool is_equal = ((mismatch_pair.first == data.end()) && (mismatch_pair.second == vd.end())); Chris@16: Chris@16: if(is_equal) Chris@16: { Chris@16: return 0; Chris@16: } Chris@16: else Chris@16: { Chris@16: return ((*mismatch_pair.first > *mismatch_pair.second) ? 1 : -1); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@101: int cpp_dec_float::compare(const cpp_dec_float& v) const Chris@16: { Chris@16: // Compare v with *this. Chris@101: // Return +1 for *this > v Chris@101: // 0 for *this = v Chris@101: // -1 for *this < v Chris@16: Chris@16: // Handle all non-finite cases. Chris@16: if((!(isfinite)()) || (!(v.isfinite)())) Chris@16: { Chris@16: // NaN can never equal NaN. Return an implementation-dependent Chris@16: // signed result. Also note that comparison of NaN with NaN Chris@16: // using operators greater-than or less-than is undefined. Chris@16: if((isnan)() || (v.isnan)()) { return ((isnan)() ? 1 : -1); } Chris@16: Chris@16: if((isinf)() && (v.isinf)()) Chris@16: { Chris@16: // Both *this and v are infinite. They are equal if they have the same sign. Chris@16: // Otherwise, *this is less than v if and only if *this is negative. Chris@16: return ((neg == v.neg) ? 0 : (neg ? -1 : 1)); Chris@16: } Chris@16: Chris@16: if((isinf)()) Chris@16: { Chris@16: // *this is infinite, but v is finite. Chris@16: // So negative infinite *this is less than any finite v. Chris@16: // Whereas positive infinite *this is greater than any finite v. Chris@16: return (isneg() ? -1 : 1); Chris@16: } Chris@16: else Chris@16: { Chris@16: // *this is finite, and v is infinite. Chris@16: // So any finite *this is greater than negative infinite v. Chris@16: // Whereas any finite *this is less than positive infinite v. Chris@16: return (v.neg ? 1 : -1); Chris@16: } Chris@16: } Chris@16: Chris@16: // And now handle all *finite* cases. Chris@16: if(iszero()) Chris@16: { Chris@16: // The value of *this is zero and v is either zero or non-zero. Chris@16: return (v.iszero() ? 0 Chris@16: : (v.neg ? 1 : -1)); Chris@16: } Chris@16: else if(v.iszero()) Chris@16: { Chris@16: // The value of v is zero and *this is non-zero. Chris@16: return (neg ? -1 : 1); Chris@16: } Chris@16: else Chris@16: { Chris@16: // Both *this and v are non-zero. Chris@16: Chris@16: if(neg != v.neg) Chris@16: { Chris@16: // The signs are different. Chris@16: return (neg ? -1 : 1); Chris@16: } Chris@16: else if(exp != v.exp) Chris@16: { Chris@16: // The signs are the same and the exponents are different. Chris@16: const int val_cexpression = ((exp < v.exp) ? 1 : -1); Chris@16: Chris@16: return (neg ? val_cexpression : -val_cexpression); Chris@16: } Chris@16: else Chris@16: { Chris@16: // The signs are the same and the exponents are the same. Chris@16: // Compare the data. Chris@16: const int val_cmp_data = cmp_data(v.data); Chris@16: Chris@16: return ((!neg) ? val_cmp_data : -val_cmp_data); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@101: bool cpp_dec_float::isone() const Chris@16: { Chris@16: // Check if the value of *this is identically 1 or very close to 1. Chris@16: Chris@16: const bool not_negative_and_is_finite = ((!neg) && (isfinite)()); Chris@16: Chris@16: if(not_negative_and_is_finite) Chris@16: { Chris@16: if((data[0u] == static_cast(1u)) && (exp == static_cast(0))) Chris@16: { Chris@16: const typename array_type::const_iterator it_non_zero = std::find_if(data.begin(), data.end(), data_elem_is_non_zero_predicate); Chris@16: return (it_non_zero == data.end()); Chris@16: } Chris@16: else if((data[0u] == static_cast(cpp_dec_float_elem_mask - 1)) && (exp == static_cast(-cpp_dec_float_elem_digits10))) Chris@16: { Chris@16: const typename array_type::const_iterator it_non_nine = std::find_if(data.begin(), data.end(), data_elem_is_non_nine_predicate); Chris@16: return (it_non_nine == data.end()); Chris@16: } Chris@16: } Chris@16: Chris@16: return false; Chris@16: } Chris@16: Chris@16: template Chris@101: bool cpp_dec_float::isint() const Chris@16: { Chris@16: if(fpclass != cpp_dec_float_finite) { return false; } Chris@16: Chris@16: if(iszero()) { return true; } Chris@16: Chris@16: if(exp < static_cast(0)) { return false; } // |*this| < 1. Chris@16: Chris@16: const typename array_type::size_type offset_decimal_part = static_cast(exp / cpp_dec_float_elem_digits10) + 1u; Chris@16: Chris@16: if(offset_decimal_part >= static_cast(cpp_dec_float_elem_number)) Chris@16: { Chris@16: // The number is too large to resolve the integer part. Chris@16: // It considered to be a pure integer. Chris@16: return true; Chris@16: } Chris@16: Chris@16: typename array_type::const_iterator it_non_zero = std::find_if(data.begin() + offset_decimal_part, data.end(), data_elem_is_non_zero_predicate); Chris@16: Chris@16: return (it_non_zero == data.end()); Chris@16: } Chris@16: Chris@16: template Chris@101: void cpp_dec_float::extract_parts(double& mantissa, ExponentType& exponent) const Chris@16: { Chris@16: // Extract the approximate parts mantissa and base-10 exponent from the input cpp_dec_float value x. Chris@16: Chris@16: // Extracts the mantissa and exponent. Chris@16: exponent = exp; Chris@16: Chris@101: boost::uint32_t p10 = static_cast(1u); Chris@16: boost::uint32_t test = data[0u]; Chris@16: Chris@16: for(;;) Chris@16: { Chris@16: test /= static_cast(10u); Chris@16: Chris@16: if(test == static_cast(0u)) Chris@16: { Chris@16: break; Chris@16: } Chris@16: Chris@16: p10 *= static_cast(10u); Chris@16: ++exponent; Chris@16: } Chris@16: Chris@16: // Establish the upper bound of limbs for extracting the double. Chris@101: const int max_elem_in_double_count = static_cast(static_cast(std::numeric_limits::digits10) / cpp_dec_float_elem_digits10) Chris@16: + (static_cast(static_cast(std::numeric_limits::digits10) % cpp_dec_float_elem_digits10) != 0 ? 1 : 0) Chris@16: + 1; Chris@16: Chris@16: // And make sure this upper bound stays within bounds of the elems. Chris@16: const std::size_t max_elem_extract_count = static_cast((std::min)(static_cast(max_elem_in_double_count), cpp_dec_float_elem_number)); Chris@16: Chris@16: // Extract into the mantissa the first limb, extracted as a double. Chris@16: mantissa = static_cast(data[0]); Chris@16: double scale = 1.0; Chris@16: Chris@16: // Extract the rest of the mantissa piecewise from the limbs. Chris@16: for(std::size_t i = 1u; i < max_elem_extract_count; i++) Chris@16: { Chris@16: scale /= static_cast(cpp_dec_float_elem_mask); Chris@16: mantissa += (static_cast(data[i]) * scale); Chris@16: } Chris@16: Chris@16: mantissa /= static_cast(p10); Chris@16: Chris@16: if(neg) { mantissa = -mantissa; } Chris@16: } Chris@16: Chris@16: template Chris@101: double cpp_dec_float::extract_double() const Chris@16: { Chris@16: // Returns the double conversion of a cpp_dec_float. Chris@16: Chris@16: // Check for non-normal cpp_dec_float. Chris@16: if(!(isfinite)()) Chris@16: { Chris@16: if((isnan)()) Chris@16: { Chris@16: return std::numeric_limits::quiet_NaN(); Chris@16: } Chris@16: else Chris@16: { Chris@101: return ((!neg) ? std::numeric_limits::infinity() Chris@16: : -std::numeric_limits::infinity()); Chris@16: } Chris@16: } Chris@16: Chris@16: cpp_dec_float xx(*this); Chris@16: if(xx.isneg()) Chris@16: xx.negate(); Chris@16: Chris@16: // Check if *this cpp_dec_float is zero. Chris@16: if(iszero() || (xx.compare(double_min()) < 0)) Chris@16: { Chris@16: return 0.0; Chris@16: } Chris@16: Chris@16: // Check if *this cpp_dec_float exceeds the maximum of double. Chris@16: if(xx.compare(double_max()) > 0) Chris@16: { Chris@101: return ((!neg) ? std::numeric_limits::infinity() Chris@16: : -std::numeric_limits::infinity()); Chris@16: } Chris@16: Chris@16: std::stringstream ss; Chris@16: Chris@16: ss << str(std::numeric_limits::digits10 + (2 + 1), std::ios_base::scientific); Chris@16: Chris@16: double d; Chris@16: ss >> d; Chris@16: Chris@16: return d; Chris@16: } Chris@16: Chris@16: template Chris@101: long double cpp_dec_float::extract_long_double() const Chris@16: { Chris@16: // Returns the long double conversion of a cpp_dec_float. Chris@16: Chris@16: // Check if *this cpp_dec_float is subnormal. Chris@16: if(!(isfinite)()) Chris@16: { Chris@16: if((isnan)()) Chris@16: { Chris@16: return std::numeric_limits::quiet_NaN(); Chris@16: } Chris@16: else Chris@16: { Chris@101: return ((!neg) ? std::numeric_limits::infinity() Chris@16: : -std::numeric_limits::infinity()); Chris@16: } Chris@16: } Chris@16: Chris@16: cpp_dec_float xx(*this); Chris@16: if(xx.isneg()) Chris@16: xx.negate(); Chris@16: Chris@16: // Check if *this cpp_dec_float is zero. Chris@16: if(iszero() || (xx.compare(long_double_min()) < 0)) Chris@16: { Chris@16: return static_cast(0.0); Chris@16: } Chris@16: Chris@16: // Check if *this cpp_dec_float exceeds the maximum of double. Chris@16: if(xx.compare(long_double_max()) > 0) Chris@16: { Chris@101: return ((!neg) ? std::numeric_limits::infinity() Chris@16: : -std::numeric_limits::infinity()); Chris@16: } Chris@16: Chris@16: std::stringstream ss; Chris@16: Chris@16: ss << str(std::numeric_limits::digits10 + (2 + 1), std::ios_base::scientific); Chris@16: Chris@16: long double ld; Chris@16: ss >> ld; Chris@16: Chris@16: return ld; Chris@16: } Chris@16: Chris@16: template Chris@101: signed long long cpp_dec_float::extract_signed_long_long() const Chris@16: { Chris@16: // Extracts a signed long long from *this. Chris@16: // If (x > maximum of signed long long) or (x < minimum of signed long long), Chris@16: // then the maximum or minimum of signed long long is returned accordingly. Chris@16: Chris@16: if(exp < static_cast(0)) Chris@16: { Chris@16: return static_cast(0); Chris@16: } Chris@16: Chris@16: const bool b_neg = isneg(); Chris@16: Chris@16: unsigned long long val; Chris@16: Chris@16: if((!b_neg) && (compare(long_long_max()) > 0)) Chris@16: { Chris@16: return (std::numeric_limits::max)(); Chris@16: } Chris@101: else if(b_neg && (compare(long_long_min()) < 0)) Chris@16: { Chris@16: return (std::numeric_limits::min)(); Chris@16: } Chris@16: else Chris@16: { Chris@16: // Extract the data into an unsigned long long value. Chris@16: cpp_dec_float xn(extract_integer_part()); Chris@16: if(xn.isneg()) Chris@16: xn.negate(); Chris@16: Chris@16: val = static_cast(xn.data[0]); Chris@16: Chris@16: const boost::int32_t imax = (std::min)(static_cast(static_cast(xn.exp) / cpp_dec_float_elem_digits10), static_cast(cpp_dec_float_elem_number - static_cast(1))); Chris@16: Chris@16: for(boost::int32_t i = static_cast(1); i <= imax; i++) Chris@16: { Chris@16: val *= static_cast(cpp_dec_float_elem_mask); Chris@16: val += static_cast(xn.data[i]); Chris@16: } Chris@16: } Chris@16: Chris@101: if (!b_neg) Chris@101: { Chris@101: return static_cast(val); Chris@101: } Chris@101: else Chris@101: { Chris@101: // This strange expression avoids a hardware trap in the corner case Chris@101: // that val is the most negative value permitted in long long. Chris@101: // See https://svn.boost.org/trac/boost/ticket/9740. Chris@101: // Chris@101: signed long long sval = static_cast(val - 1); Chris@101: sval = -sval; Chris@101: --sval; Chris@101: return sval; Chris@101: } Chris@16: } Chris@16: Chris@16: template Chris@101: unsigned long long cpp_dec_float::extract_unsigned_long_long() const Chris@16: { Chris@16: // Extracts an unsigned long long from *this. Chris@16: // If x exceeds the maximum of unsigned long long, Chris@16: // then the maximum of unsigned long long is returned. Chris@16: // If x is negative, then the unsigned long long cast of Chris@16: // the signed long long extracted value is returned. Chris@16: Chris@16: if(isneg()) Chris@16: { Chris@16: return static_cast(extract_signed_long_long()); Chris@16: } Chris@16: Chris@16: if(exp < static_cast(0)) Chris@16: { Chris@16: return static_cast(0u); Chris@16: } Chris@16: Chris@16: const cpp_dec_float xn(extract_integer_part()); Chris@16: Chris@16: unsigned long long val; Chris@16: Chris@16: if(xn.compare(ulong_long_max()) > 0) Chris@16: { Chris@16: return (std::numeric_limits::max)(); Chris@16: } Chris@16: else Chris@16: { Chris@16: // Extract the data into an unsigned long long value. Chris@16: val = static_cast(xn.data[0]); Chris@16: Chris@16: const boost::int32_t imax = (std::min)(static_cast(static_cast(xn.exp) / cpp_dec_float_elem_digits10), static_cast(cpp_dec_float_elem_number - static_cast(1))); Chris@16: Chris@16: for(boost::int32_t i = static_cast(1); i <= imax; i++) Chris@16: { Chris@16: val *= static_cast(cpp_dec_float_elem_mask); Chris@16: val += static_cast(xn.data[i]); Chris@16: } Chris@16: } Chris@16: Chris@16: return val; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float cpp_dec_float::extract_integer_part() const Chris@16: { Chris@16: // Compute the signed integer part of x. Chris@16: Chris@16: if(!(isfinite)()) Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: if(exp < static_cast(0)) Chris@16: { Chris@16: // The absolute value of the number is smaller than 1. Chris@16: // Thus the integer part is zero. Chris@16: return zero(); Chris@16: } Chris@16: Chris@16: // Truncate the digits from the decimal part, including guard digits Chris@16: // that do not belong to the integer part. Chris@16: Chris@16: // Make a local copy. Chris@16: cpp_dec_float x = *this; Chris@16: Chris@16: // Clear out the decimal portion Chris@16: const size_t first_clear = (static_cast(x.exp) / static_cast(cpp_dec_float_elem_digits10)) + 1u; Chris@101: const size_t last_clear = static_cast(cpp_dec_float_elem_number); Chris@16: Chris@16: if(first_clear < last_clear) Chris@16: std::fill(x.data.begin() + first_clear, x.data.begin() + last_clear, static_cast(0u)); Chris@16: Chris@16: return x; Chris@16: } Chris@16: Chris@16: template Chris@16: std::string cpp_dec_float::str(boost::intmax_t number_of_digits, std::ios_base::fmtflags f) const Chris@16: { Chris@16: if((this->isinf)()) Chris@16: { Chris@16: if(this->isneg()) Chris@16: return "-inf"; Chris@16: else if(f & std::ios_base::showpos) Chris@16: return "+inf"; Chris@101: else Chris@16: return "inf"; Chris@16: } Chris@16: else if((this->isnan)()) Chris@16: { Chris@16: return "nan"; Chris@16: } Chris@16: Chris@16: std::string str; Chris@16: boost::intmax_t org_digits(number_of_digits); Chris@16: ExponentType my_exp = order(); Chris@101: Chris@16: if(number_of_digits == 0) Chris@16: number_of_digits = cpp_dec_float_total_digits10; Chris@101: Chris@16: if(f & std::ios_base::fixed) Chris@16: { Chris@16: number_of_digits += my_exp + 1; Chris@16: } Chris@16: else if(f & std::ios_base::scientific) Chris@16: ++number_of_digits; Chris@16: // Determine the number of elements needed to provide the requested digits from cpp_dec_float. Chris@16: const std::size_t number_of_elements = (std::min)(static_cast((number_of_digits / static_cast(cpp_dec_float_elem_digits10)) + 2u), Chris@16: static_cast(cpp_dec_float_elem_number)); Chris@16: Chris@16: // Extract the remaining digits from cpp_dec_float after the decimal point. Chris@16: str = boost::lexical_cast(data[0]); Chris@16: Chris@16: // Extract all of the digits from cpp_dec_float, beginning with the first data element. Chris@16: for(std::size_t i = static_cast(1u); i < number_of_elements; i++) Chris@16: { Chris@16: std::stringstream ss; Chris@16: Chris@16: ss << std::setw(static_cast(cpp_dec_float_elem_digits10)) Chris@16: << std::setfill(static_cast('0')) Chris@16: << data[i]; Chris@16: Chris@16: str += ss.str(); Chris@16: } Chris@101: Chris@16: bool have_leading_zeros = false; Chris@101: Chris@16: if(number_of_digits == 0) Chris@16: { Chris@16: // We only get here if the output format is "fixed" and we just need to Chris@16: // round the first non-zero digit. Chris@101: number_of_digits -= my_exp + 1; // reset to original value Chris@101: str.insert(static_cast(0), std::string::size_type(number_of_digits), '0'); Chris@16: have_leading_zeros = true; Chris@16: } Chris@101: Chris@16: if(number_of_digits < 0) Chris@16: { Chris@16: str = "0"; Chris@16: if(isneg()) Chris@101: str.insert(static_cast(0), 1, '-'); Chris@16: boost::multiprecision::detail::format_float_string(str, 0, number_of_digits - my_exp - 1, f, this->iszero()); Chris@16: return str; Chris@16: } Chris@16: else Chris@16: { Chris@16: // Cut the output to the size of the precision. Chris@16: if(str.length() > static_cast(number_of_digits)) Chris@16: { Chris@16: // Get the digit after the last needed digit for rounding Chris@16: const boost::uint32_t round = static_cast(static_cast(str[static_cast(number_of_digits)]) - static_cast('0')); Chris@16: Chris@16: bool need_round_up = round >= 5u; Chris@16: Chris@16: if(round == 5u) Chris@16: { Chris@16: const boost::uint32_t ix = static_cast(static_cast(str[static_cast(number_of_digits - 1)]) - static_cast('0')); Chris@16: if((ix & 1u) == 0) Chris@16: { Chris@16: // We have an even digit followed by a 5, so we might not actually need to round up Chris@16: // if all the remaining digits are zero: Chris@16: if(str.find_first_not_of('0', static_cast(number_of_digits + 1)) == std::string::npos) Chris@16: { Chris@16: bool all_zeros = true; Chris@16: // No none-zero trailing digits in the string, now check whatever parts we didn't convert to the string: Chris@16: for(std::size_t i = number_of_elements; i < data.size(); i++) Chris@16: { Chris@16: if(data[i]) Chris@16: { Chris@16: all_zeros = false; Chris@16: break; Chris@16: } Chris@16: } Chris@16: if(all_zeros) Chris@101: need_round_up = false; // tie break - round to even. Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: // Truncate the string Chris@16: str.erase(static_cast(number_of_digits)); Chris@16: Chris@16: if(need_round_up) Chris@16: { Chris@16: std::size_t ix = static_cast(str.length() - 1u); Chris@16: Chris@16: // Every trailing 9 must be rounded up Chris@16: while(ix && (static_cast(str.at(ix)) - static_cast('0') == static_cast(9))) Chris@16: { Chris@16: str.at(ix) = static_cast('0'); Chris@16: --ix; Chris@16: } Chris@16: Chris@16: if(!ix) Chris@16: { Chris@16: // There were nothing but trailing nines. Chris@16: if(static_cast(static_cast(str.at(ix)) - static_cast(0x30)) == static_cast(9)) Chris@16: { Chris@16: // Increment up to the next order and adjust exponent. Chris@16: str.at(ix) = static_cast('1'); Chris@16: ++my_exp; Chris@16: } Chris@16: else Chris@16: { Chris@16: // Round up this digit. Chris@16: ++str.at(ix); Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: // Round up the last digit. Chris@16: ++str[ix]; Chris@16: } Chris@16: } Chris@16: } Chris@16: } Chris@101: Chris@16: if(have_leading_zeros) Chris@16: { Chris@16: // We need to take the zeros back out again, and correct the exponent Chris@16: // if we rounded up: Chris@16: if(str[std::string::size_type(number_of_digits - 1)] != '0') Chris@16: { Chris@16: ++my_exp; Chris@16: str.erase(0, std::string::size_type(number_of_digits - 1)); Chris@16: } Chris@16: else Chris@16: str.erase(0, std::string::size_type(number_of_digits)); Chris@16: } Chris@101: Chris@16: if(isneg()) Chris@101: str.insert(static_cast(0), 1, '-'); Chris@101: Chris@16: boost::multiprecision::detail::format_float_string(str, my_exp, org_digits, f, this->iszero()); Chris@16: return str; Chris@16: } Chris@16: Chris@16: template Chris@16: bool cpp_dec_float::rd_string(const char* const s) Chris@16: { Chris@16: try{ Chris@16: Chris@16: std::string str(s); Chris@16: Chris@16: // TBD: Using several regular expressions may significantly reduce Chris@16: // the code complexity (and perhaps the run-time) of rd_string(). Chris@16: Chris@16: // Get a possible exponent and remove it. Chris@16: exp = static_cast(0); Chris@16: Chris@16: std::size_t pos; Chris@16: Chris@101: if( ((pos = str.find('e')) != std::string::npos) Chris@16: || ((pos = str.find('E')) != std::string::npos) Chris@16: ) Chris@16: { Chris@16: // Remove the exponent part from the string. Chris@16: exp = boost::lexical_cast(static_cast(str.c_str() + (pos + 1u))); Chris@16: str = str.substr(static_cast(0u), pos); Chris@16: } Chris@16: Chris@16: // Get a possible +/- sign and remove it. Chris@16: neg = false; Chris@16: Chris@16: if(str.size()) Chris@16: { Chris@16: if(str[0] == '-') Chris@16: { Chris@16: neg = true; Chris@16: str.erase(0, 1); Chris@16: } Chris@16: else if(str[0] == '+') Chris@16: { Chris@16: str.erase(0, 1); Chris@16: } Chris@16: } Chris@16: // Chris@16: // Special cases for infinities and NaN's: Chris@16: // Chris@16: if((str == "inf") || (str == "INF") || (str == "infinity") || (str == "INFINITY")) Chris@16: { Chris@16: if(neg) Chris@16: { Chris@16: *this = this->inf(); Chris@16: this->negate(); Chris@16: } Chris@16: else Chris@16: *this = this->inf(); Chris@16: return true; Chris@16: } Chris@16: if((str.size() >= 3) && ((str.substr(0, 3) == "nan") || (str.substr(0, 3) == "NAN") || (str.substr(0, 3) == "NaN"))) Chris@16: { Chris@16: *this = this->nan(); Chris@16: return true; Chris@16: } Chris@16: Chris@16: // Remove the leading zeros for all input types. Chris@16: const std::string::iterator fwd_it_leading_zero = std::find_if(str.begin(), str.end(), char_is_nonzero_predicate); Chris@16: Chris@16: if(fwd_it_leading_zero != str.begin()) Chris@16: { Chris@16: if(fwd_it_leading_zero == str.end()) Chris@16: { Chris@16: // The string contains nothing but leading zeros. Chris@16: // This string represents zero. Chris@16: operator=(zero()); Chris@16: return true; Chris@16: } Chris@16: else Chris@16: { Chris@16: str.erase(str.begin(), fwd_it_leading_zero); Chris@16: } Chris@16: } Chris@16: Chris@16: // Put the input string into the standard cpp_dec_float input form Chris@16: // aaa.bbbbE+/-n, where aaa has 1...cpp_dec_float_elem_digits10, bbbb has an Chris@16: // even multiple of cpp_dec_float_elem_digits10 which are possibly zero padded Chris@16: // on the right-end, and n is a signed 64-bit integer which is an Chris@16: // even multiple of cpp_dec_float_elem_digits10. Chris@16: Chris@16: // Find a possible decimal point. Chris@16: pos = str.find(static_cast('.')); Chris@16: Chris@16: if(pos != std::string::npos) Chris@16: { Chris@16: // Remove all trailing insignificant zeros. Chris@16: const std::string::const_reverse_iterator rit_non_zero = std::find_if(str.rbegin(), str.rend(), char_is_nonzero_predicate); Chris@16: Chris@101: if(rit_non_zero != static_cast(str.rbegin())) Chris@16: { Chris@16: const std::string::size_type ofs = str.length() - std::distance(str.rbegin(), rit_non_zero); Chris@16: str.erase(str.begin() + ofs, str.end()); Chris@16: } Chris@16: Chris@16: // Check if the input is identically zero. Chris@16: if(str == std::string(".")) Chris@16: { Chris@16: operator=(zero()); Chris@16: return true; Chris@16: } Chris@16: Chris@16: // Remove leading significant zeros just after the decimal point Chris@16: // and adjust the exponent accordingly. Chris@16: // Note that the while-loop operates only on strings of the form ".000abcd..." Chris@16: // and peels away the zeros just after the decimal point. Chris@16: if(str.at(static_cast(0u)) == static_cast('.')) Chris@16: { Chris@16: const std::string::iterator it_non_zero = std::find_if(str.begin() + 1u, str.end(), char_is_nonzero_predicate); Chris@16: Chris@16: std::size_t delta_exp = static_cast(0u); Chris@16: Chris@16: if(str.at(static_cast(1u)) == static_cast('0')) Chris@16: { Chris@16: delta_exp = std::distance(str.begin() + 1u, it_non_zero); Chris@16: } Chris@16: Chris@16: // Bring one single digit into the mantissa and adjust the exponent accordingly. Chris@16: str.erase(str.begin(), it_non_zero); Chris@101: str.insert(static_cast(1u), "."); Chris@16: exp -= static_cast(delta_exp + 1u); Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: // Input string has no decimal point: Append decimal point. Chris@16: str.append("."); Chris@16: } Chris@16: Chris@16: // Shift the decimal point such that the exponent is an even multiple of cpp_dec_float_elem_digits10. Chris@101: std::size_t n_shift = static_cast(0u); Chris@16: const std::size_t n_exp_rem = static_cast(exp % static_cast(cpp_dec_float_elem_digits10)); Chris@16: Chris@16: if((exp % static_cast(cpp_dec_float_elem_digits10)) != static_cast(0)) Chris@16: { Chris@16: n_shift = ((exp < static_cast(0)) Chris@16: ? static_cast(n_exp_rem + static_cast(cpp_dec_float_elem_digits10)) Chris@16: : static_cast(n_exp_rem)); Chris@16: } Chris@16: Chris@16: // Make sure that there are enough digits for the decimal point shift. Chris@16: pos = str.find(static_cast('.')); Chris@16: Chris@16: std::size_t pos_plus_one = static_cast(pos + 1u); Chris@16: Chris@16: if((str.length() - pos_plus_one) < n_shift) Chris@16: { Chris@16: const std::size_t sz = static_cast(n_shift - (str.length() - pos_plus_one)); Chris@16: Chris@16: str.append(std::string(sz, static_cast('0'))); Chris@16: } Chris@16: Chris@16: // Do the decimal point shift. Chris@16: if(n_shift != static_cast(0u)) Chris@16: { Chris@101: str.insert(static_cast(pos_plus_one + n_shift), "."); Chris@101: Chris@101: str.erase(pos, static_cast(1u)); Chris@16: Chris@16: exp -= static_cast(n_shift); Chris@16: } Chris@16: Chris@16: // Cut the size of the mantissa to <= cpp_dec_float_elem_digits10. Chris@101: pos = str.find(static_cast('.')); Chris@16: pos_plus_one = static_cast(pos + 1u); Chris@16: Chris@16: if(pos > static_cast(cpp_dec_float_elem_digits10)) Chris@16: { Chris@101: const boost::int32_t n_pos = static_cast(pos); Chris@16: const boost::int32_t n_rem_is_zero = ((static_cast(n_pos % cpp_dec_float_elem_digits10) == static_cast(0)) ? static_cast(1) : static_cast(0)); Chris@101: const boost::int32_t n = static_cast(static_cast(n_pos / cpp_dec_float_elem_digits10) - n_rem_is_zero); Chris@16: Chris@16: str.insert(static_cast(static_cast(n_pos - static_cast(n * cpp_dec_float_elem_digits10))), "."); Chris@16: Chris@16: str.erase(pos_plus_one, static_cast(1u)); Chris@16: Chris@16: exp += static_cast(static_cast(n) * static_cast(cpp_dec_float_elem_digits10)); Chris@16: } Chris@16: Chris@16: // Pad the decimal part such that its value is an even Chris@16: // multiple of cpp_dec_float_elem_digits10. Chris@101: pos = str.find(static_cast('.')); Chris@16: pos_plus_one = static_cast(pos + 1u); Chris@16: Chris@16: const boost::int32_t n_dec = static_cast(static_cast(str.length() - 1u) - static_cast(pos)); Chris@16: const boost::int32_t n_rem = static_cast(n_dec % cpp_dec_float_elem_digits10); Chris@101: Chris@101: boost::int32_t n_cnt = ((n_rem != static_cast(0)) Chris@101: ? static_cast(cpp_dec_float_elem_digits10 - n_rem) Chris@101: : static_cast(0)); Chris@16: Chris@16: if(n_cnt != static_cast(0)) Chris@16: { Chris@16: str.append(static_cast(n_cnt), static_cast('0')); Chris@16: } Chris@16: Chris@16: // Truncate decimal part if it is too long. Chris@16: const std::size_t max_dec = static_cast((cpp_dec_float_elem_number - 1) * cpp_dec_float_elem_digits10); Chris@16: Chris@16: if(static_cast(str.length() - pos) > max_dec) Chris@16: { Chris@16: str = str.substr(static_cast(0u), Chris@16: static_cast(pos_plus_one + max_dec)); Chris@16: } Chris@16: Chris@16: // Now the input string has the standard cpp_dec_float input form. Chris@16: // (See the comment above.) Chris@16: Chris@16: // Set all the data elements to 0. Chris@16: std::fill(data.begin(), data.end(), static_cast(0u)); Chris@16: Chris@16: // Extract the data. Chris@16: Chris@16: // First get the digits to the left of the decimal point... Chris@16: data[0u] = boost::lexical_cast(str.substr(static_cast(0u), pos)); Chris@16: Chris@16: // ...then get the remaining digits to the right of the decimal point. Chris@16: const std::string::size_type i_end = ((str.length() - pos_plus_one) / static_cast(cpp_dec_float_elem_digits10)); Chris@16: Chris@16: for(std::string::size_type i = static_cast(0u); i < i_end; i++) Chris@16: { Chris@101: const std::string::const_iterator it = str.begin() Chris@16: + pos_plus_one Chris@16: + (i * static_cast(cpp_dec_float_elem_digits10)); Chris@16: Chris@16: data[i + 1u] = boost::lexical_cast(std::string(it, it + static_cast(cpp_dec_float_elem_digits10))); Chris@16: } Chris@16: Chris@16: // Check for overflow... Chris@16: if(exp > cpp_dec_float_max_exp10) Chris@16: { Chris@16: const bool b_result_is_neg = neg; Chris@16: Chris@16: *this = inf(); Chris@16: if(b_result_is_neg) Chris@16: negate(); Chris@16: } Chris@16: Chris@16: // ...and check for underflow. Chris@16: if(exp <= cpp_dec_float_min_exp10) Chris@16: { Chris@16: if(exp == cpp_dec_float_min_exp10) Chris@16: { Chris@16: // Check for identity with the minimum value. Chris@16: cpp_dec_float test = *this; Chris@16: Chris@16: test.exp = static_cast(0); Chris@16: Chris@16: if(test.isone()) Chris@16: { Chris@16: *this = zero(); Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@16: *this = zero(); Chris@16: } Chris@16: } Chris@16: Chris@16: } Chris@16: catch(const bad_lexical_cast&) Chris@16: { Chris@16: // Rethrow with better error message: Chris@16: std::string msg = "Unable to parse the string \""; Chris@16: msg += s; Chris@16: msg += "\" as a floating point value."; Chris@16: throw std::runtime_error(msg); Chris@16: } Chris@16: Chris@16: return true; Chris@16: } Chris@16: Chris@16: template Chris@101: cpp_dec_float::cpp_dec_float(const double mantissa, const ExponentType exponent) Chris@101: : data (), Chris@101: exp (static_cast(0)), Chris@101: neg (false), Chris@101: fpclass (cpp_dec_float_finite), Chris@16: prec_elem(cpp_dec_float_elem_number) Chris@16: { Chris@16: // Create *this cpp_dec_float from a given mantissa and exponent. Chris@16: // Note: This constructor does not maintain the full precision of double. Chris@16: Chris@16: const bool mantissa_is_iszero = (::fabs(mantissa) < ((std::numeric_limits::min)() * (1.0 + std::numeric_limits::epsilon()))); Chris@16: Chris@16: if(mantissa_is_iszero) Chris@16: { Chris@16: std::fill(data.begin(), data.end(), static_cast(0u)); Chris@16: return; Chris@16: } Chris@16: Chris@16: const bool b_neg = (mantissa < 0.0); Chris@16: Chris@16: double d = ((!b_neg) ? mantissa : -mantissa); Chris@101: ExponentType e = exponent; Chris@16: Chris@16: while(d > 10.0) { d /= 10.0; ++e; } Chris@101: while(d < 1.0) { d *= 10.0; --e; } Chris@16: Chris@16: boost::int32_t shift = static_cast(e % static_cast(cpp_dec_float_elem_digits10)); Chris@16: Chris@16: while(static_cast(shift-- % cpp_dec_float_elem_digits10) != static_cast(0)) Chris@16: { Chris@16: d *= 10.0; Chris@16: --e; Chris@16: } Chris@16: Chris@16: exp = e; Chris@16: neg = b_neg; Chris@16: Chris@16: std::fill(data.begin(), data.end(), static_cast(0u)); Chris@16: Chris@16: static const boost::int32_t digit_ratio = static_cast(static_cast(std::numeric_limits::digits10) / static_cast(cpp_dec_float_elem_digits10)); Chris@16: static const boost::int32_t digit_loops = static_cast(digit_ratio + static_cast(2)); Chris@16: Chris@16: for(boost::int32_t i = static_cast(0); i < digit_loops; i++) Chris@16: { Chris@16: boost::uint32_t n = static_cast(static_cast(d)); Chris@101: data[i] = static_cast(n); Chris@101: d -= static_cast(n); Chris@101: d *= static_cast(cpp_dec_float_elem_mask); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: cpp_dec_float& cpp_dec_float::operator= (long double a) Chris@16: { Chris@16: // Christopher Kormanyos's original code used a cast to long long here, but that fails Chris@16: // when long double has more digits than a long long. Chris@16: using std::frexp; Chris@16: using std::ldexp; Chris@16: using std::floor; Chris@16: Chris@101: if(a == 0) Chris@16: return *this = zero(); Chris@101: Chris@101: if(a == 1) Chris@16: return *this = one(); Chris@16: Chris@16: if((boost::math::isinf)(a)) Chris@16: return *this = inf(); Chris@101: Chris@16: if((boost::math::isnan)(a)) Chris@16: return *this = nan(); Chris@16: Chris@16: int e; Chris@16: long double f, term; Chris@16: *this = zero(); Chris@16: Chris@16: f = frexp(a, &e); Chris@101: // See https://svn.boost.org/trac/boost/ticket/10924 for an example of why this may go wrong: Chris@101: BOOST_ASSERT((boost::math::isfinite)(f)); Chris@16: Chris@16: static const int shift = std::numeric_limits::digits - 1; Chris@16: Chris@16: while(f) Chris@16: { Chris@16: // extract int sized bits from f: Chris@16: f = ldexp(f, shift); Chris@101: BOOST_ASSERT((boost::math::isfinite)(f)); Chris@16: term = floor(f); Chris@16: e -= shift; Chris@16: *this *= pow2(shift); Chris@16: if(term > 0) Chris@16: add_unsigned_long_long(static_cast(term)); Chris@16: else Chris@16: sub_unsigned_long_long(static_cast(-term)); Chris@16: f -= term; Chris@16: } Chris@101: Chris@16: if(e != 0) Chris@16: *this *= pow2(e); Chris@101: Chris@16: return *this; Chris@16: } Chris@16: Chris@16: template Chris@101: void cpp_dec_float::from_unsigned_long_long(const unsigned long long u) Chris@16: { Chris@16: std::fill(data.begin(), data.end(), static_cast(0u)); Chris@16: Chris@16: exp = static_cast(0); Chris@16: neg = false; Chris@16: fpclass = cpp_dec_float_finite; Chris@16: prec_elem = cpp_dec_float_elem_number; Chris@16: Chris@16: std::size_t i =static_cast(0u); Chris@16: Chris@16: unsigned long long uu = u; Chris@16: Chris@16: boost::uint32_t temp[(std::numeric_limits::digits10 / static_cast(cpp_dec_float_elem_digits10)) + 3] = { static_cast(0u) }; Chris@16: Chris@16: while(uu != static_cast(0u)) Chris@16: { Chris@16: temp[i] = static_cast(uu % static_cast(cpp_dec_float_elem_mask)); Chris@16: uu = static_cast(uu / static_cast(cpp_dec_float_elem_mask)); Chris@16: ++i; Chris@16: } Chris@16: Chris@16: if(i > static_cast(1u)) Chris@16: { Chris@16: exp += static_cast((i - 1u) * static_cast(cpp_dec_float_elem_digits10)); Chris@16: } Chris@16: Chris@16: std::reverse(temp, temp + i); Chris@16: std::copy(temp, temp + (std::min)(i, static_cast(cpp_dec_float_elem_number)), data.begin()); Chris@16: } Chris@16: Chris@16: template Chris@101: boost::uint32_t cpp_dec_float::mul_loop_uv(boost::uint32_t* const u, const boost::uint32_t* const v, const boost::int32_t p) Chris@16: { Chris@16: // Chris@101: // There is a limit on how many limbs this algorithm can handle without dropping digits Chris@101: // due to overflow in the carry, it is: Chris@16: // Chris@101: // FLOOR( (2^64 - 1) / (10^8 * 10^8) ) == 1844 Chris@16: // Chris@16: 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."); Chris@16: Chris@16: boost::uint64_t carry = static_cast(0u); Chris@16: Chris@16: for(boost::int32_t j = static_cast(p - 1u); j >= static_cast(0); j--) Chris@16: { Chris@16: boost::uint64_t sum = carry; Chris@16: Chris@16: for(boost::int32_t i = j; i >= static_cast(0); i--) Chris@16: { Chris@16: sum += static_cast(u[j - i] * static_cast(v[i])); Chris@16: } Chris@16: Chris@101: u[j] = static_cast(sum % static_cast(cpp_dec_float_elem_mask)); Chris@16: carry = static_cast(sum / static_cast(cpp_dec_float_elem_mask)); Chris@16: } Chris@16: Chris@16: return static_cast(carry); Chris@16: } Chris@16: Chris@16: template Chris@101: boost::uint32_t cpp_dec_float::mul_loop_n(boost::uint32_t* const u, boost::uint32_t n, const boost::int32_t p) Chris@16: { Chris@16: boost::uint64_t carry = static_cast(0u); Chris@16: Chris@16: // Multiplication loop. Chris@16: for(boost::int32_t j = p - 1; j >= static_cast(0); j--) Chris@16: { Chris@16: const boost::uint64_t t = static_cast(carry + static_cast(u[j] * static_cast(n))); Chris@101: carry = static_cast(t / static_cast(cpp_dec_float_elem_mask)); Chris@101: u[j] = static_cast(t - static_cast(static_cast(cpp_dec_float_elem_mask) * static_cast(carry))); Chris@16: } Chris@16: Chris@16: return static_cast(carry); Chris@16: } Chris@16: Chris@16: template Chris@101: boost::uint32_t cpp_dec_float::div_loop_n(boost::uint32_t* const u, boost::uint32_t n, const boost::int32_t p) Chris@16: { Chris@16: boost::uint64_t prev = static_cast(0u); Chris@16: Chris@16: for(boost::int32_t j = static_cast(0); j < p; j++) Chris@16: { Chris@16: const boost::uint64_t t = static_cast(u[j] + static_cast(prev * static_cast(cpp_dec_float_elem_mask))); Chris@101: u[j] = static_cast(t / n); Chris@101: prev = static_cast(t - static_cast(n * static_cast(u[j]))); Chris@16: } Chris@16: Chris@16: return static_cast(prev); Chris@16: } Chris@16: Chris@16: template Chris@16: cpp_dec_float cpp_dec_float::pow2(const long long p) Chris@16: { Chris@16: // Create a static const table of p^2 for -128 < p < +128. Chris@16: // Note: The size of this table must be odd-numbered and Chris@16: // symmetric about 0. Chris@16: init.do_nothing(); Chris@16: static const boost::array, 255u> p2_data = Chris@16: {{ Chris@16: cpp_dec_float("5.877471754111437539843682686111228389093327783860437607543758531392086297273635864257812500000000000e-39"), Chris@16: cpp_dec_float("1.175494350822287507968736537222245677818665556772087521508751706278417259454727172851562500000000000e-38"), Chris@16: cpp_dec_float("2.350988701644575015937473074444491355637331113544175043017503412556834518909454345703125000000000000e-38"), Chris@16: cpp_dec_float("4.701977403289150031874946148888982711274662227088350086035006825113669037818908691406250000000000000e-38"), Chris@16: cpp_dec_float("9.403954806578300063749892297777965422549324454176700172070013650227338075637817382812500000000000000e-38"), Chris@16: cpp_dec_float("1.880790961315660012749978459555593084509864890835340034414002730045467615127563476562500000000000000e-37"), Chris@16: cpp_dec_float("3.761581922631320025499956919111186169019729781670680068828005460090935230255126953125000000000000000e-37"), Chris@16: cpp_dec_float("7.523163845262640050999913838222372338039459563341360137656010920181870460510253906250000000000000000e-37"), Chris@16: cpp_dec_float("1.504632769052528010199982767644474467607891912668272027531202184036374092102050781250000000000000000e-36"), Chris@16: cpp_dec_float("3.009265538105056020399965535288948935215783825336544055062404368072748184204101562500000000000000000e-36"), Chris@16: cpp_dec_float("6.018531076210112040799931070577897870431567650673088110124808736145496368408203125000000000000000000e-36"), Chris@16: cpp_dec_float("1.203706215242022408159986214115579574086313530134617622024961747229099273681640625000000000000000000e-35"), Chris@16: cpp_dec_float("2.407412430484044816319972428231159148172627060269235244049923494458198547363281250000000000000000000e-35"), Chris@16: cpp_dec_float("4.814824860968089632639944856462318296345254120538470488099846988916397094726562500000000000000000000e-35"), Chris@16: cpp_dec_float("9.629649721936179265279889712924636592690508241076940976199693977832794189453125000000000000000000000e-35"), Chris@16: cpp_dec_float("1.925929944387235853055977942584927318538101648215388195239938795566558837890625000000000000000000000e-34"), Chris@16: cpp_dec_float("3.851859888774471706111955885169854637076203296430776390479877591133117675781250000000000000000000000e-34"), Chris@16: cpp_dec_float("7.703719777548943412223911770339709274152406592861552780959755182266235351562500000000000000000000000e-34"), Chris@16: cpp_dec_float("1.540743955509788682444782354067941854830481318572310556191951036453247070312500000000000000000000000e-33"), Chris@16: cpp_dec_float("3.081487911019577364889564708135883709660962637144621112383902072906494140625000000000000000000000000e-33"), Chris@16: cpp_dec_float("6.162975822039154729779129416271767419321925274289242224767804145812988281250000000000000000000000000e-33"), Chris@16: cpp_dec_float("1.232595164407830945955825883254353483864385054857848444953560829162597656250000000000000000000000000e-32"), Chris@16: cpp_dec_float("2.465190328815661891911651766508706967728770109715696889907121658325195312500000000000000000000000000e-32"), Chris@16: cpp_dec_float("4.930380657631323783823303533017413935457540219431393779814243316650390625000000000000000000000000000e-32"), Chris@16: cpp_dec_float("9.860761315262647567646607066034827870915080438862787559628486633300781250000000000000000000000000000e-32"), Chris@16: cpp_dec_float("1.972152263052529513529321413206965574183016087772557511925697326660156250000000000000000000000000000e-31"), Chris@16: cpp_dec_float("3.944304526105059027058642826413931148366032175545115023851394653320312500000000000000000000000000000e-31"), Chris@16: cpp_dec_float("7.888609052210118054117285652827862296732064351090230047702789306640625000000000000000000000000000000e-31"), Chris@16: cpp_dec_float("1.577721810442023610823457130565572459346412870218046009540557861328125000000000000000000000000000000e-30"), Chris@16: cpp_dec_float("3.155443620884047221646914261131144918692825740436092019081115722656250000000000000000000000000000000e-30"), Chris@16: cpp_dec_float("6.310887241768094443293828522262289837385651480872184038162231445312500000000000000000000000000000000e-30"), Chris@16: cpp_dec_float("1.262177448353618888658765704452457967477130296174436807632446289062500000000000000000000000000000000e-29"), Chris@16: cpp_dec_float("2.524354896707237777317531408904915934954260592348873615264892578125000000000000000000000000000000000e-29"), Chris@16: cpp_dec_float("5.048709793414475554635062817809831869908521184697747230529785156250000000000000000000000000000000000e-29"), Chris@16: cpp_dec_float("1.009741958682895110927012563561966373981704236939549446105957031250000000000000000000000000000000000e-28"), Chris@16: cpp_dec_float("2.019483917365790221854025127123932747963408473879098892211914062500000000000000000000000000000000000e-28"), Chris@16: cpp_dec_float("4.038967834731580443708050254247865495926816947758197784423828125000000000000000000000000000000000000e-28"), Chris@16: cpp_dec_float("8.077935669463160887416100508495730991853633895516395568847656250000000000000000000000000000000000000e-28"), Chris@16: cpp_dec_float("1.615587133892632177483220101699146198370726779103279113769531250000000000000000000000000000000000000e-27"), Chris@16: cpp_dec_float("3.231174267785264354966440203398292396741453558206558227539062500000000000000000000000000000000000000e-27"), Chris@16: cpp_dec_float("6.462348535570528709932880406796584793482907116413116455078125000000000000000000000000000000000000000e-27"), Chris@16: cpp_dec_float("1.292469707114105741986576081359316958696581423282623291015625000000000000000000000000000000000000000e-26"), Chris@16: cpp_dec_float("2.584939414228211483973152162718633917393162846565246582031250000000000000000000000000000000000000000e-26"), Chris@16: cpp_dec_float("5.169878828456422967946304325437267834786325693130493164062500000000000000000000000000000000000000000e-26"), Chris@16: cpp_dec_float("1.033975765691284593589260865087453566957265138626098632812500000000000000000000000000000000000000000e-25"), Chris@16: cpp_dec_float("2.067951531382569187178521730174907133914530277252197265625000000000000000000000000000000000000000000e-25"), Chris@16: cpp_dec_float("4.135903062765138374357043460349814267829060554504394531250000000000000000000000000000000000000000000e-25"), Chris@16: cpp_dec_float("8.271806125530276748714086920699628535658121109008789062500000000000000000000000000000000000000000000e-25"), Chris@16: cpp_dec_float("1.654361225106055349742817384139925707131624221801757812500000000000000000000000000000000000000000000e-24"), Chris@16: cpp_dec_float("3.308722450212110699485634768279851414263248443603515625000000000000000000000000000000000000000000000e-24"), Chris@16: cpp_dec_float("6.617444900424221398971269536559702828526496887207031250000000000000000000000000000000000000000000000e-24"), Chris@16: cpp_dec_float("1.323488980084844279794253907311940565705299377441406250000000000000000000000000000000000000000000000e-23"), Chris@16: cpp_dec_float("2.646977960169688559588507814623881131410598754882812500000000000000000000000000000000000000000000000e-23"), Chris@16: cpp_dec_float("5.293955920339377119177015629247762262821197509765625000000000000000000000000000000000000000000000000e-23"), Chris@16: cpp_dec_float("1.058791184067875423835403125849552452564239501953125000000000000000000000000000000000000000000000000e-22"), Chris@16: cpp_dec_float("2.117582368135750847670806251699104905128479003906250000000000000000000000000000000000000000000000000e-22"), Chris@16: cpp_dec_float("4.235164736271501695341612503398209810256958007812500000000000000000000000000000000000000000000000000e-22"), Chris@16: cpp_dec_float("8.470329472543003390683225006796419620513916015625000000000000000000000000000000000000000000000000000e-22"), Chris@16: cpp_dec_float("1.694065894508600678136645001359283924102783203125000000000000000000000000000000000000000000000000000e-21"), Chris@16: cpp_dec_float("3.388131789017201356273290002718567848205566406250000000000000000000000000000000000000000000000000000e-21"), Chris@16: cpp_dec_float("6.776263578034402712546580005437135696411132812500000000000000000000000000000000000000000000000000000e-21"), Chris@16: cpp_dec_float("1.355252715606880542509316001087427139282226562500000000000000000000000000000000000000000000000000000e-20"), Chris@16: cpp_dec_float("2.710505431213761085018632002174854278564453125000000000000000000000000000000000000000000000000000000e-20"), Chris@16: cpp_dec_float("5.421010862427522170037264004349708557128906250000000000000000000000000000000000000000000000000000000e-20"), Chris@16: cpp_dec_float("1.084202172485504434007452800869941711425781250000000000000000000000000000000000000000000000000000000e-19"), Chris@16: cpp_dec_float("2.168404344971008868014905601739883422851562500000000000000000000000000000000000000000000000000000000e-19"), Chris@16: cpp_dec_float("4.336808689942017736029811203479766845703125000000000000000000000000000000000000000000000000000000000e-19"), Chris@16: cpp_dec_float("8.673617379884035472059622406959533691406250000000000000000000000000000000000000000000000000000000000e-19"), Chris@16: cpp_dec_float("1.734723475976807094411924481391906738281250000000000000000000000000000000000000000000000000000000000e-18"), Chris@16: cpp_dec_float("3.469446951953614188823848962783813476562500000000000000000000000000000000000000000000000000000000000e-18"), Chris@16: cpp_dec_float("6.938893903907228377647697925567626953125000000000000000000000000000000000000000000000000000000000000e-18"), Chris@16: cpp_dec_float("1.387778780781445675529539585113525390625000000000000000000000000000000000000000000000000000000000000e-17"), Chris@16: cpp_dec_float("2.775557561562891351059079170227050781250000000000000000000000000000000000000000000000000000000000000e-17"), Chris@16: cpp_dec_float("5.551115123125782702118158340454101562500000000000000000000000000000000000000000000000000000000000000e-17"), Chris@16: cpp_dec_float("1.110223024625156540423631668090820312500000000000000000000000000000000000000000000000000000000000000e-16"), Chris@16: cpp_dec_float("2.220446049250313080847263336181640625000000000000000000000000000000000000000000000000000000000000000e-16"), Chris@16: cpp_dec_float("4.440892098500626161694526672363281250000000000000000000000000000000000000000000000000000000000000000e-16"), Chris@16: cpp_dec_float("8.881784197001252323389053344726562500000000000000000000000000000000000000000000000000000000000000000e-16"), Chris@16: cpp_dec_float("1.776356839400250464677810668945312500000000000000000000000000000000000000000000000000000000000000000e-15"), Chris@16: cpp_dec_float("3.552713678800500929355621337890625000000000000000000000000000000000000000000000000000000000000000000e-15"), Chris@16: cpp_dec_float("7.105427357601001858711242675781250000000000000000000000000000000000000000000000000000000000000000000e-15"), Chris@16: cpp_dec_float("1.421085471520200371742248535156250000000000000000000000000000000000000000000000000000000000000000000e-14"), Chris@16: cpp_dec_float("2.842170943040400743484497070312500000000000000000000000000000000000000000000000000000000000000000000e-14"), Chris@16: cpp_dec_float("5.684341886080801486968994140625000000000000000000000000000000000000000000000000000000000000000000000e-14"), Chris@16: cpp_dec_float("1.136868377216160297393798828125000000000000000000000000000000000000000000000000000000000000000000000e-13"), Chris@16: cpp_dec_float("2.273736754432320594787597656250000000000000000000000000000000000000000000000000000000000000000000000e-13"), Chris@16: cpp_dec_float("4.547473508864641189575195312500000000000000000000000000000000000000000000000000000000000000000000000e-13"), Chris@16: cpp_dec_float("9.094947017729282379150390625000000000000000000000000000000000000000000000000000000000000000000000000e-13"), Chris@16: cpp_dec_float("1.818989403545856475830078125000000000000000000000000000000000000000000000000000000000000000000000000e-12"), Chris@16: cpp_dec_float("3.637978807091712951660156250000000000000000000000000000000000000000000000000000000000000000000000000e-12"), Chris@16: cpp_dec_float("7.275957614183425903320312500000000000000000000000000000000000000000000000000000000000000000000000000e-12"), Chris@16: cpp_dec_float("1.455191522836685180664062500000000000000000000000000000000000000000000000000000000000000000000000000e-11"), Chris@16: cpp_dec_float("2.910383045673370361328125000000000000000000000000000000000000000000000000000000000000000000000000000e-11"), Chris@16: cpp_dec_float("5.820766091346740722656250000000000000000000000000000000000000000000000000000000000000000000000000000e-11"), Chris@16: cpp_dec_float("1.164153218269348144531250000000000000000000000000000000000000000000000000000000000000000000000000000e-10"), Chris@16: cpp_dec_float("2.328306436538696289062500000000000000000000000000000000000000000000000000000000000000000000000000000e-10"), Chris@16: cpp_dec_float("4.656612873077392578125000000000000000000000000000000000000000000000000000000000000000000000000000000e-10"), Chris@16: cpp_dec_float("9.313225746154785156250000000000000000000000000000000000000000000000000000000000000000000000000000000e-10"), Chris@16: cpp_dec_float("1.862645149230957031250000000000000000000000000000000000000000000000000000000000000000000000000000000e-9"), Chris@16: cpp_dec_float("3.725290298461914062500000000000000000000000000000000000000000000000000000000000000000000000000000000e-9"), Chris@16: cpp_dec_float("7.450580596923828125000000000000000000000000000000000000000000000000000000000000000000000000000000000e-9"), Chris@16: cpp_dec_float("1.490116119384765625000000000000000000000000000000000000000000000000000000000000000000000000000000000e-8"), Chris@16: cpp_dec_float("2.980232238769531250000000000000000000000000000000000000000000000000000000000000000000000000000000000e-8"), Chris@16: cpp_dec_float("5.960464477539062500000000000000000000000000000000000000000000000000000000000000000000000000000000000e-8"), Chris@16: cpp_dec_float("1.192092895507812500000000000000000000000000000000000000000000000000000000000000000000000000000000000e-7"), Chris@16: cpp_dec_float("2.384185791015625000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-7"), Chris@16: cpp_dec_float("4.768371582031250000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-7"), Chris@16: cpp_dec_float("9.536743164062500000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-7"), Chris@16: cpp_dec_float("1.907348632812500000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-6"), Chris@16: cpp_dec_float("3.814697265625000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-6"), Chris@16: cpp_dec_float("7.629394531250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e-6"), Chris@16: cpp_dec_float("0.000015258789062500000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.000030517578125000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.000061035156250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.000122070312500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.000244140625000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.000488281250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.000976562500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.001953125000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.003906250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.007812500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.01562500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.03125000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.06250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), Chris@16: cpp_dec_float("0.125"), Chris@16: cpp_dec_float("0.25"), Chris@16: cpp_dec_float("0.5"), Chris@16: one(), Chris@16: two(), Chris@16: cpp_dec_float(static_cast(4)), Chris@16: cpp_dec_float(static_cast(8)), Chris@16: cpp_dec_float(static_cast(16)), Chris@16: cpp_dec_float(static_cast(32)), Chris@16: cpp_dec_float(static_cast(64)), Chris@16: cpp_dec_float(static_cast(128)), Chris@16: cpp_dec_float(static_cast(256)), Chris@16: cpp_dec_float(static_cast(512)), Chris@16: cpp_dec_float(static_cast(1024)), Chris@16: cpp_dec_float(static_cast(2048)), Chris@16: cpp_dec_float(static_cast(4096)), Chris@16: cpp_dec_float(static_cast(8192)), Chris@16: cpp_dec_float(static_cast(16384)), Chris@16: cpp_dec_float(static_cast(32768)), Chris@16: cpp_dec_float(static_cast(65536)), Chris@16: cpp_dec_float(static_cast(131072)), Chris@16: cpp_dec_float(static_cast(262144)), Chris@16: cpp_dec_float(static_cast(524288)), Chris@16: cpp_dec_float(static_cast(1uL << 20u)), Chris@16: cpp_dec_float(static_cast(1uL << 21u)), Chris@16: cpp_dec_float(static_cast(1uL << 22u)), Chris@16: cpp_dec_float(static_cast(1uL << 23u)), Chris@16: cpp_dec_float(static_cast(1uL << 24u)), Chris@16: cpp_dec_float(static_cast(1uL << 25u)), Chris@16: cpp_dec_float(static_cast(1uL << 26u)), Chris@16: cpp_dec_float(static_cast(1uL << 27u)), Chris@16: cpp_dec_float(static_cast(1uL << 28u)), Chris@16: cpp_dec_float(static_cast(1uL << 29u)), Chris@16: cpp_dec_float(static_cast(1uL << 30u)), Chris@16: cpp_dec_float(static_cast(1uL << 31u)), Chris@16: cpp_dec_float(static_cast(1uLL << 32u)), Chris@16: cpp_dec_float(static_cast(1uLL << 33u)), Chris@16: cpp_dec_float(static_cast(1uLL << 34u)), Chris@16: cpp_dec_float(static_cast(1uLL << 35u)), Chris@16: cpp_dec_float(static_cast(1uLL << 36u)), Chris@16: cpp_dec_float(static_cast(1uLL << 37u)), Chris@16: cpp_dec_float(static_cast(1uLL << 38u)), Chris@16: cpp_dec_float(static_cast(1uLL << 39u)), Chris@16: cpp_dec_float(static_cast(1uLL << 40u)), Chris@16: cpp_dec_float(static_cast(1uLL << 41u)), Chris@16: cpp_dec_float(static_cast(1uLL << 42u)), Chris@16: cpp_dec_float(static_cast(1uLL << 43u)), Chris@16: cpp_dec_float(static_cast(1uLL << 44u)), Chris@16: cpp_dec_float(static_cast(1uLL << 45u)), Chris@16: cpp_dec_float(static_cast(1uLL << 46u)), Chris@16: cpp_dec_float(static_cast(1uLL << 47u)), Chris@16: cpp_dec_float(static_cast(1uLL << 48u)), Chris@16: cpp_dec_float(static_cast(1uLL << 49u)), Chris@16: cpp_dec_float(static_cast(1uLL << 50u)), Chris@16: cpp_dec_float(static_cast(1uLL << 51u)), Chris@16: cpp_dec_float(static_cast(1uLL << 52u)), Chris@16: cpp_dec_float(static_cast(1uLL << 53u)), Chris@16: cpp_dec_float(static_cast(1uLL << 54u)), Chris@16: cpp_dec_float(static_cast(1uLL << 55u)), Chris@16: cpp_dec_float(static_cast(1uLL << 56u)), Chris@16: cpp_dec_float(static_cast(1uLL << 57u)), Chris@16: cpp_dec_float(static_cast(1uLL << 58u)), Chris@16: cpp_dec_float(static_cast(1uLL << 59u)), Chris@16: cpp_dec_float(static_cast(1uLL << 60u)), Chris@16: cpp_dec_float(static_cast(1uLL << 61u)), Chris@16: cpp_dec_float(static_cast(1uLL << 62u)), Chris@16: cpp_dec_float(static_cast(1uLL << 63u)), Chris@16: cpp_dec_float("1.844674407370955161600000000000000000000000000000000000000000000000000000000000000000000000000000000e19"), Chris@16: cpp_dec_float("3.689348814741910323200000000000000000000000000000000000000000000000000000000000000000000000000000000e19"), Chris@16: cpp_dec_float("7.378697629483820646400000000000000000000000000000000000000000000000000000000000000000000000000000000e19"), Chris@16: cpp_dec_float("1.475739525896764129280000000000000000000000000000000000000000000000000000000000000000000000000000000e20"), Chris@16: cpp_dec_float("2.951479051793528258560000000000000000000000000000000000000000000000000000000000000000000000000000000e20"), Chris@16: cpp_dec_float("5.902958103587056517120000000000000000000000000000000000000000000000000000000000000000000000000000000e20"), Chris@16: cpp_dec_float("1.180591620717411303424000000000000000000000000000000000000000000000000000000000000000000000000000000e21"), Chris@16: cpp_dec_float("2.361183241434822606848000000000000000000000000000000000000000000000000000000000000000000000000000000e21"), Chris@16: cpp_dec_float("4.722366482869645213696000000000000000000000000000000000000000000000000000000000000000000000000000000e21"), Chris@16: cpp_dec_float("9.444732965739290427392000000000000000000000000000000000000000000000000000000000000000000000000000000e21"), Chris@16: cpp_dec_float("1.888946593147858085478400000000000000000000000000000000000000000000000000000000000000000000000000000e22"), Chris@16: cpp_dec_float("3.777893186295716170956800000000000000000000000000000000000000000000000000000000000000000000000000000e22"), Chris@16: cpp_dec_float("7.555786372591432341913600000000000000000000000000000000000000000000000000000000000000000000000000000e22"), Chris@16: cpp_dec_float("1.511157274518286468382720000000000000000000000000000000000000000000000000000000000000000000000000000e23"), Chris@16: cpp_dec_float("3.022314549036572936765440000000000000000000000000000000000000000000000000000000000000000000000000000e23"), Chris@16: cpp_dec_float("6.044629098073145873530880000000000000000000000000000000000000000000000000000000000000000000000000000e23"), Chris@16: cpp_dec_float("1.208925819614629174706176000000000000000000000000000000000000000000000000000000000000000000000000000e24"), Chris@16: cpp_dec_float("2.417851639229258349412352000000000000000000000000000000000000000000000000000000000000000000000000000e24"), Chris@16: cpp_dec_float("4.835703278458516698824704000000000000000000000000000000000000000000000000000000000000000000000000000e24"), Chris@16: cpp_dec_float("9.671406556917033397649408000000000000000000000000000000000000000000000000000000000000000000000000000e24"), Chris@16: cpp_dec_float("1.934281311383406679529881600000000000000000000000000000000000000000000000000000000000000000000000000e25"), Chris@16: cpp_dec_float("3.868562622766813359059763200000000000000000000000000000000000000000000000000000000000000000000000000e25"), Chris@16: cpp_dec_float("7.737125245533626718119526400000000000000000000000000000000000000000000000000000000000000000000000000e25"), Chris@16: cpp_dec_float("1.547425049106725343623905280000000000000000000000000000000000000000000000000000000000000000000000000e26"), Chris@16: cpp_dec_float("3.094850098213450687247810560000000000000000000000000000000000000000000000000000000000000000000000000e26"), Chris@16: cpp_dec_float("6.189700196426901374495621120000000000000000000000000000000000000000000000000000000000000000000000000e26"), Chris@16: cpp_dec_float("1.237940039285380274899124224000000000000000000000000000000000000000000000000000000000000000000000000e27"), Chris@16: cpp_dec_float("2.475880078570760549798248448000000000000000000000000000000000000000000000000000000000000000000000000e27"), Chris@16: cpp_dec_float("4.951760157141521099596496896000000000000000000000000000000000000000000000000000000000000000000000000e27"), Chris@16: cpp_dec_float("9.903520314283042199192993792000000000000000000000000000000000000000000000000000000000000000000000000e27"), Chris@16: cpp_dec_float("1.980704062856608439838598758400000000000000000000000000000000000000000000000000000000000000000000000e28"), Chris@16: cpp_dec_float("3.961408125713216879677197516800000000000000000000000000000000000000000000000000000000000000000000000e28"), Chris@16: cpp_dec_float("7.922816251426433759354395033600000000000000000000000000000000000000000000000000000000000000000000000e28"), Chris@16: cpp_dec_float("1.584563250285286751870879006720000000000000000000000000000000000000000000000000000000000000000000000e29"), Chris@16: cpp_dec_float("3.169126500570573503741758013440000000000000000000000000000000000000000000000000000000000000000000000e29"), Chris@16: cpp_dec_float("6.338253001141147007483516026880000000000000000000000000000000000000000000000000000000000000000000000e29"), Chris@16: cpp_dec_float("1.267650600228229401496703205376000000000000000000000000000000000000000000000000000000000000000000000e30"), Chris@16: cpp_dec_float("2.535301200456458802993406410752000000000000000000000000000000000000000000000000000000000000000000000e30"), Chris@16: cpp_dec_float("5.070602400912917605986812821504000000000000000000000000000000000000000000000000000000000000000000000e30"), Chris@16: cpp_dec_float("1.014120480182583521197362564300800000000000000000000000000000000000000000000000000000000000000000000e31"), Chris@16: cpp_dec_float("2.028240960365167042394725128601600000000000000000000000000000000000000000000000000000000000000000000e31"), Chris@16: cpp_dec_float("4.056481920730334084789450257203200000000000000000000000000000000000000000000000000000000000000000000e31"), Chris@16: cpp_dec_float("8.112963841460668169578900514406400000000000000000000000000000000000000000000000000000000000000000000e31"), Chris@16: cpp_dec_float("1.622592768292133633915780102881280000000000000000000000000000000000000000000000000000000000000000000e32"), Chris@16: cpp_dec_float("3.245185536584267267831560205762560000000000000000000000000000000000000000000000000000000000000000000e32"), Chris@16: cpp_dec_float("6.490371073168534535663120411525120000000000000000000000000000000000000000000000000000000000000000000e32"), Chris@16: cpp_dec_float("1.298074214633706907132624082305024000000000000000000000000000000000000000000000000000000000000000000e33"), Chris@16: cpp_dec_float("2.596148429267413814265248164610048000000000000000000000000000000000000000000000000000000000000000000e33"), Chris@16: cpp_dec_float("5.192296858534827628530496329220096000000000000000000000000000000000000000000000000000000000000000000e33"), Chris@16: cpp_dec_float("1.038459371706965525706099265844019200000000000000000000000000000000000000000000000000000000000000000e34"), Chris@16: cpp_dec_float("2.076918743413931051412198531688038400000000000000000000000000000000000000000000000000000000000000000e34"), Chris@16: cpp_dec_float("4.153837486827862102824397063376076800000000000000000000000000000000000000000000000000000000000000000e34"), Chris@16: cpp_dec_float("8.307674973655724205648794126752153600000000000000000000000000000000000000000000000000000000000000000e34"), Chris@16: cpp_dec_float("1.661534994731144841129758825350430720000000000000000000000000000000000000000000000000000000000000000e35"), Chris@16: cpp_dec_float("3.323069989462289682259517650700861440000000000000000000000000000000000000000000000000000000000000000e35"), Chris@16: cpp_dec_float("6.646139978924579364519035301401722880000000000000000000000000000000000000000000000000000000000000000e35"), Chris@16: cpp_dec_float("1.329227995784915872903807060280344576000000000000000000000000000000000000000000000000000000000000000e36"), Chris@16: cpp_dec_float("2.658455991569831745807614120560689152000000000000000000000000000000000000000000000000000000000000000e36"), Chris@16: cpp_dec_float("5.316911983139663491615228241121378304000000000000000000000000000000000000000000000000000000000000000e36"), Chris@16: cpp_dec_float("1.063382396627932698323045648224275660800000000000000000000000000000000000000000000000000000000000000e37"), Chris@16: cpp_dec_float("2.126764793255865396646091296448551321600000000000000000000000000000000000000000000000000000000000000e37"), Chris@16: cpp_dec_float("4.253529586511730793292182592897102643200000000000000000000000000000000000000000000000000000000000000e37"), Chris@16: cpp_dec_float("8.507059173023461586584365185794205286400000000000000000000000000000000000000000000000000000000000000e37"), Chris@16: cpp_dec_float("1.701411834604692317316873037158841057280000000000000000000000000000000000000000000000000000000000000e38") Chris@16: }}; Chris@16: Chris@16: if((p > static_cast(-128)) && (p < static_cast(+128))) Chris@16: { Chris@16: return p2_data[static_cast(p + ((p2_data.size() - 1u) / 2u))]; Chris@16: } Chris@16: else Chris@16: { Chris@16: // Compute and return 2^p. Chris@16: if(p < static_cast(0)) Chris@16: { Chris@16: return pow2(static_cast(-p)).calculate_inv(); Chris@16: } Chris@16: else Chris@16: { Chris@16: cpp_dec_float t; Chris@16: default_ops::detail::pow_imp(t, two(), p, mpl::true_()); Chris@16: return t; Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: Chris@16: template Chris@101: inline void eval_add(cpp_dec_float& result, const cpp_dec_float& o) Chris@16: { Chris@16: result += o; Chris@16: } Chris@16: template Chris@101: inline void eval_subtract(cpp_dec_float& result, const cpp_dec_float& o) Chris@16: { Chris@16: result -= o; Chris@16: } Chris@16: template Chris@101: inline void eval_multiply(cpp_dec_float& result, const cpp_dec_float& o) Chris@16: { Chris@16: result *= o; Chris@16: } Chris@16: template Chris@101: inline void eval_divide(cpp_dec_float& result, const cpp_dec_float& o) Chris@16: { Chris@16: result /= o; Chris@16: } Chris@16: Chris@16: template Chris@101: inline void eval_add(cpp_dec_float& result, const unsigned long long& o) Chris@16: { Chris@16: result.add_unsigned_long_long(o); Chris@16: } Chris@16: template Chris@101: inline void eval_subtract(cpp_dec_float& result, const unsigned long long& o) Chris@16: { Chris@16: result.sub_unsigned_long_long(o); Chris@16: } Chris@16: template Chris@101: inline void eval_multiply(cpp_dec_float& result, const unsigned long long& o) Chris@16: { Chris@16: result.mul_unsigned_long_long(o); Chris@16: } Chris@16: template Chris@101: inline void eval_divide(cpp_dec_float& result, const unsigned long long& o) Chris@16: { Chris@16: result.div_unsigned_long_long(o); Chris@16: } Chris@16: Chris@16: template Chris@101: inline void eval_add(cpp_dec_float& result, long long o) Chris@16: { Chris@16: if(o < 0) Chris@101: result.sub_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o)); Chris@16: else Chris@16: result.add_unsigned_long_long(o); Chris@16: } Chris@16: template Chris@101: inline void eval_subtract(cpp_dec_float& result, long long o) Chris@16: { Chris@16: if(o < 0) Chris@101: result.add_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o)); Chris@16: else Chris@16: result.sub_unsigned_long_long(o); Chris@16: } Chris@16: template Chris@101: inline void eval_multiply(cpp_dec_float& result, long long o) Chris@16: { Chris@16: if(o < 0) Chris@16: { Chris@101: result.mul_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o)); Chris@16: result.negate(); Chris@16: } Chris@16: else Chris@16: result.mul_unsigned_long_long(o); Chris@16: } Chris@16: template Chris@101: inline void eval_divide(cpp_dec_float& result, long long o) Chris@16: { Chris@16: if(o < 0) Chris@16: { Chris@101: result.div_unsigned_long_long(boost::multiprecision::detail::unsigned_abs(o)); Chris@16: result.negate(); Chris@16: } Chris@16: else Chris@16: result.div_unsigned_long_long(o); Chris@16: } Chris@16: Chris@16: template Chris@101: inline void eval_convert_to(unsigned long long* result, const cpp_dec_float& val) Chris@16: { Chris@16: *result = val.extract_unsigned_long_long(); Chris@16: } Chris@16: template Chris@101: inline void eval_convert_to(long long* result, const cpp_dec_float& val) Chris@16: { Chris@16: *result = val.extract_signed_long_long(); Chris@16: } Chris@16: template Chris@16: inline void eval_convert_to(long double* result, cpp_dec_float& val) Chris@16: { Chris@16: *result = val.extract_long_double(); Chris@16: } Chris@16: Chris@16: // Chris@16: // Non member function support: Chris@16: // Chris@16: template Chris@101: inline int eval_fpclassify(const cpp_dec_float& x) Chris@16: { Chris@16: if((x.isinf)()) Chris@16: return FP_INFINITE; Chris@16: if((x.isnan)()) Chris@16: return FP_NAN; Chris@16: if(x.iszero()) Chris@16: return FP_ZERO; Chris@16: return FP_NORMAL; Chris@16: } Chris@16: Chris@16: template Chris@101: inline void eval_abs(cpp_dec_float& result, const cpp_dec_float& x) Chris@16: { Chris@16: result = x; Chris@16: if(x.isneg()) Chris@16: result.negate(); Chris@16: } Chris@16: Chris@16: template Chris@101: inline void eval_fabs(cpp_dec_float& result, const cpp_dec_float& x) Chris@16: { Chris@16: result = x; Chris@16: if(x.isneg()) Chris@16: result.negate(); Chris@16: } Chris@16: Chris@16: template Chris@101: inline void eval_sqrt(cpp_dec_float& result, const cpp_dec_float& x) Chris@16: { Chris@16: result = x; Chris@16: result.calculate_sqrt(); Chris@16: } Chris@16: Chris@16: template Chris@101: inline void eval_floor(cpp_dec_float& result, const cpp_dec_float& x) Chris@16: { Chris@16: result = x; Chris@101: if(!(x.isfinite)() || x.isint()) Chris@101: { Chris@101: return; Chris@16: } Chris@16: Chris@16: if(x.isneg()) Chris@16: result -= cpp_dec_float::one(); Chris@16: result = result.extract_integer_part(); Chris@16: } Chris@16: Chris@16: template Chris@101: inline void eval_ceil(cpp_dec_float& result, const cpp_dec_float& x) Chris@16: { Chris@16: result = x; Chris@101: if(!(x.isfinite)() || x.isint()) Chris@101: { Chris@101: return; Chris@16: } Chris@16: Chris@16: if(!x.isneg()) Chris@16: result += cpp_dec_float::one(); Chris@16: result = result.extract_integer_part(); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void eval_trunc(cpp_dec_float& result, const cpp_dec_float& x) Chris@16: { Chris@101: if(!(x.isfinite)()) Chris@101: { Chris@16: result = boost::math::policies::raise_rounding_error("boost::multiprecision::trunc<%1%>(%1%)", 0, number >(x), number >(x), boost::math::policies::policy<>()).backend(); Chris@16: return; Chris@16: } Chris@16: else if(x.isint()) Chris@16: { Chris@16: result = x; Chris@16: return; Chris@16: } Chris@16: result = x.extract_integer_part(); Chris@16: } Chris@16: Chris@101: template Chris@101: inline ExponentType eval_ilogb(const cpp_dec_float& val) Chris@101: { Chris@101: // Set result, to the exponent of val: Chris@101: return val.order(); Chris@101: } Chris@16: template Chris@101: inline void eval_scalbn(cpp_dec_float& result, const cpp_dec_float& val, ArgType e_) Chris@101: { Chris@101: using default_ops::eval_multiply; Chris@101: const ExponentType e = e_; Chris@101: cpp_dec_float t(1.0, e); Chris@101: eval_multiply(result, val, t); Chris@101: } Chris@101: Chris@101: template Chris@101: inline void eval_ldexp(cpp_dec_float& result, const cpp_dec_float& x, ArgType e) Chris@16: { Chris@16: const long long the_exp = static_cast(e); Chris@16: Chris@16: if((the_exp > (std::numeric_limits::max)()) || (the_exp < (std::numeric_limits::min)())) Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error(std::string("Exponent value is out of range."))); Chris@16: Chris@16: result = x; Chris@16: Chris@101: if ((the_exp > static_cast(-std::numeric_limits::digits)) && (the_exp < static_cast(0))) Chris@16: result.div_unsigned_long_long(1ULL << static_cast(-the_exp)); Chris@16: else if((the_exp < static_cast( std::numeric_limits::digits)) && (the_exp > static_cast(0))) Chris@16: result.mul_unsigned_long_long(1ULL << the_exp); Chris@16: else if(the_exp != static_cast(0)) Chris@16: result *= cpp_dec_float::pow2(e); Chris@16: } Chris@16: Chris@16: template Chris@16: inline void eval_frexp(cpp_dec_float& result, const cpp_dec_float& x, ExponentType* e) Chris@16: { Chris@16: result = x; Chris@16: if(result.isneg()) Chris@16: result.negate(); Chris@16: Chris@16: if(result.iszero()) Chris@16: { Chris@16: *e = 0; Chris@16: return; Chris@16: } Chris@16: Chris@16: ExponentType t = result.order(); Chris@16: BOOST_MP_USING_ABS Chris@16: if(abs(t) < ((std::numeric_limits::max)() / 1000)) Chris@16: { Chris@16: t *= 1000; Chris@16: t /= 301; Chris@16: } Chris@16: else Chris@16: { Chris@16: t /= 301; Chris@16: t *= 1000; Chris@16: } Chris@16: Chris@16: result *= cpp_dec_float::pow2(-t); Chris@16: Chris@16: if(result.iszero() || (result.isinf)() || (result.isnan)()) Chris@16: { Chris@16: // pow2 overflowed, slip the calculation up: Chris@16: result = x; Chris@16: if(result.isneg()) Chris@16: result.negate(); Chris@16: t /= 2; Chris@16: result *= cpp_dec_float::pow2(-t); Chris@16: } Chris@16: BOOST_MP_USING_ABS Chris@16: if(abs(result.order()) > 5) Chris@16: { Chris@16: // If our first estimate doesn't get close enough then try recursion until we do: Chris@16: ExponentType e2; Chris@16: cpp_dec_float r2; Chris@16: eval_frexp(r2, result, &e2); Chris@16: // overflow protection: Chris@16: if((t > 0) && (e2 > 0) && (t > (std::numeric_limits::max)() - e2)) Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Exponent is too large to be represented as a power of 2.")); Chris@16: if((t < 0) && (e2 < 0) && (t < (std::numeric_limits::min)() - e2)) Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Exponent is too large to be represented as a power of 2.")); Chris@16: t += e2; Chris@16: result = r2; Chris@16: } Chris@16: Chris@16: while(result.compare(cpp_dec_float::one()) >= 0) Chris@16: { Chris@16: result /= cpp_dec_float::two(); Chris@16: ++t; Chris@16: } Chris@16: while(result.compare(cpp_dec_float::half()) < 0) Chris@16: { Chris@16: result *= cpp_dec_float::two(); Chris@16: --t; Chris@16: } Chris@16: *e = t; Chris@16: if(x.isneg()) Chris@16: result.negate(); Chris@16: } Chris@16: Chris@16: template Chris@16: inline typename disable_if >::type eval_frexp(cpp_dec_float& result, const cpp_dec_float& x, int* e) Chris@16: { Chris@16: ExponentType t; Chris@16: eval_frexp(result, x, &t); Chris@16: if((t > (std::numeric_limits::max)()) || (t < (std::numeric_limits::min)())) Chris@16: BOOST_THROW_EXCEPTION(std::runtime_error("Exponent is outside the range of an int")); Chris@16: *e = static_cast(t); Chris@16: } Chris@16: Chris@16: template Chris@101: inline bool eval_is_zero(const cpp_dec_float& val) Chris@16: { Chris@16: return val.iszero(); Chris@16: } Chris@16: template Chris@101: inline int eval_get_sign(const cpp_dec_float& val) Chris@16: { Chris@16: return val.iszero() ? 0 : val.isneg() ? -1 : 1; Chris@16: } Chris@16: Chris@16: } // namespace backends Chris@16: Chris@16: using boost::multiprecision::backends::cpp_dec_float; Chris@16: Chris@16: Chris@16: typedef number > cpp_dec_float_50; Chris@16: typedef number > cpp_dec_float_100; Chris@16: Chris@16: #ifdef BOOST_NO_SFINAE_EXPR Chris@16: Chris@16: namespace detail{ Chris@16: Chris@16: template Chris@16: struct is_explicitly_convertible, cpp_dec_float > : public mpl::true_ {}; Chris@16: Chris@16: } Chris@16: Chris@16: #endif Chris@16: Chris@16: Chris@16: }} Chris@16: Chris@16: namespace std Chris@16: { Chris@101: template Chris@16: class numeric_limits, ExpressionTemplates> > Chris@16: { Chris@16: public: Chris@101: BOOST_STATIC_CONSTEXPR bool is_specialized = true; Chris@101: BOOST_STATIC_CONSTEXPR bool is_signed = true; Chris@101: BOOST_STATIC_CONSTEXPR bool is_integer = false; Chris@101: BOOST_STATIC_CONSTEXPR bool is_exact = false; Chris@101: BOOST_STATIC_CONSTEXPR bool is_bounded = true; Chris@101: BOOST_STATIC_CONSTEXPR bool is_modulo = false; Chris@101: BOOST_STATIC_CONSTEXPR bool is_iec559 = false; Chris@101: BOOST_STATIC_CONSTEXPR int digits = boost::multiprecision::cpp_dec_float::cpp_dec_float_digits10; Chris@101: BOOST_STATIC_CONSTEXPR int digits10 = boost::multiprecision::cpp_dec_float::cpp_dec_float_digits10; Chris@101: BOOST_STATIC_CONSTEXPR int max_digits10 = boost::multiprecision::cpp_dec_float::cpp_dec_float_total_digits10; Chris@101: BOOST_STATIC_CONSTEXPR ExponentType min_exponent = boost::multiprecision::cpp_dec_float::cpp_dec_float_min_exp; // Type differs from int. Chris@101: BOOST_STATIC_CONSTEXPR ExponentType min_exponent10 = boost::multiprecision::cpp_dec_float::cpp_dec_float_min_exp10; // Type differs from int. Chris@101: BOOST_STATIC_CONSTEXPR ExponentType max_exponent = boost::multiprecision::cpp_dec_float::cpp_dec_float_max_exp; // Type differs from int. Chris@101: BOOST_STATIC_CONSTEXPR ExponentType max_exponent10 = boost::multiprecision::cpp_dec_float::cpp_dec_float_max_exp10; // Type differs from int. Chris@101: BOOST_STATIC_CONSTEXPR int radix = boost::multiprecision::cpp_dec_float::cpp_dec_float_radix; Chris@101: BOOST_STATIC_CONSTEXPR std::float_round_style round_style = std::round_indeterminate; Chris@101: BOOST_STATIC_CONSTEXPR bool has_infinity = true; Chris@101: BOOST_STATIC_CONSTEXPR bool has_quiet_NaN = true; Chris@101: BOOST_STATIC_CONSTEXPR bool has_signaling_NaN = false; Chris@101: BOOST_STATIC_CONSTEXPR std::float_denorm_style has_denorm = std::denorm_absent; Chris@101: BOOST_STATIC_CONSTEXPR bool has_denorm_loss = false; Chris@101: BOOST_STATIC_CONSTEXPR bool traps = false; Chris@101: BOOST_STATIC_CONSTEXPR bool tinyness_before = false; Chris@101: Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> (min) () { return (boost::multiprecision::cpp_dec_float::min)(); } Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> (max) () { return (boost::multiprecision::cpp_dec_float::max)(); } Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> lowest () { return boost::multiprecision::cpp_dec_float::zero(); } Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> epsilon () { return boost::multiprecision::cpp_dec_float::eps(); } Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> round_error () { return 0.5L; } Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> infinity () { return boost::multiprecision::cpp_dec_float::inf(); } Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> quiet_NaN () { return boost::multiprecision::cpp_dec_float::nan(); } Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> signaling_NaN() { return boost::multiprecision::cpp_dec_float::zero(); } Chris@101: BOOST_STATIC_CONSTEXPR boost::multiprecision::number, ExpressionTemplates> denorm_min () { return boost::multiprecision::cpp_dec_float::zero(); } Chris@16: }; Chris@16: Chris@16: #ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION Chris@16: Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST int numeric_limits, ExpressionTemplates> >::digits; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST int numeric_limits, ExpressionTemplates> >::digits10; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST int numeric_limits, ExpressionTemplates> >::max_digits10; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::is_signed; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::is_integer; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::is_exact; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST int numeric_limits, ExpressionTemplates> >::radix; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST ExponentType numeric_limits, ExpressionTemplates> >::min_exponent; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST ExponentType numeric_limits, ExpressionTemplates> >::min_exponent10; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST ExponentType numeric_limits, ExpressionTemplates> >::max_exponent; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST ExponentType numeric_limits, ExpressionTemplates> >::max_exponent10; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::has_infinity; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::has_quiet_NaN; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::has_signaling_NaN; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST float_denorm_style numeric_limits, ExpressionTemplates> >::has_denorm; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::has_denorm_loss; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::is_iec559; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::is_bounded; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::is_modulo; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::traps; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST bool numeric_limits, ExpressionTemplates> >::tinyness_before; Chris@16: template Chris@16: BOOST_CONSTEXPR_OR_CONST float_round_style numeric_limits, ExpressionTemplates> >::round_style; Chris@16: Chris@16: #endif Chris@16: } Chris@16: Chris@16: namespace boost{ namespace math{ Chris@16: Chris@16: namespace policies{ Chris@16: Chris@16: template Chris@16: struct precision< boost::multiprecision::number, ExpressionTemplates>, Policy> Chris@16: { Chris@16: // Define a local copy of cpp_dec_float_digits10 because it might differ Chris@16: // from the template parameter Digits10 for small or large digit counts. Chris@16: static const boost::int32_t cpp_dec_float_digits10 = boost::multiprecision::cpp_dec_float::cpp_dec_float_digits10; Chris@16: Chris@16: typedef typename Policy::precision_type precision_type; Chris@16: typedef digits2<((cpp_dec_float_digits10 + 1LL) * 1000LL) / 301LL> digits_2; Chris@16: typedef typename mpl::if_c< Chris@101: ((digits_2::value <= precision_type::value) Chris@16: || (Policy::precision_type::value <= 0)), Chris@16: // Default case, full precision for RealType: Chris@16: digits_2, Chris@101: // User customized precision: Chris@16: precision_type Chris@16: >::type type; Chris@16: }; Chris@16: Chris@16: } // namespace policies Chris@16: Chris@16: }} // namespaces boost::math Chris@16: Chris@16: #endif