Tricky floating point
How do you think what the code below should print out? I expected two numbers 115.
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char* argv[]) {
double f = 1.15;
int a = f * 100.0 + 0.1E-9;
int b = f * 100.0;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
No, on VS2008 it prints out:
115
114
Be careful.
Original post | Disclaimer