1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::{
    fmt::Display,
    ops::{Add, Div, Mul, Rem, Sub},
};

use crate::utils::num_traits::{Bounded, One, Zero};

macro_rules! impl_ord_f64 {
    (f64, $op_trait:ident, $op_func:ident, $op:tt) => {
        impl $op_trait<f64> for OrdF64 {
            type Output = Self;
            fn $op_func(self, rhs: f64) -> Self::Output {
                Self(self.0 $op rhs)
            }
        }
    };
    ($op_trait:ident, $op_func:ident, $op:tt) => {
        impl $op_trait for OrdF64 {
            type Output = Self;
            fn $op_func(self, rhs: Self) -> Self::Output {
                Self(self.0 $op rhs.0)
            }
        }
    };
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub struct OrdF64(pub f64);

impl Eq for OrdF64 {}

impl PartialOrd for OrdF64 {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl Ord for OrdF64 {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.partial_cmp(other).unwrap()
    }
}

impl Display for OrdF64 {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<f64> for OrdF64 {
    fn from(value: f64) -> Self {
        Self(value)
    }
}

impl Bounded for OrdF64 {
    fn min_value() -> Self {
        Self(f64::MIN)
    }
    fn max_value() -> Self {
        Self(f64::MAX)
    }
}

impl Zero for OrdF64 {
    fn zero() -> Self {
        Self(0.0)
    }
}

impl One for OrdF64 {
    fn one() -> Self {
        Self(1.0)
    }
}

// 演算の定義
impl_ord_f64!(Add, add, +);
impl_ord_f64!(Sub, sub, -);
impl_ord_f64!(Mul, mul, *);
impl_ord_f64!(Div, div, /);
impl_ord_f64!(Rem, rem, %);
impl_ord_f64!(f64, Add, add, +);
impl_ord_f64!(f64, Sub, sub, -);
impl_ord_f64!(f64, Mul, mul, *);
impl_ord_f64!(f64, Div, div, /);
impl_ord_f64!(f64, Rem, rem, %);