Thread Subject: What happened to so simple Matlab code?

Subject: What happened to so simple Matlab code?

From: crammer008

Date: 21 Nov, 2007 09:11:12

Message: 1 of 10

Dear all, I have the following code and the corresponding results:
+++++++++++++++++++++++++++++++
>> aa = 0.005:0.01:0.025
aa =
    0.0050 0.0150 0.0250
>> daa = diff(aa)
daa =
    0.0100 0.0100
>> daa(1)-daa(2)
ans =
  1.7347e-018
++++++++++++++++++++++++++++++++++
Obviously, we should have daa(1)-daa(2) = 0. Who can tell me why Matlab can not give the correct result? Thanks!

Subject: What happened to so simple Matlab code?

From: Steve Amphlett

Date: 21 Nov, 2007 09:25:03

Message: 2 of 10

crammer008 <luochao2000@hotmail.com> wrote in message
<8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.o
rg>...
> Dear all, I have the following code and the corresponding
results:
> +++++++++++++++++++++++++++++++
> >> aa = 0.005:0.01:0.025
> aa =
> 0.0050 0.0150 0.0250
> >> daa = diff(aa)
> daa =
> 0.0100 0.0100
> >> daa(1)-daa(2)
> ans =
> 1.7347e-018
> ++++++++++++++++++++++++++++++++++
> Obviously, we should have daa(1)-daa(2) = 0. Who can tell
me why Matlab can not give the correct result? Thanks!


Check out the FAQ.

http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-
0.1_not_equal_to_zero_.28or_similar.29.3F

Subject: What happened to so simple Matlab code?

From: Steve Amphlett

Date: 21 Nov, 2007 09:30:51

Message: 3 of 10

"Steve Amphlett" <Firstname.Lastname@Where-I-Work.com>
wrote in message <fi0thf$9p1$1@fred.mathworks.com>...
> crammer008 <luochao2000@hotmail.com> wrote in message
>
<8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.o
> rg>...
> > Dear all, I have the following code and the
corresponding
> results:
> > +++++++++++++++++++++++++++++++
> > >> aa = 0.005:0.01:0.025
> > aa =
> > 0.0050 0.0150 0.0250
> > >> daa = diff(aa)
> > daa =
> > 0.0100 0.0100
> > >> daa(1)-daa(2)
> > ans =
> > 1.7347e-018
> > ++++++++++++++++++++++++++++++++++
> > Obviously, we should have daa(1)-daa(2) = 0. Who can
tell
> me why Matlab can not give the correct result? Thanks!
>
>
> Check out the FAQ.
>
> http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-
> 0.1_not_equal_to_zero_.28or_similar.29.3F

The URL got chopped up. Try this:


http://matlabwiki.mathworks.com/MATLAB_FAQ

Section 6.1

Subject: What happened to so simple Matlab code?

From: crammer008

Date: 21 Nov, 2007 14:49:20

Message: 4 of 10

Dear Steve Amphlett,

Thanks a lot for your useful information:)

Subject: What happened to so simple Matlab code?

From: crammer008

Date: 21 Nov, 2007 15:11:44

Message: 5 of 10

Dear Steve Amphlett,

As the code listed above, if I want to let daa(1)-daa(2) = 0, what function should be used in Matlab?

Thanks a lot!
LUO Chao

Subject: What happened to so simple Matlab code?

From: Steve Amphlett

Date: 21 Nov, 2007 16:42:03

Message: 6 of 10

crammer008 <luochao2000@hotmail.com> wrote in message
<12361651.1195657935111.JavaMail.jakarta@nitrogen.mathforum.
org>...
> Dear Steve Amphlett,
>
> As the code listed above, if I want to let daa(1)-daa(2)
= 0, what function should be used in Matlab?
>
> Thanks a lot!
> LUO Chao

You can't. 0.01 is one of the floating point "bogey"
numbers. It has no exact representation. You really have
to decide how close the values have to be to be considered
the same. Not a Matlab issue, just floating point. Now if
there was a BCD mode...

Subject: What happened to so simple Matlab code?

From: crammer008

Date: 22 Nov, 2007 02:44:21

