mathverse-core 0.1.3

MathVerse core: numeric traits, errors, constants, precision utilities, generic operations, common algorithms
Documentation
//! High-precision mathematical constants, in `f64` and `f32` widths.
//!
//! All constants are defined as `pub const f64` with `f32` variants available
//! under the `f32` submodule (e.g., `constants::f32::PI`).

macro_rules! constants {
    ($($name:ident: $val:expr),* $(,)?) => {
        $(
            #[allow(missing_docs)]
            pub const $name: f64 = $val;
        )*
        /// `f32` variants of all constants.
        pub mod f32 {
            use super::*;
            $(
                #[allow(missing_docs)]
                pub const $name: f32 = super::$name as f32;
            )*
        }
    };
}

constants! {
    PI: 3.14159265358979323846264338327950288,
    TAU: 6.28318530717958647692528676655900577,
    E: 2.71828182845904523536028747135266250,
    PHI: 1.61803398874989484820458683436563812,
    SQRT_2: 1.41421356237309504880168872420969808,
    SQRT_3: 1.73205080756887729352744634150587237,
    SQRT_5: 2.23606797749978969640917366873127623,
    SQRT_6: 2.44948974278317809819728407470589139,
    SQRT_7: 2.64575131106459059050161575363926042,
    SQRT_PI: 1.77245385090551602729816748334114518,
    SQRT_2PI: 2.50662827463100050241559627119775686,
    SQRT_LN4: 1.17741002251547401367847600393163626,
    SQRT_2_INV: 0.70710678118654752440084436210484904,
    SQRT_3_INV: 0.57735026918962576450914878050195746,
    CUBE_ROOT_2: 1.25992104989487316476721060727822835,
    CUBE_ROOT_3: 1.44224957030740838232163831419462179,
    E_SQRT: 1.64872127070012814684865078781416357,
    E_SQ: 7.38905609893065022723042746057500781,
    PI_SQ: 9.86960440108935861883449099987615114,
    PI_CUBE: 31.006276680299820175476315067101395,
    LN_2: 0.69314718055994530941723212145817657,
    LN_10: 2.30258509299404568401799145468436421,
    LN_PI: 1.14472988584940017414342735135305871,
    LOG2_E: 1.44269504088896340735992468100189256,
    LOG10_E: 0.43429448190325182765112891891660508,
    LOG2_10: 3.32192809488736234787031942948939018,
    LOG_2: 0.69314718055994530941723212145817657,
    LOG_10: 2.30258509299404568401799145468436421,
    EULER_GAMMA: 0.57721566490153286060651209008240243,
    CATALAN: 0.91596559417721901505460351493238411,
    APERY: 1.20205690315959428539973816151144999,
    FEIGENBAUM: 4.66920160910299067185320383797060023,
    FEIGENBAUM2: 2.50290787509589282228342812458338185,
    DEG_TO_RAD: 0.01745329251994329576923690768488613,
    RAD_TO_DEG: 57.29577951308232087679815481410517033,
    DEG_TO_GRAD: 1.11111111111111111111111111111111111,
    GRAD_TO_RAD: 0.01570796326794896619231321691639751,
    GRAD_TO_DEG: 0.9,
    HALF_PI: 1.57079632679489661923132169163975144,
    THIRD_PI: 1.04719755119659774615421446109713842,
    QUARTER_PI: 0.78539816339744830961566084581987572,
    LN_2PI: 1.83787706640934548356065947281122532,
    INV_PI: 0.31830988618379067153776752674502872,
    INV_SQRT_PI: 0.56418958354775628694807945156077259,
    TWO_SQRT_PI: 1.12837916709551257389615890312154518,
    E_INV: 0.36787944117144232159552377016146086,
    LOG10_2: 0.30102999566398119521373889472449303,
    SILVER_RATIO: 2.41421356237309504880168872420969808,
    INF: f64::INFINITY,
    NAN: f64::NAN,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[allow(clippy::suboptimal_flops)]
    fn values() {
        assert!((PI - 3.141592653589793).abs() < 1e-15);
        assert!((TAU - 2.0 * PI).abs() < 1e-15);
        assert!((E - 2.718281828459045).abs() < 1e-15);
        assert!((180.0 * DEG_TO_RAD - PI).abs() < 1e-15);
        assert!((f32::PI - f32::TAU / 2.0).abs() < 1e-6);
        assert!((HALF_PI - PI / 2.0).abs() < 1e-15);
        assert!((SQRT_PI - PI.sqrt()).abs() < 1e-15);
        assert!((LOG2_E * LN_2 - 1.0).abs() < 1e-15);
        assert!((LOG10_E * LN_10 - 1.0).abs() < 1e-15);
        assert!((SQRT_2_INV - 1.0 / SQRT_2).abs() < 1e-15);
        assert!(INF.is_infinite());
        assert!(NAN.is_nan());
        assert!((LN_2PI - (2.0 * PI).ln()).abs() < 1e-15);
        assert!((INV_PI - 1.0 / PI).abs() < 1e-15);
        assert!((INV_SQRT_PI - 1.0 / SQRT_PI).abs() < 1e-15);
        assert!((TWO_SQRT_PI - 2.0 / SQRT_PI).abs() < 1e-15);
        assert!((E_INV - 1.0 / E).abs() < 1e-15);
        assert!((LOG10_2 - 2.0_f64.log10()).abs() < 1e-15);
        assert!((SILVER_RATIO - (1.0 + SQRT_2)).abs() < 1e-15);
    }
}