Functional Weave
Code in Rust

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 zero
  • sin_cos(0.524) → sin 0.5, cos 0.866 30 degrees
  • sin_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.

pub fn sin_cos(radians: f64) -> SinCos
radiansfloatbetween -1,000,000 and 1,000,000
returnsSinCos

The type it declares, generated into your project

/// Sine and cosine of one angle, computed together because they share the range reduction.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SinCos {
    pub sin: f64,
    pub cos: f64,
}

Your code names it in one line, in the file that uses it

fune!(math.sin-cos@^1);  // then call sin_cos(…)
impl/rust.rs · 66 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

use super::funejson::Value;  ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one

// 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: f64 = 1.57079632673412561417e+00;
const PIO2_1T: f64 = 6.07710050650619224932e-11;
const INV_PIO2: f64 = 6.36619772367581382433e-01;
const LIMIT: f64 = 1000000.0;

fn sin_small(r: f64) -> f64 {
    let s = r * r;
    let mut p = 1.0 / 355687428096000.0;
    p = -1.0 / 1307674368000.0 + s * p;
    p = 1.0 / 6227020800.0 + s * p;
    p = -1.0 / 39916800.0 + s * p;
    p = 1.0 / 362880.0 + s * p;
    p = -1.0 / 5040.0 + s * p;
    p = 1.0 / 120.0 + s * p;
    p = -1.0 / 6.0 + s * p;
    r + r * s * p
}

fn cos_small(r: f64) -> f64 {
    let s = r * r;
    let mut p = -1.0 / 6402373705728000.0;
    p = 1.0 / 20922789888000.0 + s * p;
    p = -1.0 / 87178291200.0 + s * p;
    p = 1.0 / 479001600.0 + s * p;
    p = -1.0 / 3628800.0 + s * p;
    p = 1.0 / 40320.0 + s * p;
    p = -1.0 / 720.0 + s * p;
    p = 1.0 / 24.0 + s * p;
    p = -1.0 / 2.0 + s * p;
    1.0 + s * p
}

/// Sine and cosine using only +, -, * and /, in the same order as the
/// TypeScript and Python versions, so all three return the same doubles.
/// `f64::sin_cos` calls the platform's maths library, whose last bit may differ.
///
/// # Panics
/// Panics unless `radians` is finite and within -1,000,000..=1,000,000.
pub fn sin_cos(radians: f64) -> SinCos {
    if !radians.is_finite() || radians < -LIMIT || radians > LIMIT {
        panic!("radians must be a finite number between -1000000 and 1000000, received {}", radians);
    }
    let kf = (radians * INV_PIO2 + 0.5).floor();
    let r = radians - kf * PIO2_1 - kf * PIO2_1T;
    let s = sin_small(r);
    let c = cos_small(r);
    let quadrant = ((kf as i64 % 4) + 4) % 4;
    match quadrant {
        0 => SinCos { sin: s + 0.0, cos: c + 0.0 },
        1 => SinCos { sin: c + 0.0, cos: -s + 0.0 },
        2 => SinCos { sin: -s + 0.0, cos: -c + 0.0 },
        _ => SinCos { sin: -c + 0.0, cos: s + 0.0 },
    }
}

pub fn sin_cos_to_value(v: &SinCos) -> Value {
    Value::obj(vec![("sin", Value::Float(v.sin)), ("cos", Value::Float(v.cos))])
}

pub fn fune_vector(args: &[Value]) -> Value {
    sin_cos_to_value(&sin_cos(args[0].as_f64()))
}

Install

fune build

With that line in your source, in a Rust project (language rust in fune.project), fune build resolves it and nothing else, pins them in fune.lock, downloads only the Rust 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. A crate’s build.rs runs it before every compile. Or pin a range in fune.project and build in one step:

fune add math.sin-cos
Download for Rust math.sin-cos-1.0.0-rust.fune · 6,523 bytes sha256 8af8c4d78c49f3fb273765dac9be72eb5378f16257d2834e4b8af4f6e7a671e0

The manifest, vectors and README with only the Rust implementation. Install it without the registry with fune add ./math.sin-cos-1.0.0-rust.fune, or fetch it from a terminal with fune pull math.sin-cos@1.0.0:rust.

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.

CaseArgumentsExpected
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

PathBytes
README.md1,263
impl/python.py1,987
impl/rust.rs2,273
impl/typescript.ts2,267
vectors.json1,182