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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use super::{Number, Signed, UnsignedDecimal};
use anyhow::{anyhow, Result};
use std::{
    cmp::Ordering,
    ops::{Add, Div, Mul, Sub},
};

// Intentionally keeping operations pegged to Number for now.
//
// The whole point of the newtype wrappers is to ensure the mathematical
// operations we perform are logical. Providing a general purpose `checked_mul`
// that multiplies two collaterals together would defeat the whole purpose of
// the exercise. Going forward, we'll add type-specific operations.
//
// Addition and subtraction, however, can be added. All our numeric newtypes can
// be added and subtracted sanely.

impl<T: UnsignedDecimal> Signed<T> {
    /// Addition that checks for integer overflow.
    pub fn checked_add(self, rhs: Self) -> Result<Self> {
        Ok(match (self.is_negative(), rhs.is_negative()) {
            (false, false) => Self::new_positive(self.value().checked_add(rhs.value())?),
            (true, true) => Self::new_negative(self.value().checked_add(rhs.value())?),
            (false, true) => {
                if self.value() >= rhs.value() {
                    Self::new_positive(self.value().checked_sub(rhs.value())?)
                } else {
                    Self::new_negative(rhs.value().checked_sub(self.value())?)
                }
            }
            (true, false) => {
                if self.value() >= rhs.value() {
                    Self::new_negative(self.value().checked_sub(rhs.value())?)
                } else {
                    Self::new_positive(rhs.value().checked_sub(self.value())?)
                }
            }
        })
    }

    /// Subtraction that checks for underflow
    pub fn checked_sub(self, rhs: Self) -> Result<Self> {
        self.checked_add(-rhs)
    }
}

impl Number {
    /// Multiplication that checks for integer overflow
    pub fn checked_mul(self, rhs: Self) -> Result<Self> {
        match self.value().checked_mul(rhs.value()).ok() {
            None => Err(anyhow!(
                "Overflow while multiplying {} and {}",
                self.value(),
                rhs.value()
            )),
            Some(value) => Ok(if self.is_negative() == rhs.is_negative() {
                Signed::new_positive(value)
            } else {
                Signed::new_negative(value)
            }),
        }
    }

    /// Division that checks for underflow and divide-by-zero.
    pub fn checked_div(self, rhs: Self) -> Result<Self> {
        if rhs.is_zero() {
            Err(anyhow!("Cannot divide with zero"))
        } else {
            match self.value().checked_div(rhs.value()).ok() {
                None => Err(anyhow!(
                    "Overflow while dividing {} by {}",
                    self.value(),
                    rhs.value()
                )),
                Some(value) => Ok(if self.is_negative() == rhs.is_negative() {
                    Signed::new_positive(value)
                } else {
                    Signed::new_negative(value)
                }),
            }
        }
    }

    /// equality check with allowance for precision diff
    pub fn approx_eq(self, other: Number) -> Result<bool> {
        Ok((self - other)?.abs() < Self::EPS_E7)
    }

    /// equality check with allowance for precision diff
    pub fn approx_eq_eps(self, other: Number, eps: Number) -> Result<bool> {
        Ok((self - other)?.abs() < eps)
    }

    /// less-than with allowance for precision diff
    pub fn approx_lt_relaxed(self, other: Number) -> Result<bool> {
        Ok(self < (other + Self::EPS_E7)?)
    }

    /// greater-than with allowance for precision diff
    pub fn approx_gt_relaxed(self, other: Number) -> Result<bool> {
        Ok(self > (other - Self::EPS_E7)?)
    }

    /// greater-than with restriction for precision diff
    pub fn approx_gt_strict(self, other: Number) -> Result<bool> {
        Ok(self > (other + Self::EPS_E7)?)
    }
}

impl Mul for Number {
    type Output = anyhow::Result<Self>;

    fn mul(self, rhs: Self) -> Self::Output {
        self.checked_mul(rhs)
    }
}

impl Mul<u64> for Number {
    type Output = anyhow::Result<Self>;

    fn mul(self, rhs: u64) -> Self::Output {
        self.checked_mul(rhs.into())
    }
}

impl Div<u64> for Number {
    type Output = anyhow::Result<Self>;

    fn div(self, rhs: u64) -> Self::Output {
        self.checked_div(rhs.into())
    }
}

impl Div for Number {
    type Output = anyhow::Result<Self>;

    fn div(self, rhs: Self) -> Self::Output {
        self.checked_div(rhs)
    }
}

impl<T: UnsignedDecimal> Add for Signed<T> {
    type Output = anyhow::Result<Self>;

    fn add(self, rhs: Self) -> Self::Output {
        self.checked_add(rhs)
    }
}

impl<T: UnsignedDecimal> Sub for Signed<T> {
    type Output = anyhow::Result<Self>;

    fn sub(self, rhs: Self) -> Self::Output {
        self.checked_sub(rhs)
    }
}

impl<T: UnsignedDecimal> std::cmp::PartialOrd for Signed<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: UnsignedDecimal> std::cmp::Ord for Signed<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        match (self.is_positive_or_zero(), other.is_positive_or_zero()) {
            (true, true) => self.value().cmp(&other.value()),
            (false, false) => other.value().cmp(&self.value()),
            (true, false) => Ordering::Greater,
            (false, true) => Ordering::Less,
        }
    }
}