0xff800001 和 0xffb00000 之间的长值重新解释为浮动错误

问题描述

我在将二进制值转换为 float 时遇到问题,促使我进一步研究该问题。我发现 0xff8000010xffb00000 之间的值被重新解释为 float 时有它们的 15th 22nd LSB 位翻转。我为此使用的测试程序:

unsigned long long ca = 0;
unsigned long long cb = 0;
for(unsigned long long tmpLongLong = 0x00000000; tmpLongLong <= 0xffffffff; tmpLongLong++)
{
    unsigned long tmpLong1 = tmpLongLong;
    float tmpFloat = *(reinterpret_cast<float*>(&tmpLong1));
    unsigned long tmpLong2 = *(reinterpret_cast<unsigned long*>(&tmpFloat));
    ca++;
    if(tmpLong1 != tmpLong2)
    {
        cb++;
    }
    cout << (tmpLong2 == tmpLong1 ? "YES " : "NO ") << std::hex << tmpLong1 << " vs. " << tmpLong2 << std::dec << endl;
}
cout << "bad: " << cb << "/" << ca << " " << 100.0 / ca * cb << "%" << endl;

2 个损坏值的输出示例:

NO ff8003cf vs. ffc003cf
NO ff8003d0 vs. ffc003d0

此问题的原因是什么,我该如何克服?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)