def rat(n, d, nan=float('nan'), mul=1.0):
    """ Calculate a ratio while avoiding division by zero errors.
        Strictly speaking we should have nan=float('nan') but for practical
        purposes we'll maybe want to report None (or 0.0?).
    """
    try:
        return ( float(n) * mul ) / float(d)
    except (ZeroDivisionError, TypeError):
        return nan