puremp 0.1.9

A pure-Rust arbitrary-precision arithmetic library — integers, rationals and MPFR-class floats — with a dependency-free clean-room core (optional serde/rand bridges), plus a C ABI and a CLI.
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
//! Arbitrary-precision base-10 floating point.
//!
//! [`Decimal`] stores `coefficient · 10^exponent` with an arbitrary-precision
//! [`Int`] coefficient and an `i64` exponent — the exact analog of Python's
//! `Decimal` / Java's `BigDecimal`. Addition, subtraction, and multiplication
//! are **exact** (base-10 numbers are closed under them); division and explicit
//! rounding take a target number of significant digits (or a fixed exponent,
//! via [`Decimal::quantize`]) and a [`Rounding`] mode.
//!
//! Because trailing zeros are preserved, `"1.50"` and `"1.5"` are distinct
//! representations that compare *equal* — handy for money, where the scale is
//! meaningful.

use core::cmp::Ordering;
use core::fmt;

use alloc::string::{String, ToString};

use crate::int::Int;

/// Rounding modes for [`Decimal`] division and rounding.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Rounding {
    /// Round toward zero (truncate).
    Down,
    /// Round away from zero.
    Up,
    /// Round toward negative infinity.
    Floor,
    /// Round toward positive infinity.
    Ceiling,
    /// Round to nearest; ties away from zero.
    HalfUp,
    /// Round to nearest; ties toward zero.
    HalfDown,
    /// Round to nearest; ties to even (banker's rounding). The default.
    #[default]
    HalfEven,
}

/// An arbitrary-precision base-10 floating-point number, `coefficient · 10^exponent`.
#[derive(Clone)]
pub struct Decimal {
    coeff: Int,
    exp: i64,
}

/// Returns `10^n` as an [`Int`].
fn pow10(n: u32) -> Int {
    Int::from_i64(10).pow(n)
}

/// `10^e` as an `f64` by repeated squaring (no `powi`, so it works in `no_std`).
fn pow10_f64(e: i64) -> f64 {
    let mut base = if e < 0 { 0.1 } else { 10.0 };
    let mut n = e.unsigned_abs();
    let mut acc = 1.0f64;
    while n > 0 {
        if n & 1 == 1 {
            acc *= base;
        }
        base *= base;
        n >>= 1;
    }
    acc
}

/// Number of decimal digits in `|n|` (`1` for zero).
fn digit_count(n: &Int) -> usize {
    if n.is_zero() {
        1
    } else {
        n.magnitude().to_string().len()
    }
}

/// Rounds `coeff / 10^drop` to an integer under `mode` (`drop >= 1`).
fn round_div_pow10(coeff: &Int, drop: u32, mode: Rounding) -> Int {
    let divisor = pow10(drop);
    let (q, r) = coeff.div_rem(&divisor).expect("non-zero divisor");
    if r.is_zero() {
        return q;
    }
    let neg = coeff.is_negative();
    let r2 = r.abs().mul_2k(1); // 2·|r|, compared against the divisor for halves
    let increment = match mode {
        Rounding::Down => false,
        Rounding::Up => true,
        Rounding::Floor => neg,
        Rounding::Ceiling => !neg,
        Rounding::HalfUp => r2 >= divisor,
        Rounding::HalfDown => r2 > divisor,
        Rounding::HalfEven => match r2.cmp(&divisor) {
            Ordering::Greater => true,
            Ordering::Less => false,
            Ordering::Equal => q.is_odd(),
        },
    };
    if increment {
        q.add(&Int::from_i64(coeff.signum() as i64))
    } else {
        q
    }
}

impl Decimal {
    /// The value zero.
    pub fn zero() -> Decimal {
        Decimal {
            coeff: Int::ZERO,
            exp: 0,
        }
    }

    /// The value one.
    pub fn one() -> Decimal {
        Decimal {
            coeff: Int::ONE,
            exp: 0,
        }
    }

    /// Builds `coefficient · 10^exponent` (kept as given; not normalized).
    pub fn new(coefficient: Int, exponent: i64) -> Decimal {
        Decimal {
            coeff: coefficient,
            exp: exponent,
        }
    }

    /// Builds the decimal value of an integer.
    #[inline]
    pub fn from_int(n: Int) -> Decimal {
        Decimal { coeff: n, exp: 0 }
    }

    /// Returns the coefficient.
    #[inline]
    pub fn coefficient(&self) -> &Int {
        &self.coeff
    }

    /// Returns the base-10 exponent.
    #[inline]
    pub fn exponent(&self) -> i64 {
        self.exp
    }

    /// Returns `true` if this value is zero (any exponent).
    #[inline]
    pub fn is_zero(&self) -> bool {
        self.coeff.is_zero()
    }

    /// Returns `-1`, `0`, or `1` according to the sign.
    #[inline]
    pub fn signum(&self) -> i32 {
        self.coeff.signum()
    }

    /// Returns `-self`.
    pub fn neg(&self) -> Decimal {
        Decimal {
            coeff: self.coeff.neg(),
            exp: self.exp,
        }
    }

