以下代码似乎从单精度转换为双精度。我将把它作为练习留给读者来实现缩小版本。不过,这应该可以帮助您入门。最难的部分是在有效数字中正确地获取位位置。我包括一些评论,其中包括正在发生的事情。
double
extend_float(float f)
{
unsigned char flt_bits[sizeof(float)];
unsigned char dbl_bits[sizeof(double)] = {0};
unsigned char sign_bit;
unsigned char exponent;
unsigned int significand;
double out;
memcpy(&flt_bits[0], &f, sizeof(flt_bits));
/// printf("---------------------------------------\n");
/// printf("float = %f\n", f);
#if LITTLE_ENDIAN
reverse_bytes(flt_bits, sizeof(flt_bits));
#endif
/// dump_bits(&flt_bits[0], sizeof(flt_bits));
/* IEEE 754 single precision
* 1 sign bit flt_bits[0] & 0x80
* 8 exponent bits flt_bits[0] & 0x7F | flt_bits[1] & 0x80
* 23 fractional bits flt_bits[1] & 0x7F | flt_bits[2] & 0xFF |
* flt_bits[3] & 0xFF
*
* E = 0 & F = 0 -> +/- zero
* E = 0 & F != 0 -> sub-normal
* E = 127 & F = 0 -> +/- INF
* E = 127 & F != 0 -> NaN
*/
sign_bit = (flt_bits[0] & 0x80) >> 7;
exponent = ((flt_bits[0] & 0x7F) << 1) | ((flt_bits[1] & 0x80) >> 7);
significand = (((flt_bits[1] & 0x7F) << 16) |
(flt_bits[2] << 8) |
(flt_bits[3]));
/* IEEE 754 double precision
* 1 sign bit dbl_bits[0] & 0x80
* 11 exponent bits dbl_bits[0] & 0x7F | dbl_bits[1] & 0xF0
* 52 fractional bits dbl_bits[1] & 0x0F | dbl_bits[2] & 0xFF
* dbl_bits[3] & 0xFF | dbl_bits[4] & 0xFF
* dbl_bits[5] & 0xFF | dbl_bits[6] & 0xFF
* dbl_bits[7] & 0xFF
*
* E = 0 & F = 0 -> +/- zero
* E = 0 & F != 0 -> sub-normal
* E = x7FF & F = 0 -> +/- INF
* E = x7FF & F != 0 -> NaN
*/
dbl_bits[0] = flt_bits[0] & 0x80; /* pass the sign bit along */
if (exponent == 0) {
if (significand == 0) { /* +/- zero */
/* nothing left to do for the outgoing double */
} else { /* sub-normal number */
/* not sure ... pass on the significand?? */
}
} else if (exponent == 0xFF) { /* +/-INF and NaN */
dbl_bits[0] |= 0x7F;
dbl_bits[1] = 0xF0;
/* pass on the significand */
} else { /* normal number */
signed int int_exp = exponent;
int_exp -= 127; /* IEEE754 single precision exponent bias */
int_exp += 1023; /* IEEE754 double precision exponent bias */
dbl_bits[0] |= (int_exp & 0x7F0) >> 4; /* 7 bits */
dbl_bits[1] = (int_exp & 0x00F) << 4; /* 4 bits */
}
if (significand != 0) {
/* pass on the significand most-significant-bit first */
dbl_bits[1] |= (flt_bits[1] & 0x78) >> 3; /* 4 bits */
dbl_bits[2] = (((flt_bits[1] & 0x07) << 5) | /* 3 bits */
((flt_bits[2] & 0xF8) >> 3)); /* 5 bits */
dbl_bits[3] = (((flt_bits[2] & 0x07) << 5) | /* 3 bits */
((flt_bits[3] & 0xF8) >> 3)); /* 5 bits */
dbl_bits[4] = ((flt_bits[3] & 0x07) << 5); /* 3 bits */
}
///dump_bits(&dbl_bits[0], sizeof(dbl_bits));
#if LITTLE_ENDIAN
reverse_bytes(&dbl_bits[0], sizeof(dbl_bits));
#endif
memcpy(&out, &dbl_bits[0], sizeof(out));
return out;
}
我留下了一些printf
行,但在 C++ 风格的注释中注释掉了。您必须为 、 和 提供适当reverse_bytes
的LITTLE_ENDIAN
定义dump_bits
。毕竟我不想破坏你所有的乐趣。关于单精度和双精度数字的维基百科条目非常好。
如果你要经常修改浮点数,你应该阅读David Goldberg 的
“What Every Computer Scientist Should Know About Floating Point Arithmetic”和 Steele 和 White 的“How to Print Floating Point Numbers Accurately”。在了解浮点数的工作原理时,它们是两篇信息量最大的文章。