numerical oddity with decimal multiplication

12 ビュー (過去 30 日間)
Michael Lenz
Michael Lenz 2023 年 8 月 9 日
編集済み: Stephen23 2023 年 8 月 9 日
Running R2022b, I noticed the following:
58*0.1-5.8
ans =
8.8818e-16
However,
58/(1/.1)-5.8
ans =
0
Why is that, and how can it be avoided? It messes up subsequent equality operations.
Some further testing shows that
58*0.1-5.8-4*eps
ans =
0
57*0.1-5.7
ans =
0
59*0.1-5.9
ans =
0
and in fact it's 0 for most numbers´I randomly tested, however
28*0.1-2.8-2*eps
ans =
0
Thanks in advance to anyone able and willing to clarify :)
  1 件のコメント
Stephen23
Stephen23 2023 年 8 月 9 日
編集済み: Stephen23 2023 年 8 月 9 日
"Why is that"
Because binary floating point numbers have finite precision, which has the effect that arithmetic operations on them necessarily accumulate floating point error. You (the programmer) need to understand that, and take that into account in your algorithms.
"and how can it be avoided?"
It can't be avoided (if you want to store data using binary floating point numbers).
This is worth reading as well:

サインインしてコメントする。

回答 (1 件)

the cyclist
the cyclist 2023 年 8 月 9 日
Possibly the most "popular" question on this forum. Read the Accuracy of Floating-Point Data section of the Floating-Point Numbers documentation.
(You will also get many other answers here, but they will all boil down to that same information.)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by