UPDATE : Thanks, Kevin, I meant <= in the title, not <.
It is right? This is the best I managed after a difficult time on the problem:
public int candidate_answer(double f, float g) { int test = (int)Math.floor(f / g); if ((test + 1) * g <= f) { test++; } return test; }
Background:
The application is a simple game that I took upon myself for a previous programmer. Curiously, he decided to mix floats and double it pointless, both in member variables and in variables, so there are many unnecessary implicit and explicit castings going up and down.
The playerโs coordinates are double x, y (it is believed that the player is a point). There is a floating TILE_SIZE, and in the world there are a certain number of rows and columns, a number of tiles, as well as some general processing outside the borders. Assuming the coordinate (x, y) is within the borders, I'm trying to figure out which element the user is in, based on x (to get a column) or y (to get a row). This is similar to programmer 101.
WLOG; At first I just did col = (int)(x/TILE_SIZE) . But, as I found, for example, .5 / .1f <5, and therefore (int)(.5/.1f) == 4 , the wrong answer. This led to the above statement and statement of the problem.
Then I found that, for example, (int)(-9.999999747378752E-5 / .1f) == 0 , which led me to call Math.floor first.
But now I'm not sure what other errors are hidden in this approach or what a better approach would be.
(This may not seem like such a big problem if the user is right on the threshold of being on one line or another, and we accidentally rounded up to the wrong one, but the real problem is in the code, where we see unexpected changes in the sign (+, -, 0 ). For example, some of the code suggests that if the user is on the tile at the point (r, c), then it actually contains this tile geometry. When this is not the case, we get things like negative distances, when only non-negative ones are expected, etc. and this causes threats to trelby and while loop break, etc.)