mathhook_core/core/
constants.rs

1//! Mathematical constants
2
3use serde::{Deserialize, Serialize};
4
5/// Common mathematical constants
6#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
7pub enum MathConstant {
8    Pi,
9    E,
10    I,
11    Infinity,
12    NegativeInfinity,
13    Undefined,
14    GoldenRatio,
15    EulerGamma,
16    TribonacciConstant,
17}
18
19impl MathConstant {
20    /// Get the approximate floating-point value of the constant
21    ///
22    /// # Examples
23    ///
24    /// ```rust
25    /// use mathhook_core::MathConstant;
26    ///
27    /// assert!((MathConstant::Pi.to_f64() - std::f64::consts::PI).abs() < 1e-10);
28    /// assert!((MathConstant::E.to_f64() - std::f64::consts::E).abs() < 1e-10);
29    /// ```
30    pub fn to_f64(self) -> f64 {
31        match self {
32            MathConstant::Pi => std::f64::consts::PI,
33            MathConstant::E => std::f64::consts::E,
34            MathConstant::I => f64::NAN,
35            MathConstant::Infinity => f64::INFINITY,
36            MathConstant::NegativeInfinity => f64::NEG_INFINITY,
37            MathConstant::Undefined => f64::NAN,
38            MathConstant::GoldenRatio => 1.618033988749895,
39            MathConstant::EulerGamma => 0.5772156649015329,
40            MathConstant::TribonacciConstant => 1.839286755214161,
41        }
42    }
43}
44
45/// Epsilon value for floating-point comparisons
46///
47/// Used to handle floating-point precision errors.
48/// Two floats are considered equal if their absolute difference is less than EPSILON.
49pub const EPSILON: f64 = 1e-10;