UVA - 11809 Floating-Point Numbers

来源:互联网 发布:买房子哪个网好 知乎 编辑:程序博客网 时间:2024/04/30 16:26


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 232.

 

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 . The left most digit of mantissa must always be 1 to maintain the constraint . 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) . The decimal equivalent to this number is: . 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 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

Sample Output

5.699141892149156e76

9.205357638345294e18

0e0

5 8

8 6

 




[cpp] view plaincopyprint?
  1. #include <bits/stdc++.h>  
  2. #define DET 1e-4  
  3. using namespace std;  
  4.   
  5. double A[16][64];  
  6. long long int B[16][64];  
  7.   
  8. void init()  
  9. {  
  10.     for(int i = 0; i <= 9; i++)  
  11.     {  
  12.         for(int j = 1; j <= 30; j++)  
  13.         {  
  14.             double E = 1 - pow(2, -(i+1));  
  15.             long long int M = pow(2, j) - 1;  
  16.             double X = log10(E) + M*log10(2);  
  17.             long long int b = floor(X);  
  18.             double a = pow(10.0, X-b);  
  19.             A[i][j] = a;  
  20.             B[i][j] = b;  
  21.         }  
  22.     }  
  23. }  
  24.   
  25.   
  26. int main()  
  27. {  
  28.     init();  
  29.     char in[32];  
  30.     while(cin >> in)  
  31.     {  
  32.         if(strcmp(in, "0e0")==0)  
  33.             break;  
  34.         double a;  
  35.         long long int b;  
  36.         *(strchr(in, 'e')) = ' ';  
  37.         sscanf(in, "%lf %lld", &a, &b);  
  38.         for(int i = 0; i <= 9; i++)  
  39.         {  
  40.             for(int j = 1; j <= 30; j++)  
  41.             {  
  42.                 if(fabs(A[i][j]-a)<DET && B[i][j]==b)  
  43.                     printf("%d %d\n", i, j);  
  44.             }  
  45.         }  
  46.     }  
  47.     return 0;  
  48. }
0 0
原创粉丝点击