You could use nan
from math import sqrt
nan = float('nan')
def mysqrt(x):
return sqrt(x) if x >= 0.0 else nan
See https://en.wikipedia.org/wiki/NaN
Edit: Hi Tony! It's been a while...
Edit2: numpy.sqrt()
already returns nan for negative numbers!
Edit3: please @steven.rose.94, use python tag as well as python3 to increase the audience of your posts.