uva-11809

Problem background

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  while a 32-bit integer can only represent values as high as .

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 members 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: and .  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

My Solution

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define maxnum 100

using namespace std;

int findindex(char * str, char sep, int len){
    for(int i = 0; i < len; i++)
        if(str[i] == sep) return i;
    return -1;
}

int main()
{
    char input[maxnum];
    while(scanf("%s", input)){
        if(input[0] == '0' && input[1] == 'e' && input[2] == '0') break;
        int len = strlen(input);
        int indexofe = findindex(input,'e', len);

        //generate A and B
        double A = input[0] - '0';
        int B = 0;
        for(int i = 2; i < indexofe; i++)
            A += (input[i] - '0') * pow(0.1, i - 1);
        for(int i = indexofe + 1; i < len; i++)
            B += (input[i] - '0') * pow(10, len - i - 1);

        bool tag = false;
        for(int m = 0; m <=9; m++){
            for(int e = 1; e <= 30; e++){
                //Get m and e by the given A and B. I will explain the equation later.
                if(fabs((double)log(A) + B * log(10) - log(1 - pow(0.5, m + 1)) - (pow(2, e) - 1) * log(2)) < 0.00001){ //0.0001 is the threshold
                    printf("%d %d\n", m, e);
                    tag = true;
                    break;
                }
            }
            if(tag) break;
        }
    }
    return 0;
}

Analysis

According to the description, we know that

One transformation of the equation above is

So the condition when M and E meet the requirement is



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值