inline int eval_fpclassify(const logged_adaptor<Backend>& arg) { using default_ops::eval_fpclassify; log_prefix_event(arg.value(), "fpclassify"); int r = eval_fpclassify(arg.value()); log_postfix_event(arg.value(), r, "fpclassify"); return r; }
void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_floating_point>& /*from_type*/) { #ifdef BOOST_MSVC #pragma warning(push) #pragma warning(disable:4127) #endif // // The code here only works when the radix of "From" is 2, we could try shifting by other // radixes but it would complicate things.... use a string conversion when the radix is other // than 2: // if(std::numeric_limits<number<From> >::radix != 2) { to = from.str(0, std::ios_base::fmtflags()).c_str(); return; } typedef typename canonical<unsigned char, To>::type ui_type; using default_ops::eval_fpclassify; using default_ops::eval_add; using default_ops::eval_subtract; using default_ops::eval_convert_to; // // First classify the input, then handle the special cases: // int c = eval_fpclassify(from); if(c == FP_ZERO) { to = ui_type(0); return; } else if(c == FP_NAN) { to = "nan"; return; } else if(c == FP_INFINITE) { to = "inf"; if(eval_get_sign(from) < 0) to.negate(); return; } typename From::exponent_type e; From f, term; to = ui_type(0); eval_frexp(f, from, &e); static const int shift = std::numeric_limits<boost::intmax_t>::digits - 1; while(!eval_is_zero(f)) { // extract int sized bits from f: eval_ldexp(f, f, shift); eval_floor(term, f); e -= shift; eval_ldexp(to, to, shift); typename boost::multiprecision::detail::canonical<boost::intmax_t, To>::type ll; eval_convert_to(&ll, term); eval_add(to, ll); eval_subtract(f, term); } typedef typename To::exponent_type to_exponent; if((e > (std::numeric_limits<to_exponent>::max)()) || (e < (std::numeric_limits<to_exponent>::min)())) { to = "inf"; if(eval_get_sign(from) < 0) to.negate(); return; } eval_ldexp(to, to, static_cast<to_exponent>(e)); #ifdef BOOST_MSVC #pragma warning(pop) #endif }
std::string convert_to_string(Backend b, std::streamsize digits, std::ios_base::fmtflags f) { using default_ops::eval_log10; using default_ops::eval_floor; using default_ops::eval_pow; using default_ops::eval_convert_to; using default_ops::eval_multiply; using default_ops::eval_divide; using default_ops::eval_subtract; using default_ops::eval_fpclassify; typedef typename mpl::front<typename Backend::unsigned_types>::type ui_type; typedef typename Backend::exponent_type exponent_type; std::string result; bool iszero = false; bool isneg = false; exponent_type expon = 0; std::streamsize org_digits = digits; BOOST_ASSERT(digits > 0); int fpt = eval_fpclassify(b); if(fpt == (int)FP_ZERO) { result = "0"; iszero = true; } else if(fpt == (int)FP_INFINITE) { if(b.compare(ui_type(0)) < 0) return "-inf"; else return ((f & std::ios_base::showpos) == std::ios_base::showpos) ? "+inf" : "inf"; } else if(fpt == (int)FP_NAN) { return "nan"; } else { // // Start by figuring out the exponent: // isneg = b.compare(ui_type(0)) < 0; if(isneg) b.negate(); Backend t; Backend ten; ten = ui_type(10); eval_log10(t, b); eval_floor(t, t); eval_convert_to(&expon, t); if(-expon > std::numeric_limits<number<Backend> >::max_exponent10 - 3) { int e = -expon / 2; Backend t2; eval_pow(t2, ten, e); eval_multiply(t, t2, b); eval_multiply(t, t2); if(expon & 1) eval_multiply(t, ten); } else { eval_pow(t, ten, -expon); eval_multiply(t, b); } // // Make sure we're between [1,10) and adjust if not: // if(t.compare(ui_type(1)) < 0) { eval_multiply(t, ui_type(10)); --expon; } else if(t.compare(ui_type(10)) >= 0) { eval_divide(t, ui_type(10)); ++expon; } Backend digit; ui_type cdigit; // // Adjust the number of digits required based on formatting options: // if(((f & std::ios_base::fixed) == std::ios_base::fixed) && (expon != -1)) digits += expon + 1; if((f & std::ios_base::scientific) == std::ios_base::scientific) ++digits; // // Extract the digits one at a time: // for(unsigned i = 0; i < digits; ++i) { eval_floor(digit, t); eval_convert_to(&cdigit, digit); result += static_cast<char>('0' + cdigit); eval_subtract(t, digit); eval_multiply(t, ten); } // // Possibly round result: // if(digits >= 0) { eval_floor(digit, t); eval_convert_to(&cdigit, digit); eval_subtract(t, digit); if((cdigit == 5) && (t.compare(ui_type(0)) == 0)) { // Bankers rounding: if((*result.rbegin() - '0') & 1) { round_string_up_at(result, result.size() - 1, expon); } } else if(cdigit >= 5) { round_string_up_at(result, result.size() - 1, expon); } } } while((result.size() > digits) && result.size()) { // We may get here as a result of rounding... if(result.size() > 1) result.erase(result.size() - 1); else { if(expon > 0) --expon; // so we put less padding in the result. else ++expon; ++digits; } } BOOST_ASSERT(org_digits >= 0); if(isneg) result.insert(static_cast<std::string::size_type>(0), 1, '-'); format_float_string(result, expon, org_digits, f, iszero); return result; }
inline int eval_fpclassify(const debug_adaptor<Backend>& arg) { using default_ops::eval_fpclassify; return eval_fpclassify(arg.value()); }