C 将IEEE-754浮点编码转换为浮点值
如何将IEEE-754基本32位浮点编码(如0x466F9100)转换为表示的值15332.25?C 将IEEE-754浮点编码转换为浮点值,c,C,如何将IEEE-754基本32位浮点编码(如0x466F9100)转换为表示的值15332.25?#include #include <inttypes.h> #include <math.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> /* Interpret a string containing a numeral for an integer va
#include
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
/* Interpret a string containing a numeral for an integer value as an encoding
of an IEEE-754 basic 32-bit binary floating-point value.
*/
static float Interpret(const char *s)
{
// Interpret the string as an integer numeral. Errors are not handled.
uint32_t x = strtoumax(s, NULL, 16);
// Separate the sign (1 bit), exponent (8), and significand (23) fields.
uint32_t sign = x>>31;
uint32_t exponent = (x>>23) & 0xff;
uint32_t significand = x & 0x7fffff;
// Interpret the sign field.
float Sign = sign ? -1 : +1;
// Create an object to hold the magnitude (or NaN).
float Magnitude;
// How we form the magnitude depends on the exponent field.
switch (exponent)
{
// If the exponent field is zero, the number is zero or subnormal.
case 0:
{
// In a zero or subnormal number, the significand starts with 0.
float Significand = 0 + significand * 0x1p-23f;
// In a zero or subnormal number, the exponent has its minimum value.
int Exponent = 1 - 127;
// Form the magnitude from the significand and exponent.
Magnitude = ldexpf(Significand, Exponent);
break;
}
// If the exponent field is all ones, the datum is infinity or a NaN.
case 0x7fffff:
{
/* If the significand field is zero, the datum is infinity.
Otherwise it is a NaN. Note that different NaN payloads and
types (quiet or signaling) are not supported here. Standard C
does not provide good support for these.
*/
Magnitude = significand == 0 ? INFINITY : NAN;
break;
}
// Otherwise, the number is normal.
default:
{
// In a normal number, the significand starts with 1.
float Significand = 1 + significand * 0x1p-23f;
// In a normal number, the exponent is biased by 127.
int Exponent = (int) exponent - 127;
// Form the magnitude from the significand and exponent.
Magnitude = ldexpf(Significand, Exponent);
}
}
// Combine the sign and magnitude and return the result.
return copysignf(Magnitude, Sign);
}
int main(void)
{
printf("%.99g\n", Interpret("0x466F9100"));
}
#包括
#包括
#包括
#包括
/*将包含整数数字的字符串解释为编码
IEEE-754基本32位二进制浮点值。
*/
静态浮点解释(常量字符*s)
{
//将字符串解释为整数。不会处理错误。
uint32_t x=strtumax(s,NULL,16);
//分隔符号(1位)、指数(8)和有效位(23)字段。
uint32\u t符号=x>>31;
uint32_t指数=(x>>23)&0xff;
uint32\u t有效位=x&0x7fffff;
//解释符号字段。
浮动符号=符号?-1:+1;
//创建一个对象以保持幅值(或NaN)。
浮动幅度;
//我们如何形成震级取决于指数场。
开关(指数)
{
//如果指数字段为零,则数字为零或低于正常值。
案例0:
{
//在零或低于正常值的数字中,有效位以0开头。
浮点有效位=0+有效位*0x1p-23f;
//在零或次正常数中,指数有其最小值。
int指数=1-127;
//根据有效位和指数形成震级。
幅值=ldexpf(有效位,指数);
打破
}
//如果指数字段为“全1”,则基准为无穷大或NaN。
案例0x7fffff:
{
/*如果有效位字段为零,则基准为无穷大。
否则它是一个NaN。请注意,不同的NaN有效负载和
此处不支持类型(安静或信号)。标准C
没有为这些提供良好的支持。
*/
幅值=有效位==0?无穷大:NAN;
打破
}
//否则,数字是正常的。
违约:
{
//在正常数字中,有效位以1开头。
浮点有效位=1+有效位*0x1p-23f;
//在正常数中,指数偏移127。
int指数=(int)指数-127;
//根据有效位和指数形成震级。
幅值=ldexpf(有效位,指数);
}
}
//将符号和幅值组合起来并返回结果。
返回copysignf(大小、符号);
}
内部主(空)
{
printf(“%.99g\n”,解释(“0x466F9100”);
}
谢谢,太好了!