I have a positive float array and would like to convert this array to an unsigned short array with rounding. How can I do that in an efficient way? Here is an example:
float floatArr[] = {1.2, 1.8, 2.1, 2.5, 3.2};
unsigned short usArr[5];
// I could do this
std::copy(floatArr, 5, usArr);
However, it will only do the casting, since it basically copies the first two bytes. So the result is usArr[] = {1, 1, 2, 2, 3}. My question is how can I convert this float array with rounding, instead of casting, to be usArr[] = {1, 2, 2, 3, 3}? Thank you and I would appreciate any help!
floorisn't needed here. Assignment to an integral type does that.