math.sin-cos
Sine and cosine of an angle in radians from + - * / only, so every language returns the same doubles.
1.0.0 (not the latest) · 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
sin_cos(0)→ sin 0, cos 1 zerosin_cos(0.524)→ sin 0.5, cos 0.866 30 degreessin_cos(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.
def sin_cos(radians: float) -> SinCos
| radians | float | between -1,000,000 and 1,000,000 |
| returns | SinCos |
The type it declares, generated into your project
@dataclass(frozen=True)
class SinCos:
"""Sine and cosine of one angle, computed together because they share the range reduction."""
sin: float
cos: float
Your code names it in one line, in the file that uses it
from fune.math.sin_cos import sin_cos # math.sin-cos@^1
import math
from .math_sin_cos_types import SinCos
# 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.
PIO2_1 = 1.57079632673412561417e+00
PIO2_1T = 6.07710050650619224932e-11
INV_PIO2 = 6.36619772367581382433e-01
LIMIT = 1000000
def _sin_small(r: float) -> float:
s = r * r
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
def _cos_small(r: float) -> float:
s = r * r
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
def sin_cos(radians: float) -> SinCos:
"""Sine and cosine using only +, -, * and /, in the same order as the
TypeScript and Rust versions, so all three return the same doubles.
math.sin and math.cos come from the C library and may differ in the last
bit. See the README for the method."""
if (
isinstance(radians, bool)
or not isinstance(radians, (int, float))
or not math.isfinite(radians)
or radians < -LIMIT
or radians > LIMIT
):
raise ValueError("radians must be a finite number between -1000000 and 1000000, received %r" % (radians,))
x = float(radians)
k = math.floor(x * INV_PIO2 + 0.5)
r = x - k * PIO2_1 - k * PIO2_1T
s = _sin_small(r)
c = _cos_small(r)
quadrant = k % 4
if quadrant == 0:
return SinCos(sin=s + 0.0, cos=c + 0.0)
if quadrant == 1:
return SinCos(sin=c + 0.0, cos=-s + 0.0)
if quadrant == 2:
return SinCos(sin=-s + 0.0, cos=-c + 0.0)
return SinCos(sin=-c + 0.0, cos=s + 0.0)Install
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.sin-cos
The manifest, vectors and README with only the Python implementation. Install it without the registry with fune add ./math.sin-cos-1.0.0-python.fune, or fetch it from a terminal with fune pull math.sin-cos@1.0.0:python.
The whole function, every language, is one file too: math.sin-cos-1.0.0.fune, 10,949 bytes, sha256 b59a5e1a8d56abe53dcc0c342d1361407ec02316c586513bf381648c8fdefae2. 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).
Files
| Path | Bytes |
|---|---|
| README.md | 1,263 |
| impl/python.py | 1,987 |
| impl/rust.rs | 2,273 |
| impl/typescript.ts | 2,267 |
| vectors.json | 1,182 |