Cannot subtract number of order smaller then e-4 from variable

9 ビュー (過去 30 日間)
Dominik Stolfa
Dominik Stolfa 2024 年 11 月 26 日
コメント済み: Dominik Stolfa 2024 年 11 月 26 日

a=0.1; a=a-1e-5 gives result a=0.1000

採用された回答

Torsten
Torsten 2024 年 11 月 26 日
編集済み: Torsten 2024 年 11 月 26 日
It's just a question of how the numbers are displayed.
Internally, the precision of computing is much higher.
format long % Use display format long
a = 0.1;
a = a - 1e-5
a =
0.099990000000000
format short
a
a = 0.1000
  3 件のコメント
Walter Roberson
Walter Roberson 2024 年 11 月 26 日
編集済み: Walter Roberson 2024 年 11 月 26 日
format long % Use display format long
a = 0.1;
fprintf('%.999g\n', a)
0.1000000000000000055511151231257827021181583404541015625
a = a - 1e-5
a =
0.099990000000000
fprintf('%.999g\n', a)
0.09999000000000000942801392511682934127748012542724609375
Dominik Stolfa
Dominik Stolfa 2024 年 11 月 26 日
Thank you for nice example. I was confused before whether the Matlab changes the values at all because even after subtracting 6e-5 or so, the values were still same. I didn’t go past up to 9e-5 because I was like “what are the chances”. :D

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by