Discrepancy in two Matlab session results

2 ビュー (過去 30 日間)
Abol Hol
Abol Hol 2021 年 1 月 25 日
回答済み: Gaurav Garg 2021 年 1 月 27 日
I am dividing similar numbers in two matlab sessions, in one I get 0.5 as expected in the another I get: 0.499999825156179
Does someone know why?
  2 件のコメント
Mischa Kim
Mischa Kim 2021 年 1 月 25 日
Please share your code
dpb
dpb 2021 年 1 月 25 日
>> 0.5-0.499999825156179
ans =
1.7484e-07
>>
As Mischa requests, we couldn't say why precisely w/o the code to see the details of how the two sessions differ and the code and data used.
As the above difference shows, the difference is just about the precision of a single precision floating point number; the usual reason for such differences something like having written a value to a text file with only 7-8 digits of precision and then reading it back as compared to another case of entering an exact value at keyboard.
There are an infinite number of "variation on a theme" of the scenario, of course.

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

回答 (1 件)

Gaurav Garg
Gaurav Garg 2021 年 1 月 27 日
Hi,
You can set the precision of the variables used by using digits function (doc link).
Print the operation using vpa operation and you will get the answer according to the specified precision.
vpa(0.5-0.499999825156179)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by