From: Walter Roberson on
James Tursa wrote:

> In general, this will probably give the same result in the DLL as it did
> in the m-file, but there are some subtle issues that may depend on the
> C/C++ compiler. In the m-file, both 2 and 2.0 are double constants and
> MATLAB will read them in that way. In C/C++ source code, 2 is an int
> constant and 2.0 is a double constant, so if this were C code it will be
> up to the compiler to promote one of them to the other kind, in this
> case promoting the int to a double. What does MATLAB do with this when
> creating the DLL? I don't know ... maybe it turns the 2 into a 2.0
> before handing it to the C compiler and maybe it doesn't.

As far as matlab is concerned, 2==2.0 is comparing two double precision
numbers. You would have to have changed the type on the 2 for it to be
otherwise, such as

int32(2)==2.0

In the call f(x,y) where f is a mex routine, f(int32(2),2) would hand f two
different mx arrays, one marked as int32 type and the other marked as double.
It would be up to the program to either reject the combination or to detect it
and handle it appropriately.
From: Mathew on
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.

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?

Mathew
From: James Tursa on
Walter Roberson <roberson(a)hushmail.com> wrote in message <hnu0fp$mv1$1(a)canopus.cc.umanitoba.ca>...
> James Tursa wrote:
>
> > In general, this will probably give the same result in the DLL as it did
> > in the m-file, but there are some subtle issues that may depend on the
> > C/C++ compiler. In the m-file, both 2 and 2.0 are double constants and
> > MATLAB will read them in that way. In C/C++ source code, 2 is an int
> > constant and 2.0 is a double constant, so if this were C code it will be
> > up to the compiler to promote one of them to the other kind, in this
> > case promoting the int to a double. What does MATLAB do with this when
> > creating the DLL? I don't know ... maybe it turns the 2 into a 2.0
> > before handing it to the C compiler and maybe it doesn't.
>
> As far as matlab is concerned, 2==2.0 is comparing two double precision
> numbers.

Yes. That is exactly what I wrote in my post above:

"In the m-file, both 2 and 2.0 are double constants"

James Tursa
From: James Tursa on
"Mathew " <jhwang2k(a)gmail.com> wrote in message <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.
>
> 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?
>
> Mathew

If the i, j, etc. values are integral (even though the type is double), then the == comparison should be safe. Otherwise you will have the floating point comparison issues pointed out by several posters.

James Tursa
From: James Tursa on
Walter Roberson <roberson(a)hushmail.com> wrote in message <hntun8$ka8$1(a)canopus.cc.umanitoba.ca>...
>
> The normal situation with C++ is the same, but since it is allowed to overload
> the == operator with an (int,double) method, 2==2.0 could mean nearly anything
> in C++ ...

No. You are not allowed to overload the operators for cases where all of the operands are intrinsic types. At least one of the operands must be a class object. 2==2.0 always means exactly the same thing in all C++ compilers.

James Tursa