trig-const

Crates.iotrig-const
lib.rstrig-const
version0.3.0
created_at2025-07-25 17:41:44.666413+00
updated_at2025-08-01 17:25:55.875693+00
descriptionConst trig functions in Rust
homepagehttps://github.com/michaelciraci/trig-const
repositoryhttps://github.com/michaelciraci/trig-const
max_upload_size
id1767868
size128,759
Michael Ciraci (michaelciraci)

documentation

http://docs.rs/trig-const

README

trig-const

Rust implementation of const trig functions.

The majority of functions have been implemented using a modified version of libm for const Rust.

This implementation carries forward the original MIT license.

Project Goals

  • Correctness while const (same result as std within a rounding error)
  • no-std
  • No unsafe

Requirements

This crate supports any compiler version back to rustc 1.85.0

[dependencies]
trig-const = "0"

Example

use std::f64::consts::PI;
use trig_const::cos;

const COS_PI: f64 = cos(PI);
assert_eq!(COS_PI, -1.0);
use std::f64::consts::PI;
use trig_const::{atan2, cos, sin};

/// 45° in radians
const DEG_45: f64 = 45.0 * PI / 180.0;

/// Pre-computed matrix to rotate object 30°
const ROTATIONAL_MATRIX: [[f64; 3]; 3] = [
    [cos(DEG_45), 0.0, sin(DEG_45)],
    [0.0, 1.0, 0.0],
    [-sin(DEG_45), 0.0, cos(DEG_45)],
];

/// atan2 calculation
const ATAN2_0_0: f64 = atan2(0.0, 0.0);

fn main() {
    println!("{:?}", ROTATIONAL_MATRIX);
    println!("{}", ATAN2_0_0);
}

History

This crate was originally implemented using trigonometric Taylor series approximations, inspired by the work of Dr. Austin Henley and Dr. Stephen Marz:

However, several functions have since been implemented using a modified version of lib for const Rust which improved precision.

Commit count: 0

cargo fmt