Comparison of very large numbers
13 views (last 30 days)
Show older comments
shlomo odem
on 12 Mar 2022
Commented: Walter Roberson
on 12 Mar 2022
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 Comments
Accepted Answer
Steven Lord
on 12 Mar 2022
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 Comments
More Answers (3)
Walter Roberson
on 12 Mar 2022
Edited: Walter Roberson
on 12 Mar 2022
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 Comments
John D'Errico
on 12 Mar 2022
Edited: John D'Errico
on 12 Mar 2022
@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 Comment
Walter Roberson
on 12 Mar 2022
Possibly 10^-21 for some measurements; https://www.quora.com/What-is-the-most-accurate-physical-measurement-ever-measured
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!