I have a big data set of floating point numbers. I iterate through them and evaluate np.log(x) for each of them. I get
np.log(x)
RuntimeWarning: divide b
use exception handling:
In [27]: def safe_ln(x): try: return math.log(x) except ValueError: # np.log(x) might raise some other error though return float("-inf") ....: In [28]: safe_ln(0) Out[28]: -inf In [29]: safe_ln(1) Out[29]: 0.0 In [30]: safe_ln(-100) Out[30]: -inf