Are all 64 bits of doubles stored/used?

7 views (last 30 days)
Phil
Phil on 10 May 2020
Edited: Phil on 10 May 2020
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!

Accepted Answer

Walter Roberson
Walter Roberson on 10 May 2020
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)

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!