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:
Is it OK to set almost zero values to zero?

Subject: Is it OK to set almost zero values to zero?

From: Jeff

Date: 1 Sep, 2011 22:36:27

Message: 1 of 16

For my project I have some initial conditions which are set to a sine way. That is:
n=(1:N);
s0=[sin(2*pi*n/N)];

If I were to set s0 by paper, the s0(N)=sin(2*pi*16/16)=sin(2*pi)=0, for any N.

Because of Matlab's rounding (or other) issues, when N=16, s0(N)=-2.44929359829471e-16, which is almost zero.

Does it make things better if I add s0(N)=0 to my code? It's always true, so theoretically it should make things more accurate.

But I don't know exactly what's happening in Matlab's internals. Will that value make things worse later on?

(P.S. Same question for s0(N/2), which equals 0, but Matlab sets to 1.22464679914735e-16.)

Subject: Is it OK to set almost zero values to zero?

From: Nasser M. Abbasi

Date: 1 Sep, 2011 23:22:13

Message: 2 of 16

On 9/1/2011 3:36 PM, Jeff wrote:
> For my project I have some initial conditions which are set to a sine way. That is:
> n=(1:N);
> s0=[sin(2*pi*n/N)];
>
> If I were to set s0 by paper, the s0(N)=sin(2*pi*16/16)=sin(2*pi)=0, for any N.
>
...
> (P.S. Same question for s0(N/2), which equals 0, but Matlab sets to 1.22464679914735e-16.)


You could always write your own 'chop' function which does this.

Have the function take the number and some epsilon, and check if
the absolute value of the number is less than this epsilon. If it is, return 0.

btw, Mathematica has this exact function build in, called Chop

http://reference.wolfram.com/mathematica/ref/Chop.html

May be you can use the above to write your Matlab function. It should
be trivial to do.

--Nasser

Subject: Is it OK to set almost zero values to zero?

From: Nasser M. Abbasi

Date: 1 Sep, 2011 23:26:16

Message: 3 of 16

On 9/1/2011 4:22 PM, Nasser M. Abbasi wrote:
> On 9/1/2011 3:36 PM, Jeff wrote:
>> For my project I have some initial conditions which are set to a sine way. That is:
>> n=(1:N);
>> s0=[sin(2*pi*n/N)];
>>
>> If I were to set s0 by paper, the s0(N)=sin(2*pi*16/16)=sin(2*pi)=0, for any N.
>>
> ...
>> (P.S. Same question for s0(N/2), which equals 0, but Matlab sets to 1.22464679914735e-16.)
>

>
> You could always write your own 'chop' function which does this.
>
> Have the function take the number and some epsilon, and check if
> the absolute value of the number is less than this epsilon. If it is, return 0.
>
> btw, Mathematica has this exact function build in, called Chop
>
> http://reference.wolfram.com/mathematica/ref/Chop.html
>
> May be you can use the above to write your Matlab function. It should
> be trivial to do.
>

and btw, you do really not want to do this chopping in the middle of
computation. I normally use it at the very end of the computation, and
for display of data purposes (make report, print, etc...)

--Nasser

Subject: Is it OK to set almost zero values to zero?

From: TideMan

Date: 1 Sep, 2011 23:27:03

Message: 4 of 16

On Sep 2, 10:36 am, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
wrote:
> For my project I have some initial conditions which are set to a sine way. That is:
> n=(1:N);
> s0=[sin(2*pi*n/N)];
>
> If I were to set s0 by paper, the s0(N)=sin(2*pi*16/16)=sin(2*pi)=0, for any N.
>
> Because of Matlab's rounding (or other) issues, when N=16, s0(N)=-2.44929359829471e-16, which is almost zero.
>
> Does it make things better if I add s0(N)=0 to my code?  It's always true, so theoretically it should make things more accurate.
>
> But I don't know exactly what's happening in Matlab's internals. Will that value make things worse later on?
>
> (P.S. Same question for s0(N/2), which equals 0, but Matlab sets to 1.22464679914735e-16.)

IMHO, it is NOT a good idea.
You need to write code that accepts that such values are essentially
zero.
Otherwise, on the day when you forget to do it, you will get caught.

This means when dealing with floating point numbers you should always
do this:
if abs(y-y0) < 1e-6
not this:
if y == y0

Subject: Is it OK to set almost zero values to zero?

From: Jeff

Date: 1 Sep, 2011 23:55:30

Message: 5 of 16

