ospf_rust_math/algebra/concept/
precision.rs

1use super::{Arithmetic, SemiArithmetic};
2
3pub trait Precision: 'static {
4    const EPSILON: &'static Self;
5    const DECIMAL_DIGITS: Option<usize>;
6    const DECIMAL_PRECISION: &'static Self;
7}
8
9default impl<T: Arithmetic> Precision for T {
10    const EPSILON: &'static Self = Self::ZERO;
11    const DECIMAL_DIGITS: Option<usize> = None;
12    const DECIMAL_PRECISION: &'static Self = Self::EPSILON;
13}
14
15macro_rules! int_precision_template {
16    ($($type:ident)*) => ($(
17        impl Precision for $type {
18            const EPSILON: &'static Self = Self::ZERO;
19            const DECIMAL_DIGITS: Option<usize> = None;
20            const DECIMAL_PRECISION: &'static Self = Self::EPSILON;
21         }
22    )*)
23}
24int_precision_template! { bool i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
25
26impl Precision for f32 {
27    const EPSILON: &'static Self = &<f32>::MIN_POSITIVE;
28    const DECIMAL_DIGITS: Option<usize> = Some(<f32>::DIGITS as usize);
29    const DECIMAL_PRECISION: &'static Self = &Self::EPSILON;
30}
31
32impl Precision for f64 {
33    const EPSILON: &'static Self = &<f64>::MIN_POSITIVE;
34    const DECIMAL_DIGITS: Option<usize> = Some(<f64>::DIGITS as usize);
35    const DECIMAL_PRECISION: &'static Self = &Self::EPSILON;
36}