필터 지우기
필터 지우기

Comparison of very large numbers

조회 수: 5 (최근 30일)
shlomo odem
shlomo odem 2022년 3월 12일
댓글: Walter Roberson 2022년 3월 12일
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?

채택된 답변

Steven Lord
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
ans = logical
1
What's the distance from x to the next largest floating point number?
distance = eps(x)
distance = 1.7592e+13
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.

추가 답변 (3개)

Jan
Jan 2022년 3월 12일
편집: Jan 2022년 3월 12일
No, ismember does not round the values. Your assumption does not match the facts.
Why do you think that a rounding is applied? The rounding happens anywhere else. Prefer to avoid the rounding there.
Maybe ismembertol can help you.

Walter Roberson
Walter Roberson 2022년 3월 12일
편집: Walter Roberson 2022년 3월 12일
log2(1.056375500190191e+29)
ans = 96.4150
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)
A = 
105637550019019093335143874560
B = A + 1
B = 
105637550019019093335143874561
simplify(A == B)
ans = 
symfalse

John D'Errico
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.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by