def sgn(val):
    """Sign function. Returns -1 if val negative, 0 if zero, and 1 if
    positive.
    """
    try:
        return val._sgn_()
    except AttributeError:
        if val == 0:
            return 0
        if val > 0:
            return 1
        else:
            return -1