"Nasser M. Abbasi" <nma@12000.org> wrote in message <j3p4bs$59h$2@speranza.aioe.org>...
> On 9/1/2011 4:22 PM, Nasser M. Abbasi wrote:

>
> and btw, you do really not want to do this chopping in the middle of
> computation. I normally use it at the very end of the computation, and
> for display of data purposes (make report, print, etc...)
>
> --Nasser

That's my real question. Is it a good or bad idea to do that?


TideMan <mulgor@gmail.com> wrote in message <5a1a9551-f74d-4545-8460-b6df4f29b96c@f31g2000prj.googlegroups.com>...
> On Sep 2, 10:36 am, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
> wrote:
> > For my project I have some initial conditions which are set to a sine way. That is:
> > n=(1:N);
> > s0=[sin(2*pi*n/N)];
> >
> > If I were to set s0 by paper, the s0(N)=sin(2*pi*16/16)=sin(2*pi)=0, for any N.
> >
> > Because of Matlab's rounding (or other) issues, when N=16, s0(N)=-2.44929359829471e-16, which is almost zero.
> >
> > Does it make things better if I add s0(N)=0 to my code?  It's always true, so theoretically it should make things more accurate.
> >
> > But I don't know exactly what's happening in Matlab's internals. Will that value make things worse later on?
> >
> > (P.S. Same question for s0(N/2), which equals 0, but Matlab sets to 1.22464679914735e-16.)
>
> IMHO, it is NOT a good idea.
> You need to write code that accepts that such values are essentially
> zero.
> Otherwise, on the day when you forget to do it, you will get caught.
>
> This means when dealing with floating point numbers you should always
> do this:
> if abs(y-y0) < 1e-6
> not this:
> if y == y0

That's kind of what I thought. Thanks.

Subject: Is it OK to set almost zero values to zero?

From: Nasser M. Abbasi

Date: 2 Sep, 2011 01:17:12

Message: 6 of 16

On 9/1/2011 4:55 PM, Jeff wrote:
> "Nasser M. Abbasi"<nma@12000.org> wrote in message<j3p4bs$59h$2@speranza.aioe.org>...
>> On 9/1/2011 4:22 PM, Nasser M. Abbasi wrote:
>
>>
>> and btw, you do really not want to do this chopping in the middle of
>> computation. I normally use it at the very end of the computation, and
>> for display of data purposes (make report, print, etc...)
>>
>> --Nasser
>
> That's my real question. Is it a good or bad idea to do that?
>
>

You have to decide. If for example, you are making a report
on the final results of a physical or engineering simulation
and the main scale of the problem main variable(s) (after it was
normalized) is 1, then you probably do not care about values in the
10^-10 scale.

So, I do not think there is a rule on this. It depends on the
context and the problem.

--Nasser

Subject: Is it OK to set almost zero values to zero?

From: Derek O'Connor

Date: 2 Sep, 2011 15:40:31

Message: 7 of 16

TideMan <mulgor@gmail.com> wrote in message <5a1a9551-f74d-4545-8460-b6df4f29b96c@f31g2000prj.googlegroups.com>...
> ----------------------snip---------------------
>
> This means when dealing with floating point numbers you should always
> do this:
> if abs(y-y0) < 1e-6
> not this:
> if y == y0

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

y0 = 1; y = 2; abs(y-y0) < 1e-6 ?

y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6 ?

Do you wish to modify your advice?

Subject: Is it OK to set almost zero values to zero?

From: Derek O'Connor

Date: 2 Sep, 2011 15:41:10

Message: 8 of 16

TideMan <mulgor@gmail.com> wrote in message <5a1a9551-f74d-4545-8460-b6df4f29b96c@f31g2000prj.googlegroups.com>...
> ----------------------snip---------------------
>
> This means when dealing with floating point numbers you should always
> do this:
> if abs(y-y0) < 1e-6
> not this:
> if y == y0

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

y0 = 1; y = 2; abs(y-y0) < 1e-6 ?

y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6 ?

Do you wish to modify your advice?

Subject: Is it OK to set almost zero values to zero?

From: TideMan

Date: 2 Sep, 2011 21:21:59

Message: 9 of 16

