nanoexpr 0.1.0

Fast no_std math expression parser and evaluator with zero heap allocation: formulas with variables and functions for microcontrollers, WebAssembly and C, in f32, f64, integers or calculator-exact decimals
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//! A decimal floating-point number, the representation pocket calculators use.

use core::cmp::Ordering;
use core::fmt::{self, Write};
use core::str::FromStr;

use crate::{ErrorKind, Number};

/// A decimal floating-point number, as pocket calculators compute with.
///
/// The value is a coefficient of `DIGITS` significant decimal digits times a power of ten, with a scientific exponent
/// between `-MAX_EXPONENT` and `MAX_EXPONENT`. The defaults, 14 digits and an exponent of ±99, are those of TI-83 and
/// TI-84 calculators.
///
/// Decimal fractions are exact, so `0.1 + 0.2` is `0.3`. Each operation rounds its exact result once to `DIGITS`
/// digits, half to even. Results beyond the exponent range are [`ErrorKind::Overflow`] errors, and results too
/// small for it become zero.
///
/// Showing fewer digits than are kept, as calculators do, hides the rounding of intermediate results: format with a
/// precision to round to that many significant digits.
///
/// ```
/// use nanoexpr::Decimal;
///
/// let third: Decimal = nanoexpr::evaluate("1 / 3").unwrap();
/// assert_eq!(third.to_string(), "0.33333333333333");
///
/// let one: Decimal = nanoexpr::evaluate("1 / 3 * 3").unwrap();
/// assert_eq!(one.to_string(), "0.99999999999999");
/// assert_eq!(format!("{one:.10}"), "1");
///
/// let sum: Decimal = nanoexpr::evaluate("0.1 + 0.2").unwrap();
/// assert_eq!(sum.to_string(), "0.3");
/// ```
///
/// `DIGITS` must be between 1 and 18, and `MAX_EXPONENT` between 1 and 9999.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Decimal<const DIGITS: u32 = 14, const MAX_EXPONENT: i32 = 99> {
    /// Whether the number is below zero, never set for zero.
    negative: bool,
    /// Exactly `DIGITS` digits, or zero.
    coefficient: u64,
    /// Power of ten the coefficient is multiplied by, zero for zero.
    exponent: i32,
}

/// Returns `10^exponent`, for exponents up to 38.
const fn power_of_ten(exponent: u32) -> u128 {
    10_u128.pow(exponent)
}

/// Returns how many decimal digits `value` has, zero for zero.
const fn digit_count(value: u128) -> u32 {
    match value.checked_ilog10() {
        Some(logarithm) => logarithm + 1,
        None => 0,
    }
}

