Skip to main content

tacet_core/
math.rs

1//! Math functions for no_std compatibility.
2//!
3//! In no_std mode, f64 doesn't have transcendental methods like sqrt, ln, cos, etc.
4//! This module provides these functions using libm.
5
6/// Square root.
7#[inline]
8pub fn sqrt(x: f64) -> f64 {
9    libm::sqrt(x)
10}
11
12/// Natural logarithm.
13#[inline]
14pub fn ln(x: f64) -> f64 {
15    libm::log(x)
16}
17
18/// Base-10 logarithm.
19#[inline]
20pub fn log10(x: f64) -> f64 {
21    libm::log10(x)
22}
23
24/// Cosine.
25#[inline]
26pub fn cos(x: f64) -> f64 {
27    libm::cos(x)
28}
29
30/// Exponential (e^x).
31#[inline]
32pub fn exp(x: f64) -> f64 {
33    libm::exp(x)
34}
35
36/// Ceiling (round up).
37#[inline]
38pub fn ceil(x: f64) -> f64 {
39    libm::ceil(x)
40}
41
42/// Power (x^y).
43#[inline]
44pub fn pow(x: f64, y: f64) -> f64 {
45    libm::pow(x, y)
46}
47
48/// Cube root.
49#[inline]
50pub fn cbrt(x: f64) -> f64 {
51    libm::cbrt(x)
52}
53
54/// Floor (round down).
55#[inline]
56pub fn floor(x: f64) -> f64 {
57    libm::floor(x)
58}
59
60/// Round to nearest integer.
61#[inline]
62pub fn round(x: f64) -> f64 {
63    libm::round(x)
64}
65
66/// Square (x^2).
67#[inline]
68pub fn sq(x: f64) -> f64 {
69    x * x
70}
71
72/// Standard normal CDF: Φ(x) = (1 + erf(x/√2)) / 2
73#[inline]
74pub fn normal_cdf(x: f64) -> f64 {
75    0.5 * (1.0 + libm::erf(x * core::f64::consts::FRAC_1_SQRT_2))
76}
77
78/// Absolute value.
79#[inline]
80pub fn abs(x: f64) -> f64 {
81    libm::fabs(x)
82}