Why 0.6+0.3+0.1-1 is -1.11022302462516e-16, not 0 ? why?? A serials bug??

조회 수: 6 (최근 30일)
denny
denny 2018년 6월 20일
답변: cdarling 2025년 6월 4일 1:46
0.6+0.3+0.1-1
  댓글 수: 1
denny
denny 2019년 9월 9일
편집: denny 2025년 2월 26일
This is a bug? this is bucase the numeric calculation in a computer.
You can use a fraction to calculate it.

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

답변 (1개)

cdarling
cdarling 2025년 6월 4일 1:46
As a numerical calculation software, MATLAB stores data as double data type by default.
IEEE-754 is the standard for double data type.
This may better explained by the following code:
>> num2hex(0.1)
ans =
'3fb999999999999a'
>> num2hex(0.1+0.1+0.1)
ans =
'3fd3333333333334'
>> num2hex(0.3)
ans =
'3fd3333333333333'
If you need to calculate the theoretical value, you may try Symbolic Math Toolbox:
sym('0.6')+sym('0.3')+sym('0.1')-sym('1')

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!