Error on summation of three numbers

조회 수: 6 (최근 30일)
alireza
alireza 2014년 3월 5일
답변: Roger Stafford 2014년 3월 5일
Hi, When we used this commands on commands windows of Matlab 2013a, we found a problem and we couldn't understand why that happened.
>> a=4.2; b=0.8; c=5;
>> a+b-c
ans = 0
>> a-c+b
ans = 2.2204e-16
>> b-c+a
ans = 0
The question is why on the second step (a-c+b) Matlab reached a number except zero! and how we can be sure that doesn't happen anymore?!

채택된 답변

A Jenkins
A Jenkins 2014년 3월 5일
  댓글 수: 2
alireza
alireza 2014년 3월 5일
Why? any body knows?
Jos (10584)
Jos (10584) 2014년 3월 5일
follow the link in A Jenkins' answer …

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

추가 답변 (1개)

Roger Stafford
Roger Stafford 2014년 3월 5일
To give you a very brief answer, your computer is using a binary representation of floating point numbers and cannot therefore exactly represent 4.2 and .8 . The resulting round-off errors produce the tiny difference you see here. In other words, round-off errors are causing the addition operation to not be strictly associative. It's the same difficulty a decimal calculator would have adding fractions like 1/3, 1/7, etc. It cannot represent them exactly and therefore makes tiny errors with them.

카테고리

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