Search code examples
pythonnumpydot-product

Python's Numpy dot function returning incorrect value, why?


Real simple, my code is:

import numpy as np


a = np.array([0.4, 0.3])
b = np.array([-0.15, 0.2])

print(np.dot(a,b))

The dot product of this should be 0, and instead i get:

3.3306690738754695e-18

Solution

  • Floating-point!

    Floating-point (i.e. non-integer) arithmetic tends not to be 100% accurate.

    See here for more info.

    Also, note that your result is very close to zero.