Floating-point numbers are represented differently in computers than integers. That is why a 32-bit floating-point number can represent values in the magnitude of 1038 while a 32-bit integer can only represent values as high as 2^32.
Although there are variations in the ways floating-point numbers are stored in Computers, in this problem we will assume that floating-point numbers are stored in the following way:
Floating-point numbers have two parts mantissa and exponent. M-bits are allotted for mantissa and E bits are allotted for exponent. There is also one bit that denotes the sign of number (If this bit is 0 then the number is positive and if it is 1 then the number is negative) and another bit that denotes the sign of exponent (If this bit is 0 then exponent is positive otherwise negative). The value of mantissa and exponent together make the value of the floating-point number. If the value of mantissa is m then it maintains the constraints 1 2 ≤ m < 1. The left most digit of mantissa must always be 1 to maintain the constraint 1 2 ≤ m < 1. So this bit is not stored as it is always 1. So the bits in mantissa actually denote the digits at the right side of decimal point of a binary number (Excluding the digit just to the right of 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 represent is (in binary) 0.1111111112 ×2 1111112 . The decimal equivalent to this number is: 0.998046875 × 2 63 = 920535763834529382410. Given the maximum possible value represented by a certain floating point type, you will 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 file contains around 300 line of input. Each line contains a floating-point number F that denotes the maximum value that can be represented by a certain floating-point type. The floating point number is 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 15 digits after the decimal point.
Output
For each line of input 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) has a possible and unique solution. You can also assume that inputs will be such that 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 a multiple of 8.
Sample Input
5.699141892149156e76
9.205357638345294e18
0e0
Sample Output
5
8
8
6
题目类型:简单数学
算法分析:由于尾数和阶数的组合数较小(只有300组),所以考虑打表求解。使用m[i]=1-(1/2)^(i+1)表示位数为i时的尾数值,e[j]=2^j-1表示位数为j时的阶数值,v[i][j]=m[i]*2^e[j]表示尾数为m[i],阶数为e[j]的浮点数值。由于v[i][j]比较大,一个技巧是对其取10为底的对数,最后每次读入输入后对其取对数在v[i][j]中查找(i, j),注意由于存在对数运算(会丢失精度),所以两数比较时的EPS要取的小一些
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
/************************************************** filename :s.cpp author :maksyuki created time :2017/12/3 17:52:29 last modified :2017/12/3 18:19:58 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-5; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; double m[66], v[66][66], va; int e[66], vb; char s[66]; void pre() { double tmpa = 0.5; for(int i = 0; i <= 9; i++, tmpa /= 2.0) m[i] = 1.0 - tmpa; for(int i = 1, tmpb = 2; i <= 30; i++, tmpb *= 2) e[i] = tmpb - 1; for(int i = 0; i <= 9; i++) for(int j = 1; j <= 30; j++) v[i][j] = log10(m[i]) + e[j] * log10(2.0); } int main() { #ifdef LOCAL CFF; //CFO; #endif pre(); while(scanf(" %s", s) != EOF) { if(s[0] == '0' && s[2] == '0') break; for(int i = 0; s[i]; i++) if(s[i] == 'e') { s[i] = ' '; break; } sscanf(s, " %lf %d", &va, &vb); double tmp = log10(va) + vb; //printf("%.15lf\n", tmp); //printf("%.15lf\n", v[0][26]); bool is_find = false; int m1 = -1, e1 = -1; for(int i = 0; i <= 9 && !is_find; i++) for(int j = 1; j <= 30 && !is_find; j++) if(fabs(tmp - v[i][j]) < EPS) { is_find = true; m1 = i, e1 = j; break; } printf("%d %d\n", m1, e1); } return 0; } |