nanoexpr 0.2.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
//! The [`Fraction`] number type, which computes with rational numbers exactly.

use core::cmp::Ordering;
use core::fmt;
use core::str::FromStr;

use crate::{ErrorKind, Number};

/// An exact rational number, such as `-7/2`, always in lowest terms.
///
/// The numerator and the denominator are each at most `u64::MAX`, the numerator holding the sign, so the range is
/// symmetric and negation never fails. Operations compute exactly with 128-bit integers, then reduce: a result
/// whose numerator or denominator does not fit is an [`ErrorKind::Overflow`] error, never a rounded value.
///
/// Expressions written with `/` build fractions, so `1/3 + 1/6` is `1/2`. Literals are exact too: `0.75` is `3/4`
/// and `1.5e-3` is `3/2000`. Literals that do not fit, or have more than 38 significant digits, are invalid
/// numbers.
///
/// Raising to an integer power is exact. Raising to a fractional power `p/q` is exact when the `q`-th root is
/// rational, such as `(4/9)^(1/2) = 2/3` or `(-8)^(1/3) = -2`, and an [`ErrorKind::Inexact`] error otherwise, as
/// `2^(1/2)` is. Even roots of negative numbers are [`ErrorKind::Domain`] errors.
///
/// It displays as `7/2`, as `3` when its denominator is 1, or as a mixed number such as `3 1/2` with the alternate
/// flag `{:#}`.
///
/// ```
/// # #[cfg(feature = "fraction")] {
/// use nanoexpr::Fraction;
///
/// let sum: Fraction = nanoexpr::evaluate("1/3 + 1/6").unwrap();
/// assert_eq!(sum.to_string(), "1/2");
///
/// let total: Fraction = nanoexpr::evaluate("0.1 + 0.2 + 3").unwrap();
/// assert_eq!(total.to_string(), "33/10");
/// assert_eq!(format!("{total:#}"), "3 3/10");
///
/// let root: Fraction = nanoexpr::evaluate("sqrt(4/9)").unwrap();
/// assert_eq!(root, "2/3".parse().unwrap());
/// # }
/// ```
///
/// Equality, ordering and hashing compare values, as every value has a single representation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Fraction {
    /// Whether the fraction is below zero, never set for zero.
    negative: bool,
    /// The numerator, without its sign.
    numerator: u64,
    /// The denominator, at least 1.
    denominator: u64,
}

/// Returns the greatest common divisor of `a` and `b`, or the other one when one of them is zero.
pub(crate) const fn gcd(mut a: u128, mut b: u128) -> u128 {
    while b != 0 {
        (a, b) = (b, a % b);
    }
    a
}

/// Returns `base^exponent`, or an [`ErrorKind::Overflow`] error if it does not fit a `u64`.
fn power(base: u64, exponent: u64) -> Result<u64, ErrorKind> {
    match base {
        0 => Ok(u64::from(exponent == 0)),
        1 => Ok(1),
        _ => u32::try_from(exponent)
            .ok()
            .and_then(|exponent| base.checked_pow(exponent))
            .ok_or(ErrorKind::Overflow),
    }
}

/// Returns the integer `degree`-th root of `value`, or `None` if `value` is not a perfect power.
fn root(value: u64, degree: u64) -> Option<u64> {
    if value < 2 {
        return Some(value);
    }
    let degree = u32::try_from(degree).ok().filter(|&degree| degree < 64)?;
    let (mut low, mut high) = (1_u64, 1_u64 << 32);
    while low < high {
        let middle = low + (high - low).div_ceil(2);
        match middle.checked_pow(degree) {
            Some(result) if result <= value => low = middle,
            _ => high = middle - 1,
        }
    }
    (low.checked_pow(degree) == Some(value)).then_some(low)
}

impl Fraction {
    /// Zero.
    pub const ZERO: Self = Self {
        negative: false,
        numerator: 0,
        denominator: 1,
    };

    /// One.
    pub const ONE: Self = Self {
        negative: false,
        numerator: 1,
        denominator: 1,
    };

    /// Creates `numerator / denominator`, below zero if `negative`, reduced to lowest terms.
    ///
    /// A zero `denominator` is a [`ErrorKind::DivisionByZero`] error.
    ///
    /// ```
    /// use nanoexpr::Fraction;
    ///
    /// let fraction = Fraction::new(true, 14, 4).unwrap();
    /// assert_eq!(fraction.to_string(), "-7/2");
    /// assert_eq!((fraction.numerator(), fraction.denominator()), (7, 2));
    /// ```
    pub fn new(negative: bool, numerator: u64, denominator: u64) -> Result<Self, ErrorKind> {
        Self::reduce(negative, u128::from(numerator), u128::from(denominator))
    }

