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:
the sign of the sum of the difference different from the sign of the difference of the sums

Subject: the sign of the sum of the difference different from the sign of the difference of the sums

From: Lorenzo Rossi

Date: 23 Dec, 2009 15:40:25

Message: 1 of 5

Dear experts,
I'm using matlab 2009b with win xp (coreduo E8500). I should calcolate the difference of the sum of two vectors and I noticed this misbehavior: (you can find the workspace with the two vectors at http://www.box.net/shared/1rtxb8xt28 ):

>> format long
>> sum(lpt -lvt)

ans =

   -1.455870024968571e-015

>> sum(lpt)-sum(lvt)

ans =

    1.136868377216160e-013

ok, the numbers are not equal due to the finite precision arithmetic. But, what about the sign? I cannot distinguish which one of the two sums is larger. Is there a way to determine the correct sign of the result, i.e. which one of the sums is larger?
Thanks in advance,

Lorenzo

Subject: the sign of the sum of the difference different from the sign of the difference of the sums

From: Bruno Luong

Date: 23 Dec, 2009 18:37:04

Message: 2 of 5

"Lorenzo Rossi" <lorenzo.rossi@epfl.ch> wrote in message <hgtdl9$212$1@fred.mathworks.com>...
> Dear experts,
> I'm using matlab 2009b with win xp (coreduo E8500). I should calcolate the difference of the sum of two vectors and I noticed this misbehavior: (you can find the workspace with the two vectors at http://www.box.net/shared/1rtxb8xt28 ):
>
> >> format long
> >> sum(lpt -lvt)
>
> ans =
>
> -1.455870024968571e-015
>
> >> sum(lpt)-sum(lvt)
>
> ans =
>
> 1.136868377216160e-013
>
> ok, the numbers are not equal due to the finite precision arithmetic. But, what about the sign? I cannot distinguish which one of the two sums is larger. Is there a way to determine the correct sign of the result, i.e. which one of the sums is larger?

The only way I can think of is to prove mathematically if you could do so.

Bruno

Subject: the sign of the sum of the difference different from the sign of the difference of the sums

From: Jan Simon

Date: 23 Dec, 2009 21:48:03

Message: 3 of 5

Dear Lorenzo!

> I'm using matlab 2009b with win xp (coreduo E8500). I should calcolate the difference of the sum of two vectors and I noticed this misbehavior: (you can find the workspace with the two vectors at http://www.box.net/shared/1rtxb8xt28 ):
>
> >> format long
> >> sum(lpt -lvt)
>
> ans =
>
> -1.455870024968571e-015
>
> >> sum(lpt)-sum(lvt)
>
> ans =
>
> 1.136868377216160e-013
>
> ok, the numbers are not equal due to the finite precision arithmetic. But, what about the sign? I cannot distinguish which one of the two sums is larger. Is there a way to determine the correct sign of the result, i.e. which one of the sums is larger?

In the presence of rounding errors, what does "correct sign" mean for results near to zero?
You can reduce the rounding errors by sorting the vectors and summing from the smallest to the largest values. Then you can sum the positive and the negative elements of the vectors separately. Or you can use a toolbox with a higher precision.
But with each method, you will probably get another result. So "the correct sign" will be meaningful only, if the applied method is specified together with the result.

Kind regards, Jan

Subject: the sign of the sum of the difference different from the sign of the difference of the sums

From: Derek O'Connor

Date: 23 Dec, 2009 23:28:02

Message: 4 of 5

"Lorenzo Rossi" <lorenzo.rossi@epfl.ch> wrote in message <hgtdl9$212$1@fred.mathworks.com>...
> Dear experts,
> I'm using matlab 2009b with win xp (coreduo E8500). I should calcolate the difference of the sum of two vectors and I noticed this misbehavior: (you can find the workspace with the two vectors at http://www.box.net/shared/1rtxb8xt28 ):
>
> >> format long
> >> sum(lpt -lvt)
>
> ans =
>
> -1.455870024968571e-015
>
> >> sum(lpt)-sum(lvt)
>
> ans =
>
> 1.136868377216160e-013
>
> ok, the numbers are not equal due to the finite precision arithmetic. But, what about the sign? I cannot distinguish which one of the two sums is larger. Is there a way to determine the correct sign of the result, i.e. which one of the sums is larger?
> Thanks in advance,
>
> Lorenzo

 
Lorenzo, here is what I got:
>> version
ans =
7.5.0.342 (R2007b)
>> format long
>> sum(lpt -lvt)
ans =
   -2.391210223535868e-013
>> sum(lpt)-sum(lvt)
ans =
   -2.273736754432321e-013

I don't understand why my results are not the same as yours. I thought the IEEE FP Standard was supposed to fix this.

Here are the results I got using Matlab's sum(v) and
Rump's IntLab 5.5 AccSum(v):

7.5.0.342 (R2007b)

v sum(v) AccSum(v)
lpt -1.749806794347390e+002 -1.749806794347391e+002
lvt -1.749806794347388e+002 -1.749806794347389e+002
(lpt-lvt) -2.273736754432321e-013 -2.391210223535868e-013
Sum(lpt)-Sum(lvt) -2.391210223535868e-013 -2.273736754432321e-013

>> sum(lvt<0)
ans =
   999
>> sum(lpt<0)
ans =
        1000
This shows that all the values in lpt and lvt are negative (except lvt(1)=0) so the signs of the sums
are the same -- negative.

Note that AccSum(lpt-lvt) = sum(lpt)-sum(lvt) and vice versa. Strange.

I tried to calculate the exact answers using Maxima and PariGP, but I gave up.
Maxima's HELP on file input was not helpful. PariGP --" what is file input?"

Anyway, an interesting summation problem.

Subject: the sign of the sum of the difference different from the sign of the difference of the sums

From: Lorenzo Rossi

Date: 11 Jan, 2010 13:27:04

Message: 5 of 5

> Lorenzo, here is what I got:
> >> version
> ans =
> 7.5.0.342 (R2007b)


oh, yes, this is my version:

>> version

ans =

7.9.0.529 (R2009b)

I should check on the 2007b...

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