Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Long double precision mxArray in mex file?

Subject: Long double precision mxArray in mex file?

From: Christian

Date: 18 Feb, 2010 12:04:06

Message: 1 of 14

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

Subject: Long double precision mxArray in mex file?

From: Rune Allnor

Date: 18 Feb, 2010 12:35:19

Message: 2 of 14

On 18 Feb, 13:04, "Christian " <pep...@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

Subject: Long double precision mxArray in mex file?

From: Bruno Luong

Date: 18 Feb, 2010 12:52:02

Message: 3 of 14

Rune Allnor <allnor@tele.ntnu.no> wrote in message <a87725e5-154a-4b58-b81a-17e2a70b36bf@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

Subject: Long double precision mxArray in mex file?

From: Jan Simon

Date: 18 Feb, 2010 14:17:25

Message: 4 of 14

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

Subject: Long double precision mxArray in mex file?

From: Oliver Woodford

Date: 18 Feb, 2010 14:54:05

Message: 5 of 14

"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

Subject: Long double precision mxArray in mex file?

From: Jan Simon

Date: 18 Feb, 2010 15:23:05

Message: 6 of 14

Dear Oliver!

> 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

Without setting it manually with _control87 or in Matlab with
  systemdependent('setprecision', 64)
the calculations happen with 53 bit in Matlab and in the Mex file.

But be aware that calculations need not be more accurate with 64 bit! E.g. Kahan's method for compensating the errors in the sum of floating point numbers fails when the intermediate values are computed with the higher precision, and finally the result is worse.

Kind regards, Jan

Subject: Long double precision mxArray in mex file?

From: Christian

Date: 18 Feb, 2010 15:39:20

Message: 7 of 14

"Oliver Woodford" <o.j.woodford.98@cantab.net> wrote in message <hljkad$se6$1@fred.mathworks.com>...
> "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

Well, I could not say which one is more accurate. I am trying a technique to detect the information signal sent, and using only matlab the number of errors is smaller. It would be more convenient for me that Matlab used a higher precision than mex.

The algorithm involves complex operations, the results depends on previous computations,...a smaller difference in the precision computation may explain the difference. Why Matlab does obtain a smaller number of errors? Strange

Subject: Long double precision mxArray in mex file?

From: Oliver Woodford

Date: 18 Feb, 2010 15:43:05

Message: 8 of 14

"Jan Simon" wrote:
> Without setting it manually with _control87 or in Matlab with
> systemdependent('setprecision', 64)
> the calculations happen with 53 bit in Matlab and in the Mex file.

Interesting. Thanks, Jan!

Subject: Long double precision mxArray in mex file?

From: Jan Simon

Date: 18 Feb, 2010 15:53:05

Message: 9 of 14

Typo:
> > systemdependent('setprecision', 64)
system_dependent('setprecision', 64)

Jan

Subject: Long double precision mxArray in mex file?

From: Christian

Date: 19 Feb, 2010 09:30:24

Message: 10 of 14

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hljnp1$hkd$1@fred.mathworks.com>...
> Typo:
> > > systemdependent('setprecision', 64)
> system_dependent('setprecision', 64)
>
> Jan

May you tell me please which steps I should take to use the same precision in MEX than that used in matlab?

Subject: Long double precision mxArray in mex file?

From: Bruno Luong

Date: 19 Feb, 2010 09:48:04

Message: 11 of 14

"Christian " <pepico@hotmail.com> wrote in message <hlllnf$e59$1@fred.mathworks.com>...
> "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hljnp1$hkd$1@fred.mathworks.com>...
> > Typo:
> > > > systemdependent('setprecision', 64)
> > system_dependent('setprecision', 64)
> >
> > Jan
>
> May you tell me please which steps I should take to use the same precision in MEX than that used in matlab?

As Jan told, Matlab uses floating point control word of 53 bits internal computation.

Under windows, you might set the word by something like this (see msdn page for relevant functions):

        // Set the control word to Matlab preference
        fpmask = _MCW_EM | _MCW_IC | _MCW_RC | _MCW_PC;
        fpMatlabstate = _EM_DENORMAL | _EM_INVALID | EM_ZERODIVIDE |
                        _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT |
                        _IC_AFFINE | _PC_53; // 0x000d001f
        _clearfp(); // _clearfp before enabling/unmasking a FPU exception
        _controlfp(fpMatlabstate, fpmask);

NOTE: this is undocumented, use at your own risk.

Bruno

Subject: Long double precision mxArray in mex file?

From: Christian

Date: 19 Feb, 2010 10:05:20

Message: 12 of 14

"Christian " <pepico@hotmail.com> wrote in message <hljabl$72u$1@fred.mathworks.com>...
> 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 want to be sure that the precision used in MEX file is the same that that used in Matlab. My results with only Matlab are more satisfactory, so I want to use the same precision in MEX file.
Maybe the difference is in the algorithm, but I just want to be sure it is not because of the precision.

Subject: Long double precision mxArray in mex file?

From: Jan Simon

Date: 20 Feb, 2010 23:52:04

Message: 13 of 14

Dear Christian!

> I want to be sure that the precision used in MEX file is the same that that used in Matlab.

Then do not call the function _control87 or _controlfp (depends on the compiler?!) but let the Mex interface do this automatically for you.

Kind regards, Jan

Subject: Long double precision mxArray in mex file?

From: Gordon Freeman

Date: 2 Jun, 2011 18:26:04

Message: 14 of 14

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message

> As Jan told, Matlab uses floating point control word of 53 bits internal computation.
>
> Under windows, you might set the word by something like this (see msdn page for relevant functions):
>
> // Set the control word to Matlab preference
> fpmask = _MCW_EM | _MCW_IC | _MCW_RC | _MCW_PC;
> fpMatlabstate = _EM_DENORMAL | _EM_INVALID | EM_ZERODIVIDE |
> _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT |
> _IC_AFFINE | _PC_53; // 0x000d001f
> _clearfp(); // _clearfp before enabling/unmasking a FPU exception
> _controlfp(fpMatlabstate, fpmask);
>
> NOTE: this is undocumented, use at your own risk.
>
> Bruno

Hi Bruno,

Based on the code above, I tried to set the control word to 0x0009001f inside a MEX file. However, when I print out the control word, it always prints 0x0008001f instead. It looks like my statement has no effect at all. Any suggestions ?

Here is the code I tried :

    unsigned int fpmask = _MCW_EM | _MCW_IC | _MCW_RC | _MCW_PC;
    unsigned int fpMatlabstate = _EM_DENORMAL | _EM_INVALID | EM_ZERODIVIDE |_EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT | _IC_PROJECTIVE | _PC_53; // 0x0009001f
    _clearfp(); // _clearfp before enabling/unmasking a FPU exception
    _control87(fpMatlabstate, fpmask);
    
    fpMatlabstate = _control87(0,0);
    mexPrintf("New FPCW=%012x \n",fpMatlabstate);

Thanks,

Gordon

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us