Double-precision numbers are stored in MATLAB using 64 bits as per the IEEE standard for floating-point arithmetic. With 64 bits in the binary representation, the maximum possible precision in base 10 is 15 to 17 digits. Therefore, it should not matter if 0.05 is represented as a string with more than 15 decimals.
The "mat2str" function uses "sprintf" to print a number with n digits. The 1 that appears near the end with "mat2str(0.005,90)" is due to the fact that 0.005 cannot be exactly represented on a computer with 64 bits. However, if we look at the hexadecimal representation of the output of "mat2str", we get the same number:
>> format hex
>> s = mat2str(0.005,15)
s =
0.005
>> eval(s)
ans =
3f747ae147ae147b
>> s = mat2str(0.005,90)
s =
0.0050000000000000001
>> eval(s)
ans =
3f747ae147ae147b
The string "s" might be different, but the garbage digit at the end does not matter. Only the first 15 digits after the decimal point matter and this is shown by evaluating "s": we obtain the same binary number.
Instead of using "mat2str" you could store the exact representation of the number in hexadecimal form. The "num2hex" function converts a double-precision number into its hexadecimal representation as a string. This might be a better solution if you only plan to use it on scalars -- "num2hex" does not work on arrays unlike "mat2str".
Best Answer