float dis = calc_distance(a,b); int answer = (int)(dis * 10); std::cout << dis << ", " << answer << std::endl;
Now this code seems fine but occasionally gives output similar to the following:
2.1, 20
Obviously the 20 should be 21 and this is the problem. I've tried chaging the code to:
float dis = calc_distance(a,b); int answer = (int)(dis * 10.0000001); std::cout << dis << ", " << answer << std::endl;
This allows for a small amount of rounding error for the floats and solves almost all the problems but I still get the rare case where it doesn't work.
The competition requires that your answers are perfect so a difference in 1 in the answer will result in a mark of 0. Does anyone have any concrete ways of converting floats to ints accurately?
Thanks in advance












