What header I need in a mexfunction so nextafter is recognized?

I am a little confuse. I am doing a mexfunction I had some rounding problem with the ceil function in line 46 of the following code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <limits>
#include <utility>
#include <assert.h>

#ifdef _CHAR16T  //for matlab 2010a
#define CHAR16_T
#endif
#include "mex.h"
#include "matrix.h"

using namespace std;
some code
.
.
void mexFunction (int nlhs, mxArray *plhs[], 
                  int nrhs, const mxArray *prhs[])
{
    double *pr, *pi;
    int *pr2, *pi2;
    mwIndex *ir, *jc;
    mwIndex *ir2, *jc2;

/* Right hand side: input of the function */

    if( nrhs!=3 || 
        !mxIsNumeric(prhs[0]) || 
        !mxIsNumeric(prhs[1]) ||
        mxIsEmpty(prhs[0])    ||
		mxIsEmpty(prhs[1]))
		{
		 mexErrMsgTxt("Expects three real matrices, two vectors of the same size and one 2 x k or empty matrix");
		}
		

    const mxArray *d = prhs[0];
    const mxArray *p = prhs[1];
	const mxArray *match0 = prhs[2];

    
	mwSize n    = mxGetN(d);
	int numW = ceil( double(n/2) );
	int numU = n-numW;
	mexPrintf("n = %d. numW = %d,numU = %d \n",n,numW,numU);
	if (n != mxGetN(p)){
		mexErrMsgTxt("The vector of distances and predecessors must have the same size");
	}
more code ...

}


then I decide to check in the internet and create a small program to try a solution. The program worked fine. I tried using cygwin. It was too tedious to do a mexfunction for that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

using namespace std;

int main()
{
	int n = 131;
	int m = ceil(nextafter(double(n/2),n));
	printf("%d /2 =%d",n,m);
		return 0;

}


But when I change line 46 of my mexfunction with line 10 of my trial program and run it in matlab 2010a. I have this answer:
 
 error C3861: 'nextafter': identifier not found 

So, somehow math.h does not define nextafter, I know that math.h for C is different that (cmath) math.h of C++. I try to use #include <cmath.h> but then it said that could not find the file.
I really would appreciate any help with this issue.
Last edited on
It's not <cmath.h>, it's <cmath>
thanks, but still does not solve my problem. If I use <cmath> still the error is:
error C3861: 'nextafter': identifier not found

I give up, trying to use functions. What I did was defined a constant pad slightly less than .5 and add it inside the ceil function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <limits>
#include <utility>
#include <assert.h>

#ifdef _CHAR16T  //for matlab 2010a
#define CHAR16_T
#endif
#include "mex.h"
#include "matrix.h"

#define PAD 0.49999 /* A hair below 1/2, to avoid roundoff errors */

using namespace std;
some code
.
.
void mexFunction (int nlhs, mxArray *plhs[], 
                  int nrhs, const mxArray *prhs[])
{
    double *pr, *pi;
    int *pr2, *pi2;
    mwIndex *ir, *jc;
    mwIndex *ir2, *jc2;

/* Right hand side: input of the function */

    if( nrhs!=3 || 
        !mxIsNumeric(prhs[0]) || 
        !mxIsNumeric(prhs[1]) ||
        mxIsEmpty(prhs[0])    ||
		mxIsEmpty(prhs[1]))
		{
		 mexErrMsgTxt("Expects three real matrices, two vectors of the same size and one 2 x k or empty matrix");
		}
		

    const mxArray *d = prhs[0];
    const mxArray *p = prhs[1];
	const mxArray *match0 = prhs[2];

    
	mwSize n    = mxGetN(d);
	int numW = ceil( double(n/2) + PAD );
	int numU = n-numW;
	mexPrintf("n = %d. numW = %d,numU = %d \n",n,numW,numU);
	if (n != mxGetN(p)){
		mexErrMsgTxt("The vector of distances and predecessors must have the same size");
	}
more code ...

}
Last edited on
closed account (zb0S216C)
CaribeGirl wrote:
I had some rounding problem

The CPU is capable of performing over 4 billion computations a second but struggles with rounding.

Is nextafter( ) a function you defined, or is it part of another header module within the standard library? I can't see nextafter( ) defined anywhere else other than main( ).

Wazzak
nextafter is a c function which is declared in math.h

But is not defined in C++. What I think, which is only an intuition, is that matlab compiler reads <math.h> as <cmath> in C++ and not as <math.h> in C. In cmath nextafter is not defined. That is why I got the error. But I do not know how to make it work using the nextafter function.
That reply was for Framework.

I never worked on matlab, so cannot comment. nextafter is included in c99 specification, so if your matlab version doesn't comply with c99, it probably will not be having that function.

you said you have cygwin, try that if it works there?
Matlab compiler is Visual studio 2010 express with SDK 7.1. I do not know how to compile a mexfunction in cygwin. In my trial example(see second code in my first post) nextafter works just fine in cygwin.
Now, when I used a constant to round the number instead of nextafter (my second post) somehow I got a segmentation violation from matlab and close the program :(

if you are using VS 2010, its easy. use _nextafter(), instead of nextafter(). It will work for you.
Thanks! Now the mexfile compile. But still keep throwing a segmentation violation from matlab and close the program. I am going to check the logic and the code. But thanks very much!!
this may be due to lots of pointers you have taken. avoid pointers whenever you can. It is not necessary to use pointers for each variable.
Topic archived. No new replies allowed.