Possible Duplicate:
C++ double precision and rounding off
守则:
int main(void)
{
double a = 12;
double b = 0.5;
double c = 0.1;
std::cout.precision(25);
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
std::cout << a + b << std::endl;
std::cout << a + c << std::endl;
return 0;
}
产出:
12
0.5
0.1000000000000000055511151
12.5
12.09999999999999964472863
Why does GCC represent 0.1 and 0.5 differently? When adding, they are represented differently. It seems 0.5 and whole numbers a represented differently that other floats. Or is this just something going on in the io library? What causes this behavior?