Numpy doesn’t throw FloatingPointError for dot product

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


Read more here: https://stackoverflow.com/questions/64401148/numpy-doesnt-throw-floatingpointerror-for-dot-product

Content Attribution

This content was originally published by Nur L at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: