Floats are approximations, so you if you for example have approximately 2, then that might be 1.999999 or 2.000001, converting those to int will have very differenty results, one of them becomes a much smaller number
This is unlikely to be what causes your incorrect result, it's a secondary problem. Computing which index to use is something exact, so there shouldn't be any approximating involved, and there should especially not be large differences in outcome based on which way the approximations are off. simple as that
Add prints to your code to print out what it's doing. Like looking over the shoulder of someone solving a math problem, checking that they're doing it right.