## 【UVA – 11809】Floating-Point Numbers【紫书】

【UVA – 11809】Floating-Point Numbers【紫书】

Solution：

$$(1 – \frac{1}{2^{m + 1}}) \times 2^{2^e – 1} = a \times 10 ^ b$$

m和e确定是整数，同时注意到m的取值范围更小一些，所以我们可以枚举m来判断等式是否成立。

$$log_{10}(2^{m + 1} – 1) – (m + 1)log_{10}2 + (2^e – 1)log_{10}2 = log_{10}a \times b$$.

#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>

using namespace std;

const double EPS = 1e-6;
const int LIM = 256;

int fastPow(int x, int p) {
int ret = 1;
while (p) {
if (p & 1) ret *= x;
x *= x;
p >>= 1;
}
return ret;
}

int main(void) {
ios::sync_with_stdio(false);
const double lg2 = log10(2);
string str;
while (cin >> str && str != "0e0") {
double a;
int b;
str[str.find('e')] = ' ';
stringstream ss;
ss << str;
ss >> a >> b;
double right = log10(a) + b;
for (int m = 1; m <= 10; m++) {
int e = round(log10((right + m * lg2 - log10(fastPow(2, m) - 1)) / lg2 + 1) / lg2);
if (fabs(((1 << e) - 1) * lg2 + log10(fastPow(2, m) - 1) - m * lg2 - right) <= EPS) {
cout << m - 1 << " " << e << endl;
break;
}
}
}
return 0;
}