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:
(a+b) -a is not b ??

Subject: (a+b) -a is not b ??

From: rahul

Date: 12 Sep, 2011 21:39:30

Message: 1 of 9

copied from matlab screen :

a=10^5;
b=10^5+10^-11;
b-a

ans =

  1.4552e-011

    could anyone please tell me how to get rid of this problem? I guess that maltab uses fourier transform for adding two numbers and this happens due to gibbs phenomena.

Subject: (a+b) -a is not b ??

From: TideMan

Date: 12 Sep, 2011 21:51:59

Message: 2 of 9

On Sep 13, 9:39 am, "rahul " <rahuli...@gmail.com> wrote:
> copied from matlab screen :
>
> a=10^5;
> b=10^5+10^-11;
> b-a
>
> ans =
>
>   1.4552e-011
>
>     could anyone please tell me how to get rid of this problem? I guess that maltab uses fourier transform for adding two numbers and this happens due to gibbs phenomena.  

No, it's nothing so complicated.
See 6.1 here:
http://matlab.wikia.com/wiki/FAQ

Subject: (a+b) -a is not b ??

From: rahul

Date: 12 Sep, 2011 22:12:28

Message: 3 of 9

TideMan <mulgor@gmail.com> wrote in message <8ee7d936-4d5c-4705-82a6-28ef0231eb45@u7g2000pro.googlegroups.com>...
> On Sep 13, 9:39 am, "rahul " <rahuli...@gmail.com> wrote:
> > copied from matlab screen :
> >
> > a=10^5;
> > b=10^5+10^-11;
> > b-a
> >
> > ans =
> >
> >   1.4552e-011
> >
> >     could anyone please tell me how to get rid of this problem? I guess that maltab uses fourier transform for adding two numbers and this happens due to gibbs phenomena.  
>
> No, it's nothing so complicated.
> See 6.1 here:
> http://matlab.wikia.com/wiki/FAQ

Tideman,
   thanks for the reply, but i coiuldn't find anything which explains this in the link you posted. did you mean section 6.1? there isn't any.

Subject: (a+b) -a is not b ??

From: ScottB

Date: 12 Sep, 2011 22:41:29

Message: 4 of 9

"rahul " <rahuliitk@gmail.com> wrote in message <j4m04c$58i$1@newscl01ah.mathworks.com>...
> TideMan <mulgor@gmail.com> wrote in message <8ee7d936-4d5c-4705-82a6-28ef0231eb45@u7g2000pro.googlegroups.com>...
> > On Sep 13, 9:39 am, "rahul " <rahuli...@gmail.com> wrote:
> > > copied from matlab screen :
> > >
> > > a=10^5;
> > > b=10^5+10^-11;
> > > b-a
> > >
> > > ans =
> > >
> > >   1.4552e-011
> > >
> > >     could anyone please tell me how to get rid of this problem? I guess that maltab uses fourier transform for adding two numbers and this happens due to gibbs phenomena.  
> >
> > No, it's nothing so complicated.
> > See 6.1 here:
> > http://matlab.wikia.com/wiki/FAQ
>
> Tideman,
> thanks for the reply, but i coiuldn't find anything which explains this in the link you posted. did you mean section 6.1? there isn't any.

Rahul,
 Take a look at this: http://www.mathworks.com/support/tech-notes/1100/1108.html

hth,
ScottB

Subject: (a+b) -a is not b ??

From: TideMan

Date: 12 Sep, 2011 22:47:43

Message: 5 of 9

On Sep 13, 10:12 am, "rahul " <rahuli...@gmail.com> wrote:
> TideMan <mul...@gmail.com> wrote in message <8ee7d936-4d5c-4705-82a6-28ef0231e...@u7g2000pro.googlegroups.com>...
> > On Sep 13, 9:39 am, "rahul " <rahuli...@gmail.com> wrote:
> > > copied from matlab screen :
>
> > > a=10^5;
> > > b=10^5+10^-11;
> > > b-a
>
> > > ans =
>
> > >   1.4552e-011
>
> > >     could anyone please tell me how to get rid of this problem? I guess that maltab uses fourier transform for adding two numbers and this happens due to gibbs phenomena.  
>
> > No, it's nothing so complicated.
> > See 6.1 here:
> >http://matlab.wikia.com/wiki/FAQ
>
> Tideman,
>    thanks for the reply, but i coiuldn't find anything which explains this in the link you posted. did you mean section 6.1? there isn't any.    

Well, you didn't look very hard.
Try this (which is Section 6.1):
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Subject: (a+b) -a is not b ??

From: Derek O'Connor

