def safemod(a, b):
    """Modulus of a and 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:
        return float('nan')