Skip to content

std.math.trig

Native trigonometric library for the Janus standard library. All functions are pure, comptime-eligible, and carry zero external dependencies. No libm, no graft c, no libc linkage required.

Advancement Loop note: This module started as a graft c adapter over libm. It now consists entirely of native Janus polynomial approximations. The port closed a stdlib gap and advanced the ecosystem; every future project using trig gets sovereign code that compiles cleanly on bare-metal targets.


use std.math.trig

Or pull the most common names through the std.math facade:

use std.math // exposes sin, cos, tan, atan2 as top-level wrappers

When precision matters or you need the full API – sincos, fmod, asin, and friends – import std.math.trig directly.


ConstantTypeValue
PIf643.14159265358979323846…
PI_F32f323.1415927
HALF_PIf64PI / 2
TAUf642 × PI
INV_PIf641 / PI
SQRT2f641.41421356237…

FunctionSignatureDescription
sinfunc sin(x: f64) -> f64Sine of angle in radians
cosfunc cos(x: f64) -> f64Cosine of angle in radians
tanfunc tan(x: f64) -> f64Tangent of angle in radians
sin_f32func sin_f32(x: f32) -> f32f32 sine – computed via f64, cast back
cos_f32func cos_f32(x: f32) -> f32f32 cosine
tan_f32func tan_f32(x: f32) -> f32f32 tangent
FunctionSignatureDescription
asinfunc asin(x: f64) -> f64Inverse sine, domain [-1, 1], range [-π/2, π/2]
acosfunc acos(x: f64) -> f64Inverse cosine, domain [-1, 1], range [0, π]
atanfunc atan(x: f64) -> f64Inverse tangent, range (-π/2, π/2)
atan2func atan2(y: f64, x: f64) -> f64Four-quadrant arctangent, range (-π, π]
atan2_f32func atan2_f32(y: f32, x: f32) -> f32f32 variant of atan2
FunctionSignatureDescription
sinhfunc sinh(x: f64) -> f64Hyperbolic sine
coshfunc cosh(x: f64) -> f64Hyperbolic cosine
tanhfunc tanh(x: f64) -> f64Hyperbolic tangent
asinhfunc asinh(x: f64) -> f64Inverse hyperbolic sine
acoshfunc acosh(x: f64) -> f64Inverse hyperbolic cosine, domain [1, ∞)
atanhfunc atanh(x: f64) -> f64Inverse hyperbolic tangent, domain (-1, 1)
FunctionSignatureDescription
fmodfunc fmod(x: f64, y: f64) -> f64Floating-point remainder, same sign as x
fmod_f32func fmod_f32(x: f32, y: f32) -> f32f32 variant
deg_to_radfunc deg_to_rad(d: f64) -> f64Degrees → radians
rad_to_degfunc rad_to_deg(r: f64) -> f64Radians → degrees

Computing sine and cosine simultaneously is faster than two separate calls because the reduction and polynomial evaluation happen once.

struct SinCos64 {
sin: f64,
cos: f64,
}
struct SinCos32 {
sin: f32,
cos: f32,
}
func sincos(x: f64) -> SinCos64
func sincos_f32(x: f32) -> SinCos32

use std.math.trig
func main() do
let angle = trig.PI / 4.0 // 45 degrees in radians
let s = trig.sin(angle) // ≈ 0.7071
let c = trig.cos(angle) // ≈ 0.7071
let t = trig.tan(angle) // ≈ 1.0
println("sin(π/4) = ", s)
println("cos(π/4) = ", c)
println("tan(π/4) = ", t)
end
use std.math.trig
func rotate(x: f64, y: f64, theta: f64) -> { x: f64, y: f64 } do
let sc = trig.sincos(theta)
return {
x: x * sc.cos - y * sc.sin,
y: x * sc.sin + y * sc.cos,
}
end
use std.math.trig
func bearing(dx: f64, dy: f64) -> f64 do
// atan2 returns angle in (-π, π]; negate y for screen coords
return trig.atan2(dy, dx)
end
use std.math.trig
/// Normalise an angle to [0, 2π).
func wrap_angle(a: f64) -> f64 do
let wrapped = trig.fmod(a, trig.TAU)
if wrapped < 0.0 do
return wrapped + trig.TAU
end
return wrapped
end
use std.math.trig
/// 16-entry sine table baked at compile time — zero runtime cost.
const SINE_TABLE: [16]f32 = comptime do
var t: [16]f32 = undefined
inline for 0..16 |i| do
let angle = trig.TAU * as[f64](i) / 16.0
t[i] = as[f32](trig.sin(angle))
end
t
end

TypeMax ErrorMethod
f64≤ 4 ULPMinimax polynomial with Payne-Hanek argument reduction
f32≤ 4 ULPComputed in f64, cast to f32 at return

ULP = Units in the Last Place. 4 ULP is indistinguishable from libm quality for nearly all applications.


Gap IDScopeStatus
trig-large-argsin/cos/tan when `x
fmod-large-ratiofmod(x, y) when x/y > 2^52Precision loss in extreme ratios. Flag open; tracked in Janus issues.

Both gaps are documented and on the advancement roadmap. Neither affects typical usage in graphics, physics, or signal processing.


Next: std.math.trig_int — integer-only trig for zero-FPU paths and the BitNet inference pipeline.