0
A = array([[  1., 0., 0.], [ -9., 28., -9.], [ 0., 0., 1.]])

X = array([-0., -0.125,  0. ])

y = np.linalg.solve(A, X)

Is there an explanation for why python keeps giving me the answer:

y = array([-1.92747053e-18, -4.46428571e-03,  0.00000000e+00])

When y[0] and y[2] should clearly both be 0?

Andrew98
  • 41
  • 1
  • 6

1 Answers1

0

Computer can never give accurate results for floating point operation, and this behaviour is not restricted to python. The floating point precision is different from one computer to another.

You can find the precision for double floating points in numpy

print (np.finfo(np.double).precision)

or

print (np.finfo(float).precision)

If you wish you can change display precision of numpy by just typing.

np.set_printoptions(suppress=True)

or

print(np.array_str(y, suppress_small=True))

This will suppress results within the calculation precision limit.

check here for some clarification https://docs.python.org/3.6/tutorial/floatingpoint.html

https://en.wikipedia.org/wiki/Floating-point_arithmetic#Accuracy_problems

In numerical methods, rounding errors creates issues for operation involving millions of floating point calculation, Thus you find many such numerical methods involve setting an accuracy limit.

Khalil Al Hooti
  • 4,207
  • 5
  • 23
  • 40