我需要将几个 float 写入一个文本文件并存储一个 CRC32 校验和。然后,当我从文本文件中读回 float 时,我想重新计算校验和并将其与之前保存文件时计算的校验和进行比较。我的问题是校验和有时会失败。这是因为相等的 float 可以用不同的位模式表示。为了完整起见,我将在接下来的段落中总结代码。
我已经改编了this CRC32 algorithm这是我阅读后发现的 this question .这是它的样子:
uint32_t updC32(uint32_t octet, uint32_t crc) {
return CRC32Tab[(crc ^ octet) & 0xFF] ^ (crc >> 8);
}
template <typename T>
uint32_t updateCRC32(T s, uint32_t crc) {
const char* buf = reinterpret_cast<const char*>(&s);
size_t len = sizeof(T);
for (; len; --len, ++buf)
crc = updC32(static_cast<uint32_t>(*buf), crc);
return crc;
}
CRC32Tab
包含与上面链接文件中的大数组完全相同的值。
这是我如何将 float 写入文件并计算校验和的简化版本:
float x, y, z;
// set them to some values
uint32_t crc = 0xFFFFFFFF;
crc = Utility::updateCRC32(x, crc);
crc = Utility::updateCRC32(y, crc);
crc = Utility::updateCRC32(z, crc);
const uint32_t actualCrc = ~crc;
// stream is a FILE pointer, and I don't mind the scientific representation
fprintf(stream, " ( %g %g %g )", x, y, z);
fprintf(stream, " CRC %u\n", actualCrc);
我从文件中读回值如下。由于文件具有更复杂的语法并且必须进行解析,因此实际上涉及的内容更多,但我们假设 getNextFloat()
返回之前编写的每个 float 的文本表示形式。
float x = std::atof(getNextFloat());
float y = std::atof(getNextFloat());
float z = std::atof(getNextFloat());
uint32_t crc = 0xFFFFFFFF;
crc = Utility::updateCRC32(x, crc);
crc = Utility::updateCRC32(y, crc);
crc = Utility::updateCRC32(z, crc);
const uint32_t actualCrc = ~crc;
const uint32_t fileCrc = // read the CRC from the file
assert(fileCrc == actualCrc); // fails often, but not always
这个问题的根源是 std::atof 将返回从文件读取的字符串中编码的 float 的不同位表示,而不是用于将该字符串写入文件。
所以,我的问题是:除了对字符串本身进行校验和之外,还有另一种方法可以实现我的校验和 float 的目标吗?
感谢阅读!
最佳答案
问题的根源从您的评论中显而易见:
If I'm not completely mistaken, there is no rounding happening here. The
%g
specifier chooses the shortest string representation that exactly represents the number.
这是不正确的。如果未指定精度,则默认为 6,并且对于大多数浮点输入,肯定会进行舍入。
如果您需要一种人类可读的可往返格式,%a
是迄今为止最好的选择。如果做不到这一点,您将需要指定至少 9 的精度(假设您系统上的 float
是 IEEE-754 单精度)。
您可能仍然被 NaN 编码绊倒,因为标准没有指定如何或是否必须打印它们。
关于c++ - 通过文本文件往返的 float 校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15429593/