math.sin-cos
Sine and cosine of an angle in radians from + - * / only, so every language returns the same doubles.
1.0.1 · published 2026-10-03 by charlie · Anterra
Pinned by 10 tests, run in TypeScript, Python and Rust.
What it does
Sine and cosine of an angle in radians, computed together with nothing but `+`, `-`, `*` and `/`, so TypeScript, Python and Rust return the same doubles. Used for arcs and pie slices, where a coordinate that lands on x.xx5 must round the same way in every language. See `math.ln` for why the platform's `sin` and `cos` cannot promise that.
## Method
For example
sinCos(0)→ sin 0, cos 1 zerosinCos(0.524)→ sin 0.5, cos 0.866 30 degreessinCos(1.571)→ sin 1, cos 0 90 degrees: cos is a rounding residue, not exactly 0
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 sinCos(radians: number): SinCos
| radians | float | between -1,000,000 and 1,000,000 |
| returns | SinCos |
The type it declares, generated into your project
/** Sine and cosine of one angle, computed together because they share the range reduction. */
export interface SinCos {
readonly sin: number;
readonly cos: number;
}
Your code names it in one line, in the file that uses it
import { sinCos } from "#fune/math.sin-cos@^1";
// Derived from fdlibm e_rem_pio2.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 { type SinCos } from "./math_sin_cos_types.ts";
// pi/2 in two parts (fdlibm): PIO2_1 has 33 significant bits, so k * PIO2_1 is
// exact for every k up to 2^20, which covers the permitted range.
const PIO2_1 = 1.57079632673412561417e+00;
const PIO2_1T = 6.07710050650619224932e-11;
const INV_PIO2 = 6.36619772367581382433e-01;
const LIMIT = 1000000;
// sin and cos on |r| <= pi/4 by Taylor series, which converge there to well
// below an ulp. Coefficients are correctly rounded 1/n!.
function sinSmall(r: number): number {
const s = r * r;
let p = 1 / 355687428096000;
p = -1 / 1307674368000 + s * p;
p = 1 / 6227020800 + s * p;
p = -1 / 39916800 + s * p;
p = 1 / 362880 + s * p;
p = -1 / 5040 + s * p;
p = 1 / 120 + s * p;
p = -1 / 6 + s * p;
return r + r * s * p;
}
function cosSmall(r: number): number {
const s = r * r;
let p = -1 / 6402373705728000;
p = 1 / 20922789888000 + s * p;
p = -1 / 87178291200 + s * p;
p = 1 / 479001600 + s * p;
p = -1 / 3628800 + s * p;
p = 1 / 40320 + s * p;
p = -1 / 720 + s * p;
p = 1 / 24 + s * p;
p = -1 / 2 + s * p;
return 1 + s * p;
}
/**
* Sine and cosine using only +, -, * and /, which IEEE 754 defines exactly.
*
* Math.sin and Math.cos come from each platform's maths library and may differ
* in the last bit between JavaScript, Python and Rust; after rounding, a value
* next to a boundary can then go either way (an SVG arc end at x.xx5). The
* angle is reduced to a quarter turn k and a remainder |r| <= pi/4 with a
* two-part pi/2 (Cody and Waite), and the series above do the rest.
*/
export function sinCos(radians: number): SinCos {
if (typeof radians !== "number" || !Number.isFinite(radians) || radians < -LIMIT || radians > LIMIT) {
throw new RangeError(`radians must be a finite number between -1000000 and 1000000, received ${radians}`);
}
const k = Math.floor(radians * INV_PIO2 + 0.5);
const r = radians - k * PIO2_1 - k * PIO2_1T;
const s = sinSmall(r);
const c = cosSmall(r);
const quadrant = ((k % 4) + 4) % 4;
if (quadrant === 0) return { sin: s + 0, cos: c + 0 };
if (quadrant === 1) return { sin: c + 0, cos: -s + 0 };
if (quadrant === 2) return { sin: -s + 0, cos: -c + 0 };
return { sin: -c + 0, cos: s + 0 };
}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.sin-cos
The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./math.sin-cos-1.0.1-typescript.fune, or fetch it from a terminal with fune pull math.sin-cos@1.0.1:typescript.
The whole function, every language, is one file too: math.sin-cos-1.0.1.fune, 12,682 bytes, sha256 4d26576babecb864dc8bf2da42d2ee2798ace554299b378af172bb29cee14e35. 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.sin-cos
after — your function gets the result and the arguments, and returns the final result.
// fune: after math.sin-cos
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.sin-cos --steps.
// fune: step math.sin-cos 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 | → | sin 0, cos 1 |
| 30 degrees | 0.524 | → | sin 0.5, cos 0.866 |
| 90 degrees: cos is a rounding residue, not exactly 0 | 1.571 | → | sin 1, cos 0 |
| 180 degrees | 3.142 | → | sin 0, cos -1 |
| -45 degrees | -0.785 | → | sin -0.707, cos 0.707 |
| -270 degrees | -4.712 | → | sin 1, cos -0 |
| one radian | 1 | → | sin 0.841, cos 0.54 |
| a large angle | 1,000 | → | sin 0.827, cos 0.562 |
| near the limit | 999,999 | → | sin -0.977, cos 0.212 |
| beyond the limit is an error | 1,000,001 | → | error: radians must be a finite number between -1000000 and 1000000 |
More from the author
1. Reduce: k = the nearest whole number of quarter turns, r = x - k pi/2, with pi/2 in two parts (fdlibm's `pio2_1`, 33 bits, and `pio2_1t`), so that k x pio2_1 is exact for every k the permitted range needs. 2. sin r and cos r by Taylor series to r^17 and r^18 on |r| <= pi/4, where they converge to well below an ulp. 3. Rotate by the quadrant k mod 4.
Results are within about 2e-16 of the true values. `sinCos(pi / 2).cos` is 6.1e-17, not 0, because pi/2 as a double is not exactly pi/2; every library says the same. No `-0` is returned.
The angle must be within ±1,000,000 radians: beyond that the two-part pi/2 no longer reduces exactly, and an angle that large is almost always a bug (degrees passed as radians many times over).
Sources: W. J. Cody and W. Waite, *Software Manual for the Elementary Functions* (1980); Sun Microsystems fdlibm, `e_rem_pio2.c` (the constants).
## Notices
Derived from fdlibm e_rem_pio2.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 | 292 |
| README.md | 1,759 |
| impl/python.py | 2,278 |
| impl/rust.rs | 2,570 |
| impl/typescript.ts | 2,564 |
| vectors.json | 1,182 |