Skip to main content

use_number/
constants.rs

1//! Small numeric constants that are reused across the workspace.
2
3/// The golden ratio as an `f64`.
4pub const GOLDEN_RATIO: f64 = 1.618_033_988_749_895;
5
6/// The golden ratio as an `f32`.
7pub const GOLDEN_RATIO_F32: f32 = 1.618_034;
8
9/// The square root of three as an `f64`.
10pub const SQRT_3: f64 = 1.732_050_807_568_877_2;
11
12/// The square root of three as an `f32`.
13pub const SQRT_3_F32: f32 = 1.732_050_8;
14
15#[cfg(test)]
16mod tests {
17    use super::{GOLDEN_RATIO, GOLDEN_RATIO_F32, SQRT_3, SQRT_3_F32};
18
19    #[test]
20    fn constants_match_their_reference_relationships() {
21        assert!(
22            GOLDEN_RATIO
23                .mul_add(GOLDEN_RATIO, -(GOLDEN_RATIO + 1.0))
24                .abs()
25                < 1.0e-12
26        );
27        assert!(
28            GOLDEN_RATIO_F32
29                .mul_add(GOLDEN_RATIO_F32, -(GOLDEN_RATIO_F32 + 1.0))
30                .abs()
31                < 1.0e-5
32        );
33        assert!(SQRT_3.mul_add(SQRT_3, -3.0).abs() < 1.0e-12);
34        assert!(SQRT_3_F32.mul_add(SQRT_3_F32, -3.0).abs() < 1.0e-5);
35    }
36}