The result of the following program is a little bit strange to me on my machine.
#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;
}
Output:
$ ./test
a < b * c
I know double is not that accurate because of the precision. But I don't expect that value changed and give an inconsistent comparison result.
If the a < b * c
get printed out, I do expect that a < d
should also get printed. But when I run this code on my i686 server and even on my cygwin. I can see a < b * c
but cannot see a < d
.
This issue has been confirmed to be platform dependent. Is this caused by the different instruction and implementation of double assignment?
UPDATE
The generated assembly:
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