ospf_rust_math/algebra/concept/
arithmetic.rs

1use std::fmt::{Debug, Display, Formatter};
2use chrono::Duration;
3
4pub trait SemiArithmetic: 'static + Sized + Clone + PartialEq + PartialOrd {
5    const ZERO: &'static Self;
6}
7
8pub trait Arithmetic: SemiArithmetic {
9    const ONE: &'static Self;
10}
11
12impl SemiArithmetic for bool {
13    const ZERO: &'static Self = &false;
14}
15
16impl Arithmetic for bool {
17    const ONE: &'static Self = &true;
18}
19
20macro_rules! int_arithmetic_template {
21    ($($type:ident)*) => ($(
22        impl SemiArithmetic for $type {
23            const ZERO: &'static $type = &0;
24        }
25
26        impl Arithmetic for $type {
27            const ONE: &'static $type = &1;
28        }
29    )*)
30}
31int_arithmetic_template! { i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
32
33macro_rules! floating_arithmetic_template {
34    ($($type:ident)*) => ($(
35        impl SemiArithmetic for $type {
36            const ZERO: &'static Self = &0.;
37        }
38
39        impl Arithmetic for $type {
40            const ONE: &'static Self = &1.;
41        }
42    )*)
43}
44floating_arithmetic_template! { f32 f64 }
45
46pub struct Infinity {}
47
48pub const INF: Infinity = Infinity {};
49
50impl Display for Infinity {
51    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
52        write!(f, "inf")
53    }
54}
55
56impl Debug for Infinity {
57    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
58        write!(f, "inf")
59    }
60}
61
62pub struct NegativeInfinity {}
63
64pub const NEG_INF: NegativeInfinity = NegativeInfinity {};
65
66impl Display for NegativeInfinity {
67    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
68        write!(f, "-inf")
69    }
70}
71
72impl Debug for NegativeInfinity {
73    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
74        write!(f, "-inf")
75    }
76}
77
78pub struct NaN {}
79
80pub const NAN: NaN = NaN {};
81
82impl Display for NaN {
83    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
84        write!(f, "nan")
85    }
86}
87
88impl Debug for NaN {
89    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
90        write!(f, "nan")
91    }
92}