Home:ALL Converter>Matlab Double cast from Uint64

Matlab Double cast from Uint64

Ask Time:2017-09-07T13:34:47         Author:user1876942

Json Formatter

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

Author:user1876942,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/46088535/matlab-double-cast-from-uint64
m7913d :

If you want to maintain the same number, you should definitely use double to convert it:\n\n\n double Convert to double precision. \n double(X) returns the double precision value for X.\n\n\nWhereas typecast maintains the internal representation, i.e. the bytes are maintained the same but or differently interpreted:\n\n\n typecast Convert datatypes without changing underlying data.\n Y = typecast(X, DATATYPE) convert X to DATATYPE. If DATATYPE has\n fewer bits than the class of X, Y will have more elements than X. If\n DATATYPE has more bits than the class of X, Y will have fewer\n elements than X.\n\n\nNote that it is only possible to use typecast when the number of bytes are the same, which is not true for double as it tries to represent the same number as close as possible in double precision. For example, you cannot typecast uint32 to double, but you can typecast two uint32 to one double number. If you use double to convert it, you will obtain respectively one and two doubles.\n\nC++ equivalent\n\nX = double(uint64(123)); \n => int64_t x = 123; double X = x;\n\nX = typecast(uint64(123), 'double') \n => int64_t x = 123; double X = reinterpret_cast<double>(x);",
2017-09-07T05:51:10
yy