UVA 11809 Floating-Point Numbers(暴力)

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;
}


xilinx floating-point ip是指xilinx公司开发的用于浮点数数据处理的IP核。它可以实现浮点数的转换、 #### 引用[.reference_title] - *1* [十三、Xilinx floating-point IP 使用说明](https://blog.csdn.net/weixin_41838250/article/details/125874095)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [【教程】Xilinx FPGA里面的AXI DMA IP核的简单用法(以读写Floating-point IP核数据为例)](https://blog.csdn.net/ZLK1214/article/details/113144766)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [fp23fftk:最新的Xilinx FPGA的浮点正向反向快速傅立叶变换(FFT)IP内核(源语言-VHDL)](https://download.csdn.net/download/weixin_42102713/14990566)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值