/// How [`Decimal::integer`] rounds to an integer.
#[derive(Clone, Copy)]
enum Rounding {
    /// Toward zero.
    Toward,
    /// Toward negative infinity.
    Down,
    /// Toward positive infinity.
    Up,
    /// To the nearest, half away from zero.
    Nearest,
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> Decimal<DIGITS, MAX_EXPONENT> {
    /// Fails to compile when the parameters are out of range.
    const VALID: () = assert!(
        DIGITS >= 1 && DIGITS <= 18 && MAX_EXPONENT >= 1 && MAX_EXPONENT <= 9999,
        "Decimal needs 1 to 18 digits and a maximum exponent of 1 to 9999"
    );

    /// Zero.
    pub const ZERO: Self = Self {
        negative: false,
        coefficient: 0,
        exponent: 0,
    };

    /// Creates `mantissa × 10^exponent`, rounded to `DIGITS` digits.
    ///
    /// ```
    /// use nanoexpr::Decimal;
    ///
    /// let price = Decimal::<14, 99>::new(1999, -2).unwrap();
    /// assert_eq!(price.to_string(), "19.99");
    /// ```
    pub fn new(mantissa: i64, exponent: i32) -> Result<Self, ErrorKind> {
        Self::from_parts(
            mantissa < 0,
            u128::from(mantissa.unsigned_abs()),
            i64::from(exponent),
            false,
        )
    }

    /// Rounds `coefficient × 10^exponent` to `DIGITS` digits, half to even.
    ///
    /// `sticky` tells that the exact value is slightly above `coefficient × 10^exponent`, for operations that had to
    /// drop nonzero digits below the ones they kept. Callers keep at least two digits more than `DIGITS` when they
    /// set it, so that it only breaks ties.
    fn from_parts(
        negative: bool,
        mut coefficient: u128,
        mut exponent: i64,
        sticky: bool,
    ) -> Result<Self, ErrorKind> {
        let () = Self::VALID;
        if coefficient == 0 {
            return Ok(Self::ZERO);
        }
        let digits = digit_count(coefficient);
        if digits > DIGITS {
            let dropped = digits - DIGITS;
            let divisor = power_of_ten(dropped);
            let remainder = coefficient % divisor;
            let half = divisor / 2;
            coefficient /= divisor;
            exponent += i64::from(dropped);
            if remainder > half || (remainder == half && (sticky || coefficient % 2 == 1)) {
                coefficient += 1;
                if coefficient == power_of_ten(DIGITS) {
                    coefficient /= 10;
                    exponent += 1;
                }
            }
        } else {
            let missing = DIGITS - digits;
            coefficient *= power_of_ten(missing);
            exponent -= i64::from(missing);
        }
        let scientific = exponent + i64::from(DIGITS) - 1;
        if scientific > i64::from(MAX_EXPONENT) {
            return Err(ErrorKind::Overflow);
        }
        if scientific < -i64::from(MAX_EXPONENT) {
            return Ok(Self::ZERO);
        }
        Ok(Self {
            negative,
            coefficient: coefficient as u64,
            exponent: exponent as i32,
        })
    }

    /// One.
    fn one() -> Self {
        Self {
            negative: false,
            coefficient: power_of_ten(DIGITS - 1) as u64,
            exponent: 1 - DIGITS as i32,
        }
    }

    /// Returns whether the number is zero.
    pub const fn is_zero(self) -> bool {
        self.coefficient == 0
    }

    /// Returns whether the number is below zero.
    pub const fn is_negative(self) -> bool {
        self.negative
    }

    /// Returns the absolute value.
    pub const fn abs(self) -> Self {
        Self {
            negative: false,
            ..self
        }
    }

    /// Returns the exponent of the number in scientific notation, such as 2 for `123`, or zero for zero.
    pub const fn scientific_exponent(self) -> i32 {
        if self.is_zero() {
            0
        } else {
            self.exponent + DIGITS as i32 - 1
        }
    }

    /// Returns the closest `f64`.
    pub fn to_f64(self) -> f64 {
        let mut buffer = Buffer::<48>::new();
        let sign = if self.negative { "-" } else { "" };
        let _ = write!(buffer, "{sign}{}e{}", self.coefficient, self.exponent);
        buffer.as_str().parse().unwrap_or(f64::NAN)
    }

    /// Returns `value` rounded to `DIGITS` digits.
    ///
    /// `NaN` is a [`ErrorKind::Domain`] error and infinities and values beyond the exponent range are
    /// [`ErrorKind::Overflow`] errors.
    pub fn from_f64(value: f64) -> Result<Self, ErrorKind> {
        if value.is_nan() {
            return Err(ErrorKind::Domain);
        }
        if value.is_infinite() {
            return Err(ErrorKind::Overflow);
        }
        let mut buffer = Buffer::<64>::new();
        let _ = write!(buffer, "{:.36e}", value.abs());
        let magnitude = Self::parse_magnitude(buffer.as_str()).ok_or(ErrorKind::Overflow)??;
        Ok(if value < 0.0 {
            magnitude.flip()
        } else {
            magnitude
        })
    }

    /// Returns the number with the opposite sign, keeping zero positive.
    const fn flip(self) -> Self {
        Self {
            negative: !self.negative && !self.is_zero(),
            ..self
        }
    }

    /// Parses an unsigned literal such as `12`, `.5`, `3.` or `1.5e-3`.
    ///
    /// Returns `None` for malformed text, or the rounded value or its overflow.
    fn parse_magnitude(text: &str) -> Option<Result<Self, ErrorKind>> {
        let bytes = text.as_bytes();
        let mut coefficient: u128 = 0;
        let mut digits = 0;
        let mut exponent: i64 = 0;
        let mut sticky = false;
        let mut fraction = false;
        let mut seen = false;
        let mut index = 0;
        while let Some(&byte) = bytes.get(index) {
            match byte {
                b'0'..=b'9' => {
                    seen = true;
                    let digit = u128::from(byte - b'0');
                    if digits < 38 {
                        coefficient = coefficient * 10 + digit;
                        if coefficient != 0 {
                            digits += 1;
                        }
                        if fraction {
                            exponent -= 1;
                        }
                    } else {
                        if !fraction {
                            exponent += 1;
                        }
                        sticky |= digit != 0;
                    }
                }
                b'.' if !fraction => fraction = true,
                b'e' | b'E' if seen => {
                    exponent += Self::parse_exponent(bytes.get(index + 1..)?)?;
                    break;
                }
                _ => return None,
            }
            index += 1;
        }
        seen.then(|| Self::from_parts(false, coefficient, exponent, sticky))
    }

    /// Parses the digits after an `e`, with an optional sign, saturating far beyond any exponent range.
    fn parse_exponent(bytes: &[u8]) -> Option<i64> {
        let (negative, digits) = match bytes.split_first()? {
            (b'-', rest) => (true, rest),
            (b'+', rest) => (false, rest),
            _ => (false, bytes),
        };
        if digits.is_empty() {
            return None;
        }
        let mut value: i64 = 0;
        for &byte in digits {
            if !byte.is_ascii_digit() {
                return None;
            }
            value = (value * 10 + i64::from(byte - b'0')).min(1_000_000);
        }
        Some(if negative { -value } else { value })
    }

    /// Returns the sum of the two numbers.
    fn sum(self, rhs: Self) -> Result<Self, ErrorKind> {
        if self.is_zero() {
            return Ok(rhs);
        }
        if rhs.is_zero() {
            return Ok(self);
        }
        let (large, small) = if self.exponent >= rhs.exponent {
            (self, rhs)
        } else {
            (rhs, self)
        };
        let difference = (large.exponent - small.exponent) as u32;
        let subtract = large.negative != small.negative;
        if difference > DIGITS + 2 {
            let shifted = u128::from(large.coefficient) * 1000;
            let exponent = i64::from(large.exponent) - 3;
            let coefficient = if subtract { shifted - 1 } else { shifted };
            return Self::from_parts(large.negative, coefficient, exponent, true);
        }
        let aligned = u128::from(large.coefficient) * power_of_ten(difference);
        let other = u128::from(small.coefficient);
        let exponent = i64::from(small.exponent);
        if !subtract {
            return Self::from_parts(large.negative, aligned + other, exponent, false);
        }
        match aligned.cmp(&other) {
            Ordering::Greater => Self::from_parts(large.negative, aligned - other, exponent, false),
            Ordering::Less => Self::from_parts(small.negative, other - aligned, exponent, false),
            Ordering::Equal => Ok(Self::ZERO),
        }
    }

    /// Returns `self` raised to the integer power `exponent`, by squaring.
    fn integer_power(self, exponent: i64) -> Result<Self, ErrorKind> {
        let mut result = Self::one();
        let mut base = self;
        let mut remaining = exponent.unsigned_abs();
        let magnitude = loop {
            if remaining & 1 == 1 {
                result = match result.multiply(base) {
                    Ok(value) => value,
                    Err(ErrorKind::Overflow) if exponent < 0 => return Ok(Self::ZERO),
                    Err(kind) => return Err(kind),
                };
            }
            remaining >>= 1;
            if remaining == 0 {
                break result;
            }
            base = match base.multiply(base) {
                Ok(value) => value,
                Err(ErrorKind::Overflow) if exponent < 0 => return Ok(Self::ZERO),
                Err(kind) => return Err(kind),
            };
        };
        if exponent < 0 {
            Self::one().divide(magnitude)
        } else {
            Ok(magnitude)
        }
    }

    /// Returns whether the number is an integer.
    fn is_integer(self) -> bool {
        self.exponent >= 0
            || (self.exponent.unsigned_abs() <= 19
                && u128::from(self.coefficient) % power_of_ten(self.exponent.unsigned_abs()) == 0)
    }

    /// Returns the number as an `i64`, if it is an integer that fits.
    fn to_integer(self) -> Option<i64> {
        if !self.is_integer() {
            return None;
        }
        let magnitude = if self.exponent >= 0 {
            u128::from(self.coefficient)
                .checked_mul(10_u128.checked_pow(self.exponent.unsigned_abs())?)?
        } else {
            u128::from(self.coefficient) / power_of_ten(self.exponent.unsigned_abs())
        };
        let magnitude = i64::try_from(magnitude).ok()?;
        Some(if self.negative { -magnitude } else { magnitude })
    }

    /// Rounds the number to an integer.
    fn integer(self, rounding: Rounding) -> Result<Self, ErrorKind> {
        if self.exponent >= 0 {
            return Ok(self);
        }
        let shift = self.exponent.unsigned_abs();
        let (whole, rest) = if shift > DIGITS {
            (0, u128::from(self.coefficient))
        } else {
            let divisor = power_of_ten(shift);
            (
                u128::from(self.coefficient) / divisor,
                u128::from(self.coefficient) % divisor,
            )
        };
        let half = if shift > DIGITS {
            0
        } else {
            power_of_ten(shift) / 2
        };
        let away = match rounding {
            Rounding::Toward => false,
            Rounding::Down => self.negative && rest != 0,
            Rounding::Up => !self.negative && rest != 0,
            Rounding::Nearest => shift <= DIGITS && rest >= half,
        };
        Self::from_parts(self.negative, whole + u128::from(away), 0, false)
    }

    /// Rounds toward negative infinity.
    pub fn floor(self) -> Result<Self, ErrorKind> {
        self.integer(Rounding::Down)
    }

    /// Rounds toward positive infinity.
    pub fn ceil(self) -> Result<Self, ErrorKind> {
        self.integer(Rounding::Up)
    }

    /// Rounds to the nearest integer, half away from zero.
    pub fn round(self) -> Result<Self, ErrorKind> {
        self.integer(Rounding::Nearest)
    }

    /// Rounds toward zero.
    pub fn trunc(self) -> Result<Self, ErrorKind> {
        self.integer(Rounding::Toward)
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> Default for Decimal<DIGITS, MAX_EXPONENT> {
    fn default() -> Self {
        Self::ZERO
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> Number for Decimal<DIGITS, MAX_EXPONENT> {
    fn parse(literal: &str) -> Option<Self> {
        Self::parse_magnitude(literal)?.ok()
    }

    fn add(self, rhs: Self) -> Result<Self, ErrorKind> {
        self.sum(rhs)
    }

    fn subtract(self, rhs: Self) -> Result<Self, ErrorKind> {
        self.sum(rhs.flip())
    }

    fn multiply(self, rhs: Self) -> Result<Self, ErrorKind> {
        Self::from_parts(
            self.negative != rhs.negative,
            u128::from(self.coefficient) * u128::from(rhs.coefficient),
            i64::from(self.exponent) + i64::from(rhs.exponent),
            false,
        )
    }

    fn divide(self, rhs: Self) -> Result<Self, ErrorKind> {
        if rhs.is_zero() {
            return Err(ErrorKind::DivisionByZero);
        }
        let numerator = u128::from(self.coefficient) * power_of_ten(DIGITS + 2);
        let denominator = u128::from(rhs.coefficient);
        Self::from_parts(
            self.negative != rhs.negative,
            numerator / denominator,
            i64::from(self.exponent) - i64::from(rhs.exponent) - i64::from(DIGITS + 2),
            numerator % denominator != 0,
        )
    }

    fn remainder(self, rhs: Self) -> Result<Self, ErrorKind> {
        if rhs.is_zero() {
            return Err(ErrorKind::DivisionByZero);
        }
        if self.exponent < rhs.exponent {
            return Ok(self);
        }
        let divisor = u128::from(rhs.coefficient);
        let mut remainder = u128::from(self.coefficient) % divisor;
        for _ in rhs.exponent..self.exponent {
            remainder = remainder * 10 % divisor;
        }
        Self::from_parts(self.negative, remainder, i64::from(rhs.exponent), false)
    }

    fn power(self, rhs: Self) -> Result<Self, ErrorKind> {
        if let Some(exponent) = rhs.to_integer() {
            return self.integer_power(exponent);
        }
        if self.negative && !rhs.is_integer() {
            return Err(ErrorKind::Domain);
        }
        if self.is_zero() {
            return if rhs.negative {
                Err(ErrorKind::DivisionByZero)
            } else {
                Ok(Self::ZERO)
            };
        }
        let power = libm::pow(self.abs().to_f64(), rhs.to_f64());
        if power == 0.0 {
            return Ok(Self::ZERO);
        }
        Self::from_f64(power)
    }

    fn negate(self) -> Result<Self, ErrorKind> {
        Ok(self.flip())
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> PartialOrd for Decimal<DIGITS, MAX_EXPONENT> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> Ord for Decimal<DIGITS, MAX_EXPONENT> {
    fn cmp(&self, other: &Self) -> Ordering {
        let sign = |value: &Self| match (value.is_zero(), value.negative) {
            (true, _) => 0,
            (false, true) => -1,
            (false, false) => 1,
        };
        sign(self).cmp(&sign(other)).then_with(|| {
            let magnitude = self
                .exponent
                .cmp(&other.exponent)
                .then(self.coefficient.cmp(&other.coefficient));
            if self.negative {
                magnitude.reverse()
            } else {
                magnitude
            }
        })
    }
}

/// The error of parsing a [`Decimal`] from text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ParseDecimalError {
    /// [`ErrorKind::InvalidNumber`] for malformed text, or [`ErrorKind::Overflow`] for a value beyond the range.
    pub kind: ErrorKind,
}

impl fmt::Display for ParseDecimalError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.kind.fmt(formatter)
    }
}

impl core::error::Error for ParseDecimalError {}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> FromStr for Decimal<DIGITS, MAX_EXPONENT> {
    type Err = ParseDecimalError;

    /// Parses a number such as `-12.5` or `6.02e23`, rounded to `DIGITS` digits.
    fn from_str(text: &str) -> Result<Self, Self::Err> {
        let (negative, magnitude) = match text.as_bytes().first() {
            Some(b'-') => (true, &text[1..]),
            Some(b'+') => (false, &text[1..]),
            _ => (false, text),
        };
        let value = Self::parse_magnitude(magnitude)
            .ok_or(ParseDecimalError {
                kind: ErrorKind::InvalidNumber,
            })?
            .map_err(|kind| ParseDecimalError { kind })?;
        Ok(if negative { value.flip() } else { value })
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> TryFrom<i64> for Decimal<DIGITS, MAX_EXPONENT> {
    type Error = ErrorKind;

    fn try_from(value: i64) -> Result<Self, Self::Error> {
        Self::new(value, 0)
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> TryFrom<f64> for Decimal<DIGITS, MAX_EXPONENT> {
    type Error = ErrorKind;

    fn try_from(value: f64) -> Result<Self, Self::Error> {
        Self::from_f64(value)
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> From<Decimal<DIGITS, MAX_EXPONENT>> for f64 {
    fn from(value: Decimal<DIGITS, MAX_EXPONENT>) -> Self {
        value.to_f64()
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> fmt::Display for Decimal<DIGITS, MAX_EXPONENT> {
    /// Writes the number with all its significant digits, or with as many as the precision asks for, rounded half
    /// away from zero.
    ///
    /// Numbers are written positionally when their scientific exponent is between -5 and the number of digits
    /// shown, and in scientific notation otherwise, such as `6.02e23`.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let shown = formatter.precision().map_or(DIGITS, |precision| {
            u32::try_from(precision).unwrap_or(DIGITS).clamp(1, DIGITS)
        });
        let mut coefficient = self.coefficient;
        let mut exponent = self.scientific_exponent();
        if shown < DIGITS && !self.is_zero() {
            let divisor = power_of_ten(DIGITS - shown) as u64;
            let remainder = coefficient % divisor;
            coefficient /= divisor;
            if remainder >= divisor / 2 {
                coefficient += 1;
                if u128::from(coefficient) == power_of_ten(shown) {
                    coefficient /= 10;
                    exponent += 1;
                }
            }
        }
        let mut digits = Buffer::<20>::new();
        let _ = write!(digits, "{coefficient}");
        let digits = digits.as_str().trim_end_matches('0');
        let digits = if digits.is_empty() { "0" } else { digits };
        let mut text = Buffer::<48>::new();
        if (-5..i32::try_from(shown).unwrap_or(i32::MAX)).contains(&exponent) {
            if exponent < 0 {
                text.write_str("0.")?;
                for _ in 1..exponent.unsigned_abs() {
                    text.write_char('0')?;
                }
                text.write_str(digits)?;
            } else {
                let whole = exponent.unsigned_abs() as usize + 1;
                if digits.len() > whole {
                    let (integer, fraction) = digits.split_at(whole);
                    write!(text, "{integer}.{fraction}")?;
                } else {
                    text.write_str(digits)?;
                    for _ in digits.len()..whole {
                        text.write_char('0')?;
                    }
                }
            }
        } else {
            let (first, rest) = digits.split_at(1);
            text.write_str(first)?;
            if !rest.is_empty() {
                write!(text, ".{rest}")?;
            }
            write!(text, "e{exponent}")?;
        }
        formatter.pad_integral(!self.negative, "", text.as_str())
    }
}

impl<const DIGITS: u32, const MAX_EXPONENT: i32> fmt::Debug for Decimal<DIGITS, MAX_EXPONENT> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, formatter)
    }
}

/// A fixed-size text buffer, to format without allocating.
struct Buffer<const SIZE: usize> {
    /// The written bytes, then unused space.
    bytes: [u8; SIZE],
    /// How many bytes are written.
    length: usize,
}

impl<const SIZE: usize> Buffer<SIZE> {
    /// Creates an empty buffer.
    const fn new() -> Self {
        Self {
            bytes: [0; SIZE],
            length: 0,
        }
    }

    /// Returns the written text.
    fn as_str(&self) -> &str {
        core::str::from_utf8(&self.bytes[..self.length]).unwrap_or("")
    }
}

impl<const SIZE: usize> Write for Buffer<SIZE> {
    fn write_str(&mut self, text: &str) -> fmt::Result {
        let end = self.length + text.len();
        self.bytes
            .get_mut(self.length..end)
            .ok_or(fmt::Error)?
            .copy_from_slice(text.as_bytes());
        self.length = end;
        Ok(())
    }
}