我写了这个程序:
#include <stdio.h>
#include <stdint.h>
/*
* See also : http://class.ece.iastate.edu/arun/CprE281_F05/ieee754/ie5.html
*/
union FloatingPointSinglePrecisionIEEE754 {
struct {
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} raw;
float f;
} fnumber;
union FloatingPointDoublePrecisionIEEE754 {
struct {
uint64_t mantissa : 52;
unsigned int exponent : 11;
unsigned int sign : 1;
} raw;
double d;
} dnumber;
int main() {
printf("sign\texponent\tmantissa\n");
fnumber.f = -6.8;
printf("%x\t%x\t\t%x\n",
fnumber.raw.sign,
fnumber.raw.exponent,
fnumber.raw.mantissa);
dnumber.d = -6.8;
printf("%x\t%x\t\t%x\n",
dnumber.raw.sign,
dnumber.raw.exponent,
dnumber.raw.mantissa);
return 0;
}
对于单精度,我得到了真正的输出,但对于双精度,程序失败了。任何人都可以帮助我如何编写双精度转换器?