    /// Returns `|self|`.
    pub fn abs(&self) -> Decimal {
        Decimal {
            coeff: self.coeff.abs(),
            exp: self.exp,
        }
    }

    /// Returns `self · 10^n` (exact; adjusts only the exponent).
    pub fn scaleb(&self, n: i64) -> Decimal {
        Decimal {
            coeff: self.coeff.clone(),
            exp: self.exp + n,
        }
    }

    /// Removes trailing zeros from the coefficient (raising the exponent), giving
    /// the minimal-length equal representation (`"1.500"` → `"1.5"`).
    pub fn normalized(&self) -> Decimal {
        if self.coeff.is_zero() {
            return Decimal::zero();
        }
        let mut coeff = self.coeff.clone();
        let mut exp = self.exp;
        let ten = Int::from_i64(10);
        loop {
            let (q, r) = coeff.div_rem(&ten).unwrap();
            if !r.is_zero() {
                break;
            }
            coeff = q;
            exp += 1;
        }
        Decimal { coeff, exp }
    }

    /// Aligns two values to the common (smaller) exponent, returning the two
    /// coefficients at that scale and the exponent.
    fn aligned(&self, other: &Decimal) -> (Int, Int, i64) {
        let emin = self.exp.min(other.exp);
        let a = self.coeff.mul(&pow10((self.exp - emin) as u32));
        let b = other.coeff.mul(&pow10((other.exp - emin) as u32));
        (a, b, emin)
    }

    /// Returns `self + rhs` (exact).
    pub fn add(&self, rhs: &Decimal) -> Decimal {
        let (a, b, emin) = self.aligned(rhs);
        Decimal {
            coeff: a.add(&b),
            exp: emin,
        }
    }

    /// Returns `self - rhs` (exact).
    pub fn sub(&self, rhs: &Decimal) -> Decimal {
        self.add(&rhs.neg())
    }

    /// Returns `self · rhs` (exact).
    pub fn mul(&self, rhs: &Decimal) -> Decimal {
        Decimal {
            coeff: self.coeff.mul(&rhs.coeff),
            exp: self.exp + rhs.exp,
        }
    }

    /// Rounds to at most `sig_digits` significant digits under `mode`
    /// (`sig_digits >= 1`).
    pub fn round_to_digits(&self, sig_digits: u32, mode: Rounding) -> Decimal {
        if self.coeff.is_zero() {
            return self.clone();
        }
        let digits = digit_count(&self.coeff) as i64;
        let drop = digits - sig_digits.max(1) as i64;
        if drop <= 0 {
            return self.clone();
        }
        let coeff = round_div_pow10(&self.coeff, drop as u32, mode);
        Decimal {
            coeff,
            exp: self.exp + drop,
        }
    }

    /// Rounds to the fixed exponent `target_exp` under `mode` (like Python's
    /// `quantize`): scaling up is exact, scaling down rounds.
    pub fn quantize(&self, target_exp: i64, mode: Rounding) -> Decimal {
        if target_exp <= self.exp {
            // Finer or equal scale: multiply out exactly.
            Decimal {
                coeff: self.coeff.mul(&pow10((self.exp - target_exp) as u32)),
                exp: target_exp,
            }
        } else {
            let drop = (target_exp - self.exp) as u32;
            Decimal {
                coeff: round_div_pow10(&self.coeff, drop, mode),
                exp: target_exp,
            }
        }
    }

    /// Returns `self / rhs` rounded to `sig_digits` significant digits under
    /// `mode`. Panics if `rhs` is zero.
    pub fn div(&self, rhs: &Decimal, sig_digits: u32, mode: Rounding) -> Decimal {
        assert!(!rhs.is_zero(), "Decimal::div: division by zero");
        if self.is_zero() {
            return Decimal::zero();
        }
        let da = digit_count(&self.coeff) as i64;
        let db = digit_count(&rhs.coeff) as i64;
        // Scale the numerator so the quotient has a few more than sig_digits.
        let work = (sig_digits as i64 + 4 + db - da).max(1);
        let scaled = self.coeff.mul(&pow10(work as u32));
        let (mut q, r) = scaled.div_rem(&rhs.coeff).expect("non-zero divisor");
        // Sticky: keep a nonzero low digit when the division was inexact.
        if !r.is_zero() && q.rem_euclid(&Int::from_i64(10)).is_zero() {
            q = q.add(&Int::from_i64(q.signum() as i64));
        }
        Decimal {
            coeff: q,
            exp: self.exp - rhs.exp - work,
        }
        .round_to_digits(sig_digits, mode)
        // Strip the trailing zeros the scaling introduced (e.g. 1/8 → 0.125).
        .normalized()
    }

    /// Returns the value as the nearest `f64` (best-effort).
    pub fn to_f64(&self) -> f64 {
        self.coeff.to_f64() * pow10_f64(self.exp)
    }
}

impl PartialEq for Decimal {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.cmp(other) == Ordering::Equal
    }
}

impl Eq for Decimal {}