On Sep 3, 3:41 am, "Derek O'Connor" <derekrocon...@eircom.net> wrote:
> TideMan <mul...@gmail.com> wrote in message <5a1a9551-f74d-4545-8460-b6df4f29b...@f31g2000prj.googlegroups.com>...
> >  ----------------------snip---------------------
>
> > This means when dealing with floating point numbers you should always
> > do this:
> > if abs(y-y0) < 1e-6
> > not this:
> > if y == y0
>
> ------------------------------------------------------
>
> y0 = 1; y = 2; abs(y-y0) < 1e-6   ?
>
> y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6   ?
>
> Do you wish to modify your advice?

And how many angels do you reckon can dance on the head of a pin?

Subject: Is it OK to set almost zero values to zero?

From: Jeff

Date: 2 Sep, 2011 21:53:11

Message: 10 of 16

"Derek O'Connor" wrote in message <j3qtem$ndb$1@newscl01ah.mathworks.com>...
>
> y0 = 1; y = 2; abs(y-y0) < 1e-6 ?
>
> y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6 ?
>
> Do you wish to modify your advice?

Pardon me if I've miscounted the number of angels, but I'm missing the point. I ran these two lines
EDU>> y0 = 1; y = 2; abs(y-y0) < 1e-6
ans =
     0
EDU>> y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6
ans =
     1

