I have an application where i HAVE to store unsigned short data in a double parallel vector to be able to easily move it around. Whenever I actually need the data, I cast them back to unsigned short. I know my data, although saved in a double vector, are well within the range of unsigned short i.e. the values stored are all between 0 and 2^16-1.
Question is, since sizeof(double) = 8 and sizeof(unsigned short) = 2 could this cause data loss when casting? I know one problem is if the data is changed by a small bit when in double form, it might be rounded to another number but that is not my concern right now.
thanks
You don’t have to cast to assign a
shortto adouble. There is an implicit conversion between short and double.If you are using 16-bit
shortand IEEE-754 double precisiondoublethere is no loss of precision during the conversion.To give some perspective, basically for single precision
float, the first positive integer value that cannot be represented is16777217. So for 16-bitshortanddoubleyou should be OK 😉