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:
Why does Matlab say these are not equal?

Subject: Why does Matlab say these are not equal?

From: Jeff

Date: 5 Mar, 2010 09:50:29

Message: 1 of 6

Can someone please explain why the last statement is returning 0 when the two statements seem equal to me.

EDU>> k=3;
EDU>> (9*2^k - 8*3^k)/6^k
ans =
  -0.666666666666667
EDU>> u(k+1)
ans =
  -0.666666666666667
EDU>> (9*2^k - 8*3^k)/6^k == u(k+1)
ans =
     0

The formula on the second line is the closed formula for u_k I obtained using a generating function on the following sequence:
u(0)=1
u(1)=-1
6u(k)=5u(k-1)-u(k-2)

I realize that way out in the sequence, there's bound to be some rounding error, but we're talking about the fourth term here. Why does Matlab say they're not equal?

Thanks,
-J

Subject: Why does Matlab say these are not equal?

From: Rune Allnor

Date: 5 Mar, 2010 10:03:23

Message: 2 of 6

On 5 Mar, 10:50, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net> wrote:
> Can someone please explain why the last statement is returning 0 when the two statements seem equal to me.
>
> EDU>> k=3;
> EDU>> (9*2^k - 8*3^k)/6^k
> ans =
>   -0.666666666666667
> EDU>> u(k+1)
> ans =
>   -0.666666666666667
> EDU>> (9*2^k - 8*3^k)/6^k == u(k+1)
> ans =
>      0
>
> The formula on the second line is the closed formula for u_k I obtained using a generating function on the following sequence:
> u(0)=1
> u(1)=-1
> 6u(k)=5u(k-1)-u(k-2)
>
> I realize that way out in the sequence, there's bound to be some rounding error, but we're talking about the fourth term here. Why does Matlab say they're not equal?

It's because of the rounding error.

Rune

Subject: Why does Matlab say these are not equal?

From: Bruno Luong

Date: 5 Mar, 2010 12:39:06

Message: 3 of 6

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hmqk55$f1c$1@fred.mathworks.com>...

> Why does Matlab say they're not equal?

Subtract them to see why.

Bruno

Subject: Why does Matlab say these are not equal?

From: John D'Errico

Date: 5 Mar, 2010 13:16:34

Message: 4 of 6

"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hmqk55$f1c$1@fred.mathworks.com>...
> Can someone please explain why the last statement is returning 0 when the two statements seem equal to me.
>
> EDU>> k=3;
> EDU>> (9*2^k - 8*3^k)/6^k
> ans =
> -0.666666666666667
> EDU>> u(k+1)
> ans =
> -0.666666666666667
> EDU>> (9*2^k - 8*3^k)/6^k == u(k+1)
> ans =
> 0
>
> The formula on the second line is the closed formula for u_k I obtained using a generating function on the following sequence:
> u(0)=1
> u(1)=-1
> 6u(k)=5u(k-1)-u(k-2)
>
> I realize that way out in the sequence, there's bound to be some rounding error, but we're talking about the fourth term here. Why does Matlab say they're not equal?
>
> Thanks,
> -J

What you misunderstand is that rounding error happens
after EVERY floating point addition, subtraction, etc.

The least significant bits of these numbers are not
identical, so they will not be equal.

John

Subject: Why does Matlab say these are not equal?

From: Jeff

Date: 5 Mar, 2010 20:54:22

Message: 5 of 6

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hmr07i$l98$1@fred.mathworks.com>...
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message <hmqk55$f1c$1@fred.mathworks.com>...
> > Can someone please explain why the last statement is returning 0 when the two statements seem equal to me.
> >
> > EDU>> k=3;
> > EDU>> (9*2^k - 8*3^k)/6^k
> > ans =
> > -0.666666666666667
> > EDU>> u(k+1)
> > ans =
> > -0.666666666666667
> > EDU>> (9*2^k - 8*3^k)/6^k == u(k+1)
> > ans =
> > 0
> >
> > The formula on the second line is the closed formula for u_k I obtained using a generating function on the following sequence:
> > u(0)=1
> > u(1)=-1
> > 6u(k)=5u(k-1)-u(k-2)
> >
> > I realize that way out in the sequence, there's bound to be some rounding error, but we're talking about the fourth term here. Why does Matlab say they're not equal?
> >
> > Thanks,
> > -J
>
> What you misunderstand is that rounding error happens
> after EVERY floating point addition, subtraction, etc.
>
> The least significant bits of these numbers are not
> identical, so they will not be equal.
>
> John

OK. Thanks all. That makes sense now.

How can I write this type of code correctly? Do I just multiply both by 10^n, round, and compare?

Subject: Why does Matlab say these are not equal?

From: Matt Fig

Date: 5 Mar, 2010 21:01:34

Message: 6 of 6

One approach:

>> A = -.1 + .4 -.1 + .1
A =
          0.3
>> B = .3
B =
          0.3
>> A==B
ans =
     0
>> tol = 1e-10; % Define a tolerence
>> abs(A-B)<tol
ans =
     1

Tags for 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