I'm not really sure why you want to know this exactly for any matrix of numbers. It will get a bit long to write down the exact values as stored.
You can't really print out the exact value easily, because the exact value will not be representable in a limited number of decimal digits, only about 16 of them.
format long g
a = 1.23
a =
1.23
So a looks like 1.23. But of course it is not. We need to write out 55 digits or so, using a tool like sprintf (or num2str) to show the exact value, since a is really stored in binary form.
sprintf('%0.55f',a)
ans =
1.2299999999999999822364316059974953532218933105468750000
32 digits is NOT sufficient to show the exact value.
num2str(a,32)
ans =
1.2299999999999999822364316059975
See that the actual value of a went on for another 20 decimal digits or so. num2str does work, IF you give it enough room to work.
num2str(a,55)
ans =
1.229999999999999982236431605997495353221893310546875
This does not mean that you can represent all floating point numbers with 50 or so decimal digits, only that when you try to write a decimal fraction in binary form, you will get only an approximation most of the time. In fact, the very next larger floating point number than a that we can represent is
num2str(a+eps(a),55)
ans =
1.2300000000000002042810365310288034379482269287109375
Why have I been using 55 digits here? In fact, we should be able to get away with 53 digits. But 55 adds a safety factor. All doubles in MATLAB are stored using 52 binary bits for the mantissa.
Note that in general, all negative integer powers of 2 (thus 2^(-n))can be written using exactly n digits to the right of the decimal. We can see that here. Each negative power of 2 adds a digit.
2.^-(1:12)'
ans =
0.5
0.25
0.125
0.0625
0.03125
0.015625
0.0078125
0.00390625
0.001953125
0.0009765625
0.00048828125
0.000244140625
Since any mantissa stored in a binary form will be a linear combination of these numbers, then in general, it will take 52 decimal digits to represent a number with 52 binary bits.
0 Comments
Sign in to comment.