From: Steven Lord on

"Mathew " <jhwang2k(a)gmail.com> wrote in message
news:hnu0sc$hq1$1(a)fred.mathworks.com...
> Thanks for everyone.
>
> It is better to avoid a==b as much as we can. As we can see:
> 0.3-0.3-0.1==0 is not true even in MATLAB.

0.3-0.3-0.1 == 0 better not be true OUTSIDE of MATLAB either :) Unless you
somehow had something like an "unsigned float" ...

But yes, with your typo fixed 0.3-0.2-0.1 is not guaranteed to be exactly
equal to 0 in floating-point.

> But I want to make sure if we cannot use == at all. In MATLAB, we can see
> i==j, i==1, length(x)==100 everywhere, even in function from MATHWORKS.
> How safe they are then?

As James said, if we KNOW that i and j must be of an integer class or must
be floating-point numbers with integer values (flints) then that's pretty
safe. In the case with LENGTH, the function by its definition should return
a nonnegative flint -- if it returns a negative value, or a noninteger value
and x is not an object for which you have SPECIFICALLY overloaded LENGTH
(and EQ) to handle that scenario, you probably have other problems to deal
with.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Mathew on
Now I am feeling safe as the flints are OK. Otherwise, it is really scary...

"Steven Lord" <slord(a)mathworks.com> wrote in message <hnu3me$9b9$1(a)fred.mathworks.com>...
>
> "Mathew " <jhwang2k(a)gmail.com> wrote in message
> news:hnu0sc$hq1$1(a)fred.mathworks.com...
> > Thanks for everyone.
> >
> > It is better to avoid a==b as much as we can. As we can see:
> > 0.3-0.3-0.1==0 is not true even in MATLAB.
>
> 0.3-0.3-0.1 == 0 better not be true OUTSIDE of MATLAB either :) Unless you
> somehow had something like an "unsigned float" ...
>
> But yes, with your typo fixed 0.3-0.2-0.1 is not guaranteed to be exactly
> equal to 0 in floating-point.
>
> > But I want to make sure if we cannot use == at all. In MATLAB, we can see
> > i==j, i==1, length(x)==100 everywhere, even in function from MATHWORKS.
> > How safe they are then?
>
> As James said, if we KNOW that i and j must be of an integer class or must
> be floating-point numbers with integer values (flints) then that's pretty
> safe. In the case with LENGTH, the function by its definition should return
> a nonnegative flint -- if it returns a negative value, or a noninteger value
> and x is not an object for which you have SPECIFICALLY overloaded LENGTH
> (and EQ) to handle that scenario, you probably have other problems to deal
> with.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>
From: James Tursa on
"Mathew " <jhwang2k(a)gmail.com> wrote in message <hnu4ig$oaa$1(a)fred.mathworks.com>...
> Now I am feeling safe as the flints are OK. Otherwise, it is really scary...

A little fear is a good thing when working with floating point numbers.

James Tursa