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

10 次查看(过去 30 天)
In epsilon test for comparing equality of two numbers. How to improve the test?

回答(2 个)

Walter Roberson
Walter Roberson 2022-9-9
  • 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 2022-9-9
编辑:John D'Errico 2022-9-9
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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by