x*x!= x*x在自动变量?

Ste*_*ann 3 c++ variables floating-point

如何x * x通过将其存储在" auto变量" 中来改变?我认为它应该仍然是相同的,我的测试显示类型,大小和值显然都是相同的.

但即使x * x == (xx = x * x)是假的.我勒个去?

(注意:我知道IEEE 754以及浮动和双重工作以及它们的常见问题,但是这个让我感到困惑.)

#include <iostream>
#include <cmath>
#include <typeinfo>
#include <iomanip>
using namespace std;

int main() {
    auto x = sqrt(11);
    auto xx = x * x;
    cout << boolalpha << fixed << setprecision(130);
    cout << "   xx == 11           " << (   xx == 11          ) << endl;
    cout << "x * x == 11           " << (x * x == 11          ) << endl;
    cout << "x * x == xx           " << (x * x == xx          ) << endl;
    cout << "x * x == (xx = x * x) " << (x * x == (xx = x * x)) << endl;
    cout << "x * x == x * x        " << (x * x == x * x       ) << endl;
    cout << "types        " << typeid(xx).name() << " " << typeid(x * x).name() << endl;
    cout << "sizeofs      " << sizeof(xx) << " " << sizeof(x * x) << endl;
    cout << "xx           " << xx    << endl;
    cout << "x * x        " << x * x << endl;
}
Run Code Online (Sandbox Code Playgroud)

这是输出:

   xx == 11           true
x * x == 11           false
x * x == xx           false
x * x == (xx = x * x) false
x * x == x * x        true
types        d d
sizeofs      8 8
xx           11.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
x * x        11.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Run Code Online (Sandbox Code Playgroud)

编译如下:

C:\Stefan\code\leetcode>g++ test4.cpp -static-libstdc++ -std=c++11 -o a.exe

C:\Stefan\code\leetcode>g++ --version
g++ (GCC) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)

120*_*arm 6

这是双打通常的不精确.您没有提到您的硬件,但在x86(32位Intel)上,计算期间使用的临时值是10字节长的双倍.的x * x将是那些之一,而xx = x * x将被加载回到FPU用于比较之前被存储一个8字节双.

如果启用优化或构建64位可执行文件,则可能会得到不同的结果.