c++ - How to get the coefficient from a std::decimal? -
background
i want write is_even( decimal::decimal64 d ) function returns true if least-significant digit even.
unfortunately, can't seem find methods extract coefficient decimal64.
code
#include <iostream> #include <decimal/decimal> using namespace std; static bool is_even( decimal::decimal64 d ) { return true; // fix - want to: return coefficient(d)%2==0; } int main() { auto d1 = decimal::make_decimal64( 60817ull, -4 ); // not auto d2 = decimal::make_decimal64( 60816ull, -4 ); // cout << decimal64_to_float( d1 ) << " " << is_even( d1 ) << endl; cout << decimal64_to_float( d2 ) << " " << is_even( d2 ) << endl; return 0; }
it's little odd there's no provided function recover coefficient of decimal; can multiply 10 raised negative exponent:
bool is_even(decimal::decimal64 d) { auto q = quantexpd64(d); auto coeff = static_cast<long long>(d * decimal::make_decimal64(1, -q)); return coeff % 2 == 0; } assert(!is_even(decimal::make_decimal64(60817ull, -4))); assert(!is_even(decimal::make_decimal64(60816ull, -4)));
Comments
Post a Comment