EXTREMELY WEIRD arithmetic bug

조회 수: 1 (최근 30일)
louise
louise 2011년 8월 3일
Hi there,
Basically I'm adding 0.001 to a number over and over again. The series starts with zero so the resulting number should ALWAYS be a multiple of 0.01, but instead it eventually degenerates into numbers like 5.874000000000296 - obviously NOT a multiple of 0.001!
Can you explain why? Can it REALLY BE that Matlab just can't cope with numbers of that precision? It's a mathematics package!!!
Run the following code to reproduce the bug. I'm running 7.10.0(R2010a).
Just run log=testcase() and then inspect the latter half of log in the variable editor- when you double-click on a cell to expand it you will be able to see the lack of precision.
function [ log ] = testcase( )
%RUN LIKE THIS: log=testcase()
%Then inspect the log in the variable editor.
runTime=20;
t=0;
dx=0.001;
i=1;
log(i,1)=t;
it=2;
for i=2:runTime/dx
t=t+dx;
log(i,1)=t;
end
end

채택된 답변

Arnaud Miege
Arnaud Miege 2011년 8월 3일
  댓글 수: 1
louise
louise 2011년 8월 3일
I'm being an idiot. It's the floating point problem. I know all about the floating point problem.
SIGH.
I was stupid enough to assume that Matlab was using some futuristic high-precision arithmetic. Nope.
As usual, the problem is BAD ASSUMPTIONS.
Thanks for your fast help.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by