import math


def safediv(a, b):
    """Divide a by b, but return NaN or infinity on division by zero

    The behavior is equivalent to Numpy with the 'ignore' setting.
    """
    try:
        return a / b
    except ZeroDivisionError:
        sign = a * b
        if a and not math.isnan(a):
            return math.copysign(float('inf'), sign)
        else:
            return float('nan')