Skip to main content

Crate trig_const

Crate trig_const 

Source
Expand description

§trig-const

Rust minimum rustc 1.85

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 45°
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);
}

§Features

  • nightly: Running in nightly exposes the function const_eval_select. This allows the compiler to call a different function if the function is to be evaluated at compile-time or run-time
    • Any const function calls will use this library
    • Any runtime function calls will forward to libm.
  • std: This feature can be used in conjunction with nightly. This will forward runtime function calls to std instead of libm, to take advantage of hardware intrinsics (SIMD/FPU)

§Precision

Precision will be different platform to platform. There is a precision comparison within examples, under examples/std_cmp.rs (to run: cargo run --release --example std_cmp).

On aarch64, I get:

FuncTotal TestsDiff CountMax Diff
acos20000003494194.44089e-16
acosh9900000185053188.88178e-16
asin20000001737902.22045e-16
asinh9900000187149138.88178e-16
atan5026548332908262.22045e-16
atanh19999987710643.34115e-11
cos5026548321733391.11022e-16
cosh2513274264986632.91038e-11
ln9999900134139558.88178e-16
exp2000000119443233.63798e-12
fabs2000000100.00000e0
floor2000000100.00000e0
sin5026548322556091.11022e-16
sinh2513274272006412.91038e-11
sqrt1000000125009534.44089e-16
tan50265483207772073.72529e-9

§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 libm for const Rust which improved precision.

Functions§

acos
Arccosine
acosh
Inverse hyperbolic cosine (f64)
asin
Arcsine
asinh
Inverse hyperbolic sine (f64)
atan
Arctangent
atan2
Arctan2
atanh
Inverse hyperbolic tangent (f64)
cos
Cosine
cosh
Hyperbolic Cosine
cot
Cotangent
csc
Cosecant
exp
Exponential, base e (f64)
expi
x^pow
fabs
factorial
Factorial (x!)
floor
ln
Computes natural log using a port from Rust’s libm
pow
Power function
sec
Secant
sin
Sine
sinh
Hyperbolic Sine
sqrt
Const sqrt function using Newton’s method
tan
The tangent of x (f64).