資源簡(jiǎn)介
浮點(diǎn)數(shù)格式,有exponent和mantissa,使用C語(yǔ)言實(shí)現(xiàn)浮點(diǎn)格式數(shù)據(jù)加減乘除程序。
不要全部照抄,老師或助教一看代碼就會(huì)發(fā)現(xiàn)的,hhhh
最好可以?xún)?yōu)化一下

代碼片段和文件信息
#include?
#include
#include?
#define?Bias?15
#define?Max?pow(2?16)
#define?Min?pow(2?-14)
//使用struct存放浮點(diǎn)格式數(shù)據(jù)(以半精度浮點(diǎn)數(shù)為例)?
struct?Float?
{
int?S;
????int?expo[5];
????int?mant[10];
};
//將輸入的浮點(diǎn)數(shù)轉(zhuǎn)化為浮點(diǎn)格式數(shù)據(jù)?
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;
????}
}
//獲取浮點(diǎn)格式數(shù)據(jù)的指數(shù)?
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;
}
//獲取浮點(diǎn)格式數(shù)據(jù)的小數(shù)
float?getFloat_fraction(struct?Float?a)
{
????int?i?=?0;
????float?ans?=?1;
????while?(i?10)
????{
????????ans?=?ans?*?2?+?a.mant[i];
????????++i;
????}
????return?ans;
}
//以浮點(diǎn)格式數(shù)據(jù)輸出?
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);
}
//浮點(diǎn)結(jié)構(gòu)加法
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);
}
}
//浮點(diǎn)結(jié)構(gòu)減法?
void?F_sub(struct?Float?a?struct?Float?b)
{
????b.S?^=?1;
????F_add(a?b);
}
//浮點(diǎn)結(jié)構(gòu)乘法?
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?????{
????????if?(aExp?>?10)
????????aFrac?*=?2;
????????else?aFrac?/=?2;
??????
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????文件????????5293??2019-03-28?15:13??浮點(diǎn)格式數(shù)據(jù)計(jì)算程序\T1.c
?????文件??????161550??2019-03-28?15:39??浮點(diǎn)格式數(shù)據(jù)計(jì)算程序\T1.exe
?????文件??????777260??2019-03-28?16:04??浮點(diǎn)格式數(shù)據(jù)計(jì)算程序\浮點(diǎn)格式數(shù)據(jù)計(jì)算程序.docx
- 上一篇:c 編譯器 masm32 匯編 可自舉
- 下一篇:物流管理系統(tǒng)V4.8
評(píng)論
共有 條評(píng)論