在我的机器上,以下程序的结果对我来说有点奇怪。为什么double的值似乎在赋值后被改变?
#include <iostream>
using namespace std;
int main(){
double a = 20;
double b = 0.020;
double c = 1000.0;
double d = b * c;
if(a < b * c)
cout << "a < b * c" << endl;
if(a < d)
cout << "a < d" << endl;
return 0;
}
输出:
$ ./test
a < b * c
我知道双是因为精度不准确。但我并不认为这个价值会发生变化,并会导致不一致的比较结果。
如果打印出a < b * c
,我确实希望a < d
也应该打印出来。但是当我在我的i686服务器上甚至在我的cygwin上运行这个代码。我可以看到a < b * c
,但看不到a < d
。
此问题已被确认为与平台相关。这是由双重任务的不同指令和执行引起的吗?
UPDATE
生成的组件:
main:
.LFB1482:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
subl $56, %esp
.LCFI2:
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $0, -8(%ebp)
movl $1077149696, -4(%ebp)
movl $1202590843, -16(%ebp)
movl $1066695393, -12(%ebp)
movl $0, -24(%ebp)
movl $1083129856, -20(%ebp)
fldl -16(%ebp)
fmull -24(%ebp)
fstpl -32(%ebp)
fldl -16(%ebp)
fmull -24(%ebp)
fldl -8(%ebp)
fxch %st(1)
fucompp
fnstsw %ax
sahf
ja .L3
jmp .L2
//.L3 will call stdout
也许它是与不断优化。如果从'std :: cin'中抓住'a','b'和'c',你能显示程序集还是重现它? –
没有来自编译器的错误/警告。 – StarPinkER
我可以重现它如果我从std :: cin得到它,我会在稍后发布它。 @NateKohl – StarPinkER