MATLAB Answers

0

The return type of mxIsDouble, mxIsSingle, and mxIsClass (mex for Fortran)

Asked by Zaikun Zhang on 29 Jul 2019
Latest activity Commented on by Zaikun Zhang on 30 Jul 2019
the return of mxIsDouble for Fortran (also mxIsSingle and mxIsClass) is INTEGER*4.
Here are my question:
  1. Should the declaration of mxIsDouble be always
INTEGER*4 :: mxIsDouble
What if I declare it as
INTEGER :: mxIsDouble
or
LOGICAL :: mxIsDouble
2. It is said that INTEGER*4 is not standard and should be replaced by kind. How should I do it? I know that INTEGER(KIND=4) is incorrect. In addition, why does mex continue to use such a nonstandard feature?
Thank you very much!

  0 Comments

Sign in to comment.

Products


Release

R2016a

1 Answer

Answer by James Tursa
on 29 Jul 2019

Do what the documentation says and use INTEGER*4. Yes, it is non-standard but you are very unlikely to run into a compiler that does not understand it the way you want it. That being said, just plain INTEGER will probably also work (unless you have compiler directives forcing INTEGER to be some other size), since most compilers will use a 4-byte integer for the default INTEGER type even on 64-bit systems (I suspect they do this to try and maximize backwards compatibility for older source code). The only thing I would add is the EXTERNAL qualifier. E.g.,
INTEGER*4, EXTERNAL :: mxIsDouble
Do NOT use LOGICAL for these functions, since that would bring into play the byte-size of a logical vs integer and the bit-patterns of true vs false for your particular compiler. No need to complicate it with this.
As for INTEGER(KIND=4), I know this isn't required to map into a 4-byte integer per the standard, but my guess is your compiler will do exactly that and it will probably also work.

  3 Comments

Thank you very much for the informative answer!
Just a small observation on "most compilers will use a 4-byte integer for the default INTEGER type even on 64-bit systems": indeed, I found that mex -largeArrayDims will lead to a 8-byte detault INTEGER on my machine. I am using a 64-bit Linux PC with MATLAB R2018a, the compiler being gfortran6.
I suppose it is not suprising that the extra qualifier -largeArrayDims led to compiler directives forcing the default Fortran INTEGER type to be 8-bytes. But this just supports my original advice to use exactly what is in the doc for argument types.
I totally agree. Thanks to MATLAB, we may have to forget about the Fortran standards and use the nonstandard INTEGER*4.

Sign in to comment.