With a case differentiation. And also, better use atan2 when you really program this algorithm. Such subtleties don't appear in an algorithm description.