Skip to content

Asari-AI/libmcs-safe-rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

libmcs_safe_rust

A complete, memory-safe Rust translation of the libmCS (Mathematical Library for Critical Systems) library.

This translation was created by Asari AI. For details on how this translation was produced, see the accompanying blog post.

Features

  • 100% Safe Rust: Zero unsafe code blocks (#![forbid(unsafe_code)] enforced)
  • Complete API: 216 mathematical functions including trigonometric, exponential, logarithmic, special functions (Bessel, Gamma, Error), and full complex number support
  • Dual Precision: Both f32 and f64 implementations for all functions
  • Verified Against External Standards: Numerical accuracy validated through testing against external specifications. We thank the spaceflight software verification company GTD GmbH for providing test cases for validation.

Dependencies

This library uses the following external crates:

Crate Purpose
num-complex Complex number types (Complex<f64>, Complex<f32>)
num-traits Numeric traits (transitive dependency)
flate2 Test data decompression (test infrastructure only)

Implementation Notes

Key differences from the original:

Standard Library Usage

The translation uses Rust standard library methods in the following contexts:

Method Usage Affected Functions
.is_nan() Input validation (edge cases) 51 functions (24%)
.is_infinite() Boundary handling (edge cases) 20 functions (9%)
.abs() Input normalization and computation 15 functions (7%)
std::f64::consts::PI etc. Mathematical constants 12 functions (6%)

Note: ~90% of .is_nan() and .is_infinite() usage is for input validation at function entry points (returning early for special IEEE 754 values), not in core algorithm paths.

Functions Using External Library Methods

A small number of functions use num-complex methods directly:

Function Method Used Context
conjd::conj .conj() Entire implementation
cexpd::cexp .sin_cos() Main computation path
casinhd::casinh .asinh() Special case branch only (when |re| ≈ |im| ≈ 1.0)
cacoshd::cacosh .sqrt(), .ln(), .norm() Special case branch only

Numerical Precision Enhancements

Some f32 functions use f64 intermediate precision to improve accuracy (e.g., lgammaf). This differs from the original C implementation but provides better numerical results.

Algorithm Selection

Certain complex functions use heuristic-based algorithm selection for problematic input regions (e.g., csind near |re| ≈ |im| ≈ 1.0). These thresholds were tuned for numerical accuracy against reference implementations.

Installation

Add to your Cargo.toml:

[dependencies]
libmcs = { git = "https://github.com/Asari-AI/libmcs-safe-rust" }

Usage

use libmcs::{sind, cosf, sqrtd, Complex64, cexpd};

// Double precision trigonometry
let x = sind::sin(1.0);
let y = cosf::cosf(0.5_f32);

// Square root
let root = sqrtd::sqrt(2.0);

// Complex exponential
let z = Complex64::new(1.0, 2.0);
let result = cexpd::cexp(z);

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

Original Work Attribution

This project is a Rust translation of libmCS.

libmCS is Copyright (c) GTD GmbH and incorporates code under the following licenses:

  • NetBSD License
  • Public Domain
  • Red Hat permissive license
  • Rich Felker license
  • Sun Microsystems license
  • GTDGmbH license

See the NOTICE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages