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:
== comparison for double/float values

Subject: == comparison for double/float values

From: Pushkar Pradhan

Date: 7 May, 2004 11:34:44

Message: 1 of 7

It is well known that you should never use the == operator when
comparing float/double values, rather you should check that the
difference is < or > than a threshold e.g. 0.001

So does the same apply in MATLAB or MATLAB does something special to
handle it? (There's nothing in the help about it).

I was wondering because I've got somebody's code which does this
comparison and works correctly (atleast until now).

Pushkar

Subject: == comparison for double/float values

From: Scott Seidman

Date: 7 May, 2004 16:47:10

Message: 2 of 7

Pushkar Pradhan <pushkar@gri.msstate.edu> wrote in news:409BBAA4.2010105
@gri.msstate.edu:

> It is well known that you should never use the == operator when
> comparing float/double values, rather you should check that the
> difference is < or > than a threshold e.g. 0.001
>
> So does the same apply in MATLAB or MATLAB does something special to
> handle it? (There's nothing in the help about it).
>
> I was wondering because I've got somebody's code which does this
> comparison and works correctly (atleast until now).
>
> Pushkar
>
>

Matlab, in general, does nothing to handle it. Just like in C, you can't
guarantee such a comparison will work. It's usually poor practice to use
it.

Scott

Subject: == comparison for double/float values

From: AJ \"no z\" Johnson

Date: 7 May, 2004 16:22:54

Message: 3 of 7

"Scott Seidman" <namdiesttocs@mindspring.com> wrote in message
news:Xns94E2821128A18scottseidmanmindspri@130.133.1.4...
> Pushkar Pradhan <pushkar@gri.msstate.edu> wrote in news:409BBAA4.2010105
> @gri.msstate.edu:
>
> > It is well known that you should never use the == operator when
> > comparing float/double values, rather you should check that the
> > difference is < or > than a threshold e.g. 0.001
> >
> > So does the same apply in MATLAB or MATLAB does something special to
> > handle it? (There's nothing in the help about it).
> >
> > I was wondering because I've got somebody's code which does this
> > comparison and works correctly (atleast until now).
> >
> > Pushkar
> >
> >
>
> Matlab, in general, does nothing to handle it. Just like in C, you can't
> guarantee such a comparison will work. It's usually poor practice to use
> it.
>
> Scott

Has anyone tried overload the @double/eq function, to solve this problem?
i.e. write your own function that compares doubles/floats.
-Aj

Subject: == comparison for double/float values

From: Scott Seidman

Date: 7 May, 2004 20:47:04

Message: 4 of 7

"AJ \"no z\" Johnson" <aj.jozhnson@lmco.com> wrote in
news:c7gr6f$3ek2@cui1.lmms.lmco.com:

> "Scott Seidman" <namdiesttocs@mindspring.com> wrote in message
> news:Xns94E2821128A18scottseidmanmindspri@130.133.1.4...
>> Pushkar Pradhan <pushkar@gri.msstate.edu> wrote in
>> news:409BBAA4.2010105 @gri.msstate.edu:
>>
>> > It is well known that you should never use the == operator when
>> > comparing float/double values, rather you should check that the
>> > difference is < or > than a threshold e.g. 0.001
>> >
>> > So does the same apply in MATLAB or MATLAB does something special
>> > to handle it? (There's nothing in the help about it).
>> >
>> > I was wondering because I've got somebody's code which does this
>> > comparison and works correctly (atleast until now).
>> >
>> > Pushkar
>> >
>> >
>>
>> Matlab, in general, does nothing to handle it. Just like in C, you
>> can't guarantee such a comparison will work. It's usually poor
>> practice to use it.
>>
>> Scott
>
> Has anyone tried overload the @double/eq function, to solve this
> problem? i.e. write your own function that compares doubles/floats.
> -Aj
>
>
>

I wouldn't go so far as to call it a problem. By their nature, they
shouldn't be equality tested. You could just write and call a "near
enuff" function--this way you're always reminded that you're not doing
equality testing.

Scott

Subject: == comparison for double/float values

From: pjacklam@online.no (Peter J. Acklam)

Date: 17 May, 2004 14:51:06

Message: 5 of 7

Pushkar Pradhan <pushkar@gri.msstate.edu> wrote:

> It is well known that you should never use the == operator when
> comparing float/double values, rather you should check that the
> difference is < or > than a threshold e.g. 0.001

That's not true. Comparing doubles and singles with == is ok, but
you need to do it with care.

Peter

--
If I made a copy of the The Digital Millennium Copyright Act,
would that be a violation of it?

Subject: == comparison for double/float values

From: Ulrich

Date: 25 Aug, 2009 15:52:02

Message: 6 of 7

I just realized that even if you think you are far from being effected by that, you are not!!

Just try:

(1.001-0.001)==1

cheers.

Subject: == comparison for double/float values

From: Greg

Date: 25 Aug, 2009 17:05:22

Message: 7 of 7

"Ulrich " <ukniess@mac.com> wrote in message <h711b2$gqp$1@fred.mathworks.com>...
> I just realized that even if you think you are far from being effected by that, you are not!!
>
> Just try:
>
> (1.001-0.001)==1
>
> cheers.

Here is one way to address the issue

  comparisonValue = 1;
  testValue = 1.001 - 0.001;
  abs(testValue - comparisonValue) <= eps(comparisonValue)

The EPS function determines the floating-point precision for a given value. Essentially it provides the value produced by a 1-bit error from the value being tested. This value can be used in many cases as a threshold to for determining equality of floating-point numbers.

For more information enter

  >> doc eps

at the MATLAB Command Prompt.

There is also a paper discussing this topic located here:

  http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf

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