UVA 11809 Floating-point numbers

本文探讨了计算机中浮点数的表示方式,通过给定的最大可表示浮点数值来确定用于尾数和指数的位数。介绍了如何通过数学运算找到匹配的位数配置,并提供了一个实现该功能的C++程序示例。

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

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;
}
内容概要:本文档详细介绍了基于MATLAB实现多目标差分进化(MODE)算法进行无人机三维路径规划的项目实例。项目旨在提升无人机在复杂三维环境中路径规划的精度、实时性、多目标协调处理能力、障碍物避让能力和路径平滑性。通过引入多目标差分进化算法,项目解决了传统路径规划算法在动态环境和多目标优化中的不足,实现了路径长度、飞行安全距离、能耗等多个目标的协调优化。文档涵盖了环境建模、路径编码、多目标优化策略、障碍物检测与避让、路径平滑处理等关键技术模块,并提供了部分MATLAB代码示例。 适合人群:具备一定编程基础,对无人机路径规划和多目标优化算法感兴趣的科研人员、工程师和研究生。 使用场景及目标:①适用于无人机在军事侦察、环境监测、灾害救援、物流运输、城市管理等领域的三维路径规划;②通过多目标差分进化算法,优化路径长度、飞行安全距离、能耗等多目标,提升无人机任务执行效率和安全性;③解决动态环境变化、实时路径调整和复杂障碍物避让等问题。 其他说明:项目采用模块化设计,便于集成不同的优化目标和动态环境因素,支持后续算法升级与功能扩展。通过系统实现和仿真实验验证,项目不仅提升了理论研究的实用价值,还为无人机智能自主飞行提供了技术基础。文档提供了详细的代码示例,有助于读者深入理解和实践该项目。
### 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、付费专栏及课程。

余额充值