MATLAB: Comparing two numbers by using IF in Matlab

faqif statement

I want to compare two numbers (x, and y), if they are equal or not.
However, even if these two numbers are equal, if condition in Matlab does not recognize them as equal numbers.
How I can make if statement recognize these two numbers if they are equal?
x=10.^(0.1.*4);
y=2.5119;
if x == y
z=1
'equal'
else
z=2
'Not Equal'
end

Best Answer

The code is correct.
The problem is that x - y is not precisely 0. The magnitude of the difference will be less than 1e-5
You have two options, define y to the 16th significant figure, or allow a tolerance like this:
if abs(x-y) < tolerance