UVA 11809 Floating-point numbers

来源:互联网 发布:php websocket server 编辑:程序博客网 时间:2024/06/11 08:49

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 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
Sample Input
5.699141892149156e76
9.205357638345294e18
0e0
Sample Output
5 8
8 6

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>using namespace std;double M[11][33];   //存储对应尾数long long E[11][33];    //存储对应指数const double min_differ=1e-5;   //设置误差void solve(double m,long long e)//把匹配过程放到函数中,匹配后可以及时中断{    int i,j;    for(i=0;i<=9;i++)        for(j=1;j<=30;j++)        if(e==E[i][j]&&fabs(m-M[i][j])<min_differ)        //当指数等于表中所给并且尾数差值绝对值小于误差时即匹配,输出答案,跳出函数        {            cout<<i<<" "<<j<<endl;            return;        }}int main(){    int i,j;    double m,t;    long long e;    char str[22];    for(i=0;i<=9;i++)        for(j=1;j<=30;j++)        {            e=(1<<j)-1;     //根据上述公式打表,每一个对应位的尾数和指数分别存储            m=1-1.0/(1<<(i+1));            t=log10(m)+e*log10(2);  //10^t=m*2^e,取反函数得此式            E[i][j]=t/1;//取整            M[i][j]=pow(10,t-E[i][j]);//相当于取得科学计数法的前面的浮点数        }        while(cin>>str,strcmp(str,"0e0"))        {            *(strchr(str,'e'))=' ';     //将字符串中的e替换成空格            sscanf(str,"%lf %lld",&m,&e);   //运用sscanf巧妙分割尾数和指数            solve(m,e);        }        return 0;}
原创粉丝点击