Are all 64 bits of doubles stored/used?
7 ビュー (過去 30 日間)
古いコメントを表示
What does MATLAB do with the least significant bits of the fraction in a double? MATLAB online documentation indicates that 16-digits of precision is default. Double (64-bit) is also the default data type. But you can sometimes achieve 16 decimal digits of precision without using all 64 bits of a double.
Here's why I ask. I'm sending double-format data from MATLAB to a target peripheral using serial() and fwrite():
load 'y.mat' % This loads a variable y, which is a vector of doubles.
s = serial('COM5');
fopen(s);
fwrite(y(1),'double')
When I open y in MATLAB's variable editor, the value of y(1) is showing up as 0.588213244693768. And when I drill into the binary in the command window, I get this:
K>> y64 = typecast(y(1),'int64')
y64 =
int64
4603473373444510900
K>> ybin = dec2bin(y64)
ybin =
'11111111100010110100101010010010010101001000010001010000000000'
(If you count the bits, the command window is leaving off the first two zeros in the binary. No problem.)
Converting that to hex, you get 0x 3FE2 D2A4 9521 1400.
So far, so good. But the weird thing is, in my target, the received decimal value is showing up as 0.58821324469376757 (two more digits than MATLAB). When I look at the memory storage, it's 0x 3FE2 D2A4 9521 14B4. What's happening here? I doubt that my peripheral is just "making up" an extra byte at the end. Is MATLAB not displaying all decimal digits in the variable editor, or in the resulting type conversions? I'm asking because I'm debugging numerical differences between MATLAB and my target, and it would be really helpful to know what exact binary MATLAB is sending and/or storing. Thanks!
0 件のコメント
採用された回答
Walter Roberson
2020 年 5 月 10 日
dec2bin applied to a uint64 converts the uint64 to double first.
dec2bin(typecast(y, 'uint8'), 8)
and take into consideration byte order. Or
num2hex(y)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!