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
use crate::{BigInt, UBigInt};

impl BigInt {

    #[must_use = "method returns a new number and does not mutate the original value"]
    pub fn sub_bi(&self, other: &BigInt) -> Self {
        let result = if self.is_neg() != other.is_neg() {
            BigInt::from_ubi(self.val.add_ubi(&other.val), self.is_neg())
        }

        else {
            let self_less = self.val.lt_ubi(&other.val);
            let abs_diff = if self_less {
                other.val.sub_ubi(&self.val)
            } else {
                self.val.sub_ubi(&other.val)
            };
            let is_diff_zero = abs_diff.is_zero();

            // self - other      self_less       self.is_neg       diff.val == 0     result.is_neg()
            //    3 - 4            true            false               false             true
            //    4 - 3            false           false               false             false
            // (-3) - (-4)         true            true                false             false
            // (-4) - (-3)         false           true                false             true
            //    3 - 3            false           false               true              false
            // (-3) - (-3)         false           true                true              false

            BigInt::from_ubi(abs_diff, (self_less ^ self.is_neg()) & !is_diff_zero)
        };

        #[cfg(test)] assert!(result.is_valid());

        result
    }

    pub fn sub_bi_mut(&mut self, other: &BigInt) {

        if self.is_neg() != other.is_neg() {
            self.val.add_ubi_mut(&other.val);
        }

        else {
            let self_less = self.val.lt_ubi(&other.val);

            if self_less {
                self.val = other.val.sub_ubi(&self.val);
            }

            else {
                self.val.sub_ubi_mut(&other.val);
            }

            let is_diff_zero = self.val.is_zero();

            self._is_neg = (self_less ^ self.is_neg()) & !is_diff_zero;
        }

        #[cfg(test)] assert!(self.is_valid());
    }

    #[must_use = "method returns a new number and does not mutate the original value"]
    pub fn sub_i32(&self, other: i32) -> Self {
        let result = if self.is_neg() != (other < 0) {
            BigInt::from_ubi(self.val.add_u32(other.abs() as u32), self.is_neg())
        }

        else {
            let other_abs = other.abs() as u32;
            let self_less = self.val.lt_u32(other_abs);
            let abs_diff = if self_less {
                UBigInt::from_u32(other_abs - self.val.to_u32().unwrap())
            } else {
                self.val.sub_u32(other_abs)
            };
            let is_diff_zero = abs_diff.is_zero();

            BigInt::from_ubi(abs_diff, (self_less ^ self.is_neg()) & !is_diff_zero)
        };

        #[cfg(test)] assert!(result.is_valid());

        result
    }

    pub fn sub_i32_mut(&mut self, other: i32) {

        if self.is_neg() != (other < 0) {
            self.val.add_u32_mut(other.abs() as u32);
        }

        else {
            let other_abs = other.abs() as u32;
            let self_less = self.val.lt_u32(other_abs);

            if self_less {
                self.val = UBigInt::from_u32(other_abs - self.val.to_u32().unwrap());
            }

            else {
                self.val.sub_u32_mut(other_abs);
            }

            let is_diff_zero = self.val.is_zero();
            self._is_neg = (self_less ^ self.is_neg()) & !is_diff_zero;
        }

        #[cfg(test)] assert!(self.is_valid());
    }

}

#[cfg(test)]
mod tests {
    use crate::BigInt;

    #[test]
    fn sign_test() {

        for x in -7..8 {

            for y in -7..8 {
                let mut x1 = BigInt::from_i32(x);
                let y1 = BigInt::from_i32(y);
                let mut x2 = BigInt::from_i32(x);
                let res1 = x1.sub_bi(&y1);
                let res2 = x1.sub_i32(y);
                let res3 = BigInt::from_i32(x - y);
                x1.sub_bi_mut(&y1);
                x2.sub_i32_mut(y);

                assert_eq!(x1, x2);
                assert_eq!(res1, res2);
                assert_eq!(res2, res3);
                assert_eq!(res1, x1);
                assert_eq!(res2, x2);
            }

        }

    }

}