why there is a small error in these simple calculations?

3e-4-3*10^-4
sum=0;
for i=1:1000
sum=sum+0.001;
end
sum_output=sum
this gives an answer of -5.421010862427522e-20 and 1.000000000000001
it should be obviously 0 and 1. what happened during the running of program??

댓글 수: 1

Stephen23
Stephen23 2019년 4월 11일
편집: Stephen23 2019년 4월 11일
"it should be obviously 0 and 1. "
Nope. Not with binary floating point numbers.
Search this forum for "floating point" and you will learn the reasons why this happens.

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

답변 (1개)

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

질문:

2019년 4월 11일

편집:

2019년 4월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by