Basically, while translating code from Matlab to Python, I realised that there was a difference of the order of e-15 in my results.
Then I realised that all of this is caused by the exp()
functions yielding different results (exp() in Matlab, exp() in Python Math, exp() in Numpy). It seems like a small difference, but this slight difference accumulates over iterations and the final results end up exploding.
For instance, if you write exp(2.34983545)
in both Python and Matlab, and export the result from Matlab to python (or viceversa), and compare it, you can see that it yields different results (again, order of e-15).
Please let me know if anyone has had this problem and if there is a way to fix it!
The simple code in matlab:
exp_result = exp(2.34983545)
save('exp_result.mat','exp_result')
The simple code in Python:
import numpy as np
import math
from scipy.io import loadmat
np_exp = np.exp(2.34983545)
math_exp = math.exp(2.34983545)
matlab_data = loadmat('exp_result.mat')
matlab_exp = matlab_data['exp_result']
print(np_exp - matlab_exp)
print(math_exp - matlab_exp)
#output yields -1.7763568394002505e-15
I would like to add that I have compared 500 million elements using the loadmat method, and the accuracy is pefrect for all results, except for the exp() functions.
I am aware of numerical impercisions due to floating point operations, but how can this be fixed in this scenario?
Thanks in advance!