MATLAB: Does the MEX-file compile correctly but produces incorrect results when I call C math functions without including “math.h”

32answercmathmath.hMATLABmexwrong

I create a C-file that uses a C math function, but do not include "math.h". Next, I use MATLAB 7.0 (R14) to create a MEX-file using:
mex myfile.c
The MEX function does not produce any errors or warnings, but when I call the MEX-file from MATLAB, I obtain incorrect results. For example, if I use the "sin" function, my results ia always 32.

Best Answer

  • We have verified that there is a bug in MATLAB 7.0 (R14) in the way that the MEX function handles compiling code that uses C math functions when the appropriate header is not included.
    To work around this issue, be sure to include the required header file, which in this case is "math.h", in your C file before creating a MEX-file.