comparison of double numbers

조회 수: 2 (최근 30일)
Pavel
Pavel 2012년 7월 25일
I just try to compare two double numbers:
q = 0.1:0.05:0.7;
q(2) == 0.15
I have "false". Ok, print this number into "txt" file:
fprintf(fileID,'%99.99e\n',q(2));
And i see follow:
1.500000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000e-01
Why the "2"(exactly)?
  댓글 수: 1
Wayne King
Wayne King 2012년 7월 25일
Why the "2" exactly? Can you clarify?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2012년 7월 25일
편집: Jan 2012년 7월 25일
An evergreen question.
You find over a hundred of related posts in this forum, when you search for "faq6.1".
Matlab (as other numerical software) stores the values as IEEE 754 format using 64 bits. Printing 99 decimals does not reveal the underlying limitation, that not all decimal numbers have an exact binary representation.
  댓글 수: 3
Jan
Jan 2012년 7월 25일
You are welcome, Pavel. This problem is not intuitively clear to beginners in numerical computations. But it is fundamental and appears such frequently, that it should appear 8 times in the FAQ. But even then it is a well known phenomenon, that the FAQ is not read by beginners, but maintained by profis.
Walter Roberson
Walter Roberson 2012년 7월 25일
There is a bug in MATLAB's fprintf() on MS Windows that causes it not to print out all the decimal places. Please see the FEX contribution num2strexact

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by