Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference in floating point numbers in Matlab and Python?

I am importing some numbers into python from a matlab "mat" file version 5 and they have a different precision.

This is the python Code:

output_data = numpy.matlab.loadmat( ( path ), mat_dtype=True )

print(output_data)

Python3 output: -0.0030967733388058967-> 20 numbers or digits
Matlab output:  -0.00309677333880590  -> 18  "        "

Why would python and matlab have different precisions?

like image 911
Michael Avatar asked Dec 12 '25 17:12

Michael


2 Answers

Both MATLAB and Python/Numpy use the same floating-point representation by default, 64-bit floats (double precision). How this is stored is dictated by your CPU, not by the program (the program basically gets to choose between 32, 64 and 80 bit floats, which are the representations understood by the CPU in your average PC).

The difference that you see is how the programs choose to display the numbers. The internal representation is identical.

If the difference between these numbers, as displayed, matter to your application, you are doing it wrong. Floating-point computations are imprecise by definition, you cannot count on those last few digits to be meaningful.

like image 172
Cris Luengo Avatar answered Dec 15 '25 06:12

Cris Luengo


Hope the answer in this link helps:

https://ww2.mathworks.cn/matlabcentral/answers/77455-raising-to-the-power-of-large-numbers-is-giving-me-the-wrong-answer

To quote the person who answered the question:

In general, MATLAB uses double-precision, which roughly means that you can expect 16 digits of precision.

like image 40
kkblue Avatar answered Dec 15 '25 05:12

kkblue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!