46 c++ double serialization portability ieee-754
C++标准没有讨论float和double类型的底层布局,只讨论它们应该表示的值的范围.(对于签名类型也是如此,这是两个恭维还是别的)
我的问题是:用于以可移植方式序列化/反序列化POD类型(如double和float)的技术是什么?目前,似乎唯一的方法是将值表示为字面意义(如"123.456"),double的ieee754布局在所有体系结构上都不是标准的.
Syl*_*sne 28
Brian"Beej Jorgensen"Hall在他的网络编程指南中给出了一些代码float(resp.double)到uint32_t(resp.uint64_t),以便能够通过网络在两台机器之间安全地传输它,这两台机器可能不同意它们的表示.它有一些限制,主要是它不支持NaN和无穷大.
这是他的包装功能:
#define pack754_32(f) (pack754((f), 32, 8))
#define pack754_64(f) (pack754((f), 64, 11))
uint64_t pack754(long double f, unsigned bits, unsigned expbits)
{
long double fnorm;
int shift;
long long sign, exp, significand;
unsigned significandbits = bits - expbits - 1; // -1 for sign bit
if (f == 0.0) return 0; // get this special case out of the way
// check sign and begin normalization
if (f < 0) { sign = 1; fnorm = -f; }
else { sign = 0; fnorm = f; }
// get the normalized form of f and track the exponent
shift = 0;
while(fnorm >= 2.0) { fnorm /= 2.0; shift++; }
while(fnorm < 1.0) { fnorm *= 2.0; shift--; }
fnorm = fnorm - 1.0;
// calculate the binary form (non-float) of the significand data
significand = fnorm * ((1LL<<significandbits) + 0.5f);
// get the biased exponent
exp = shift + ((1<<(expbits-1)) - 1); // shift + bias
// return the final answer
return (sign<<(bits-1)) | (exp<<(bits-expbits-1)) | significand;
}
Run Code Online (Sandbox Code Playgroud)
人类可读格式有什么问题.
它比二进制有两个优点:
坏处:
htonl())要以完全精度输出double:
double v = 2.20;
std::cout << std::setprecision(std::numeric_limits<double>::digits) << v;
Run Code Online (Sandbox Code Playgroud)
好.我不相信这是完全准确的.它可能会失去精确度.
看一下glib 2中的(旧)gtypes.h文件实现-它包括以下内容:
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
union _GFloatIEEE754
{
gfloat v_float;
struct {
guint mantissa : 23;
guint biased_exponent : 8;
guint sign : 1;
} mpn;
};
union _GDoubleIEEE754
{
gdouble v_double;
struct {
guint mantissa_low : 32;
guint mantissa_high : 20;
guint biased_exponent : 11;
guint sign : 1;
} mpn;
};
#elif G_BYTE_ORDER == G_BIG_ENDIAN
union _GFloatIEEE754
{
gfloat v_float;
struct {
guint sign : 1;
guint biased_exponent : 8;
guint mantissa : 23;
} mpn;
};
union _GDoubleIEEE754
{
gdouble v_double;
struct {
guint sign : 1;
guint biased_exponent : 11;
guint mantissa_high : 20;
guint mantissa_low : 32;
} mpn;
};
#else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
#error unknown ENDIAN type
#endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
Run Code Online (Sandbox Code Playgroud)
您应该将它们转换为您始终能够使用的格式,以便重新创建浮点数/双精度数。
这可以使用字符串表示形式,或者,如果您需要占用更少空间的内容,请以 ieee754(或您选择的任何其他格式)表示您的数字,然后像处理字符串一样解析它。
| 归档时间: |
|
| 查看次数: |
10229 次 |
| 最近记录: |