Hi!, I want to know how to determine through code which integers enclose a float value. E.g a variable stores the float 8.33, I want to know which two integers encloses the value 8.33 and store them in two variables
Now suppose I already know these integers (in this case 8 and 9). Then i use this two integers to read the values stored in an int array at those positions. Position 8 stores the int 84 and position 9 stores 76. What I want to know is how to interpolate the values that are stored at the respective positions, achieving a value that approximate to what should be the float between the two integers, in this case 8.33, all this through code. E.g. if pos 8 stores 84 and pos 9 stores 76, 8.33 should return a value of 81.36.
float value = 8.33f;
int floor = Mathf.FloorToInt(value);
int ceil = Mathf.CeilToInt(value);
int a = array[floor];
int b = array[ceil];
float result = Mathf.Lerp(a, b, value % 1.0f);
FloorToInt rounds down to an integer, giving 8.
CeilToInt rounds up to an integer, giving 9.
Lerp does the interpolation between a and b using value % 1.0f as the percentage.
value % 1.0f gives the fractional component of the number (0.33).
It is exactly what i was looking for, thanks a lot!
That’s good to know, although i dont think its gonna cause any problems because the float as well as the values stored in the array should be always positive. Thanks for the info!