math.atan
Arctangent in radians from + - * / only (fdlibm's method), so every language returns the same double.
1.0.1 · published 2026-10-03 by charlie · Anterra
Pinned by 22 tests, run in TypeScript, Python and Rust.
What it does
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
For example
atan(0)→ 0 zeroatan(1)→ 0.785 one is pi/4atan(-1)→ -0.785 minus one is -pi/4
The function
The same function in TypeScript, Python and Rust, pinned by the same tests. Pick your language; the choice follows you around the registry.
def atan(x: float) -> float
| x | float | any finite number |
| returns | float | the angle in radians, between -pi/2 and pi/2; within one unit in the last place of the true value, bit-identical in every language |
Your code names it in one line, in the file that uses it
from fune.math.atan import atan # math.atan@^1
# 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.
import math
# fdlibm s_atan.c (Sun Microsystems, 1993). atan of 0.5, 1, 1.5 and infinity,
# each split into a high and a low part so the sum carries more than 53 bits.
ATAN_HI = (
4.63647609000806093515e-01,
7.85398163397448278999e-01,
9.82793723247329054082e-01,
1.57079632679489655800e00,
)
ATAN_LO = (
2.26987774529616870924e-17,
3.06161699786838301793e-17,
1.39033110312309984516e-17,
6.12323399573676603587e-17,
)
AT = (
3.33333333333329318027e-01,
-1.99999999998764832476e-01,
1.42857142725034663711e-01,
-1.11111104054623557880e-01,
9.09088713343650656196e-02,
-7.69187620504482999495e-02,
6.66107313738753120669e-02,
-5.83357013379057348645e-02,
4.97687799461593236017e-02,
-3.65315727442169155270e-02,
1.62858201153657823623e-02,
)
TWO_POW_66 = 73786976294838206464.0
TWO_POW_M29 = 1.862645149230957e-09
def atan(x: float) -> float:
"""Arctangent in radians using only +, -, * and /, in the same order as the
TypeScript and Rust versions, so all three return the same double.
math.atan comes from the C library and may differ in the last bit.
fdlibm's method: reduce |x| against atan(0.5), atan(1), atan(1.5) or
pi/2, then an odd polynomial on the remainder. fdlibm picks the interval
from the high word of the double; comparing |x| with the same boundaries
(7/16, 11/16, 19/16, 39/16, 2^66, 2^-29) is exactly equivalent."""
if isinstance(x, bool) or not isinstance(x, (int, float)) or not math.isfinite(x):
raise ValueError("x must be a finite number, received %r" % (x,))
x = float(x)
negative = x < 0
a = -x if negative else x
if a >= TWO_POW_66:
z = ATAN_HI[3] + ATAN_LO[3]
return -z if negative else z
if a < 0.4375:
if a < TWO_POW_M29:
# atan(x) rounds to x here; + 0.0 turns -0 into 0.
return x + 0.0
i = -1
else:
x = a
if a < 1.1875:
if a < 0.6875:
i = 0
x = (2.0 * x - 1.0) / (2.0 + x)
else:
i = 1
x = (x - 1.0) / (x + 1.0)
elif a < 2.4375:
i = 2
x = (x - 1.5) / (1.0 + 1.5 * x)
else:
i = 3
x = -1.0 / x
z = x * x
w = z * z
s1 = z * (AT[0] + w * (AT[2] + w * (AT[4] + w * (AT[6] + w * (AT[8] + w * AT[10])))))
s2 = w * (AT[1] + w * (AT[3] + w * (AT[5] + w * (AT[7] + w * AT[9]))))
if i < 0:
return x - x * (s1 + s2)
z = ATAN_HI[i] - ((x * (s1 + s2) - ATAN_LO[i]) - x)
return -z if negative else zInstall
fune build
With that line in your source, in a Python project (language python in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Python package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:
fune add math.atan
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./math.atan-1.0.1-python.fune, or fetch it from a terminal with fune pull math.atan@1.0.1:python.
The whole function, every language, is one file too: math.atan-1.0.1.fune, 16,172 bytes, sha256 28024017684bf94c01788cdac745e870183b7f2d5e8709a02fca9422fec83d7b. It installs into a project of any language.
Customise it in your app
The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.
before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.
# fune: before math.atan
after — your function gets the result and the arguments, and returns the final result.
# fune: after math.atan
replace — it requires no other capability, so there is no dependency to replace.
step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show math.atan --steps.
# fune: step math.atan after <n|label>
Tests
A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript, Python and Rust, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json.
| Case | Arguments | Expected | |
|---|---|---|---|
| zero | 0 | → | 0 |
| one is pi/4 | 1 | → | 0.785 |
| minus one is -pi/4 | -1 | → | -0.785 |
| square root of 3 is pi/3 (60 degrees) | 1.732 | → | 1.047 |
| 1 over root 3 is pi/6 (30 degrees) | 0.577 | → | 0.524 |
| tiny: atan(x) rounds to x | 0 | → | 0 |
| exactly 2^-29, the first value that goes through the polynomial | 0 | → | 0 |
| small but not tiny: the cubic term shows | 0 | → | 0 |
| 0.1 | 0.1 | → | 0.1 |
| just below the 7/16 boundary | 0.437 | → | 0.412 |
Show the other 12 tests
| Case | Arguments | Expected | |
|---|---|---|---|
| exactly 7/16: first value reduced against atan(0.5) | 0.438 | → | 0.412 |
| 0.5 lands on the atan(0.5) constant | 0.5 | → | 0.464 |
| exactly 11/16: reduced against atan(1) | 0.688 | → | 0.602 |
| exactly 19/16: reduced against atan(1.5) | 1.188 | → | 0.871 |
| 1.5 lands on the atan(1.5) constant | 1.5 | → | 0.983 |
| exactly 39/16: reduced against pi/2 | 2.438 | → | 1.181 |
| ten | 10 | → | 1.471 |
| negative is odd: atan(-x) = -atan(x) | -7.5 | → | -1.438 |
| huge but below 2^66 | 5,000,000,000,000,000,000 | → | 1.571 |
| 2^66 and above is pi/2 (2^70 here) | 1,180,591,620,717,411,300,000 | → | 1.571 |
| hugely negative is -pi/2 | -1,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,… | → | -1.571 |
| a string is an error, not a coercion | 1 | → | error: x must be a finite number |
More from the author
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.
Files
| Path | Bytes |
|---|---|
| NOTICE | 285 |
| README.md | 2,392 |
| impl/python.py | 2,906 |
| impl/rust.rs | 3,608 |
| impl/typescript.ts | 2,863 |
| vectors.json | 2,124 |