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
324
325
326
327
//! Utility functions.
use crate::defs::BigFloatNum;
use crate::defs::Error;
use crate::defs::RoundingMode;
use crate::defs::DECIMAL_BASE;
use crate::defs::DECIMAL_BASE_LOG10;
use crate::defs::DECIMAL_MAX_EXPONENT;
use crate::defs::DECIMAL_PARTS;
use crate::defs::DECIMAL_POSITIONS;
use crate::defs::DECIMAL_SIGN_POS;
use crate::inc::inc::BigFloatInc;
impl BigFloatNum {
// compare absolute values of two big floats
// return positive if d1 > d2, negative if d1 < d2, 0 otherwise
pub(super) fn abs_cmp(d1: &[i16], d2: &[i16]) -> i16 {
let mut i: i32 = DECIMAL_PARTS as i32 - 1;
while i >= 0 {
if d1[i as usize] != d2[i as usize] {
return d1[i as usize] - d2[i as usize];
}
i -= 1;
}
0
}
// shift m to the right by n digits
pub(crate) fn shift_right(m: &mut [i16], mut n: usize) {
assert!(n > 0 && n <= DECIMAL_POSITIONS);
let mut s: i16;
let mut t: i16;
let mut x: i32 = (n % DECIMAL_BASE_LOG10) as i32;
n /= DECIMAL_BASE_LOG10;
if x == 0 {
if n > 0 {
for i in 0..DECIMAL_PARTS - n {
m[i] = m[i + n];
}
}
} else {
s = 10;
t = DECIMAL_BASE as i16 / 10;
x -= 1;
while x > 0 {
s *= 10;
t /= 10;
x -= 1;
}
let mut i = 0;
while i < DECIMAL_PARTS - n - 1 {
m[i] = (m[i + n + 1] % s) * t + m[i + n] / s;
i += 1;
}
m[i] = m[i + n] / s;
}
for i in 0..n {
m[i + DECIMAL_PARTS - n] = 0;
}
}
// return number of digits taken in mantissa
pub(crate) fn num_digits(m: &[i16]) -> i16 {
let mut n: i16 = DECIMAL_POSITIONS as i16;
let mut t: i16;
let mut p: i16 = DECIMAL_PARTS as i16 - 1;
while p >= 0 && 0 == m[p as usize] {
p -= 1;
n -= DECIMAL_BASE_LOG10 as i16;
}
if n > 0 {
t = m[p as usize];
n -= DECIMAL_BASE_LOG10 as i16;
loop {
n += 1;
t /= 10;
if t == 0 {
break;
}
}
}
n
}
// Convert to BigFloatInc.
pub(super) fn to_big_float_inc(d1: &Self) -> BigFloatInc {
let mut ret = BigFloatInc::new();
(&mut ret.m[0..DECIMAL_PARTS]).copy_from_slice(&d1.m);
ret.n = d1.n;
ret.e = d1.e;
ret.sign = d1.sign;
ret
}
// Convert from BigFloatInc.
pub(super) fn from_big_float_inc(mut d1: BigFloatInc) -> Result<Self, Error> {
let mut ret = Self::new();
if d1.n == 0 {
return Ok(ret);
}
d1.maximize_mantissa();
let mut additional_shift = 0;
if d1.n < DECIMAL_POSITIONS as i16 + DECIMAL_BASE_LOG10 as i16 {
// d1 is subnormal, but it's mantissa still can be shifted
// because resulting number exponent will be incremented by DECIMAL_BASE_LOG10
additional_shift = DECIMAL_POSITIONS + DECIMAL_BASE_LOG10 - d1.n as usize;
if additional_shift > DECIMAL_BASE_LOG10 {
additional_shift = DECIMAL_BASE_LOG10;
}
BigFloatInc::shift_left(&mut d1.m, additional_shift);
}
if BigFloatInc::round_mantissa(
&mut d1.m,
DECIMAL_BASE_LOG10 as i16,
RoundingMode::ToEven,
true,
) {
if d1.e == DECIMAL_MAX_EXPONENT {
return Err(Error::ExponentOverflow(d1.sign));
} else {
d1.e += 1;
}
}
(&mut ret.m).copy_from_slice(&d1.m[1..]);
ret.n = Self::num_digits(&ret.m);
if d1.e > DECIMAL_MAX_EXPONENT - DECIMAL_BASE_LOG10 as i8 {
return Err(Error::ExponentOverflow(d1.sign));
}
ret.e = d1.e + DECIMAL_BASE_LOG10 as i8 - additional_shift as i8;
ret.sign = d1.sign;
Ok(ret)
}
// fractional part of d1
pub(super) fn extract_fract_part(d1: &Self) -> Self {
let mut fractional = Self::new();
let e = -(d1.e as i16);
if e >= d1.n {
fractional = *d1;
fractional.sign = DECIMAL_SIGN_POS;
} else if e > 0 {
let mut i = 0;
while i + (DECIMAL_BASE_LOG10 as i16) <= e {
fractional.m[i as usize / DECIMAL_BASE_LOG10] =
d1.m[i as usize / DECIMAL_BASE_LOG10];
i += DECIMAL_BASE_LOG10 as i16;
}
if i < e {
let mut t = 1;
while i < e {
t *= 10;
i += 1;
}
fractional.m[i as usize / DECIMAL_BASE_LOG10] +=
d1.m[i as usize / DECIMAL_BASE_LOG10 as usize] % t;
}
fractional.n = Self::num_digits(&fractional.m);
if fractional.n > 0 {
fractional.e = d1.e;
}
}
fractional
}
// integer part of d1
pub(super) fn extract_int_part(d1: &Self) -> Self {
if d1.e >= 0 {
return *d1;
}
if d1.e as i16 + d1.n < 0 {
return Self::new();
}
let mut int = Self::new();
let mut i = -(d1.e as i16);
let mut t = Self::get_div_factor(i);
if i < 0 {
i = 0;
}
let mut t2 = 1;
while i < d1.n {
int.m[int.n as usize / DECIMAL_BASE_LOG10] +=
(d1.m[i as usize / DECIMAL_BASE_LOG10 as usize] / t % 10) * t2;
int.n += 1;
i += 1;
t *= 10;
if t == DECIMAL_BASE as i16 {
t = 1;
}
t2 *= 10;
if t2 == DECIMAL_BASE as i16 {
t2 = 1;
}
}
if int.n > 0 {
int.e = d1.e + (i - int.n) as i8;
}
int
}
// factor to divide by to get a digit at position n
pub(crate) fn get_div_factor(n: i16) -> i16 {
let mut x = n % DECIMAL_BASE_LOG10 as i16;
let mut t = 1;
while x > 0 {
t *= 10;
x -= 1;
}
t
}
// Round n positons to even, return true if exponent is to be incremented.
pub(crate) fn round_mantissa(
m: &mut [i16],
n: i16,
rm: RoundingMode,
is_positive: bool,
) -> bool {
if n > 0 && n <= DECIMAL_POSITIONS as i16 {
let n = n - 1;
let mut rem_zero = true;
// anything before n'th digit becomes 0
for i in 0..n as usize / DECIMAL_BASE_LOG10 {
if m[i] != 0 {
rem_zero = false;
}
m[i] = 0;
}
// analyze digits at n and at n+1
// to decide if we need to add 1 or not.
let mut c = false;
let np1 = n + 1;
let mut i = n as usize / DECIMAL_BASE_LOG10;
let i1 = np1 as usize / DECIMAL_BASE_LOG10;
let t = Self::get_div_factor(n);
let t2 = Self::get_div_factor(np1);
let num = m[i] / t % 10;
if m[i] % t != 0 {
rem_zero = false;
}
let num2 = if i1 < m.len() { m[i1] / t2 % 10 } else { 0 };
let eq5 = num == 5 && rem_zero;
let gt5 = num > 5 || (num == 5 && !rem_zero);
let gte5 = gt5 || eq5;
match rm {
RoundingMode::Up => {
if gte5 && is_positive || gt5 && !is_positive {
// add 1
c = true;
}
}
RoundingMode::Down => {
if gt5 && is_positive || gte5 && !is_positive {
// add 1
c = true;
}
}
RoundingMode::FromZero => {
if gte5 {
// add 1
c = true;
}
}
RoundingMode::ToZero => {
if gt5 {
// add 1
c = true;
}
}
RoundingMode::ToEven => {
if gt5 || (eq5 && num2 & 1 != 0) {
// add 1
c = true;
}
}
RoundingMode::ToOdd => {
if gt5 || (eq5 && num2 & 1 == 0) {
// add 1
c = true;
}
}
};
if c {
// add 1 at (n+1)'th position
if i1 > i {
m[i] = 0;
}
i = i1;
if i < m.len() {
if m[i] / t2 + 1 < DECIMAL_BASE as i16 / t2 {
m[i] = (m[i] / t2 + 1) * t2;
return false;
} else {
m[i] = 0;
}
}
// process overflows
i += 1;
while i < m.len() {
if m[i] < DECIMAL_BASE as i16 - 1 {
m[i] += 1;
return false;
} else {
m[i] = 0;
}
i += 1;
}
m[m.len() - 1] = DECIMAL_BASE as i16 / 10;
return true;
} else {
// just remove trailing digits
let t = t * 10;
m[i] = (m[i] / t) * t;
}
}
false
}
}