Numpy doesn't throw FloatingPointError for dot product

Viewed 90

I am taking the dot-product of two numpy arrays (both float32). I am deliberately setting numpy to raise a FloatingPointError in case of overflow. However, dot does not behave as expected: instead of raising a FloatingPointError as it does when using ordinary multiplication, dot returns inf.

Is this the intended behavior? Is there a way to force dot to raise an exception as well?

Minimal working example:

import numpy as np

np.seterr(over="raise")

x = np.array([2e+38], dtype=np.float32)
y = np.array([10], dtype=np.float32)

x * y
>>> FloatingPointError: overflow encountered in multiply

np.dot(x,y)
>>> inf
1 Answers

Accepting @hpaulj's comment as the answer – matmul does throw a FloatingPointError as required, while dot doesn't (no idea why). For my purpose matmul gives the same result as dot.

Related