Comparison of very large numbers

13 visualizaciones (últimos 30 días)
shlomo odem
shlomo odem el 12 de Mzo. de 2022
Comentada: Walter Roberson el 12 de Mzo. de 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?

Respuesta aceptada

Steven Lord
Steven Lord el 12 de Mzo. de 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
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.

Más respuestas (3)

Jan
Jan el 12 de Mzo. de 2022
Editada: Jan el 12 de Mzo. de 2022
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 el 12 de Mzo. de 2022
Editada: Walter Roberson el 12 de Mzo. de 2022
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 el 12 de Mzo. de 2022
Editada: John D'Errico el 12 de Mzo. de 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.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by