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
.
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!