Reputation: 11
I want to code some functionality of my matlab program in c, to make it faster. But this function does not return the result of the expression. It returns some different value like 29. Without calling the function (speaking of placing the expression inside the function at the place where I call the function) it works. even inside the function the result of the expression is correct, but not after the return...
…
double distance = 0;
// function call
distance = distpos(position[0], position[1], *(origin), *(origin+1));
printf("%f", distance); // incorrect
…
// funktion
double distpos(double x1, double y1, double x2, double y2)
{
printf("%f", sqrt(pow((x1 - x2),2) + pow((y1 - y2),2))); // correct
return sqrt(pow((x1 - x2),2) + pow((y1 - y2),2));
}
// EDIT ------
I'm using Matlab 7.3.0 (R2006b).
The following code does not work:
#include <stdio.h>
#include <math.h>
#include "mex.h"
void main()
{
double position[2] = { 5, 6 };
double origin[2] = { 3, 4 };
double distance = 0;
// function call
distance = distpos(position[0], position[1], *(origin), *(origin+1));
printf("%f\n", distance); // incorrect
}
// funktion
double distpos(double x1, double y1, double x2, double y2)
{
printf("%f\n", sqrt(pow((x1 - x2),2) + pow((y1 - y2),2))); // correct
return sqrt(pow((x1 - x2),2) + pow((y1 - y2),2));
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
main();
}
Result :
2.828427
639.000000
This code does work:
#include <stdio.h>
#include <math.h>
#include "mex.h"
// funktion
double distpos(double x1, double y1, double x2, double y2)
{
printf("%f\n", sqrt(pow((x1 - x2),2) + pow((y1 - y2),2))); // correct
return sqrt(pow((x1 - x2),2) + pow((y1 - y2),2));
}
void main()
{
double position[2] = { 5, 6 };
double origin[2] = { 3, 4 };
double distance = 0;
// function call
distance = distpos(position[0], position[1], *(origin), *(origin+1));
printf("%f\n", distance); // incorrect
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
main();
}
Result :
2.828427
2.828427
This means: There is a difference if the function is declared before its use or after its use. (I already know that you should declare a function before you use it, but you mustn't)
Upvotes: 1
Views: 799
Reputation: 124553
I can confirm that the default MEX compiler (Lcc-win32) does successfully compile the code. The reason for the wrong results was explained by @Vicky in the comments...
Now in the future, if you want to see any warnings, enable verbose mode:
>> mex -v a.c
...
Warning a.c: 16 missing return value
Warning a.c: 19 declaration of `distpos' does not match previous declaration at a.c 14
0 errors, 2 warnings
...
I should mention that both VS2010 and MinGW-GCC4 throw compilation errors:
> gcc -o a.exe a.c
a.c:18:8: error: conflicting types for 'distpos'
a.c:13:20: note: previous implicit declaration of 'distpos' was here
and
> cl a.c
a.c(18) : error C2371: 'distpos' : redefinition; different basic types
In general, you should always define functions before using them, or use function prototypes.
Upvotes: 1
Reputation: 272467
The following code works as expected (see http://ideone.com/GfLkZ):
#include <stdio.h>
#include <math.h>
// funktion
double distpos(double x1, double y1, double x2, double y2)
{
printf("%f\n", sqrt(pow((x1 - x2),2) + pow((y1 - y2),2))); // correct
return sqrt(pow((x1 - x2),2) + pow((y1 - y2),2));
}
int main(void)
{
double position[2] = { 5, 6 };
double origin[2] = { 3, 4 };
double distance = 0;
// function call
distance = distpos(position[0], position[1], *(origin), *(origin+1));
printf("%f\n", distance); // incorrect
}
Upvotes: 0