diff --git a/section_1/float/float_dump.cpp b/section_1/float/float_dump.cpp index 64d65cf..3672b14 100644 --- a/section_1/float/float_dump.cpp +++ b/section_1/float/float_dump.cpp @@ -12,6 +12,9 @@ using namespace std; +const int BIASD = 1023; +const int BIASF = 127; + const int FRAC_SIZD = 52; const int FRAC_SIZF = 23; @@ -47,8 +50,8 @@ double DeBinary(bool is_double, unsigned long frac) { int bits = (is_double ? FRAC_SIZD : FRAC_SIZF); for (int i = 0; i < bits; i++) { - if (frac & ((unsigned long)1 << (bits - 1 - i))) { - f += 1.0f / double(1 << (i + 1)); + if (frac & ((unsigned long)(1) << (bits - 1 - i))) { + f += 1.0f / double((unsigned long)(1) << (i + 1)); } } return f; @@ -57,8 +60,8 @@ double DeBinary(bool is_double, unsigned long frac) { template string MakeEquation(T & u, int bias) { stringstream ss; - bool is_double = (bias == 1023); - ss << (u.sign ? "-" : "") << 1.0 + DeBinary(is_double, u.frac) << " x 2^" << (u.expo - bias); + bool is_double = (bias == BIASD); + ss << (u.sign ? "-" : "") << dec << setprecision(11) << 1.0 + DeBinary(is_double, u.frac) << " x 2^" << (u.expo - bias); return ss.str(); } @@ -94,8 +97,8 @@ int main(int argc, char ** argv) { cout << endl; cout << setw(fore_space) << "De-biased (dec):"; - cout << setw(field_space) << dec << d.D.expo - 1023; - cout << setw(field_space) << dec << f.F.expo - 127; + cout << setw(field_space) << dec << d.D.expo - BIASD; + cout << setw(field_space) << dec << f.F.expo - BIASF; cout << endl; cout << setw(fore_space) << "Fraction (hex):"; @@ -134,8 +137,8 @@ int main(int argc, char ** argv) { cout << endl; cout << setw(fore_space) << "Equation:"; - cout << setw(field_space) << dec << MakeEquation(d.D, 1023); - cout << setw(field_space) << dec << MakeEquation(f.F, 127); + cout << setw(field_space) << dec << MakeEquation(d.D, BIASD); + cout << setw(field_space) << dec << MakeEquation(f.F, BIASF); cout << endl; return 0;