显然在我的机器上,float、double 和 long double 各有不同的大小。似乎也没有严格的标准来强制执行每种类型必须有多少字节。
然后,如何将浮点值保存到二进制文件中,然后在大小不同的情况下在不同的系统上正确读取它?例如,我的机器有 8 个字节的 double ,而乔的有 12 个字节的 double 。
无需以文本形式导出(例如“0.3232”),也就是说。看起来比二进制表示要紧凑得多。
最佳答案
你必须定义一个格式,并实现它。通常,大多数 我知道的网络协议(protocol)使用 IEEE float 和 double,输出 big-endian (但其他格式也是可能的)。使用 IEEE 格式的优势 是目前大多数日常机器使用的 内部;如果你在其中一台机器上(以及你的便携性 代码到其他机器,比如大型机,不是问题),你可以 只需将类型双关转换为 unsigned int 即可“转换”为格式 相同的大小,并输出它。因此,例如,您可能有:
obstream&
operator<<( obstream& dest, uint64_t value )
{
dest.put((value >> 56) & 0xFF);
dest.put((value >> 48) & 0xFF);
dest.put((value >> 40) & 0xFF);
dest.put((value >> 32) & 0xFF);
dest.put((value >> 24) & 0xFF);
dest.put((value >> 16) & 0xFF);
dest.put((value >> 8) & 0xFF);
dest.put((value ) & 0xFF);
return dest;
}
obstream&
operator<<( obstream& dest, double value )
{
return dest << reinterpret_cast<uint64_t const&>( value );
}
如果您必须移植到不支持 IEEE 的机器(例如任何 现代大型机),你需要一些更复杂的东西:
obstream&
obstream::operator<<( obstream& dest, double value )
{
bool isNeg = value < 0;
if ( isNeg ) {
value = - value;
}
int exp;
if ( value == 0.0 ) {
exp = 0;
} else {
value = ldexp( frexp( value, &exp ), 53 );
exp += 1022;
}
uint64_t mant = static_cast< uint64_t >( value );
dest.put( (isNeg ? 0x80 : 0x00) | exp >> 4 );
dest.put( ((exp << 4) & 0xF0) | ((mant >> 48) & 0x0F) );
dest.put( mant >> 40 );
dest.put( mant >> 32 );
dest.put( mant >> 24 );
dest.put( mant >> 16 );
dest.put( mant >> 8 );
dest.put( mant );
return dest;
}
(请注意,这不能正确处理 NaN 和无穷大。 就个人而言,我会禁止它们出现在格式中,因为不是所有的都 float 点表示支持它们。但是,没有 float 支持 1E306 的 IBM 大型机上的格式,尽管 你可以用上面的 IEEE double 格式对其进行编码。)
阅读当然是相反的。要么:
ibstream&
operator>>( ibstream& source, uint64_t& results )
{
uint64_t value = (source.get() & 0xFF) << 56;
value |= (source.get() & 0xFF) << 48;
value |= (source.get() & 0xFF) << 40;
value |= (source.get() & 0xFF) << 32;
value |= (source.get() & 0xFF) << 24;
value |= (source.get() & 0xFF) << 16;
value |= (source.get() & 0xFF) << 8;
value |= (source.get() & 0xFF) ;
if ( source )
results = value;
return source;
}
ibstream&
operator>>( ibstream& source, double& results)
{
uint64_t tmp;
source >> tmp;
if ( source )
results = reinterpret_cast<double const&>( tmp );
return source;
}
或者如果您不能指望 IEEE:
ibstream&
ibstream::operator>>( ibstream& source, double& results )
{
uint64_t tmp;
source >> tmp;
if ( source ) {
double f = 0.0;
if ( (tmp & 0x7FFFFFFFFFFFFFFF) != 0 ) {
f = ldexp( ((tmp & 0x000FFFFFFFFFFFFF) | 0x0010000000000000),
static_cast<int>( (tmp & 0x7FF0000000000000) >> 52 )
- 1022 - 53 );
}
if ( (tmp & 0x8000000000000000) != 0 ) {
f = -f;
}
dest = f;
}
return source;
}
(假设输入不是 NaN 或无穷大。)
关于c++ - 正确地(独立于 32 位/64 位)将 float 保存到二进制 ofstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7103710/