Message: 7 of 10

Dear Steve Amphlett,

Thanks a lot for your great help! Now I think I have to set a threshold and use a "if" language to get rid of the difference below my threshold so that the accumulation of errors won't affect my result. Do you think it is a good way?

Thanks a lot!
LUO Chao

Subject: What happened to so simple Matlab code?

From: Nasser Abbasi

Date: 23 Nov, 2007 19:10:38

Message: 8 of 10


"crammer008" <luochao2000@hotmail.com> wrote in message
news:8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.org...
> Dear all, I have the following code and the corresponding results:
> +++++++++++++++++++++++++++++++
>>> aa = 0.005:0.01:0.025
> aa =
> 0.0050 0.0150 0.0250
>>> daa = diff(aa)
> daa =
> 0.0100 0.0100
>>> daa(1)-daa(2)
> ans =
> 1.7347e-018
> ++++++++++++++++++++++++++++++++++
> Obviously, we should have daa(1)-daa(2) = 0. Who can tell me why Matlab
> can not give the correct result? Thanks!


use VPA on the diff, then you will get your zero:

EDU>> aa = 0.005:0.01:0.025

aa =

    0.0050 0.0150 0.0250

EDU>> daa = vpa(diff(aa));
EDU>> double(daa(1)-daa(2))

ans =

     0

Nasser

Subject: What happened to so simple Matlab code?

From: Georgios

Date: 23 Nov, 2007 23:37:59

Message: 9 of 10

"Nasser Abbasi" <nma@12000.org> wrote in message
<RAF1j.26885$aN3.11505@newsfe12.phx>...
>
> "crammer008" <luochao2000@hotmail.com> wrote in message
>
news:8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.org...
> > Dear all, I have the following code and the
corresponding results:
> > +++++++++++++++++++++++++++++++
> >>> aa = 0.005:0.01:0.025
> > aa =
> > 0.0050 0.0150 0.0250
> >>> daa = diff(aa)
> > daa =
> > 0.0100 0.0100
> >>> daa(1)-daa(2)
> > ans =
> > 1.7347e-018
> > ++++++++++++++++++++++++++++++++++
> > Obviously, we should have daa(1)-daa(2) = 0. Who can
tell me why Matlab
> > can not give the correct result? Thanks!
>
>
> use VPA on the diff, then you will get your zero:
>
> EDU>> aa = 0.005:0.01:0.025
>
> aa =
>
> 0.0050 0.0150 0.0250
>
> EDU>> daa = vpa(diff(aa));
> EDU>> double(daa(1)-daa(2))
>
> ans =
>
> 0
>
> Nasser
>
>
>
The above code works using the variable precision arithmetic
function vpa that is part of the symbolic toolbox. The
student version comes with this built in. The professional
version does not, so not all people will be able to use it.

Regards,
Georgios

Subject: What happened to so simple Matlab code?

From: tudor dima

Date: 10 Jan, 2008 14:52:02

Message: 10 of 10

you can see this by simply comparing the outputs of these
two commands :

 >>0.3 + 0.1 == 0.4
 >>0.2 + 0.1 == 0.3

or directly

0.2 + 0.1 - 0.3

there is a paper on floating point representation, it might
have been cited before here :
http://docs.sun.com/source/806-3568/ncg_goldberg.html

crammer008 <luochao2000@hotmail.com> wrote in message
<8220611.1195636303283.JavaMail.jakarta@nitrogen.mathforum.org>...
> Dear all, I have the following code and the corresponding
results:
> +++++++++++++++++++++++++++++++
> >> aa = 0.005:0.01:0.025
> aa =
> 0.0050 0.0150 0.0250
> >> daa = diff(aa)
> daa =
> 0.0100 0.0100
> >> daa(1)-daa(2)
> ans =
> 1.7347e-018
> ++++++++++++++++++++++++++++++++++
> Obviously, we should have daa(1)-daa(2) = 0. Who can tell
me why Matlab can not give the correct result? Thanks!

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
floating point tudor dima 10 Jan, 2008 09:55:00
rssFeed for this Thread

Contact us at files@mathworks.com