fastnum2 0.3.2

fork of Fast decimal numbers library
Documentation
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use core::{cmp::Ordering, fmt, fmt::Write, ops::Neg};

use crate::decimal::round::round_pair_digits;
#[cfg(not(feature = "numtraits"))]
use crate::decimal::utils::cast::ToPrimitive;
#[cfg(feature = "numtraits")]
use num_traits::ToPrimitive;

use crate::decimal::{RoundingMode, Sign};

include!(concat!(env!("OUT_DIR"), "/exponential_format_threshold.rs"));

pub(crate) fn write_scientific_notation<W: Write>(
    digits: String,
    scale: i16,
    w: &mut W,
) -> fmt::Result {
    let (first_digit, remaining_digits) = digits.as_str().split_at(1);
    w.write_str(first_digit)?;
    if !remaining_digits.is_empty() {
        w.write_str(".")?;
        w.write_str(remaining_digits)?;
    }
    write!(w, "e{}", remaining_digits.len() as i32 - scale as i32)
}

pub(crate) fn write_engineering_notation<W: Write>(
    digits: String,
    scale: i16,
    out: &mut W,
) -> fmt::Result {
    let digit_count = digits.len();

    let top_digit_exponent = digit_count as i32 - scale as i32;

    let shift_amount = match top_digit_exponent.rem_euclid(3) {
        0 => 3,
        i => i as usize,
    };

    let exp = top_digit_exponent - shift_amount as i32;

    // handle adding zero padding
    if let Some(padding_zero_count) = shift_amount.checked_sub(digits.len()) {
        let zeros = &"000"[..padding_zero_count];
        out.write_str(&digits)?;
        out.write_str(zeros)?;
        return write!(out, "e{}", exp);
    }

    let (head, rest) = digits.split_at(shift_amount);
    debug_assert_eq!(exp % 3, 0);

    out.write_str(head)?;

    if !rest.is_empty() {
        out.write_char('.')?;
        out.write_str(rest)?;
    }

    write!(out, "e{}", exp)
}

pub(crate) fn format(
    digits: String,
    scale: i16,
    sign: Sign,
    f: &mut fmt::Formatter,
) -> fmt::Result {
    // number of zeros between the most significant digit and decimal point
    let leading_zero_count = scale
        .to_u64()
        .and_then(|scale| scale.checked_sub(digits.len() as u64))
        .unwrap_or(0);

    // number of zeros between last significant digit and decimal point
    let trailing_zero_count = scale.checked_neg().and_then(|d| d.to_u64());

    // this ignores scientific-formatting if precision is requested
    let trailing_zeros = f
        .precision()
        .map(|_| 0)
        .or(trailing_zero_count)
        .unwrap_or(0);

    let leading_zero_threshold = EXPONENTIAL_FORMAT_LEADING_ZERO_THRESHOLD as u64;
    let trailing_zero_threshold = EXPONENTIAL_FORMAT_TRAILING_ZERO_THRESHOLD as u64;

    // use exponential form if decimal point is outside
    // the upper and lower thresholds of the decimal
    if leading_zero_threshold < leading_zero_count {
        format_exponential(digits, scale, sign, f, "E")
    } else if trailing_zero_threshold < trailing_zeros {
        // non-scientific notation
        format_dotless_exponential(digits, scale, sign, f, "e")
    } else {
        format_full_scale(digits, scale, sign, f)
    }
}

pub(crate) fn format_exponential(
    digits: String,
    scale: i16,
    sign: Sign,
    f: &mut fmt::Formatter,
    e_symbol: &str,
) -> fmt::Result {
    let exp = (scale as i128).neg();
    format_exponential_be_ascii_digits(digits, exp, sign, f, e_symbol)
}

fn format_dotless_exponential(
    mut digits: String,
    scale: i16,
    sign: Sign,
    f: &mut fmt::Formatter,
    e_symbol: &str,
) -> fmt::Result {
    debug_assert!(scale <= 0);
    write!(digits, "{}{:+}", e_symbol, -scale)?;
    let non_negative = matches!(sign, Sign::Plus);
    f.pad_integral(non_negative, "", &digits)
}

