Floating-point numbers arerepresented differently in computers than integers. That is why a 32-bitfloating-point number can represent values in the magnitude of 1038 whilea 32-bit integer can only represent values as high as 232.
Although thereare variations in the ways floating-point numbers are stored in Computers, inthis problem we will assume that floating-point numbers are stored in thefollowing way:
Floating-pointnumbers have two parts mantissa and exponent. M-bits are allotted for mantissa and E bits are allotted for exponent. There is also one bit thatdenotes the sign of number (If this bit is 0 then the number is positive and ifit is 1 then the number is negative) and another bit that denotes the sign ofexponent (If this bit is 0 then exponent is positive otherwise negative). Thevalue of mantissa and exponent together make the value of the floating-pointnumber. If the value of mantissa is m thenit maintains the constraints 1. The left most digit ofmantissa must always be 1 to maintain the constraint 1. So this bit is not stored asit is always 1. So the bits in mantissa actually denote the digits at the rightside of decimal point of a binary number (Excluding the digit just to the rightof decimal point)
In the figure above we can see a floating-point number where M = 8 and E = 6. The largest value this floating-point number can representis (in binary) 0.1111111112×21111112. The decimal equivalent to this number is: 0.998046875 × 263 = 920535763834529382410. Giventhe maximum possible value represented by a certain floating point type, youwill have to find how many bits are allotted for mantissa (M) and how many bits are allotted for exponent (E) in that certain type.
Input
The input filecontains around 300 line of input. Each line contains a floating-point number F that denotes the maximum value thatcan be represented by a certain floating-point type. The floating point numberis expressed in decimal exponent format. So a number AeB actually denotes the value A×10B. A line containing ‘0e0’ terminates input. The value of A will satisfy the constraint 0 < A < 10 and will have exactly 15digits after the decimal point.
Output
For each line ofinput produce one line of output. This line contains the value of M and E. You can assume that each of the inputs (except the last one) hasa possible and unique solution. You can also assume that inputs will be suchthat the value of M and E will follow the constraints: 9 ≥ M ≥ 0 and 30 ≥ E ≥ 1. Also there is no need to assume that (M + E + 2) will be amultiple of 8.
Sample Input
5.699141892149156e76
9.205357638345294e18
0e0
Sample Output
5 8
8 6
【思路】
题目给定一个最大的浮点数,用AeB表示(科学计数法),求需要的尾数尾数M和阶数尾数E。依据十进制和二进制两种表示方式的等价关系,取对数打表就好了。然后利用流读入查表。
【代码】
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;
const int MAXM = 15, MAXE = 35;
const double EPS = 1e-5;
double res[MAXM][MAXE];
int main()
{
for (int i = 0; i <= 9; i++)
for (int j = 1; j <= 30; j++) {
double m = 1.0 - pow(2, -(i + 1)), e = pow(2, j) - 1;
res[i][j] = log(m) + e * log(2);
}
string buff;
stringstream stream;
while (cin >> buff && buff != "0e0") {
for (int i = 0; i < buff.length(); i++)
if (buff[i] == 'e') buff[i] = ' ';
stream.str("");stream.clear();
stream << buff;
double a;
int b;
stream >> a >> b;
for (int i = 0; i <= 9; i++)
for (int j = 1; j <= 30; j++) {
double ans = log(a) + b * log(10);
if (-EPS < ans - res[i][j] && ans - res[i][j] < EPS) {
cout << i << " " << j << endl;
break;
}
}
}
return 0;
}