UVA 11809 Floating-Point Numbers(暴力)

本文探讨了如何根据浮点数能表示的最大值来确定其尾数位数(M)和指数位数(E)的方法。通过解析输入的浮点数最大值,采用科学记数法转换并对比预设表格,实现了快速准确地找出浮点数格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


### MATLAB 输入参数必须可转换为浮点数的解决方案 当遇到 `input arguments must be convertible to floating-point numbers` 错误时,通常是因为传递给函数的参数不是有效的数值类型。为了修复此问题,可以采取以下几种方法: #### 1. 检查数据类型 确保所有输入变量都是数值型或能够被转换成双精度浮点数 (double) 类型的数据。可以通过 `class()` 函数来验证变量的具体类别。 ```matlab if ~isnumeric(x) || ~isa(x, 'double') error('Input argument ''x'' is not a valid numeric type.'); end ``` 如果发现某些变量并非预期中的数值形式,则应先将其转换再继续操作[^1]。 #### 2. 使用 str2double 进行字符串到数字的转换 对于由字符组成的数组或其他非标准格式存储起来的实际数值,在调用目标函数之前应当尝试利用内置命令如 `str2double()` 来完成必要的转型工作。 ```matlab y = str2double(str); % 如果 y 返回 NaN 表明无法成功解析该串成为合法实数 if isnan(y) warning('Conversion failed'); else disp(['Converted value:', num2str(y)]); end ``` #### 3. 处理缺失值与异常情况 有时也会因为存在未定义元素(NaN)、无穷大(Inf)等原因而触发上述报错提示;因此建议提前过滤掉这些特殊情形下的记录项后再执行后续计算逻辑。 ```matlab data(isnan(data)) = 0; % 将所有的 NaN 替换成零 data(~isfinite(data)) = 0;% 或者处理其他类型的非法数值 ``` 通过以上措施一般能有效规避因不当传参所引发的相关运行期错误消息显示出来[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值