# Comparing two numbers by using IF in Matlab

조회 수: 45 (최근 30일)
Jamal Ahmad 2014년 10월 15일
답변: Image Analyst 2014년 10월 15일
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

댓글을 달려면 로그인하십시오.

### 채택된 답변

Iain 2014년 10월 15일
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

댓글을 달려면 로그인하십시오.

### 추가 답변 (2개)

Stephen Carter 2014년 10월 15일
They are not equal:
>> format long
>> x
x =
2.511886431509580
>> y
y =
2.511900000000000
##### 댓글 수: 0이전 댓글 -2개 표시이전 댓글 -2개 숨기기

댓글을 달려면 로그인하십시오.

Image Analyst 2014년 10월 15일
See the FAQ for an explanation and some code to make it do what you want: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by