formula for calculating residues