Why does the FPRINTF function round my double precision floating point number in MATLAB 6.5 (R13)?
113 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Why does the FPRINTF function round my double precision floating point number in MATLAB 6.5 (R13)?
I am experiencing a formatting problem in MATLAB 6.5 (R13), where only the first 19 significant digits are printed correctly to a text file when I use the FPRINTF function. The script below shows that the data is stored internally with the correct precision, but not when it is printed to a text file.
fid = fopen('out.txt','w+');
a = -0.00283527374267578125;
fprintf(fid, '%.25f\n', a);
fprintf('%.25f\n', a*2);
fclose(fid);
The result of printing "a" to a file:
-0.0028352737426757813000000
-0.0056705474853515625000000
The number should be exactly representable in floating point format, since it is only a 20 bit binary number:
Decimal Form:
a = -0.00283527374267578125;
Hexidecimal Form (format hex):
a = bf673a0000000000;
I am inclined to think the problem is concerned with how MATLAB converts the floating point number to a decimal when using the FPRINTF function.
Accepted Answer
MathWorks Support Team
on 18 Oct 2013
This error is due to the standard C programming I/O routines. When MATLAB initiates the FPRINTF function it calls the standard I/O functions. There is no workaround for this round off limitation because it is inherent to that library and to the bit limits on your computer.
This behavior is reproducible in C as shown below:
/* C program that writes a small decimal value to a file */
#include <stdio.h>
int main(void)
{
FILE * fout;
double a;
fout = fopen("myFile.txt", "w");
a = -0.00283527374267578125;
fprintf(fout, "Here is what I print ... \n");
fprintf(fout, "%.25f \n", a);
fprintf(fout, "%.30f \n", a);
fprintf(fout, "%.25lf \n", a);
fprintf(fout, "%.30lf \n", a);
fclose(fout);
}
/* The output results of the C program */
-0.0028352737426757813000000
-0.002835273742675781300000000000
-0.0028352737426757813000000
-0.002835273742675781300000000000
Note that MATLAB uses the IEEE-754 Double Precision floating-point standard to store data and perform arithmetical operations.
IEEE Specifications:
Exponent(max) = 1023
Exponent(max) = -1023
Exponent Bias = 1023
Precision (#bits) = 64
Sign Bits = 1
Exponent Bits = 11
*Fraction Bits = 52 -> Hitting this limitation.
0 Comments
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!