In real-time applications fast and accurate algorithms for calculating astronomical refraction are required. Some of the most widely-used expressions are fast, but numerically unstable, and can not be applied where the correction is largest, i.e. close to the horizon. In the present paper a new formula for refraction, which is both fast and numerically stable, is given and compared with previously published refraction formulas. The approximate calculation of refraction 'below the horizon', and of the finite distance correction near the horizon, are also discussed.