I have an array of double values where the last one will be in fact a long representing time in milliseconds. Therefore this long needs to be converted to a double to fit into the array. Later at the time of retrieval of this long value it needs to be converted back to a long type. It is critical that the long value after retrieval from the array of doubles is exactly the same value (down to a one millisecond) I had before putting it into the array. There will be no operations whatsoever performed on the long value while in the array. So the questions are:
Should I simply cast the long value to double and upon retrieval cast it back to long? Will this preserve the exact value of my long?
Or should I use Double.longBitsToDouble(time) method to put the long into the array and retrieve it with Double.doubleToLongBits(time).
Or maybe I should put the long value into the array using Double.longBitsToDouble(time) and retrieve the encoded long by simply casting it to long type?
Thank you in advance for your help.
I have an array of double values where the last one will be in
Share
I mean, what you should do is to not use an array to represent values meaning different things…
But from what it sounds like, you have no choice but to use
Double.longBitsToDouble, since adoublecannot give you full precision on alllongvalues. Just casting alongtodoublewill lose precision.