Problem with double

Can anyone tell me why this code return 28 :
1
2
3
double x = 0.29;
int y = x * 100;
return y;

What the reason of this problem?
Rounding errors...

If you want the sum rounded to the nearest int, how about

1
2
3
	double x = 0.29;
	double y = x * 100;
	return static_cast<int>(y + 0.5);


Andy

P.S. I need to add a cast to your version to get a zero warning build

1
2
3
	double x = 0.29;
	int y = static_cast<int>(x * 100);
	return y;
Last edited on
Topic archived. No new replies allowed.