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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
pub const DECIMAL_PARTS: usize = 10;
#[derive(Copy, Clone, Debug)]
pub struct BigFloatNum {
pub (crate) sign: i8,
pub (crate) e: i8,
pub (crate) n: i16,
pub (crate) m: [i16; DECIMAL_PARTS],
}
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum Error {
ExponentOverflow(i8),
DivisionByZero,
ArgumentIsNegative,
InvalidArgument,
}
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum RoundingMode {
Up,
Down,
ToZero,
FromZero,
ToEven,
ToOdd,
}
pub const DECIMAL_BASE_LOG10: usize = 4;
pub const DECIMAL_POSITIONS: usize = DECIMAL_PARTS * DECIMAL_BASE_LOG10;
pub const DECIMAL_BASE: usize = 10000;
pub const DECIMAL_SIGN_POS: i8 = 1;
pub const DECIMAL_SIGN_NEG: i8 = -1;
pub const DECIMAL_MIN_EXPONENT: i8 = -128;
pub const DECIMAL_MAX_EXPONENT: i8 = 127;
pub const ZEROED_MANTISSA: [i16; DECIMAL_PARTS] = [0; DECIMAL_PARTS];
pub const ZERO: BigFloatNum = BigFloatNum {
m: ZEROED_MANTISSA,
n: 0,
sign: DECIMAL_SIGN_POS,
e: 0,
};
pub const ONE: BigFloatNum = BigFloatNum {
m: [0, 0, 0, 0, 0, 0, 0, 0, 0, 1000],
n: DECIMAL_POSITIONS as i16,
sign: DECIMAL_SIGN_POS,
e: 1 - (DECIMAL_POSITIONS as i8),
};
pub const TWO: BigFloatNum = BigFloatNum {
m: [0, 0, 0, 0, 0, 0, 0, 0, 0, 2000],
n: DECIMAL_POSITIONS as i16,
sign: DECIMAL_SIGN_POS,
e: 1 - (DECIMAL_POSITIONS as i8),
};
pub const E: BigFloatNum = BigFloatNum {
m: [7757, 6249, 3526, 7471, 6028, 2353, 9045, 2845, 2818, 2718],
n: DECIMAL_POSITIONS as i16,
sign: DECIMAL_SIGN_POS,
e: 1 - (DECIMAL_POSITIONS as i8),
};
pub const PI: BigFloatNum = BigFloatNum {
m: [4197, 288, 2795, 3383, 6264, 2384, 9793, 5358, 5926, 3141],
n: DECIMAL_POSITIONS as i16,
sign: DECIMAL_SIGN_POS,
e: 1 - (DECIMAL_POSITIONS as i8),
};
pub const MAX: BigFloatNum = BigFloatNum {
m: [9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999,],
n: DECIMAL_POSITIONS as i16,
sign: DECIMAL_SIGN_POS,
e: DECIMAL_MAX_EXPONENT,
};
pub const MIN: BigFloatNum = BigFloatNum {
m: [9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999,],
n: DECIMAL_POSITIONS as i16,
sign: DECIMAL_SIGN_NEG,
e: DECIMAL_MAX_EXPONENT,
};
pub const MIN_POSITIVE: BigFloatNum = BigFloatNum {
m: [1, 0, 0, 0, 0, 0, 0, 0, 0, 0,],
n: 1,
sign: DECIMAL_SIGN_POS,
e: DECIMAL_MIN_EXPONENT,
};
impl BigFloatNum {
pub fn new() -> Self {
BigFloatNum {
sign: DECIMAL_SIGN_POS,
e: 0,
n: 0,
m: ZEROED_MANTISSA,
}
}
pub fn one() -> Self {
let mut val = Self::new();
val.m[DECIMAL_PARTS-1] = DECIMAL_BASE as i16/10;
val.n = DECIMAL_POSITIONS as i16;
val.e = 1 - DECIMAL_POSITIONS as i8;
val
}
pub fn from_bytes(bytes: &[u8], sign: i8, exponent: i8) -> BigFloatNum {
let mut mantissa = ZEROED_MANTISSA;
let mut n: usize = 0;
let mut p: i16 = 1;
let d = if bytes.len() > DECIMAL_POSITIONS { DECIMAL_POSITIONS } else { bytes.len() };
for i in 1..d+1 {
mantissa[n] += (bytes[d - i] % 10) as i16 * p;
p *= 10;
if p == DECIMAL_BASE as i16 {
n += 1;
p = 1;
}
}
BigFloatNum {
sign: if sign >= 0 { DECIMAL_SIGN_POS } else { DECIMAL_SIGN_NEG },
e: exponent,
n: Self::num_digits(&mantissa),
m: mantissa,
}
}
#[cfg(not(feature = "std"))]
pub fn from_f64(mut f: f64) -> Result<Self, Error> {
let mut e: i32 = 0;
let mut ret = BigFloatNum::new();
if f == 0f64 {
return Ok(ret);
}
if f.is_infinite() {
return Err(Error::ExponentOverflow(if f.is_sign_positive() {
DECIMAL_SIGN_POS
} else {
DECIMAL_SIGN_NEG
}));
}
if f.is_nan() {
return Err(Error::InvalidArgument);
}
if f < 0f64 {
ret.sign = DECIMAL_SIGN_NEG;
f = -f;
}
while f >= 1.0f64 {
f /= 10f64;
e += 1;
}
while f < 0.1f64 {
f *= 10f64;
e -= 1;
}
ret.n = DECIMAL_POSITIONS as i16;
let mut p = DECIMAL_PARTS - 1;
loop {
f *= DECIMAL_BASE as f64;
let d = f as i16;
f -= d as f64;
ret.m[p] = d;
p -= 1;
if f == 0f64 || p == 0 {
break;
}
}
e -= DECIMAL_POSITIONS as i32;
if e < DECIMAL_MIN_EXPONENT as i32 {
return Ok(ret.process_subnormal(e));
}
if e > DECIMAL_MAX_EXPONENT as i32 {
return Err(Error::ExponentOverflow(ret.sign));
}
ret.e = e as i8;
Ok(ret)
}
pub fn to_f64(&self) -> f64 {
let mut f: f64 = 0f64;
for i in 0..DECIMAL_PARTS {
f += self.m[i] as f64;
f /= DECIMAL_BASE as f64;
}
let mut e = DECIMAL_POSITIONS as i32 + self.e as i32;
while e < 0 {
f /= 10f64;
e += 1;
}
while e > 0 {
f *= 10f64;
e -= 1;
}
if self.sign == DECIMAL_SIGN_NEG {
f = -f;
}
f
}
pub fn to_f32(&self) -> f32 {
self.to_f64() as f32
}
pub fn get_mantissa_bytes(&self, bytes: &mut [u8]) {
let mut n: usize = 0;
let mut p: i16 = 1;
let d = if bytes.len() < self.n as usize { bytes.len() } else { self.n as usize };
for i in 1..d+1 {
bytes[d - i] = ((self.m[n] / p) % 10) as u8;
p *= 10;
if p == DECIMAL_BASE as i16 {
n += 1;
p = 1;
}
}
}
pub fn get_mantissa_len(&self) -> usize {
self.n as usize
}
pub fn is_zero(&self) -> bool {
self.n == 0
}
pub fn is_int_even(&self) -> bool {
let int = self.int();
if int.e < 0 {
let p = int.n + int.e as i16;
let mut d = int.m[p as usize / DECIMAL_BASE_LOG10];
let mut i = p % DECIMAL_BASE_LOG10 as i16;
while i > 0 {
d /= 10;
i -= 1;
}
d & 1 == 0
} else if int.e == 0 {
int.m[0] & 1 == 0
} else {
true
}
}
pub fn is_subnormal(&self) -> bool {
self.n < DECIMAL_POSITIONS as i16 &&
self.e == DECIMAL_MIN_EXPONENT
}
}