This question arose out of something strange that I noticed after investigating this question further...
I always understood MATLAB variables to be double-p
first :
it appears that sprintf and fprintf have different behavior on different versions of MATLAB for example in MATLAB 2018 a
num=2.7182818284590666666666;
sprintf('%0.70f', num)
ans =
'2.7182818284590668511668809514958411455154418945312500000000000000000000'
second :
Floating-Point Numbers
MATLAB® represents floating-point numbers in either double-precision or single-precision format. The default is double precision, but you can make any number single precision with a simple conversion function.
Double-Precision Floating Point
MATLAB constructs the double-precision (or double) data type according to IEEE® Standard 754 for double precision. Any value stored as a double requires 64 bits, formatted as shown in the table below:
Bits : 63
Usage : Sign (0 = positive, 1 = negative)Bits : 62 to 52 Usage : Exponent, biased by 1023
Bits : 51 to 0 Usage : Fraction f of the number 1.f
refer to this link for more info
Between 252=4,503,599,627,370,496 and 253=9,007,199,254,740,992 the representable numbers are exactly the integers. For the next range, from 253 to 254, everything is multiplied by 2, so the representable numbers are the even ones, etc. Conversely, for the previous range from 2^51 to 2^52, the spacing is 0.5, etc.
The spacing as a fraction of the numbers in the range from 2^n to 2^n+1 is 2^n−52. The maximum relative rounding error when rounding a number to the nearest representable one (the machine epsilon) is therefore 2^−53.
so in your case where n=1 (2^1 <= num <= 2^2) the spacing is 2^-51 ,
i think it is safe to assume that sprintf and sprintf algorithms for showing numbers are tricky and MATLAB Double type is based on IEEE standard,
about VPA:
vpa Uses Guard Digits to Maintain Precision
The value of the digits function specifies the minimum number of significant digits used. Internally, vpa can use more digits than digits specifies. These additional digits are called guard digits because they guard against round-off errors in subsequent calculations.
Numerically approximate 1/3 using four significant digits.
a = vpa(1/3, 4)
a =
0.3333
Approximate the result a using 20 digits. The result shows that the toolbox internally used more than four digits when computing a. The last digits in the result are incorrect because of the round-off error.
vpa(a, 20)
ans =
0.33333333333303016843
the problem you may encounter is because of spacing , gaurd digits algorithm and round off problem ,
for example using matlab 2018 a :
sprintf('%0.28f', 8.0)
ans =
'8.0000000000000000000000000000'
but:
sprintf('%0.28f', 8.1)
ans =
'8.0999999999999996447286321199'
because the number is between 2^3 and 2^4 so the spacing is 2^-49 (= 1.77 e-15) so the number is valid till 15th decimal place and
sprintf('%0.28f', 64.1)
ans =
'64.0999999999999943156581139192'
because the number is between 2^6 and 2^7 so the spacing is 2^-46 (= 1.42 e-14) so the number is valid till 14th decimal place