Date: 13 Sep, 2011 01:40:13

Message: 6 of 9

"rahul " <rahuliitk@gmail.com> wrote in message <j4lu6i$rfd$1@newscl01ah.mathworks.com>...
> copied from matlab screen :
>
> a=10^5;
> b=10^5+10^-11;
> b-a
>
> ans =
>
> 1.4552e-011
>
> could anyone please tell me how to get rid of this problem? I guess that maltab uses fourier transform for adding two numbers and this happens due to gibbs phenomena.

-----------------------------------------------

a = 10^5; b = 10^5 + 10^(-11); c= b-a gives 1.455191522836685e-011


The problem occurs in the expression b = 10^5 + 10^(-11).

The next floating point number above 10^5 is 10^5 + eps(10^5). Any
number that falls between these two adjacent fpns must be rounded
down to xL = 10^5 or up to xU = 10^5 + eps(10^5), using the
standard "round to nearest" rule.

b = 10^5 + 10^(-11) is nearer xU so it is rounded up to 10^5 + eps(10^5).

Thus b - a = 10^5 + eps(10^5) - 10^5 = eps(10^5) = 1.455191522836685e-011

How do you get rid of this "problem"? It is not a problem because Matlab is giving a correctly-rounded result, using the default "round-to-nearest" rule.

You could try changing the rounding mode, but I don't advise that unless you know what your are doing, and understand all the subtleties of floating point arithmetic.

Subject: (a+b) -a is not b ??

From: Nasser M. Abbasi

Date: 13 Sep, 2011 04:21:01

Message: 7 of 9

On 9/12/2011 2:39 PM, rahul wrote:
> copied from matlab screen :
>
> a=10^5;
> b=10^5+10^-11;
> b-a
>
> ans =
>
> 1.4552e-011
>
> could anyone please tell me how to get rid of this problem?
> I guess that maltab uses fourier transform for adding two numbers and this
> happens due to gibbs phenomena.

here is how to 'get rid' of it

EDU>> clear all
a=sym(10^5);
b=sym(10^5)+sym(10^-11);
b-a
  
ans =
  
1/100000000000


But you probably do not want to do this in your normal mode of
computation.

--Nasser

Subject: (a+b) -a is not b ??

From: rahul

Date: 13 Sep, 2011 11:18:11

Message: 8 of 9

Thanks to all of you for the help. I understand that this is due to round off and the gap between two floating numbers grow with their magnitude.
So if we have a function F(x) and we want to know the difference between it's roots (roots are closely and evenly spaced) then at smaller x we can calculate it but at larger x we can't.
"Nasser M. Abbasi" <nma@12000.org> wrote in message <j4mlnd$ohs$1@speranza.aioe.org>...
> On 9/12/2011 2:39 PM, rahul wrote:
> > copied from matlab screen :
> >
> > a=10^5;
> > b=10^5+10^-11;
> > b-a
> >
> > ans =
> >
> > 1.4552e-011
> >
> > could anyone please tell me how to get rid of this problem?
> > I guess that maltab uses fourier transform for adding two numbers and this
> > happens due to gibbs phenomena.
>
> here is how to 'get rid' of it
>
> EDU>> clear all
> a=sym(10^5);
> b=sym(10^5)+sym(10^-11);
> b-a
>
> ans =
>
> 1/100000000000
>
>
> But you probably do not want to do this in your normal mode of
> computation.
>
> --Nasser

Subject: (a+b) -a is not b ??

From: John D'Errico

Date: 13 Sep, 2011 15:21:27

Message: 9 of 9

"rahul " <rahuliitk@gmail.com> wrote in message <j4ne5j$ag3$1@newscl01ah.mathworks.com>...
> Thanks to all of you for the help. I understand that this is due to round off and the gap between two floating numbers grow with their magnitude.
> So if we have a function F(x) and we want to know the difference between it's roots (roots are closely and evenly spaced) then at smaller x we can calculate it but at larger x we can't.

No. First of all, this has no relevance to root finding. It is a simple
fact of floating point arithmetic, that the least significant bit(s) of
any non-integer is a problematic thing. It is even true for integers
when they exceed 2^53 (in double precision form.)

>> a=10^5;

See that eps(a) is on the order of b in your example.

>> eps(a)
ans =
   1.4552e-11

However, even for numbers that are near in magnitude
to each other, NEVER presume that simple arithmetical
tautologies universally hold when you are working in
floating point arithmetic.

>> a = 0.1;
>> b = rand(1)
b =
      0.90579

>> ((a + b) - b) == a
ans =
     0

Welcome to the strange and beautiful world of floating
point arithmetic.

John

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