    /// Reduces `numerator / denominator`, below zero if `negative`, to lowest terms.
    ///
    /// A zero `denominator` is a [`ErrorKind::DivisionByZero`] error, and a reduced numerator or denominator
    /// beyond `u64` an [`ErrorKind::Overflow`] error.
    pub(crate) fn reduce(
        negative: bool,
        numerator: u128,
        denominator: u128,
    ) -> Result<Self, ErrorKind> {
        if denominator == 0 {
            return Err(ErrorKind::DivisionByZero);
        }
        let divisor = gcd(numerator, denominator);
        let numerator = u64::try_from(numerator / divisor).map_err(|_| ErrorKind::Overflow)?;
        let denominator = u64::try_from(denominator / divisor).map_err(|_| ErrorKind::Overflow)?;
        Ok(Self {
            negative: negative && numerator != 0,
            numerator,
            denominator,
        })
    }

    /// Returns `mantissa × 10^exponent`, below zero if `negative`, exactly.
    ///
    /// Values whose reduced numerator or denominator does not fit are [`ErrorKind::Overflow`] errors.
    pub(crate) fn from_decimal(
        negative: bool,
        mantissa: u128,
        exponent: i64,
    ) -> Result<Self, ErrorKind> {
        if mantissa == 0 {
            return Ok(Self::ZERO);
        }
        if exponent >= 0 {
            let scale = u32::try_from(exponent)
                .ok()
                .and_then(|exponent| 10_u128.checked_pow(exponent))
                .and_then(|scale| scale.checked_mul(mantissa))
                .ok_or(ErrorKind::Overflow)?;
            return Self::reduce(negative, scale, 1);
        }
        let (mut numerator, mut twos, mut fives) =
            (mantissa, exponent.unsigned_abs(), exponent.unsigned_abs());
        while twos > 0 && numerator % 2 == 0 {
            numerator /= 2;
            twos -= 1;
        }
        while fives > 0 && numerator % 5 == 0 {
            numerator /= 5;
            fives -= 1;
        }
        let denominator = u32::try_from(twos)
            .ok()
            .and_then(|twos| 2_u128.checked_pow(twos))
            .zip(
                u32::try_from(fives)
                    .ok()
                    .and_then(|fives| 5_u128.checked_pow(fives)),
            )
            .and_then(|(twos, fives)| twos.checked_mul(fives))
            .ok_or(ErrorKind::Overflow)?;
        Self::reduce(negative, numerator, denominator)
    }

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

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

    /// Returns whether the fraction is an integer, that is whether its denominator is 1.
    pub const fn is_integer(self) -> bool {
        self.denominator == 1
    }

    /// Returns the numerator, without its sign.
    pub const fn numerator(self) -> u64 {
        self.numerator
    }

    /// Returns the denominator, at least 1.
    pub const fn denominator(self) -> u64 {
        self.denominator
    }

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

    /// Returns `-1`, `0` or `1`, the sign of the fraction.
    pub const fn sign(self) -> Self {
        if self.is_zero() {
            Self::ZERO
        } else {
            Self {
                negative: self.negative,
                ..Self::ONE
            }
        }
    }

    /// Returns `1 / self`, or a [`ErrorKind::DivisionByZero`] error for zero.
    fn reciprocal(self) -> Result<Self, ErrorKind> {
        if self.is_zero() {
            return Err(ErrorKind::DivisionByZero);
        }
        Ok(Self {
            negative: self.negative,
            numerator: self.denominator,
            denominator: self.numerator,
        })
    }

    /// Returns the integer `magnitude`, below zero if `negative`.
    fn integer(negative: bool, magnitude: u64) -> Self {
        Self {
            negative: negative && magnitude != 0,
            numerator: magnitude,
            denominator: 1,
        }
    }

    /// Rounds the magnitude down, or up if `up`, to an integer of the same sign.
    fn round_magnitude(self, up: bool) -> Self {
        let whole = self.numerator / self.denominator;
        let away = up && self.numerator % self.denominator != 0;
        Self::integer(self.negative, whole + u64::from(away))
    }

    /// Rounds toward negative infinity.
    pub fn floor(self) -> Self {
        self.round_magnitude(self.negative)
    }

    /// Rounds toward positive infinity.
    pub fn ceil(self) -> Self {
        self.round_magnitude(!self.negative)
    }

    /// Rounds toward zero.
    pub fn trunc(self) -> Self {
        self.round_magnitude(false)
    }

    /// Rounds to the nearest integer, half away from zero.
    pub fn round(self) -> Self {
        let remainder = self.numerator % self.denominator;
        let up = u128::from(remainder) * 2 >= u128::from(self.denominator);
        self.round_magnitude(up)
    }