fn format_full_scale(
    digits: String,
    scale: i16,
    sign: Sign,
    f: &mut fmt::Formatter,
) -> fmt::Result {
    let mut digits = digits.into_bytes();
    let mut exp = (scale as i128).neg();

    if scale <= 0 {
        // formatting an integer value (add trailing zeros to the right)
        zero_right_pad_integer_ascii_digits(&mut digits, &mut exp, f.precision());
    } else {
        let scale = scale as u64;
        // no-precision behaves the same as precision matching scale (i.e. no padding or
        // rounding)
        let prec = f
            .precision()
            .and_then(|prec| prec.to_u64())
            .unwrap_or(scale);

        if scale < digits.len() as u64 {
            // format both integer and fractional digits (always 'trim' to precision)
            trim_ascii_digits(&mut digits, scale, sign, prec, &mut exp);
        } else {
            // format only fractional digits
            shift_or_trim_fractional_digits(&mut digits, scale, sign, prec, &mut exp);
        }
        // never print exp when in this branch
        exp = 0;
    }

    // move digits back into String form
    let mut buf = String::from_utf8(digits).unwrap();

    // add exp part to buffer (if not zero)
    if exp != 0 {
        write!(buf, "e{:+}", exp)?;
    }

    // write buffer to formatter
    let non_negative = matches!(sign, Sign::Plus);
    f.pad_integral(non_negative, "", &buf)
}

/// Fill appropriate number of zeros and decimal point into Vec of (ascii/utf-8)
/// digits
///
/// Exponent is set to zero if zeros were added
fn zero_right_pad_integer_ascii_digits(
    digits: &mut Vec<u8>,
    exp: &mut i128,
    precision: Option<usize>,
) {
    debug_assert!(*exp >= 0);

    let trailing_zero_count = match exp.to_usize() {
        Some(n) => n,
        None => {
            return;
        }
    };
    let total_additional_zeros = trailing_zero_count.saturating_add(precision.unwrap_or(0));
    if total_additional_zeros > FMT_MAX_INTEGER_PADDING {
        return;
    }

    // requested 'prec' digits of precision after decimal point
    match precision {
        None if trailing_zero_count > 20 => {}
        None | Some(0) => {
            digits.resize(digits.len() + trailing_zero_count, b'0');
            *exp = 0;
        }
        Some(prec) => {
            digits.resize(digits.len() + trailing_zero_count, b'0');
            digits.push(b'.');
            digits.resize(digits.len() + prec, b'0');
            *exp = 0;
        }
    }
}

fn trim_ascii_digits(digits: &mut Vec<u8>, scale: u64, sign: Sign, prec: u64, exp: &mut i128) {
    debug_assert!(scale < digits.len() as u64);
    // there are both integer and fractional digits
    let integer_digit_count = (digits.len() as u64 - scale)
        .to_usize()
        .expect("Number of digits exceeds maximum usize");

    if prec < scale {
        let prec = prec.to_usize().expect("Precision exceeds maximum usize");
        apply_rounding_to_ascii_digits(digits, exp, sign, integer_digit_count + prec);
    }

    if prec != 0 {
        digits.insert(integer_digit_count, b'.');
    }

    if scale < prec {
        let trailing_zero_count = (prec - scale).to_usize().expect("Too Big");

        // precision required beyond scale
        digits.resize(digits.len() + trailing_zero_count, b'0');
    }
}

fn shift_or_trim_fractional_digits(
    digits: &mut Vec<u8>,
    scale: u64,
    sign: Sign,
    prec: u64,
    exp: &mut i128,
) {
    debug_assert!(scale >= digits.len() as u64);
    // there are no integer digits
    let leading_zeros = scale - digits.len() as u64;

    match prec.checked_sub(leading_zeros) {
        None => {
            digits.clear();
            digits.push(b'0');
            if prec > 0 {
                digits.push(b'.');
                digits.resize(2 + prec as usize, b'0');
            }
        }
        Some(0) => {
            // precision is at the decimal digit boundary, round one value
            let insig_digit = digits[0] - b'0';
            let trailing_zeros = digits[1..].iter().all(|&d| d == b'0');

            let rounded_value = round_pair_digits(
                (0, insig_digit),
                sign,
                RoundingMode::default(),
                trailing_zeros,
            );

            digits.clear();
            if leading_zeros != 0 {
                digits.push(b'0');
                digits.push(b'.');
                digits.resize(1 + leading_zeros as usize, b'0');
            }
            digits.push(rounded_value + b'0');
        }
        Some(digit_prec) => {
            let digit_prec = digit_prec as usize;
            let leading_zeros = leading_zeros
                .to_usize()
                .expect("Number of leading zeros exceeds max usize");
            let trailing_zeros = digit_prec.saturating_sub(digits.len());
            if digit_prec < digits.len() {
                apply_rounding_to_ascii_digits(digits, exp, sign, digit_prec);
            }
            digits.extend_from_slice(b"0.");
            digits.resize(digits.len() + leading_zeros, b'0');
            digits.rotate_right(leading_zeros + 2);

            // add any extra trailing zeros
            digits.resize(digits.len() + trailing_zeros, b'0');
        }
    }
}

