# math.atan The arctangent of a number, in radians between -pi/2 and pi/2, returning the same double in TypeScript, Python and Rust. Used wherever an angle has to come out of a ratio (a roof pitch from rise over run, a bearing) and then be rounded: `Math.atan`, Python's `math.atan` and Rust's `f64::atan` come from each platform's maths library, which may differ in the last bit, and a value next to a rounding boundary can then go either way. See `math.ln` for the longer argument. ## Method This is a line-for-line port of fdlibm's `s_atan.c`, using only `+`, `-`, `*`, `/` and comparisons, in the same order in every language: 1. |x| of 2^66 or more is pi/2 (its high and low parts added); |x| below 2^-29 is x itself, which is atan(x) correctly rounded there. 2. Otherwise |x| is reduced against atan(0.5), atan(1), atan(1.5) or pi/2, chosen by the boundaries 7/16, 11/16, 19/16 and 39/16 (below 7/16 there is no reduction). Each constant is held as a high and a low double. 3. An odd polynomial of degree 23 (fdlibm's coefficients) on the remainder, split into even and odd halves as fdlibm does. fdlibm chooses the interval by testing the high 32 bits of the double; every one of its thresholds has zero low bits, so comparing |x| with the threshold as a number picks exactly the same interval. ## Accuracy fdlibm documents an error below one unit in the last place. Checked against an 80-digit reference on 200,000 random arguments from 2^-35 to 2^30: 99.3% are the correctly rounded value and the rest are one ulp away, never more. Every vector here is the correctly rounded value, computed from that independent reference, not from this code. `atan(-0)` is `0`: no `-0` is returned, as in `math.sin-cos`. A non-finite argument is an error. Sources: Sun Microsystems fdlibm 5.3, `s_atan.c` (the method, constants and thresholds), https://www.netlib.org/fdlibm/s_atan.c. ## Notices Derived from fdlibm s_atan.c. Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. Developed at SunSoft, a Sun Microsystems, Inc. business. Permission to use, copy, modify, and distribute this software is freely granted, provided that this notice is preserved. The same notice heads each implementation file. 1.0.1 adds its attribution notices (NOTICE). The code is unchanged apart from that notice at the top of each implementation file; the tests are unchanged.