impl PartialOrd for Decimal {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Decimal {
    fn cmp(&self, other: &Self) -> Ordering {
        let (a, b, _) = self.aligned(other);
        a.cmp(&b)
    }
}

impl Default for Decimal {
    #[inline]
    fn default() -> Decimal {
        Decimal::zero()
    }
}

impl From<Int> for Decimal {
    #[inline]
    fn from(n: Int) -> Decimal {
        Decimal::from_int(n)
    }
}

impl From<i64> for Decimal {
    #[inline]
    fn from(v: i64) -> Decimal {
        Decimal::from_int(Int::from_i64(v))
    }
}

impl core::str::FromStr for Decimal {
    type Err = crate::error::Error;

    /// Parses `"123"`, `"-0.001"`, `"1.5"`, or scientific `"1.5e10"` / `"2E-8"`.
    fn from_str(s: &str) -> crate::error::Result<Decimal> {
        use crate::error::Error;
        let s = s.trim();
        // Split off an optional exponent.
        let (mantissa, exp_part) = match s.find(['e', 'E']) {
            Some(i) => (&s[..i], &s[i + 1..]),
            None => (s, ""),
        };
        let mut exp: i64 = if exp_part.is_empty() {
            0
        } else {
            exp_part.parse().map_err(|_| Error::Parse)?
        };
        let (neg, body) = match mantissa.strip_prefix('-') {
            Some(r) => (true, r),
            None => (false, mantissa.strip_prefix('+').unwrap_or(mantissa)),
        };
        // Integer and fractional digit runs.
        let (int_part, frac_part) = match body.find('.') {
            Some(i) => (&body[..i], &body[i + 1..]),
            None => (body, ""),
        };
        if int_part.is_empty() && frac_part.is_empty() {
            return Err(Error::Parse);
        }
        let mut digits = String::with_capacity(int_part.len() + frac_part.len());
        digits.push_str(int_part);
        digits.push_str(frac_part);
        exp -= frac_part.len() as i64;
        let mag: crate::nat::Nat = if digits.is_empty() {
            crate::nat::Nat::zero()
        } else {
            digits.parse().map_err(|_| Error::Parse)?
        };
        let coeff = Int::from_sign_magnitude(
            if neg {
                crate::int::Sign::Negative
            } else {
                crate::int::Sign::Positive
            },
            mag,
        );
        Ok(Decimal::new(coeff, exp))
    }
}

impl fmt::Display for Decimal {
    /// Formats in plain (non-scientific) decimal notation, preserving the scale.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.coeff.is_zero() {
            return f.write_str("0");
        }
        if self.coeff.is_negative() {
            f.write_str("-")?;
        }
        let digits = self.coeff.magnitude().to_string();
        if self.exp >= 0 {
            f.write_str(&digits)?;
            for _ in 0..self.exp {
                f.write_str("0")?;
            }
            Ok(())
        } else {
            let k = (-self.exp) as usize;
            if digits.len() > k {
                let point = digits.len() - k;
                f.write_str(&digits[..point])?;
                f.write_str(".")?;
                f.write_str(&digits[point..])
            } else {
                f.write_str("0.")?;
                for _ in 0..k - digits.len() {
                    f.write_str("0")?;
                }
                f.write_str(&digits)
            }
        }
    }
}

impl fmt::Debug for Decimal {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Decimal({} · 10^{})", self.coeff, self.exp)
    }
}

macro_rules! dec_binop {
    ($tr:ident, $m:ident, $atr:ident, $am:ident) => {
        impl core::ops::$tr for Decimal {
            type Output = Decimal;
            #[inline]
            fn $m(self, rhs: Decimal) -> Decimal {
                Decimal::$m(&self, &rhs)
            }
        }
        impl core::ops::$tr<&Decimal> for &Decimal {
            type Output = Decimal;
            #[inline]
            fn $m(self, rhs: &Decimal) -> Decimal {
                Decimal::$m(self, rhs)
            }
        }
        impl core::ops::$atr for Decimal {
            #[inline]
            fn $am(&mut self, rhs: Decimal) {
                *self = Decimal::$m(self, &rhs);
            }
        }
    };
}

dec_binop!(Add, add, AddAssign, add_assign);
dec_binop!(Sub, sub, SubAssign, sub_assign);
dec_binop!(Mul, mul, MulAssign, mul_assign);

impl core::ops::Neg for Decimal {
    type Output = Decimal;
    #[inline]
    fn neg(self) -> Decimal {
        Decimal::neg(&self)
    }
}

// --- conversions to the exact rational, when available ---

#[cfg(feature = "rational")]
impl Decimal {
    /// Returns the exact value as a [`Rational`](crate::rational::Rational).
    pub fn to_rational(&self) -> crate::rational::Rational {
        use crate::rational::Rational;
        if self.exp >= 0 {
            Rational::from_integer(self.coeff.mul(&pow10(self.exp as u32)))
        } else {
            Rational::new(self.coeff.clone(), pow10((-self.exp) as u32))
        }
    }
}