Some difference between MatLab and VS calculation

14 ビュー (過去 30 日間)
Igor Arkhandeev
Igor Arkhandeev 2020 年 11 月 30 日
コメント済み: Igor Arkhandeev 2020 年 11 月 30 日
Hello! The question is very simple. I try to calculate exp(-8.7) in MatLab and Visual Studio software. My simple code is
fprintf("%.30f", exp(-8.7));
The results in Visual Studio 2019 and MatLab R2018b below:
0.000166585810987633539366828406 (VS)
0.000166585810987633512261774094 (MatLab)
Who can tell me what result more correct?


John D'Errico
John D'Errico 2020 年 11 月 30 日
編集済み: John D'Errico 2020 年 11 月 30 日
Which is more correct? Neither is correct in the bolded digits you post. In fact, the digit preceding that is also incorrect, in both cases.
ans =
In both cases, the digits beyond 987633... are garbage.
Both tools are computing an essentially double precision computation of exp(x), but the result will be correct only to roughly 16 significant digits. To go beyond that point is to compute meaningless dreck.
If you want to know which is "more" correct, I guess MATLAB wins here by a hair. It as slightly less incorrect in the last digit before it begins to diverge from the "correct" value.
As another point of comparison, below i use code I wrote myself (in MATLAB), so I know it to be correct. Here, 60 decimal digits are used, then rounded so only 50 were displayed:
ans =
And if you don't believe me (nobody ever seems to) then Wolfram Alpha tells me it is:
So my rounding to 50 digits seems to have been correct.
  1 件のコメント
Igor Arkhandeev
Igor Arkhandeev 2020 年 11 月 30 日
Wow! To be honest, I'm just thrilled! Thank you very much! I certainly haven't encountered this before, but I now have food for thought.


その他の回答 (0 件)


Community Treasure Hunt

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

Start Hunting!

Translated by