MATLAB gives the integer value of subtraction when the difference between numbers is large.
2 views (last 30 days)
Hello everyone. I have a problem in my code, i will show it with an example below:
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.
Sulaymon Eshkabilov on 25 Apr 2022
Edited: Sulaymon Eshkabilov on 25 Apr 2022
Specify flotaing point format accordingly, e.g.:
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
Steven Lord on 25 Apr 2022
What's the distance from the elements of b to the next larger number?
b = 10^23
distance = eps(b)
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!
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!
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.