Functional Weave
Code in TypeScript

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 zero
  • atan(1) → 0.785 one is pi/4
  • atan(-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.

export function atan(x: number): number
xfloatany finite number
returnsfloatthe 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

import { atan } from "#fune/math.atan@^1";
impl/typescript.ts · 89 lines · open · raw
// 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.

// 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.
const ATAN_HI = [
  4.63647609000806093515e-01,
  7.85398163397448278999e-01,
  9.82793723247329054082e-01,
  1.57079632679489655800e+00,
];
const ATAN_LO = [
  2.26987774529616870924e-17,
  3.06161699786838301793e-17,
  1.39033110312309984516e-17,
  6.12323399573676603587e-17,
];
const 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,
];
const TWO_POW_66 = 73786976294838206464;
const TWO_POW_M29 = 1.862645149230957e-9;

/**
 * Arctangent in radians using only +, -, * and /, in the same order as the
 * Python and Rust versions, so all three return the same double. Math.atan
 * comes from each platform's maths 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.
 */
export function atan(x: number): number {
  if (typeof x !== "number" || !Number.isFinite(x)) {
    throw new RangeError(`x must be a finite number, received ${String(x)}`);
  }
  const negative = x < 0;
  const a = negative ? -x : x;
  if (a >= TWO_POW_66) {
    const r = ATAN_HI[3] + ATAN_LO[3];
    return negative ? -r : r;
  }
  let i: number;
  let t = x;
  if (a < 0.4375) {
    // atan(x) rounds to x here; + 0 turns -0 into 0.
    if (a < TWO_POW_M29) return x + 0;
    i = -1;
  } else {
    t = a;
    if (a < 1.1875) {
      if (a < 0.6875) {
        i = 0;
        t = (2.0 * t - 1.0) / (2.0 + t);
      } else {
        i = 1;
        t = (t - 1.0) / (t + 1.0);
      }
    } else if (a < 2.4375) {
      i = 2;
      t = (t - 1.5) / (1.0 + 1.5 * t);
    } else {
      i = 3;
      t = -1.0 / t;
    }
  }
  const z = t * t;
  const w = z * z;
  const s1 = z * (AT[0] + w * (AT[2] + w * (AT[4] + w * (AT[6] + w * (AT[8] + w * AT[10])))));
  const s2 = w * (AT[1] + w * (AT[3] + w * (AT[5] + w * (AT[7] + w * AT[9]))));
  if (i < 0) return t - t * (s1 + s2);
  const r = ATAN_HI[i] - ((t * (s1 + s2) - ATAN_LO[i]) - t);
  return negative ? -r : r;
}

Install

fune build

With that line in your source, in a TypeScript project (language typescript in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the TypeScript 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
Download for TypeScript math.atan-1.0.1-typescript.fune · 9,423 bytes sha256 0544e2cbd496b76c5ac18bbcb3bdcfb203de630bb2ae7ec9f309d11d47a3cecb

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./math.atan-1.0.1-typescript.fune, or fetch it from a terminal with fune pull math.atan@1.0.1:typescript.

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.

CaseArgumentsExpected
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
CaseArgumentsExpected
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

PathBytes
NOTICE285
README.md2,392
impl/python.py2,906
impl/rust.rs3,608
impl/typescript.ts2,863
vectors.json2,124