필터 지우기
필터 지우기

problem with low infinite number

조회 수: 1 (최근 30일)
Trop Trader
Trop Trader 2024년 3월 3일
이동: John D'Errico 2024년 3월 6일
Ediff(8,35)
ans =
7.4525e+03
profit(8,35)
ans =
7.4525e+03
bu=minus(profit,Ediff);
bu(8,35)
ans =
1.8190e-12
Why does subtracting 2 equal numbers produce a very small number?
  댓글 수: 1
Trop Trader
Trop Trader 2024년 3월 6일
이동: John D'Errico 2024년 3월 6일
ok thank

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

답변 (2개)

the cyclist
the cyclist 2024년 3월 3일
편집: the cyclist 2024년 3월 3일
Because their floating-point representations are not exactly equal.
You can see that they are not quite equal:
load("matlab_Ediff.mat","Ediff");
load("matlab_profit.mat","profit");
fprintf("Ediff = %16.14f\n",Ediff(8,35))
Ediff = 7452.50000000000182
fprintf("profit = %16.14f",profit(8,35))
profit = 7452.50000000000364

John D'Errico
John D'Errico 2024년 3월 3일
They only look the same, to 4 decimal places.
format short
x = 1.23456789;
y = 1.23456987;
[x,y]
ans = 1×2
1.2346 1.2346
They certainly look the same to me. But only if I don't look at their actual values, down to the last digits. If I subtract them of course, they are seen to be different.
x - y
ans = -1.9800e-06
Surely you would expect the difference to be non-zero, even though, when displayed as only 5 dignificant digits, they seem identical?
format long
[x,y]
ans = 1×2
1.234567890000000 1.234569870000000

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by