Skip to main content

wolfram_expr/
bignum.rs

1//! Arbitrary-precision number types — string-preserving, no arithmetic.
2//!
3//! Both [`BigInteger`] and [`BigReal`] are thin newtypes around a digit `String`
4//! exactly as it appears on the WXF wire. They preserve the textual representation
5//! losslessly so the value round-trips byte-for-byte through serialization, but
6//! they do **not** parse the value into a Rust arithmetic type. If you want to
7//! compute on a deserialized BigInteger, parse it yourself:
8//!
9//! ```ignore
10//! let bi = match expr.kind() { ExprKind::BigInteger(n) => n, _ => unreachable!() };
11//! let value: num_bigint::BigInt = bi.0.parse().unwrap();
12//! let result = value * 2;
13//! let back = BigInteger(result.to_string());
14//! ```
15//!
16//! This keeps `wolfram-expr` dependency-free with respect to bignum crates —
17//! arithmetic is a higher-level concern.
18
19/// Wolfram Language `BigInteger` — arbitrary-precision integer carried as its
20/// textual decimal representation (e.g. `"99999999999999999999999"`).
21#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
22pub struct BigInteger(pub String);
23
24impl BigInteger {
25    /// The underlying textual representation, preserved verbatim from the wire.
26    pub fn as_str(&self) -> &str {
27        &self.0
28    }
29}
30
31/// Wolfram Language `BigReal` — arbitrary-precision real carried as its WL
32/// textual representation, including any precision/accuracy markers
33/// (e.g. `"3.14159265358979323846`50."`).
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
35pub struct BigReal(pub String);
36
37impl BigReal {
38    /// The underlying textual representation, preserved verbatim from the wire.
39    pub fn as_str(&self) -> &str {
40        &self.0
41    }
42}