In epsilon test for comparing equality of two numbers. How to improve the test?

13 visualizaciones (últimos 30 días)
In epsilon test for comparing equality of two numbers. How to improve the test?

Respuestas (2)

Walter Roberson
Walter Roberson el 9 de Sept. de 2022
  • is the tolerance absolute or is it relative? If it is relative, then relative to what?
  • what change needs to be made to the test to handle demoralized numbers?
  • what change needs to be made to the test to handle mixed datatype such as double and uint64?
  • are you sure you are handling nan correctly?
  • is your test fully vectorized? Even if the vectors include multiple cases?

John D'Errico
John D'Errico el 9 de Sept. de 2022
Editada: John D'Errico el 9 de Sept. de 2022
Can you "improve" the test? What does improving it mean?
How close do two numbers need to be considered the same? For example, if you have
x1 = 1;
x2 = 1 + eps(1);
They are not the same number.
x1 == x2
ans = logical
0
MATLAB knows that to be true. Yet the difference is as small as possible, because eps(1) is the distance from 1 and the next number MATLAB can represent. Those two numbers are the same, down to the least significant bit, where there is a tiny difference in that least significant bit of the numbers.
Only the user knows how close they are willing to allow the numbers to be. So you need to use a tolerance on the test. I typically might use something like 10*eps(x) or even 100*eps(x) as a tolerance, to decide if another number is reasonably close to x, but the exact amount would be pretty fuzzy.
But how can you improve that? Floating point has not changed in the last 4 years, so the two people who have commented are hoping that some magic solution will appear. There is no magic here though. When you use floating point arithmetic, you need to worry about those least significant bits.

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by