which looks right to me (I'm running v7.8 32-bit student version).

Subject: Is it OK to set almost zero values to zero?

From: TideMan

Date: 3 Sep, 2011 04:52:49

Message: 11 of 16

On Sep 3, 9:53 am, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
wrote:
> "Derek O'Connor" wrote in message <j3qtem$nd...@newscl01ah.mathworks.com>...
>
> > y0 = 1; y = 2; abs(y-y0) < 1e-6   ?
>
> > y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6   ?
>
> > Do you wish to modify your advice?
>
> Pardon me if I've miscounted the number of angels, but I'm missing the point. I ran these two lines
> EDU>> y0 = 1; y = 2; abs(y-y0) < 1e-6  
> ans =
>      0
> EDU>> y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6
> ans =
>      1
>
> which looks right to me (I'm running v7.8 32-bit student version).

Yes, you are right.
There are some trolls about.
Just ignore him.

Subject: Is it OK to set almost zero values to zero?

From: Derek O'Connor

Date: 3 Sep, 2011 09:43:10

Message: 12 of 16

"Jeff" wrote in message <j3rj87$61t$1@newscl01ah.mathworks.com>...
> "Derek O'Connor" wrote in message <j3qtem$ndb$1@newscl01ah.mathworks.com>...
> >
> > y0 = 1; y = 2; abs(y-y0) < 1e-6 ?
> >
> > y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6 ?
> >
> > Do you wish to modify your advice?
>
> Pardon me if I've miscounted the number of angels, but I'm missing the point. I ran these two lines
> EDU>> y0 = 1; y = 2; abs(y-y0) < 1e-6
> ans =
> 0
> EDU>> y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6
> ans =
> 1
>
> which looks right to me (I'm running v7.8 32-bit student version).

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

Yes Jeff, you have missed the point.

Read page 23 of these notes http://www.derekroconnor.net/NA/Notes/NA-4-Latex.pdf
   
Read Chapter 4, page 12 of Cleve Moler's book Numerical Computing with Matlab (see link below), where he gives this convergence test for Matlab's FZERO (the zero of f is somewhere in [a,b]):

"This section is the convergence test and possible exit from the loop.
    m = 0.5*(a - b);
    tol = 2.0*eps*max(abs(b),1.0);
    if (abs(m) <= tol) | (fb == 0.0),
    break
end"

You should study this test and the code surrounding it very carefully. You will notice that, contrary to the other piece bad advice you were given, there is nothing wrong with testing two floating point numbers for equality (fb == 0.0, above). You just need to know what you are doing.

Floating point numbers are not "fuzzy", or uncertain: they are well-defined strings of bits. If your understanding of floating point arithmetic is imperfect, then the software you write will be concomitantly imperfect.


http://www.mathworks.co.uk/moler/chapters.html

Subject: Is it OK to set almost zero values to zero?

From: Steven_Lord

Date: 6 Sep, 2011 15:45:10

Message: 13 of 16



"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
news:j3rj87$61t$1@newscl01ah.mathworks.com...
> "Derek O'Connor" wrote in message
> <j3qtem$ndb$1@newscl01ah.mathworks.com>...
>>
>> y0 = 1; y = 2; abs(y-y0) < 1e-6 ?
>>
>> y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6 ?
>>
>> Do you wish to modify your advice?
>
> Pardon me if I've miscounted the number of angels, but I'm missing the
> point. I ran these two lines
> EDU>> y0 = 1; y = 2; abs(y-y0) < 1e-6 ans =
> 0
> EDU>> y0 = 1e-7; y = 2e-7; abs(y-y0) < 1e-6
> ans =
> 1
>
> which looks right to me (I'm running v7.8 32-bit student version).

Yes. I believe Derek objected to the apparent blanket suggestion of 1e-6 as
a "close enough" tolerance.

Only you can decide what "close enough" is based on your application -- if
you're looking at GPS data for an automotive navigation system, you don't
need to get as close as if you were writing software to control a robotic
arm that would perform a surgical procedure. Being off by an inch would be
unlikely to make a difference in most circumstances for GPS scenario (maybe
if you were driving along a cliff road sans safety railings), but would be
completely unacceptable in the surgical scenario.

So the answer to the question the OP asked in the subject: it depends.

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

Subject: Is it OK to set almost zero values to zero?

From: Jeff

Date: 6 Sep, 2011 16:10:30

Message: 14 of 16

"Steven_Lord" <slord@mathworks.com> wrote in message <j45f66$k4n$1@newscl01ah.mathworks.com>...
>
> Yes. I believe Derek objected to the apparent blanket suggestion of 1e-6 as
> a "close enough" tolerance.
>
> Only you can decide what "close enough" is based on your application -- if
> you're looking at GPS data for an automotive navigation system, you don't
> need to get as close as if you were writing software to control a robotic
> arm that would perform a surgical procedure. Being off by an inch would be
> unlikely to make a difference in most circumstances for GPS scenario (maybe
> if you were driving along a cliff road sans safety railings), but would be
> completely unacceptable in the surgical scenario.
>
> So the answer to the question the OP asked in the subject: it depends.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I think we're getting a little off topic, probably because my question could have been clearer.

I guess I'm really curious if I could set an initial value to 0, given that had I done these calculations by paper and pencil, the value would have been 0. Wouldn't that make my subsequent calculations closer to the real values, since I would start with a more accurate number, 0, than Matlab estimates, *almost* 0? Or am I better off just leaving Matlab's "almost 0" there and running the calculations.

I think the answer is still the same: it depends.

Subject: Is it OK to set almost zero values to zero?

From: someone

Date: 6 Sep, 2011 16:48:10

Message: 15 of 16

"Jeff" wrote in message <j45glm$pc7$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message <j45f66$k4n$1@newscl01ah.mathworks.com>...
> >
> > Yes. I believe Derek objected to the apparent blanket suggestion of 1e-6 as
> > a "close enough" tolerance.
> >
> > Only you can decide what "close enough" is based on your application -- if
> > you're looking at GPS data for an automotive navigation system, you don't
> > need to get as close as if you were writing software to control a robotic
> > arm that would perform a surgical procedure. Being off by an inch would be
> > unlikely to make a difference in most circumstances for GPS scenario (maybe
> > if you were driving along a cliff road sans safety railings), but would be
> > completely unacceptable in the surgical scenario.
> >
> > So the answer to the question the OP asked in the subject: it depends.
> >
> > --
> > Steve Lord
> > slord@mathworks.com
> > To contact Technical Support use the Contact Us link on
> > http://www.mathworks.com
>
> I think we're getting a little off topic, probably because my question could have been clearer.
>
> I guess I'm really curious if I could set an initial value to 0, given that had I done these calculations by paper and pencil, the value would have been 0. Wouldn't that make my subsequent calculations closer to the real values, since I would start with a more accurate number, 0, than Matlab estimates, *almost* 0? Or am I better off just leaving Matlab's "almost 0" there and running the calculations.
>
> I think the answer is still the same: it depends.

You could use the MATLAB sind function instead of the sin function:

doc sind

Y = sind(X) is the sine of the elements
of X, expressed in degrees. For integers n, sind(n*180) is
exactly zero, whereas sin(n*pi) reflects the accuracy of
the floating point value of pi.

Subject: Is it OK to set almost zero values to zero?

From: Bruno Luong

Date: 6 Sep, 2011 18:02:26

Message: 16 of 16

"Jeff" wrote in message <j45glm$pc7$1@newscl01ah.mathworks.com>...
>Or am I better off just leaving Matlab's "almost 0" there and running the calculations.
>

My recommendation is leave it there and run. If your code does need a manual fix of variables in order to work, then you need to fix the code rather than the data.

Bruno

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