From: Christian on
Hello all

I am trying to model a communication system in Matlab. Because of time computation, I have moved a part of the code into some mex files. When running the program with just Matlab the results are more satisfactory than the results when Matlab/MEX is used. My only explanation for this is a difference of precision in the computations involved The code programmed in these MEX files are computationally costful, complex data is involved and require a high degree of precision.

Since the mxArray are double precision, my question is: how can I use mxArray structures in MEX files with long double precision? The pointers at these mxArray have been declared as long double *, but I do not see any change and the results are still different from those obtained when only Matlab is used.

Thanks in advance
From: Rune Allnor on
On 18 Feb, 13:04, "Christian " <pep...(a)hotmail.com> wrote:
> Hello all
>
> I am trying to model a communication system in Matlab. Because of time computation, I have moved a part of the code into some mex files. When running the program with just Matlab the results are more satisfactory than the results when Matlab/MEX is used. My only explanation for this is a difference of precision in the computations involved

Wrong. Matlab uses double precision arithmetics. If the
matlab results are good, double precision should suffice
in your mex files as well. Another, more likely explanation
is that matlab uses more robust algorithms for whatever
computations are done.

If you want to roll your own, make sure you understand the
ins and outs of the numerics.

Rune
From: Bruno Luong on
Rune Allnor <allnor(a)tele.ntnu.no> wrote in message <a87725e5-154a-4b58-b81a-17e2a70b36bf(a)a18g2000yqc.googlegroups.com>...

> Wrong. Matlab uses double precision arithmetics.

Matlab uses both double (8 bytes) and single (4 bytes) floating points, except for sparse matrix.

Bruno
From: Jan Simon on
Dear Christian!

> I am trying to model a communication system in Matlab. Because of time computation, I have moved a part of the code into some mex files. When running the program with just Matlab the results are more satisfactory than the results when Matlab/MEX is used. My only explanation for this is a difference of precision in the computations involved The code programmed in these MEX files are computationally costful, complex data is involved and require a high degree of precision.
>
> Since the mxArray are double precision, my question is: how can I use mxArray structures in MEX files with long double precision? The pointers at these mxArray have been declared as long double *, but I do not see any change and the results are still different from those obtained when only Matlab is used.

There are no mxArrays with long doubles. In addition for some C compilers doubles and long doubles are identical. Please try:
mexPrintf("double: %d long double: %d\n", sizeof(double), sizeof(long double));
Then the calculations if long double precision need to set the processor precision to 64 bits:
_control87(PC_64, MCW_PC);
or for LCC compiler:
_control87(_PC_64, _MCW_PC);
Do not forget to reset it to 53 bit, otherwise Matlab might be in danger of getting seriously confused!!! If the program returns from Mex part, this is done automatically, but after mexErrMsgTxt this might be missing. Afterwards expressions like (10^9:10^9+8196) need not necessarily be integers anymore or equivalent gags.

I faced some differences between Matlab and C-Mex calculations e.g. for ACOS. I'd recommend to use the FDLIBM functions as Matlab (see: doc acos).

To get clearence if really Matlab calculates with a higher precision, I'd recommend to download LCC v3.8 from the net and perform the calculations with QFLOATs, which use 384 bits. This is far to slow for standard applications, but it helps to find instabilities caused by precision problems.

Even for simple problems like getting the angle between 2 vectors using the actually stable ATAN2 method, I got results with different quality using LCC, BCC or the OpenWatcom compiler - while the last one creates the best results --- even if I use FDLIBM functions for SQRT and ATAN2 !!!

But in general: If the results are significantly different for different implementations (e.g. C-Mex and Matlab), the algorithm is instable. This is not the fault of the computer.

Good luck, Jan
From: Oliver Woodford on
"Christian " wrote:
> Hello all
>
> I am trying to model a communication system in Matlab. Because of time computation, I have moved a part of the code into some mex files. When running the program with just Matlab the results are more satisfactory than the results when Matlab/MEX is used. My only explanation for this is a difference of precision in the computations involved The code programmed in these MEX files are computationally costful, complex data is involved and require a high degree of precision.
>
> Since the mxArray are double precision, my question is: how can I use mxArray structures in MEX files with long double precision? The pointers at these mxArray have been declared as long double *, but I do not see any change and the results are still different from those obtained when only Matlab is used.
>
> Thanks in advance

I understand why the results might be different. MATLAB follows IEEE floating point arithmetic rules, so uses a 53-bit mantissa in calculations. However, most modern CPUs use a 64-bit mantissa internally when doing the same calculations, which the compiled mex code will make use of. Therefore these results should be more accurate. Are you MATLAB's results are the more accurate of the two?
Oliver