void eval_log(T& result, const T& arg) { BOOST_STATIC_ASSERT_MSG(number_category<T>::value == number_kind_floating_point, "The log function is only valid for floating point types."); // // We use a variation of http://dlmf.nist.gov/4.45#i // using frexp to reduce the argument to x * 2^n, // then let y = x - 1 and compute: // log(x) = log(2) * n + log1p(1 + y) // typedef typename boost::multiprecision::detail::canonical<int, T>::type si_type; typedef typename boost::multiprecision::detail::canonical<unsigned, T>::type ui_type; typedef typename T::exponent_type exp_type; typedef typename boost::multiprecision::detail::canonical<exp_type, T>::type canonical_exp_type; typedef typename mpl::front<typename T::float_types>::type fp_type; exp_type e; T t; eval_frexp(t, arg, &e); bool alternate = false; if(t.compare(fp_type(2) / fp_type(3)) <= 0) { alternate = true; eval_ldexp(t, t, 1); --e; } eval_multiply(result, get_constant_ln2<T>(), canonical_exp_type(e)); INSTRUMENT_BACKEND(result); eval_subtract(t, ui_type(1)); /* -0.3 <= t <= 0.3 */ if(!alternate) t.negate(); /* 0 <= t <= 0.33333 */ T pow = t; T lim; T t2; if(alternate) eval_add(result, t); else eval_subtract(result, t); eval_multiply(lim, result, std::numeric_limits<number<T, et_on> >::epsilon().backend()); if(eval_get_sign(lim) < 0) lim.negate(); INSTRUMENT_BACKEND(lim); ui_type k = 1; do { ++k; eval_multiply(pow, t); eval_divide(t2, pow, k); INSTRUMENT_BACKEND(t2); if(alternate && ((k & 1) != 0)) eval_add(result, t2); else eval_subtract(result, t2); INSTRUMENT_BACKEND(result); }while(lim.compare(t2) < 0); }
void calc_log2(T& num, unsigned digits) { typedef typename geofeatures_boost::multiprecision::detail::canonical<geofeatures_boost::uint32_t, T>::type ui_type; typedef typename mpl::front<typename T::signed_types>::type si_type; // // String value with 1100 digits: // static const char* string_val = "0." "6931471805599453094172321214581765680755001343602552541206800094933936219696947156058633269964186875" "4200148102057068573368552023575813055703267075163507596193072757082837143519030703862389167347112335" "0115364497955239120475172681574932065155524734139525882950453007095326366642654104239157814952043740" "4303855008019441706416715186447128399681717845469570262716310645461502572074024816377733896385506952" "6066834113727387372292895649354702576265209885969320196505855476470330679365443254763274495125040606" "9438147104689946506220167720424524529612687946546193165174681392672504103802546259656869144192871608" "2938031727143677826548775664850856740776484514644399404614226031930967354025744460703080960850474866" "3852313818167675143866747664789088143714198549423151997354880375165861275352916610007105355824987941" "4729509293113897155998205654392871700072180857610252368892132449713893203784393530887748259701715591" "0708823683627589842589185353024363421436706118923678919237231467232172053401649256872747782344535347" "6481149418642386776774406069562657379600867076257199184734022651462837904883062033061144630073719489"; // // Check if we can just construct from string: // if(digits < 3640) // 3640 binary digits ~ 1100 decimal digits { num = string_val; return; } // // We calculate log2 from using the formula: // // ln(2) = 3/4 SUM[n>=0] ((-1)^n * N!^2 / (2^n(2n+1)!)) // // Numerator and denominator are calculated separately and then // divided at the end, we also precalculate the terms up to n = 5 // since these fit in a 32-bit integer anyway. // // See Gourdon, X., and Sebah, P. The logarithmic constant: log 2, Jan. 2004. // Also http://www.mpfr.org/algorithms.pdf. // num = static_cast<ui_type>(1180509120uL); T denom, next_term, temp; denom = static_cast<ui_type>(1277337600uL); next_term = static_cast<ui_type>(120uL); si_type sign = -1; ui_type limit = digits / 3 + 1; for(ui_type n = 6; n < limit; ++n) { temp = static_cast<ui_type>(2); eval_multiply(temp, ui_type(2 * n)); eval_multiply(temp, ui_type(2 * n + 1)); eval_multiply(num, temp); eval_multiply(denom, temp); sign = -sign; eval_multiply(next_term, n); eval_multiply(temp, next_term, next_term); if(sign < 0) temp.negate(); eval_add(num, temp); } eval_multiply(denom, ui_type(4)); eval_multiply(num, ui_type(3)); INSTRUMENT_BACKEND(denom); INSTRUMENT_BACKEND(num); eval_divide(num, denom); INSTRUMENT_BACKEND(num); }