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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
use crate::{BigInt, UBigInt};
use crate::err::ConversionError;
use std::fmt;

impl BigInt {

    pub fn from_i32(n: i32) -> Self {
        let _is_neg = n < 0;

        BigInt {
            val: UBigInt::from_u32(n.abs() as u32),
            _is_neg
        }
    }

    pub fn to_i32(&self) -> Result<i32, ConversionError> {

        match self.val.to_u32() {
            Ok(n) => if !self.is_neg() && n <= i32::MAX as u32 {
                Ok(n as i32)
            } else if self.is_neg() && n <= (i32::MIN as i64).abs() as u32 {
                Ok((-(n as i64)) as i32)  // i32::MIN.abs() > i32::MAX.abs()
            } else {
                Err(ConversionError::NotInRange { permitted: "-2.14e-9~2.14e9".to_string(), error: self.to_scientific_notation(5) })
            },
            Err(e) => Err(e)
        }

    }

    pub fn from_i64(n: i64) -> Self {
        let _is_neg = n < 0;

        BigInt {
            val: UBigInt::from_u64(n.abs() as u64),
            _is_neg
        }
    }

    pub fn to_i64(&self) -> Result<i64, ConversionError> {

        match self.val.to_u64() {
            Ok(n) => if !self.is_neg() && n <= i64::MAX as u64 {
                Ok(n as i64)
            } else if self.is_neg() && n <= (i64::MIN as i128).abs() as u64 {
                Ok((-(n as i128)) as i64)  // i64::MIN.abs() > i64::MAX.abs()
            } else {
                Err(ConversionError::NotInRange { permitted: "9.22e-18~9.22e18".to_string(), error: self.to_scientific_notation(5) })
            },
            Err(e) => Err(e)
        }

    }

    pub fn from_i128(n: i128) -> Self {
        let _is_neg = n < 0;

        BigInt {
            val: UBigInt::from_u128(n.abs() as u128),
            _is_neg
        }
    }

    pub fn to_i128(&self) -> Result<i128, ConversionError> {

        match self.val.to_u128() {
            Ok(n) => if !self.is_neg() && n <= i128::MAX as u128 {
                Ok(n as i128)
            } else if self.is_neg() && n < (1 << 127) {
                Ok(-(n as i128))
            } else {
                Err(ConversionError::NotInRange { permitted: "-1.7e38~1.7e38".to_string(), error: self.to_scientific_notation(5) })
            },
            Err(e) => Err(e)
        }

    }

    pub fn from_ubi(n: UBigInt, is_neg: bool) -> Self {
        let is_neg = is_neg & !n.is_zero();
        BigInt::from_raw(n.0, is_neg)
    }

    pub fn to_ubi(&self) -> Result<UBigInt, ConversionError> {

        if self.is_neg() {
            Err(ConversionError::NotInRange { permitted: "0~inf".to_string(), error: self.to_scientific_notation(5) })
        }

        else {
            Ok(self.val.clone())
        }

    }

    /// `('-')? UBigInt`\
    /// see `UBigInt::from_string`
    pub fn from_string(s: &str) -> Result<Self, ConversionError> {

        if s.len() == 0 {
            Err(ConversionError::NoData)
        }

        else if s.starts_with('-') {

            if let Some(s) = s.get(1..) {

                match UBigInt::from_string(s) {
                    Ok(n) => {
                        // it has to be neg, except '-0'
                        let _is_neg = !n.is_zero();

                        Ok(BigInt {
                            val: n,
                            _is_neg
                        })
                    },
                    Err(e) => Err(e)
                }

            }

            else {
                Err(ConversionError::NoData)
            }

        }

        else {

            match UBigInt::from_string(s) {
                Ok(n) => Ok(BigInt {
                    val: n,
                    _is_neg: false
                }),
                Err(e) => Err(e)
            }

        }

    }

    /// '9.8e5'
    pub fn to_scientific_notation(&self, digits_max_len: usize) -> String {
        format!(
            "{}{}",
            if self.is_neg() { "-" } else { "" },
            self.val.to_scientific_notation(digits_max_len)
        )
    }

    /// see `UBigInt::to_string_dec`
    pub fn to_string_dec(&self) -> String {
        format!(
            "{}{}",
            if self.is_neg() { "-" } else { "" },
            self.val.to_string_dec()
        )
    }

    /// see `UBigInt::to_string_hex`
    pub fn to_string_hex(&self, prefix: bool) -> String {
        format!(
            "{}{}",
            if self.is_neg() { "-" } else { "" },
            self.val.to_string_hex(prefix)
        )
    }

    /// see `UBigInt::to_string_oct`
    pub fn to_string_oct(&self, prefix: bool) -> String {
        format!(
            "{}{}",
            if self.is_neg() { "-" } else { "" },
            self.val.to_string_oct(prefix)
        )
    }

    /// see `UBigInt::to_string_bin`
    pub fn to_string_bin(&self, prefix: bool) -> String {
        format!(
            "{}{}",
            if self.is_neg() { "-" } else { "" },
            self.val.to_string_bin(prefix)
        )
    }

}

impl fmt::Display for BigInt {

    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", self.to_string_dec())
    }

}

impl fmt::LowerHex for BigInt {

    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", self.to_string_hex(fmt.alternate()))
    }

}

impl fmt::Octal for BigInt {

    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", self.to_string_oct(fmt.alternate()))
    }

}

impl fmt::Binary for BigInt {

    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", self.to_string_bin(fmt.alternate()))
    }

}

impl fmt::LowerExp for BigInt {

    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", self.to_scientific_notation(5))
    }

}