MATLAB gives the integer value of subtraction when the difference between numbers is large.

4 ビュー (過去 30 日間)
Mustafa Duran
Mustafa Duran 2022 年 4 月 25 日
コメント済み: Steven Lord 2022 年 4 月 25 日
Hello everyone. I have a problem in my code, i will show it with an example below:
a=[1;1;1;1];
b=[10^23;10^23;10^23;10^23];
c=a-b
fprintf("%f \n",c)
Results:
c =
1.0e+23 *
-1.000000000000000
-1.000000000000000
-1.000000000000000
-1.000000000000000
-99999999999999991611392.000000
-99999999999999991611392.000000
-99999999999999991611392.000000
-99999999999999991611392.000000
When i take action with matrix it is problematic that MATLAB give the subtraction's results as integer not float, how can i change it?
Thanks for helps.

回答 (2 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022 年 4 月 25 日
編集済み: Sulaymon Eshkabilov 2022 年 4 月 25 日
Specify flotaing point format accordingly, e.g.:
a=[1;1;1;1];
b=[10^23;10^23;10^23;10^23];
c=a-b
fprintf("%1999.0f \n",c)
Still it does not display the correct answer. To get a correct display you would need to use these scripts posted here: mathworks.com/matlabcentral/fileexchange/22725-variable-precision-integer-arithmetic
  1 件のコメント
Mustafa Duran
Mustafa Duran 2022 年 4 月 25 日
Thanks but i will use array of "c" in another calculation in a loop, it uses false answer which is integer one.
I used vpi command but it gives that error:
If N is a double, it may be no larger than 2^53 - 1

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


Steven Lord
Steven Lord 2022 年 4 月 25 日
What's the distance from the elements of b to the next larger number?
b = 10^23
b = 1.0000e+23
distance = eps(b)
distance = 16777216
If you were to add 1 to b, you don't get anywhere close to the next representable floating point number.
c = b + 1;
c == b % true, and no this is NOT a bug!
ans = logical
1
The distance between b and the next smallest representable floating point number is also much larger than 1, so if you subtract 1 you stay at b.
d = b - 1;
d == b % true and this is NOT a bug either!
ans = logical
1
Basically what you're doing is handing Elon Musk, Jeff Bezos, or Bill Gates a $1 bill and expecting his net worth to change. It's negligible compared to what they already have. See the "Accuracy of Floating-Point Data" section on this documentation page for more information.
  2 件のコメント
Mustafa Duran
Mustafa Duran 2022 年 4 月 25 日
編集済み: Mustafa Duran 2022 年 4 月 25 日
Normally, you were right at last comment however i need this value to calculate the error term until error value is lower than 0.001 etc and after that i will break the iteration. With this rounding, MATLAB calculates error value 0 immediately, just because the not changing of values.
Steven Lord
Steven Lord 2022 年 4 月 25 日
This is not just MATLAB behavior. In IEEE double precision 1 is negligible compared to 10^23.
To put that in another perspective, consider something that weighs 1 kilogram, like a 1L bottle of water. Is that negligible compared to Earth's moon (with a mass of 7.3*10^22 kg according to Wikipedia)? For practical purposes, yes.
If b is just a placeholder starting value that you're using to give you an error value that's basically guaranteed to be larger than 0.001 for the first iteration, choose a smaller b.

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

カテゴリ

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