# floating point arithmetics, mathlab's interpretation of a . after a number

조회 수: 1 (최근 30일)
David Ortega . 2020년 10월 17일
댓글: Walter Roberson . 2020년 10월 19일
Regarding mathlab's reading of the . operator after a number I know that, for example, that if i introduce it when miltuplying two matrixes A.*B of the same dimentions Mathlab will yield a matrix that has multiplied each entry like so A[i,i]*B[i,i].
When introducing values
the following values:
h=1./2.;
x=2./3.-h;
y=3./5.-h;
e=(x+x+x)-h;
printing e yields -1.110223024625157e-16
but when I do x+x+x it yields = 0.5
Why doesn't e yield 0?
I know it has to do with something of the sort of the matrix problem but i do not know for certain and floating point arithmetic but i do not know for certain.

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

### 채택된 답변

Stephen23 2020년 10월 17일
편집: Stephen23 님. 2020년 10월 17일
"Why doesn't e yield 0?"
Because most of your values cannot be exactly represented using binary floating point numbers.
The values stored in your computer's memory are only approximations of those values (to a finite precision), and that approximation means that they include some floating point error. So when you perform arithmetic operations on those values, sometimes that floating point error accumulates during the operation.. .and in some cases, that accumulation can cancel-out completely (important note: this cancelation does not change the precision of the output!). This is exactly what your two examples show: one of the operations happens to accumulate some error which remains in the output, another operation happens to cancel out the error so that the output looks like it is "exact" (in fact in both cases the output precision is much the same).
Lets have a look at the exact values that your computer is calculating with:
>> num2strexact(x)
ans =
0.16666666666666662965923251249478198587894439697265625
>> num2strexact(y)
ans =
9.999999999999997779553950749686919152736663818359375e-2
Learn about binary floating point numbers and how they behave:
This is worth reading as well:
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
David Ortega 2020년 10월 19일
thanks a lot! i'll look into it!

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

### 추가 답변 (1개)

Matlab sotere double numbers in 8 bytes (64 bits) . So if you do some aritmatic operations, there is an error due to the limits on the space for each number I you can see some small numbers instead of 0. The number 0.5 in binary format is exact and you can see 0.5 as a result. Also, it is not necessary to use . after numbers. 1. is the same as 1
##### 댓글 수: 2없음 표시없음 숨기기
David Ortega 2020년 10월 19일
thanks!
Walter Roberson 2020년 10월 19일
Side note: There are cases inside symbolic expressions where 1. is not the same as 1
For example,
>> subs(str2sym({'x^(1/2)', 'x^(1/2.)'}),x,3)
ans =
[ 3^(1/2), 1.7320508075688772935274463415059]
The 2. compared to plain 2 made the difference between treating the calculation as an exact system or as a floating-point calculation to be approximated.

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

### 카테고리

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

### Community Treasure Hunt

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

Start Hunting!

Translated by