Home > Back-end >  Python and Matlab differences in linear algebra calculates
Python and Matlab differences in linear algebra calculates

Time:01-06

I need to write a program for linear algebra calculates. I switched from Matlab to python and worked with numpy and scipy. There are some small differences in precision between Python and Matlab which cause different result in the end. For example for the matrix

A =[2       -25,
    -25     -622]

in Matlab det(A) = 627.0 while in python it is 626.9999999999978. When I check the variables, these tiny differences for all the variables will significantly change the results. what shall I do?

CodePudding user response:

Floating points are finicky. When you run into precision problems you can examine the numbers here:

Floating point representation of 626.9999999999978

In this case 626.9999999999978 is 627. Admitted the number formatter could have done a better job.

For more advice on floating point numbers: https://floating-point-gui.de/errors/comparison/

If you know that all you matrix entries are integers, then consider using a representation of matrices that doesn't use floating points.

  • Related