I am casting a 64bit fixed point number to floating point. How should that be done in Matlab? The following code gives different results. What is the difference between typecast and the double(x)
temp = 2^32*uint64(MSB) + uint64(LSB);
out_0(1, 1) = typecast(temp, 'double');
out_1(1, 1) = double(temp);
An Example:
temp = 4618350711997530112
data = typecast(temp, 'double')
data =
5.9194
>> double(temp)
ans =
4.6184e+18
If you want to maintain the same number, you should definitely use
double
to convert it:Whereas
typecast
maintains the internal representation, i.e. the bytes are maintained the same but or differently interpreted:Note that it is only possible to use
typecast
when the number of bytes are the same, which is not true fordouble
as it tries to represent the same number as close as possible in double precision. For example, you cannot typecastuint32
todouble
, but you can typecast twouint32
to onedouble
number. If you usedouble
to convert it, you will obtain respectively one and two doubles.C++ equivalent
X = double(uint64(123));
=>
int64_t x = 123; double X = x;
X = typecast(uint64(123), 'double')
=>
int64_t x = 123; double X = reinterpret_cast<double>(x);