Why does the FPRINTF function round my double precision floating point number in MATLAB 6.5 (R13)?
조회 수: 12 (최근 30일)
이전 댓글 표시
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.
채택된 답변
MathWorks Support Team
2013년 10월 18일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Numbers and Precision에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!