[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
precision problem with mex file
From: |
fredounet |
Subject: |
precision problem with mex file |
Date: |
Wed, 17 Jul 2013 22:17:00 -0700 (PDT) |
Hello,
I'm under octave 3.6.2, ubuntu 12.10.
I have a precision problem running a mex file (myfunction.c), I compile it
with mkoctfile --mex -lm myfunction.c, and run it with myfunction([1 1 1]'),
for example. Here is the code:
------------------------------------------------------------------------------------------------------------------------
#include "mex.h"
#include <math.h>
void mexFunction( int nlhs, mxArray *plhs[], int nrhs,const mxArray *prhs[])
{
const int * Dimthetap;
int i=0,Dimtheta;
double A=0.0;
Dimthetap=mxGetDimensions(prhs[0]);
Dimtheta=(int)Dimthetap[0];
//Dimtheta=3;
for (i=0;i<=Dimtheta-1;i++)
{
A=351.40999999999985448084771633148193359375000000000000*351.40999999999985448084771633148193359375000000000000;
A=A+105.45999999999912688508629798889160156250000000000000*105.45999999999912688508629798889160156250000000000000;
A=A+(-309.32999999999992724042385816574096679687500000000000)*(-309.32999999999992724042385816574096679687500000000000);
}
A=1/sqrt(A);
mexPrintf("inverse A =%.50f\n",A);
mexPrintf("real
value=0.00208380437429572578852954300998590042581781744957\n");
}
------------------------------------------------------------------------------------------------------------------------
The code is non sens because it is a part of a larger code I shortened to
post it on this forum. The problem is when I compute inverse A, I should
have the following value (inverse
A=0.00208380437429572578852954300998590042581781744957), you can check
directly on Octave if you want. But when I use the mex file, I've got a
precision problem and have the value:
inverse A =0.002083804374295725*35484867401578412682283669710159*
which is not correct. The problem I think comes from the loop and Dimtheta
which is set by getting the dimension of the input argument. When I simply
uncomment Dimtheta=3 (or = another integer value), Inverse A is correct, I
guess it comes from mxGetDimensions but I don't know where is my mistake.
when I put the following code:
------------------------------------------------------------------------------------------------------------------------
A=351.40999999999985448084771633148193359375000000000000*351.40999999999985448084771633148193359375000000000000;
A=A+105.45999999999912688508629798889160156250000000000000*105.45999999999912688508629798889160156250000000000000;
A=A+(-309.32999999999992724042385816574096679687500000000000)*(-309.32999999999992724042385816574096679687500000000000);
------------------------------------------------------------------------------------------------------------------------
outside of the loop, the value is correct, so the problem is some kind of a
combination with the loop and mxGetDimensions. I hope I was clear, thanks
for your help.
Fred
--
View this message in context:
http://octave.1599824.n4.nabble.com/precision-problem-with-mex-file-tp4655798.html
Sent from the Octave - General mailing list archive at Nabble.com.
- precision problem with mex file,
fredounet <=