资源简介
浮点数格式,有exponent和mantissa,使用C语言实现浮点格式数据加减乘除程序。
不要全部照抄,老师或助教一看代码就会发现的,hhhh
最好可以优化一下
![](http://www.nz998.com/pic/71377.jpg)
代码片段和文件信息
#include
#include
#include
#define Bias 15
#define Max pow(2 16)
#define Min pow(2 -14)
//使用struct存放浮点格式数据(以半精度浮点数为例)
struct Float
{
int S;
int expo[5];
int mant[10];
};
//将输入的浮点数转化为浮点格式数据
void getFloat_num(float a struct Float* pFl)
{
float b = a;
int expo = 0;
int i = 0;
if (a < 0)
{
pFl -> S = 1;
b = -b;
}
else
pFl -> S = 0;
while (b < 1)
{
b *= 2;
--expo;
}
while (b >= 2)
{
b /= 2;
++expo;
}
i = 5;
expo += Bias;
while (i > 0)
{
if (expo > 0) {
pFl -> expo[i - 1] = expo % 2;
expo /= 2;
} else {
pFl -> expo[i - 1] = 0;
}
--i;
}
i = 0;
--b;
while (i < 10)
{
b *= 2;
if (b >= 1)
{
--b;
pFl -> mant[i] = 1;
} else {
pFl -> mant[i] = 0;
}
++i;
}
}
//获取浮点格式数据的指数
int getFloat_exp(struct Float f)
{
int i = 0;
int ans = 0;
while (i < 5)
{
ans = ans * 2 + f.expo[i];
++i;
}
ans -= Bias;
return ans;
}
//获取浮点格式数据的小数
float getFloat_fraction(struct Float a)
{
int i = 0;
float ans = 1;
while (i < 10)
{
ans = ans * 2 + a.mant[i];
++i;
}
return ans;
}
//以浮点格式数据输出
void printFloat(struct Float f)
{
char fl[19];
int j = 0 i = 0;
fl[0] = f.S + ‘0‘;
fl[1] = ‘_‘;
j = 2;
while (j < 17)
{
fl[j] = f.expo[j - 2] + ‘0‘;
++j;
}
fl[7] = ‘_‘;
i = 8;
while (i < 18)
{
fl[i] = f.mant[i - 8] + ‘0‘;
++i;
}
fl[18] = ‘\0‘;
printf(“%s\n“ fl);
}
//浮点结构加法
void F_add(struct Float a struct Float b)
{
struct Float answer;
float aFrac bFrac;
int aExp bExp;
int i = 0 j = 0;
float answer_fl;
aFrac = getFloat_fraction(a);
bFrac = getFloat_fraction(b);
aExp = getFloat_exp(a);
bExp = getFloat_exp(b);
answer_fl = pow(-1 a.S)*aFrac * pow(2 aExp - 10) + pow(-1 b.S)*bFrac * pow(2 bExp - 10);
if(fabs(answer_fl)>=Max | fabs(answer_fl) {
printf(“Error!\n“);
}
else
{
getFloat_num(answer_fl &answer);
printf(“%f\n“ answer_fl);
printFloat(answer);
}
}
//浮点结构减法
void F_sub(struct Float a struct Float b)
{
b.S ^= 1;
F_add(a b);
}
//浮点结构乘法
void F_mul(struct Float a struct Float b)
{
struct Float answer;
float aFrac bFrac;
int aExp bExp i;
float answer_fl;
aFrac = getFloat_fraction(a);
bFrac = getFloat_fraction(b);
aExp = getFloat_exp(a);
bExp = getFloat_exp(b);
i = 0;
while (i < abs(aExp - 10))
{
if (aExp > 10)
aFrac *= 2;
else aFrac /= 2;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 5293 2019-03-28 15:13 浮点格式数据计算程序\T1.c
文件 161550 2019-03-28 15:39 浮点格式数据计算程序\T1.exe
文件 777260 2019-03-28 16:04 浮点格式数据计算程序\浮点格式数据计算程序.docx
- 上一篇:c 编译器 masm32 汇编 可自举
- 下一篇:物流管理系统V4.8
评论
共有 条评论