def calculate(air_temp_c, rh, on_error=float('nan')):
    """Calculate the heat index.

    The heat index is calculated using the air temperature in degrees 
    Celsius and the relative humidity as a percentage (0.0 to 100.0).
    See:
    http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml

    Parameters
    ----------
    air_temp_c : float
        The air temperature in degrees C.
    rh : float
        The relative humidity, as a percentage (0.0 to 100.0)
    on_error : optional, float('nan')
        The return value when an error occcurs during calculation.  May 
        be any type, but preferably a float.

    Returns
    -------
    float
        The heat index in degrees C.

    """
    try:
        at_f = float(air_temp_c) * (9.0 / 5.0) + 32.0
        rh_f = float(rh)
        heat_index = (-42.379 + 2.04901523 * at_f + 10.14333127 * rh_f - 
            0.22475541 * at_f * rh_f - 0.00683783 * at_f ** 2 - 0.05481717 *
            rh_f ** 2 + 0.00122874 * at_f ** 2 * rh_f + 0.00085282 * at_f *
            rh_f ** 2 - 1.99e-06 * at_f ** 2 * rh_f ** 2)
        if at_f <= 80.0 or rh_f <= 40.0:
            heat_index = float(air_temp_c)
        else:
            heat_index = (heat_index - 32.0) * (5.0 / 9.0)
    except:
        heat_index = on_error
    return heat_index
