Comparison of very large numbers
조회 수: 5 (최근 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!