Comparison of very large numbers
13 次查看(过去 30 天)
显示 更早的评论
I am trying to compare a very large number, for example: 1.056375500190191e+29
And an array of numbers of similar size using the ismember function.
The problem is that because the numbers are very large the function rounds the number and therefore does not compare accurately.
Is there a way around the problem?
0 个评论
采纳的回答
Steven Lord
2022-3-12
What do you think the exact value of your variable is?
x = 1.056375500190191e+29;
y = x + 1e6;
x == y % true and this is the CORRECT answer
What's the distance from x to the next largest floating point number?
distance = eps(x)
Adding one million to your x value is like adding a millionth of a penny to the price of an item: it doesn't make a big enough distance to matter.
The problem is not rounding per se. The problem is that you can't represent all integers close to your x.
0 个评论
更多回答(3 个)
Walter Roberson
2022-3-12
编辑:Walter Roberson
2022-3-12
log2(1.056375500190191e+29)
You would be needing to use 97-bit (or more) integers in order to be able to distinguish adjacent integers in that range.
You could consider using the Symbolic Toolbox, which is able to represent varying number of digits.
A = sym(1.056375500190191e+29)
B = A + 1
simplify(A == B)
0 个评论
John D'Errico
2022-3-12
编辑:John D'Errico
2022-3-12
@Steven Lord said it exactly correct. The problem is not that a tool like ismember rounds anything, nor that ismembertol would help you. The problem is that as stored in MATLAB as a double, the numbers are already quantized to live in only 52 bits for a mantissa. And that means IF you need to compare numbers where the differences are relatively smaller than a double will allow, then you need not to store or compute the numbers as a double at all. And that means working entirely in a higher precision.
Tools like syms, or my own HPF (available for download from the file exchange) would allow you to do those things more precisely. However, they will also be VASTLY slower, so your next question will be in how to make your code run more quickly.
And that begs the question of whether you really need to be doing this computation to such a high degree of accuracy anyway. Do you KNOW those numbers EXACTLY to that many digits? Was the data that went into them known to 20+ decimal digits? I would argue that few things you can possibly measure are known that accurately.
Hmm. Let me see. An atomic clock can meaure time to something like 10^-18 seconds (maybe 10-20, as I saw in another link). And the fine structure constant, while known pretty accurately, has way worse precision, only something like 85 parts per trillion.
1 个评论
Walter Roberson
2022-3-12
Possibly 10^-21 for some measurements; https://www.quora.com/What-is-the-most-accurate-physical-measurement-ever-measured
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!