Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
if statement

Subject: if statement

From: S C.Carl

Date: 20 Aug, 2013 14:20:27

Message: 1 of 4

I have these two codes in my project ;
  a= egnrm_stack(k-1,1);
  b= egnrm_stack(k,1);
 
 When I checked the values of them, I saw that
 a =

    0.0188

b =

    0.0188

 My codes are ;
    
if a==b
    p=1
else
    p=0
end

But the result of this block returns as;

p =
    0
    
As you see the result must be p=1, since a is equal to b.
Could you please help me, where is the mistake ? Why is the result as p=0 ?

Subject: if statement

From: Roger Stafford

Date: 20 Aug, 2013 15:52:08

Message: 2 of 4

"S C.Carl" wrote in message <kuvtvb$gv5$1@newscl01ah.mathworks.com>...
> a =
> 0.0188
> b =
> 0.0188
- - - - - - - - -
  The fact that the 'format short' displays of a and b are the same doesn't mean that a and b are equal. That display is rounded off to four decimal places and they could very easily be unequal. I suggest you try 'format hex' on them.

Roger Stafford

Subject: if statement

From: IceHaven

Date: 20 Aug, 2013 16:11:17

Message: 3 of 4

"Roger Stafford" wrote in message <kv03b8$mcl$1@newscl01ah.mathworks.com>...
> "S C.Carl" wrote in message <kuvtvb$gv5$1@newscl01ah.mathworks.com>...
> > a =
> > 0.0188
> > b =
> > 0.0188
> - - - - - - - - -
> The fact that the 'format short' displays of a and b are the same doesn't mean that a and b are equal. That display is rounded off to four decimal places and they could very easily be unequal. I suggest you try 'format hex' on them.
>
> Roger Stafford

With the data that you've given, your if statement could just as easily be

if abs(a-b) < 0.0001

It all depends on how exact you want to be, but matlab is able to carry a lot of places pass the decimal

Subject: if statement

From: Steven_Lord

Date: 20 Aug, 2013 16:48:43

Message: 4 of 4



"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:kv03b8$mcl$1@newscl01ah.mathworks.com...
> "S C.Carl" wrote in message <kuvtvb$gv5$1@newscl01ah.mathworks.com>...
>> a =
>> 0.0188
>> b =
>> 0.0188
> - - - - - - - - -
> The fact that the 'format short' displays of a and b are the same doesn't
> mean that a and b are equal. That display is rounded off to four decimal
> places and they could very easily be unequal. I suggest you try 'format
> hex' on them.

Or just execute:

difference = a-b

For == to say that they are the same, difference must be EXACTLY 0 (ignoring
nonfinite and nondouble edge cases involving saturation.) Close doesn't
count here; == is neither horseshoes nor hand grenades. In this case
difference will likely be a small number, but not exactly 0.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us