3

I had a np.sqrt() snippet that didn't check for negative values, so I understandably got warnings. I thought I had fixed it, but I got one again:

RuntimeWarning: invalid value encountered in sqrt
  data_std = np.sqrt(np.absolute(data_std))

Are there any invalid numbers other than negative ones for np.sqrt?

iacob
  • 20,084
  • 6
  • 92
  • 119

1 Answers1

0

Square roots are not defined for negative real numbers, and numpy will produce nan for negative inputs of "real" dtype int, float, -np.inf (and nan itself).

However, square roots are defined for all complex numbers:

dtype example x np.sqrt(x) RuntimeWarning
Positive float 1. 1.
Positive int 1 1
Positive complex 1+0J 1
Negative float -1. nan ⚠️
Negative int -1 nan ⚠️
Negative complex -1+0j 1j
np.inf np.inf
np.inf nan ⚠️
np.nan nan
iacob
  • 20,084
  • 6
  • 92
  • 119