    /// Returns `self` raised to the integer power `exponent`, or to its opposite if `reciprocal`.
    fn power_integer(self, reciprocal: bool, exponent: u64) -> Result<Self, ErrorKind> {
        let base = if reciprocal && exponent != 0 {
            self.reciprocal()?
        } else {
            self
        };
        Ok(Self {
            negative: base.negative && exponent % 2 == 1,
            numerator: power(base.numerator, exponent)?,
            denominator: power(base.denominator, exponent)?,
        })
    }

    /// Returns the real `degree`-th root, exactly.
    ///
    /// Even roots of negative numbers are [`ErrorKind::Domain`] errors, and irrational roots
    /// [`ErrorKind::Inexact`] errors.
    pub(crate) fn root(self, degree: u64) -> Result<Self, ErrorKind> {
        if self.negative && degree % 2 == 0 {
            return Err(ErrorKind::Domain);
        }
        Ok(Self {
            negative: self.negative,
            numerator: root(self.numerator, degree).ok_or(ErrorKind::Inexact)?,
            denominator: root(self.denominator, degree).ok_or(ErrorKind::Inexact)?,
        })
    }

    /// Returns the closest `f64`, exactly rounded when the numerator and the denominator are below 2^53, and within
    /// two units in the last place otherwise.
    pub fn to_f64(self) -> f64 {
        let magnitude = self.numerator as f64 / self.denominator as f64;
        if self.negative { -magnitude } else { magnitude }
    }

    /// Returns the numerator and the denominator of each operand, as 128-bit integers.
    fn wide(self, rhs: Self) -> [u128; 4] {
        [
            u128::from(self.numerator),
            u128::from(self.denominator),
            u128::from(rhs.numerator),
            u128::from(rhs.denominator),
        ]
    }

    /// Returns the sum of the two fractions.
    ///
    /// Both numerators are brought to the least common multiple of the denominators, whose terms then fit 127 bits
    /// unless the denominators are coprime, in which case a sum beyond 128 bits cannot reduce to a fitting result.
    fn sum(self, rhs: Self) -> Result<Self, ErrorKind> {
        let [a, b, c, d] = self.wide(rhs);
        let divisor = gcd(b, d);
        let left = a * (d / divisor);
        let right = c * (b / divisor);
        let (negative, numerator) = if self.negative == rhs.negative {
            (
                self.negative,
                left.checked_add(right).ok_or(ErrorKind::Overflow)?,
            )
        } else if left >= right {
            (self.negative, left - right)
        } else {
            (rhs.negative, right - left)
        };
        Self::reduce(negative, numerator, b / divisor * d)
    }

    /// Returns the product of the two fractions, reducing across them first so that nothing overflows.
    fn product(self, rhs: Self) -> Result<Self, ErrorKind> {
        let [a, b, c, d] = self.wide(rhs);
        let (first, second) = (gcd(a, d).max(1), gcd(c, b).max(1));
        Self::reduce(
            self.negative != rhs.negative,
            (a / first) * (c / second),
            (b / second) * (d / first),
        )
    }
}

impl Default for Fraction {
    fn default() -> Self {
        Self::ZERO
    }
}

impl Number for Fraction {
    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(Self {
            negative: !rhs.negative && !rhs.is_zero(),
            ..rhs
        })
    }

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

    fn divide(self, rhs: Self) -> Result<Self, ErrorKind> {
        self.product(rhs.reciprocal()?)
    }

    fn remainder(self, rhs: Self) -> Result<Self, ErrorKind> {
        if rhs.is_zero() {
            return Err(ErrorKind::DivisionByZero);
        }
        let [a, b, c, d] = self.wide(rhs);
        Self::reduce(self.negative, (a * d) % (c * b), b * d)
    }

    fn power(self, rhs: Self) -> Result<Self, ErrorKind> {
        if rhs.is_integer() {
            return self.power_integer(rhs.negative, rhs.numerator);
        }
        if self.is_zero() && rhs.negative {
            return Err(ErrorKind::DivisionByZero);
        }
        self.root(rhs.denominator)?
            .power_integer(rhs.negative, rhs.numerator)
    }

    fn negate(self) -> Result<Self, ErrorKind> {
        Ok(Self {
            negative: !self.negative && !self.is_zero(),
            ..self
        })
    }

    fn compare(self, rhs: Self) -> Result<Ordering, ErrorKind> {
        Ok(self.cmp(&rhs))
    }

    fn is_zero(self) -> Result<bool, ErrorKind> {
        Ok(Self::is_zero(self))
    }

    fn from_bool(value: bool) -> Result<Self, ErrorKind> {
        Ok(if value { Self::ONE } else { Self::ZERO })
    }
}

