I am writing a program that involves some periodic motion and, instead of tracking the specific period and time interval within that period, I am planning to instead use the interval
as a fraction of the overall period. So, the time variable will always be a real number (type double) in the range 0 - 1.
I'd also like to use the first digit after the decimal as an array index. ( The array will contain pre-assigned data to speed up program execution.) For example, if the variable is 0.314159, I would like to use the '3', convert it to an integer, and use it to access the array.
At this point, I am thinking the procedure will go somewhat like the following:
1 2 3 4 5
|
double time_interval; // Say 0.314159
int arr_index; // Array index
arr_index = floor(10.0 * time_interval); // Can now use arr_index to access array.
| |
Is this okay?
Should the last line explicitly cast the result as an int?
Is there a better way to do this? Any suggestions welcome.
What is the best way to grab the first digit after the decimal, convert it to an integer, and use it as an integer throughout the rest of the program?