Convert C++ double to DEC double -
Convert C++ double to DEC double -
i want able take user given double , write out in dec 64 dpfp format (http://www.wsmr.army.mil/rccsite/documents/106%20previous%20versions/106-07/appendixo.pdf). having problem getting line correctly, have experience or have written conversion functions dec types?
this seems pretty straight forward, allow me take shot @ it. note don't have way of testing correctness.
std::vector<unsigned char> todec64float(double d) { uint64_t dec_bits = 0ull; if (d != 0.0) { assert(sizeof(double) == sizeof(uint64_t)); uint64_t bits = *reinterpret_cast<uint64_t*>(&d); uint64_t fraction = bits & 0x000fffffffffffffull; int exp = (int)((bits >> 52) & 0x7ff) - 1023; bool sign = (bool)(bits & 0x8000000000000000ull); // convert individual values new format fraction <<= 3; exp += 1 + 128; if (exp > 255) throw std::overflow_error("overflow"); if (exp < 0 || (exp == 0 && fraction != 0)) throw std::underflow_error("underflow"); dec_bits = (uint64_t)sign << 63 | (uint64_t)exp << 55 | fraction; } std::vector<unsigned char> result; (int = 0; < 64; i+=8) result.push_back((unsigned char)((dec_bits >> i) & 0xff)); homecoming result; } c++
Comments
Post a Comment