impl PartialOrd for Fraction {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Fraction {
    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 [a, b, c, d] = self.wide(*other);
            let magnitude = (a * d).cmp(&(c * b));
            if self.negative {
                magnitude.reverse()
            } else {
                magnitude
            }
        })
    }
}

impl Fraction {
    /// Parses an unsigned literal such as `12`, `.5`, `3.` or `1.5e-3`, exactly.
    ///
    /// Malformed text is an [`ErrorKind::InvalidNumber`] error, and values that do not fit, or have more than 38
    /// significant digits, are [`ErrorKind::Overflow`] errors.
    fn parse_magnitude(text: &str) -> Result<Self, ErrorKind> {
        let bytes = text.as_bytes();
        let mut mantissa: u128 = 0;
        let mut zeros: u32 = 0;
        let mut exponent: i64 = 0;
        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;
                    if fraction {
                        exponent -= 1;
                    }
                    let digit = u128::from(byte - b'0');
                    if digit == 0 {
                        zeros = zeros.saturating_add(1);
                    } else if mantissa == 0 {
                        mantissa = digit;
                        zeros = 0;
                    } else {
                        mantissa = 10_u128
                            .checked_pow(zeros + 1)
                            .and_then(|scale| mantissa.checked_mul(scale))
                            .and_then(|scaled| scaled.checked_add(digit))
                            .ok_or(ErrorKind::Overflow)?;
                        zeros = 0;
                    }
                }
                b'.' if !fraction => fraction = true,
                b'e' | b'E' if seen => {
                    exponent += bytes
                        .get(index + 1..)
                        .and_then(parse_exponent)
                        .ok_or(ErrorKind::InvalidNumber)?;
                    break;
                }
                _ => return Err(ErrorKind::InvalidNumber),
            }
            index += 1;
        }
        if !seen {
            return Err(ErrorKind::InvalidNumber);
        }
        if mantissa == 0 {
            return Ok(Self::ZERO);
        }
        Self::from_decimal(false, mantissa, exponent + i64::from(zeros))
    }
}

/// Parses the digits after an `e`, with an optional sign, saturating far beyond any useful exponent.
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 })
}

/// The error of parsing a [`Fraction`] from text.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ParseFractionError {
    /// [`ErrorKind::InvalidNumber`] for malformed text, [`ErrorKind::Overflow`] for a value that does not fit, or
    /// [`ErrorKind::DivisionByZero`] for a zero denominator.
    pub kind: ErrorKind,
}

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

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

impl FromStr for Fraction {
    type Err = ParseFractionError;

    /// Parses a fraction such as `3/4`, `-5/4` or `7`, whose numerator and denominator may also be decimals such as
    /// `1.5/2`.
    fn from_str(text: &str) -> Result<Self, Self::Err> {
        let error = |kind| ParseFractionError { kind };
        let (negative, magnitude) = match text.as_bytes().first() {
            Some(b'-') => (true, &text[1..]),
            Some(b'+') => (false, &text[1..]),
            _ => (false, text),
        };
        let (numerator, denominator) = match magnitude.split_once('/') {
            Some((numerator, denominator)) => (numerator, Some(denominator)),
            None => (magnitude, None),
        };
        let mut value = Self::parse_magnitude(numerator).map_err(error)?;
        if let Some(denominator) = denominator {
            let denominator = Self::parse_magnitude(denominator).map_err(error)?;
            value = value.divide(denominator).map_err(error)?;
        }
        if negative {
            value = value.negate().map_err(error)?;
        }
        Ok(value)
    }
}

impl From<u64> for Fraction {
    fn from(value: u64) -> Self {
        Self::integer(false, value)
    }
}

impl From<i64> for Fraction {
    fn from(value: i64) -> Self {
        Self::integer(value < 0, value.unsigned_abs())
    }
}

impl From<Fraction> for f64 {
    fn from(value: Fraction) -> Self {
        value.to_f64()
    }
}

impl fmt::Display for Fraction {
    /// Writes `n/d`, `n` for integers, or with the alternate flag `{:#}` a mixed number such as `-3 1/2`.
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.negative {
            formatter.write_str("-")?;
        }
        let whole = self.numerator / self.denominator;
        let rest = self.numerator % self.denominator;
        if rest == 0 {
            write!(formatter, "{whole}")
        } else if formatter.alternate() && whole > 0 {
            write!(formatter, "{whole} {rest}/{}", self.denominator)
        } else {
            write!(formatter, "{}/{}", self.numerator, self.denominator)
        }
    }
}