russell_lab/math/
constants.rs1pub const PI: f64 =
3 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214;
4
5pub const SQRT_PI: f64 =
7 1.772453850905516027298167483341145182797549456122387128213807789852911284591032181374950656738544665_f64;
8
9pub const NAPIER: f64 =
11 2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138217852516642742746_f64;
12
13pub const EULER: f64 =
15 0.577215664901532860606512090082402431042159335939923598805767234884867726777664670936947063291746749_f64;
16
17pub const SQRT_2: f64 =
19 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885038753432764157f64;
20
21pub const SQRT_3: f64 = 1.7320508075688772935274463415058723669428052538103806280558069794519330169088000370811461867572485756756261414154f64;
23
24pub const SQRT_6: f64 =
26 2.44948974278317809819728407470589139196594748065667012843269256725096037745731502653985943310464023f64;
27
28pub const SQRT_2_BY_3: f64 =
30 0.816496580927726032732428024901963797321982493552223376144230855750320125819105008846619811034880078272864f64;
31
32pub const SQRT_3_BY_2: f64 =
34 1.22474487139158904909864203735294569598297374032833506421634628362548018872865751326992971655232011f64;
35
36pub const ONE_BY_3: f64 =
38 0.33333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333f64;
39
40pub const TWO_BY_3: f64 =
42 0.66666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666f64;
43
44pub const ONE_BY_SQRT_2: f64 =
46 0.707106781186547524400844362104849039284835937688474036588339868995366239231053519425193767163820786367506f64;
47
48pub const COS_PI_BY_8: f64 =
50 0.923879532511286756128183189396788286822416625863642486115097731280535007501102358714839934850344596097963f64;
51
52pub const SIN_PI_BY_8: f64 =
54 0.382683432365089771728459984030398866761344562485627041433800635627546033960089692237013785342283547148424f64;
55
56pub const LN2: f64 =
58 0.693147180559945309417232121458176568075500134360255254120680009493393621969694715605863326996418687f64;
59
60pub const LN10: f64 = 2.30258509299404568401799145468436420760110148862877297603332790096757260967735248023599f64;
62
63pub const SQRT_EPSILON: f64 = 1.49011611938476562500000e-8f64;
65
66pub const GOLDEN_RATIO: f64 =
68 1.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475f64;
69
70#[cfg(test)]
73mod tests {
74 use super::{
75 COS_PI_BY_8, GOLDEN_RATIO, NAPIER, ONE_BY_3, ONE_BY_SQRT_2, PI, SIN_PI_BY_8, SQRT_2, SQRT_2_BY_3, SQRT_3,
76 SQRT_3_BY_2, SQRT_6, SQRT_EPSILON, SQRT_PI, TWO_BY_3,
77 };
78 use crate::approx_eq;
79
80 #[test]
81 fn constants_are_correct() {
82 assert_eq!(PI, std::f64::consts::PI);
83 approx_eq(SQRT_PI, f64::sqrt(PI), 1e-15);
84 assert_eq!(NAPIER, f64::exp(1.0));
85 assert_eq!(SQRT_2, std::f64::consts::SQRT_2);
86 assert_eq!(SQRT_3, f64::sqrt(3.0));
87 assert_eq!(SQRT_6, f64::sqrt(6.0));
88 assert_eq!(SQRT_2_BY_3, f64::sqrt(2.0 / 3.0));
89 assert_eq!(SQRT_3_BY_2, f64::sqrt(3.0 / 2.0));
90 assert_eq!(ONE_BY_3, 1.0 / 3.0);
91 assert_eq!(TWO_BY_3, 2.0 / 3.0);
92 assert_eq!(ONE_BY_SQRT_2, std::f64::consts::FRAC_1_SQRT_2);
93 assert_eq!(COS_PI_BY_8, f64::cos(std::f64::consts::PI / 8.0));
94 assert_eq!(SIN_PI_BY_8, f64::sin(std::f64::consts::PI / 8.0));
95 assert_eq!(f64::EPSILON, f64::powi(2.0, -52));
96 assert_eq!(SQRT_EPSILON, f64::sqrt(f64::EPSILON));
97 assert_eq!(GOLDEN_RATIO, (1.0 + f64::sqrt(5.0)) / 2.0);
98 }
99}