fn format_exponential_be_ascii_digits(
    digits: String,
    mut exp: i128,
    sign: Sign,
    f: &mut fmt::Formatter,
    e_symbol: &str,
) -> fmt::Result {
    let mut digits = digits.into_bytes();

    // how many zeros to pad at the end of the decimal
    let mut extra_trailing_zero_count = 0;

    if let Some(prec) = f.precision() {
        // 'prec' is number of digits after the decimal point
        let total_prec = prec + 1;
        let digit_count = digits.len();

        match total_prec.cmp(&digit_count) {
            Ordering::Equal => {
                // digit count is one more than precision - do nothing
            }
            Ordering::Less => {
                // round to smaller precision
                apply_rounding_to_ascii_digits(&mut digits, &mut exp, sign, total_prec);
            }
            Ordering::Greater => {
                // increase number of zeros to add to end of digits
                extra_trailing_zero_count = total_prec - digit_count;
            }
        }
    }

    let needs_decimal_point = digits.len() > 1 || extra_trailing_zero_count > 0;

    let mut abs_int = String::from_utf8(digits).unwrap();

    // Determine the exponent value based on the scale
    //
    // # First case: the integer representation falls completely behind the
    //   decimal point.
    //
    //   Example of this.scale > abs_int.len():
    //   0.000001234509876
    //   abs_int.len() = 10
    //   scale = 15
    //   target is 1.234509876
    //   exponent = -6
    //
    //   Example of this.scale == abs_int.len():
    //   0.333333333333333314829616256247390992939472198486328125
    //   abs_int.len() = 54
    //   scale = 54
    //   target is 3.33333333333333314829616256247390992939472198486328125
    //   exponent = -1
    //
    // # Second case: the integer representation falls around, or before the
    //   decimal point
    //
    //   ## Case 2.1, entirely before the decimal point.
    //     Example of (abs_int.len() - this.scale) > abs_int.len():
    //     123450987600000
    //     abs_int.len() = 10
    //     scale = -5
    //     location = 15
    //     target is 1.234509876
    //     exponent = 14
    //
    //   ## Case 2.2, somewhere around the decimal point.
    //     Example of (abs_int.len() - this.scale) < abs_int.len():
    //     12.339999999999999857891452847979962825775146484375
    //     abs_int.len() = 50
    //     scale = 48
    //     target is 1.2339999999999999857891452847979962825775146484375
    //     exponent = 1
    //
    //     For the (abs_int.len() - this.scale) == abs_int.len() I couldn't
    //     come up with an example
    let exponent = abs_int.len() as i128 + exp - 1;

    if needs_decimal_point {
        // only add decimal point if there is more than 1 decimal digit
        abs_int.insert(1, '.');
    }

    if extra_trailing_zero_count > 0 {
        abs_int.extend(core::iter::repeat('0').take(extra_trailing_zero_count));
    }

    // always print exponent in exponential mode
    write!(abs_int, "{}{:+}", e_symbol, exponent)?;

    let non_negative = matches!(sign, Sign::Plus);
    f.pad_integral(non_negative, "", &abs_int)
}

/// Round big-endian digits in ascii
fn apply_rounding_to_ascii_digits(
    ascii_digits: &mut Vec<u8>,
    exp: &mut i128,
    sign: Sign,
    prec: usize,
) {
    if ascii_digits.len() < prec {
        return;
    }

    // shift exp to align with new length of digits
    *exp += (ascii_digits.len() - prec) as i128;

    // true if all ascii_digits after precision are zeros
    let trailing_zeros = ascii_digits[prec + 1..].iter().all(|&d| d == b'0');

    let sig_digit = ascii_digits[prec - 1] - b'0';
    let insig_digit = ascii_digits[prec] - b'0';

    let rounded_digit = round_pair_digits(
        (sig_digit, insig_digit),
        sign,
        RoundingMode::default(),
        trailing_zeros,
    );

    // remove insignificant digits
    ascii_digits.truncate(prec - 1);

    // push rounded value
    if rounded_digit < 10 {
        ascii_digits.push(rounded_digit + b'0');
        return;
    }

    debug_assert_eq!(rounded_digit, 10);

    // push zero and carry-the-one
    ascii_digits.push(b'0');

    // loop through digits in reverse order (skip the 0 we just pushed)
    let digits = ascii_digits.iter_mut().rev().skip(1);
    for digit in digits {
        if *digit < b'9' {
            // we've carried the one as far as it will go
            *digit += 1;
            return;
        }

        debug_assert_eq!(*digit, b'9');

        // digit was a 9, set to zero and carry the one
        // to the next digit
        *digit = b'0';
    }

    // at this point, all digits have become zero
    // just set a significant digit to 1 and increase exponent
    //
    // eg: 9999e2 ~> 0000e2 ~> 1000e3
    //
    ascii_digits[0] = b'1';
    *exp += 1;
}