Reputation: 11733
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
.
Upvotes: 2
Views: 1826
Reputation: 433
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!
Upvotes: 3
Reputation: 16035
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
.
Upvotes: 2