2

Hi I'm rewriting a script from MATLAB to C++ using armadillo library for linear algebra and matrix.

for getting more or less the same output i called cout method:

cout.precision(4);
cout.setf(ios::fixed);

but i'm getting different result:

Matlab result:

0.0000    0.0000    0.0000    0.0000    0.0000   
0.0012    0.0014    0.0016    0.0020    0.0281  
0.0396    0.0297    0.0297    0.0495    0.0976  

Armadillo c++ result:

0.0000    0.0000    0.0000    0.0000    0.0000 
0.0012    0.0014    0.0016    0.0020    0.0282 
0.0416    0.0312    0.0312    0.0520    0.1027

now, i don't know if thoose little imprecision (0.039 is near to 0.041) are caused by some errors in my C++ code translated or they should be considered normal differences between double precision in g++ and MATLAB

In my code I'm using a lot of cycle like this:

xi_summed = xi_summed + normalise((trans % (alpha.col(t) * b.t())));

where xi_summed, trans, alpha, b are arma::mat and % is a element-wise multiplication and mat::t() is transpose and normalise are a function that make the entries of matrix A array sum to 1.

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
nkint
  • 11,513
  • 31
  • 103
  • 174
  • 1
    definitely does not look like normal differences to me. – WeaselFox Feb 16 '12 at 12:56
  • i'm tring to rewrite some function of HMM toolkit and there are more than 500 lines of c++ code. i don't know what line post because i don't know where the error should be – nkint Feb 16 '12 at 13:01
  • If your problem is ill-conditioned then a subtle difference in the algorithm could lead to very different results. Order of operations can matter. – stardt Feb 19 '12 at 01:46

2 Answers2

3

This is certainly not a normal difference!

The machine epsilon will be orders of magnitude smaller than the errors you are getting (i.e. 2.22e-016 vs. 2.0e-3).

You can confirm your machine epsilon with the following C++ code:

#include <limits>

cout << "Machine Epsilon is: " << numeric_limits<double>::epsilon() << endl;

Your Matlab script will be bound to the same limitations; you can confirm this by entering the following into Matlab command window:

eps

If the computations you are performing in Matlab and C++ are mathematically equivalent then you should obtain the same result - especially with 4 d.p. precision!

athwaites
  • 433
  • 3
  • 7
2

Usually the precision is much better, you can find out the precision of matlab by typing eps. For me it's 2.2204e-16.

However, it also highly depends of the calculus you're doing.

For instance, if you compute the difference of two very big numbers, and the difference is very small, your precision will be very bad.

Indeed, eps is a relative precision. So if you type eps(n), you will have the precision for the given value.

For instance, eps(10^16) is 2. So for operations with numbers as big as 10^16, the precision will be 2.

Oli
  • 15,935
  • 7
  • 50
  • 66
  • and how can i check precision in c++ for set the same? – nkint Feb 16 '12 at 13:04
  • In C++. the double variables are 64 bit, so the precision should be around `2^-64=5e-20`. Similarly float should have around `2^-32=2e-10` precision – Oli Feb 16 '12 at 13:13
  • so if i want matlab to has the same precision of c++ i have to write eps(5e-20). but what about my different result? for you are caused by different precision or the error is somewhere else? – nkint Feb 16 '12 at 13:16
  • It's probably because of a bug... Usually the machine precision should not make that big difference. – Oli Feb 16 '12 at 13:18