clamping the costheta values to prevent errors for numerical overflows