!UPSTREAM: Hack: avoid rounding errors in computation of atan

TODO:

- Rather than clamp the argument to asin to [-1, 1], it would be far
  better to use the formula:

    atan(x) = pi/2 - atan(1/x)

  when x > 1

  (and similar when x < -1).

  But I want to write better tests first.