I tried to assign a calculation result to a variable, and print it out. However, depending on the type of the variable it's assigned to, the results are different. But the unexpected results only happened for certain values for this specific calculation.
int main() {
// anything above 64.02 give unexpected results
// e.g. (100.02 - 100.0) * 100.0
int a ((64.02 - 64.0) * 100.0);
double b ((64.02 - 64.0) * 100.0);
cout<<"result: "<<a<<endl; // result: 1, expected result: 2
cout<<"result: "<<b<<endl; // result: 2, expected result: 2
// anything below 64.02 give right results
int c ((63.02 - 63.0) * 100.0);
double d ((63.02 - 63.0) * 100.0);
cout<<"result: "<<c<<endl; // result: 2, expected result: 2
cout<<"result: "<<d<<endl; // result: 2, expected result: 2
return 0;
}
I know this question is very specific, but I suspect it has something to do with implicit type cast. But why only for values >=64.02?
This is my first ever question on stack overflow, please don't -1 me!!!